Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pairwise Point to Point Distance; Rename Folder distances to distance #558

Merged
merged 50 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
50146a8
Initial conversion, passes compilation and test
isVoid May 12, 2022
1861dc9
Add docstring
isVoid May 13, 2022
0d5dc94
Add RAI specification
isVoid May 13, 2022
ca54b2c
add default stream parameter
isVoid May 13, 2022
a427af6
Add first test and cast references around.
isVoid May 13, 2022
2c6ec85
Add more tests
isVoid May 13, 2022
9958f48
fix offset arrays
isVoid May 16, 2022
dc944c5
fix wrong gtest binary name
isVoid May 16, 2022
e36186a
Add precommit hooks and script for cmake format/lint
isVoid May 16, 2022
62aa1ad
update with optimized code
isVoid May 17, 2022
af38652
remove dependency on cudf atomics
isVoid May 17, 2022
ae11443
regroup includes
isVoid May 17, 2022
79d57aa
Use size_t as index type.
isVoid May 18, 2022
e2a3db7
some fixes on tests
isVoid May 18, 2022
a1512d8
Revert cmake-format and precommit hooks
isVoid May 18, 2022
9b1687c
Remove `Cart2dA` and `Cart2dB`
isVoid May 18, 2022
dea41f1
Documentation update
isVoid May 18, 2022
270cfe2
Style fix
isVoid May 18, 2022
cb5d301
fix broken compile
isVoid May 19, 2022
183c10c
Removes `device_atomics` usage
isVoid May 19, 2022
b2fbaad
Add `internal` marker to internal docstrings.
isVoid May 19, 2022
1752485
Move derived traits to traits.hpp
isVoid May 19, 2022
fe83ea2
add back raw_reference_cast
isVoid May 19, 2022
38dfffd
initial of point distance
isVoid May 25, 2022
3b937b6
Merge branch 'branch-22.08' of https://github.com/rapidsai/cuspatial …
isVoid Jun 8, 2022
6089a79
add header only api test
isVoid Jun 8, 2022
1757853
Supply more precision to hard coded test, minor fixes
isVoid Jun 8, 2022
2f4ac2d
Add cudf dependent API test
isVoid Jun 8, 2022
e77a3bf
documentation updates
isVoid Jun 8, 2022
639edcd
Merge branch 'branch-22.08' of https://github.com/rapidsai/cuspatial …
isVoid Jun 16, 2022
bbd2855
update docs
isVoid Jun 16, 2022
456b342
Remove redundant static assert check
isVoid Jun 16, 2022
3853a1d
Remove redundant test
isVoid Jun 16, 2022
4ffc18b
Add column null tests
isVoid Jun 16, 2022
bebede6
test returned iterator
isVoid Jun 22, 2022
417ae22
Merge branch 'branch-22.08' of https://github.com/rapidsai/cuspatial …
isVoid Jun 23, 2022
3828647
updating utility locations
isVoid Jun 23, 2022
6a99191
Merge branch 'branch-22.08' of https://github.com/rapidsai/cuspatial …
isVoid Jul 28, 2022
7009a94
Apply docs review comments
isVoid Jul 28, 2022
f38c22d
Merge branch 'feature/point_distance' of github.com:isVoid/cuspatial …
isVoid Jul 28, 2022
412526b
Renamed `distances` -> `distance`
isVoid Jul 28, 2022
8f974d6
Fix include paths after `distance` rename
isVoid Jul 28, 2022
7e1afdc
Include cudf API test
isVoid Jul 28, 2022
e8e7497
Fix function signature
isVoid Jul 28, 2022
f5ca35c
relocate haversine and hausdorff distance
isVoid Jul 28, 2022
0500924
style
isVoid Jul 28, 2022
285f35a
Update docs
isVoid Jul 28, 2022
3c6b2af
Add throw tests
isVoid Jul 29, 2022
31638c7
Fix cython builds
isVoid Jul 29, 2022
544d708
Apply review comments
isVoid Jul 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ add_library(cuspatial
src/spatial/haversine.cu
src/spatial/hausdorff.cu
src/spatial/linestring_distance.cu
src/spatial/point_distance.cu
src/spatial/lonlat_to_cartesian.cu
src/trajectory/derive_trajectories.cu
src/trajectory/trajectory_bounding_boxes.cu
Expand Down
42 changes: 42 additions & 0 deletions cpp/include/cuspatial/distances/point_distance.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
isVoid marked this conversation as resolved.
Show resolved Hide resolved
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <cudf/column/column_view.hpp>
#include <cudf/utilities/span.hpp>

namespace cuspatial {

/**
* @ingroup distance
* @brief Compute pairwise point to point cartesian distance
isVoid marked this conversation as resolved.
Show resolved Hide resolved
*
* @param points1_x Column of x coordinates to the first point in each pair
* @param points1_y Column of y coordinates to the first point in each pair
* @param points2_x Column of x coordinates to the second point in each pair
* @param points2_y Column of y coordinates to the second point in each pair
* @param stream The CUDA stream to use for device memory operations and kernel launches
* @return Column of distances between each pair of input points
*/
std::unique_ptr<cudf::column> pairwise_point_distance(
isVoid marked this conversation as resolved.
Show resolved Hide resolved
cudf::column_view const& points1_x,
cudf::column_view const& points1_y,
cudf::column_view const& points2_x,
cudf::column_view const& points2_y,
rmm::cuda_stream_view stream = rmm::cuda_stream_default);

} // namespace cuspatial
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,13 @@ void pairwise_linestring_distance(OffsetIterator linestring1_offsets_first,
typename std::iterator_traits<OutputIt>::value_type>(),
"Inputs and output must have floating point value type.");

static_assert(detail::is_same<T,
typename std::iterator_traits<Cart2dItB>::value_type::value_type,
typename std::iterator_traits<OutputIt>::value_type>(),
static_assert(detail::is_same<T, typename std::iterator_traits<OutputIt>::value_type>(),
"Inputs and output must have the same value type.");

static_assert(detail::is_same<cartesian_2d<T>,
typename std::iterator_traits<Cart2dItA>::value_type,
typename std::iterator_traits<Cart2dItB>::value_type>(),
"Inputs must be cuspatial::cartesian_2d");
"Input types mismatches or input types are not cuspatial::cartesian_2d");
isVoid marked this conversation as resolved.
Show resolved Hide resolved

auto const num_linestring_pairs =
thrust::distance(linestring1_offsets_first, linestring1_offsets_last);
Expand Down
64 changes: 64 additions & 0 deletions cpp/include/cuspatial/experimental/detail/point_distance.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <cuspatial/error.hpp>
#include <cuspatial/utility/traits.hpp>
#include <cuspatial/utility/vec_2d.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/exec_policy.hpp>

#include <type_traits>

namespace cuspatial {

template <class Cart2dItA, class Cart2dItB, class OutputIt>
OutputIt pairwise_point_distance(Cart2dItA points1_first,
Cart2dItA points1_last,
Cart2dItB points2_first,
OutputIt distances_first,
rmm::cuda_stream_view stream)
{
using T = typename std::iterator_traits<Cart2dItA>::value_type::value_type;

static_assert(
detail::is_floating_point<T,
typename std::iterator_traits<Cart2dItB>::value_type::value_type,
typename std::iterator_traits<OutputIt>::value_type>(),
"Inputs and output must be floating point types.");

static_assert(detail::is_same<T, typename std::iterator_traits<OutputIt>::value_type>(),
"Inputs and output must be the same types.");
Comment on lines +39 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we merge #579 you can replace this with is_same_floating_point.


static_assert(detail::is_same<cartesian_2d<T>,
typename std::iterator_traits<Cart2dItA>::value_type,
typename std::iterator_traits<Cart2dItB>::value_type>(),
"Input types mismatches or input types are not cuspatial::cartesian_2d");
isVoid marked this conversation as resolved.
Show resolved Hide resolved

return thrust::transform(rmm::exec_policy(stream),
points1_first,
points1_last,
points2_first,
distances_first,
[] __device__(auto p1, auto p2) {
auto v = p1 - p2;
return std::sqrt(dot(v, v));
});
}

} // namespace cuspatial
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace cuspatial {

/**
* @ingroup distance
* @copybrief cuspatial::pairwise_linestring_distance
*
* The shortest distance between two linestrings is defined as the shortest distance
Expand Down
57 changes: 57 additions & 0 deletions cpp/include/cuspatial/experimental/point_distance.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <rmm/cuda_stream_view.hpp>

namespace cuspatial {

/**
* @ingroup distance
* @copybrief cuspatial::pairwise_point_distance
*
* @tparam Cart2dItA iterator type for point array of the first point of each pair. Must meet
* the requirements of [LegacyRandomAccessIterator][LinkLRAI] and be device-accessible.
* @tparam Cart2dItB iterator type for point array of the second point of each pair. Must meet
* the requirements of [LegacyRandomAccessIterator][LinkLRAI] and be device-accessible.
* @tparam OutputIt iterator type for output array. Must meet the requirements of
* [LegacyRandomAccessIterator][LinkLRAI], be mutable and be device-accessible.
*
* @param points1_first beginning of range of the first point of each pair
* @param points1_last end of range of the first point of each pair
* @param points2_first beginning of range of the second point of each pair
* @param distances_first beginning iterator to output
* @param stream The CUDA stream to use for device memory operations and kernel launches
* @return Output iterator to one past the last element in the output range
*
* @pre all input iterators for coordinates must have a `value_type` of `cuspatial::cartesian_2d`.
* @pre all scalar types must be floating point types, and must be the same type for all input
* iterators and output iterators.
*
* [LinkLRAI]: https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
* "LegacyRandomAccessIterator"
*/
template <class Cart2dItA, class Cart2dItB, class OutputIt>
OutputIt pairwise_point_distance(Cart2dItA points1_first,
Cart2dItA points1_last,
Cart2dItB points2_first,
OutputIt distances_first,
rmm::cuda_stream_view stream = rmm::cuda_stream_default);

} // namespace cuspatial

#include <cuspatial/experimental/detail/point_distance.cuh>
1 change: 1 addition & 0 deletions cpp/include/doxygen_groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* @{
* @brief Distance computation APIs
*
* @file point_distance.hpp
* @file linestring_distance.hpp
* @file hausdorff.hpp
* @file haversine.hpp
Expand Down
113 changes: 113 additions & 0 deletions cpp/src/spatial/point_distance.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <cuspatial/error.hpp>
#include <cuspatial/experimental/point_distance.cuh>
#include <cuspatial/experimental/type_utils.hpp>
#include <cuspatial/utility/vec_2d.hpp>

#include <cudf/column/column_factories.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/copying.hpp>
#include <cudf/utilities/type_dispatcher.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/exec_policy.hpp>

#include <memory>
#include <type_traits>

namespace cuspatial {
namespace detail {
struct pairwise_point_distance_functor {
template <typename T>
std::enable_if_t<std::is_floating_point<T>::value, std::unique_ptr<cudf::column>> operator()(
cudf::column_view const& points1_x,
cudf::column_view const& points1_y,
cudf::column_view const& points2_x,
cudf::column_view const& points2_y,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
auto distances = cudf::make_numeric_column(cudf::data_type{cudf::type_to_id<T>()},
points1_x.size(),
cudf::mask_state::UNALLOCATED,
stream,
mr);

auto points1_it = make_cartesian_2d_iterator(points1_x.begin<T>(), points1_y.begin<T>());
auto points2_it = make_cartesian_2d_iterator(points2_x.begin<T>(), points2_y.begin<T>());

pairwise_point_distance(points1_it,
points1_it + points1_x.size(),
points2_it,
distances->mutable_view().begin<T>(),
stream);

return distances;
}

template <typename T, typename... Args>
std::enable_if_t<not std::is_floating_point<T>::value, std::unique_ptr<cudf::column>> operator()(
Args&&...)
{
CUSPATIAL_FAIL("Point distances only supports floating point coordinates.");
}
};

std::unique_ptr<cudf::column> pairwise_point_distance(cudf::column_view const& points1_x,
cudf::column_view const& points1_y,
cudf::column_view const& points2_x,
cudf::column_view const& points2_y,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUSPATIAL_EXPECTS(points1_x.size() == points1_y.size() and
isVoid marked this conversation as resolved.
Show resolved Hide resolved
points2_x.size() == points2_y.size() and points1_x.size() == points2_x.size(),
"Mismatch number of coordinate or number of points.");

CUSPATIAL_EXPECTS(points1_x.type() == points1_y.type() and
points2_x.type() == points2_y.type() and points1_x.type() == points2_x.type(),
"The types of point coordinates arrays mismatch.");
CUSPATIAL_EXPECTS(not points1_x.has_nulls() and not points1_y.has_nulls() and
not points2_x.has_nulls() and not points2_y.has_nulls(),
"The coordinate columns cannot have nulls.");

if (points1_x.size() == 0) { return cudf::empty_like(points1_x); }

return cudf::type_dispatcher(points1_x.type(),
pairwise_point_distance_functor{},
points1_x,
points1_y,
points2_x,
points2_y,
stream,
mr);
}

} // namespace detail

std::unique_ptr<cudf::column> pairwise_point_distance(cudf::column_view const& points1_x,
cudf::column_view const& points1_y,
cudf::column_view const& points2_x,
cudf::column_view const& points2_y,
rmm::mr::device_memory_resource* mr)
{
return detail::pairwise_point_distance(
points1_x, points1_y, points2_x, points2_y, rmm::cuda_stream_default, mr);
}

} // namespace cuspatial
3 changes: 3 additions & 0 deletions cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ ConfigureTest(SPATIAL_WINDOW_POINT_TEST
ConfigureTest(HAVERSINE_TEST_EXP
experimental/spatial/haversine_test.cu)

ConfigureTest(POINT_DISTANCE_TEST_EXP
isVoid marked this conversation as resolved.
Show resolved Hide resolved
experimental/spatial/point_distance_test.cu)

ConfigureTest(LINESTRING_DISTANCE_TEST_EXP
experimental/spatial/linestring_distance_test.cu)

Expand Down
Loading