|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, NVIDIA CORPORATION. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <cuspatial/cuda_utils.hpp> |
| 20 | +#include <cuspatial/detail/utility/zero_data.cuh> |
| 21 | +#include <cuspatial/error.hpp> |
| 22 | +#include <cuspatial/geometry/vec_2d.hpp> |
| 23 | +#include <cuspatial/iterator_factory.cuh> |
| 24 | +#include <cuspatial/range/multipoint_range.cuh> |
| 25 | +#include <cuspatial/range/range.cuh> |
| 26 | +#include <cuspatial/traits.hpp> |
| 27 | + |
| 28 | +#include <rmm/cuda_stream_view.hpp> |
| 29 | +#include <rmm/device_uvector.hpp> |
| 30 | +#include <rmm/exec_policy.hpp> |
| 31 | + |
| 32 | +#include <thrust/binary_search.h> |
| 33 | +#include <thrust/sort.h> |
| 34 | +#include <thrust/transform.h> |
| 35 | + |
| 36 | +#include <iterator> |
| 37 | +#include <type_traits> |
| 38 | + |
| 39 | +namespace cuspatial { |
| 40 | + |
| 41 | +namespace detail { |
| 42 | + |
| 43 | +template <class MultiPointRangeA, class MultiPointRangeB, class OutputIt> |
| 44 | +void __global__ pairwise_multipoint_equals_count_kernel(MultiPointRangeA lhs, |
| 45 | + MultiPointRangeB rhs, |
| 46 | + OutputIt output) |
| 47 | +{ |
| 48 | + using T = typename MultiPointRangeA::point_t::value_type; |
| 49 | + |
| 50 | + for (auto idx = threadIdx.x + blockIdx.x * blockDim.x; idx < lhs.num_points(); |
| 51 | + idx += gridDim.x * blockDim.x) { |
| 52 | + auto geometry_id = lhs.geometry_idx_from_point_idx(idx); |
| 53 | + vec_2d<T> lhs_point = lhs.point_begin()[idx]; |
| 54 | + auto rhs_multipoint = rhs[geometry_id]; |
| 55 | + |
| 56 | + atomicAdd( |
| 57 | + &output[geometry_id], |
| 58 | + thrust::binary_search(thrust::seq, rhs_multipoint.begin(), rhs_multipoint.end(), lhs_point)); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +} // namespace detail |
| 63 | + |
| 64 | +template <class MultiPointRangeA, class MultiPointRangeB, class OutputIt> |
| 65 | +OutputIt pairwise_multipoint_equals_count(MultiPointRangeA lhs, |
| 66 | + MultiPointRangeB rhs, |
| 67 | + OutputIt output, |
| 68 | + rmm::cuda_stream_view stream) |
| 69 | +{ |
| 70 | + using T = typename MultiPointRangeA::point_t::value_type; |
| 71 | + using index_t = typename MultiPointRangeB::index_t; |
| 72 | + |
| 73 | + static_assert(is_same_floating_point<T, typename MultiPointRangeB::point_t::value_type>(), |
| 74 | + "Origin and input must have the same base floating point type."); |
| 75 | + |
| 76 | + CUSPATIAL_EXPECTS(lhs.size() == rhs.size(), "lhs and rhs inputs should have the same size."); |
| 77 | + |
| 78 | + if (lhs.size() == 0) return output; |
| 79 | + |
| 80 | + // Create a sorted copy of the rhs points. |
| 81 | + auto key_it = make_geometry_id_iterator<index_t>(rhs.offsets_begin(), rhs.offsets_end()); |
| 82 | + |
| 83 | + rmm::device_uvector<index_t> rhs_keys(rhs.num_points(), stream); |
| 84 | + rmm::device_uvector<vec_2d<T>> rhs_point_sorted(rhs.num_points(), stream); |
| 85 | + |
| 86 | + thrust::copy(rmm::exec_policy(stream), key_it, key_it + rhs.num_points(), rhs_keys.begin()); |
| 87 | + thrust::copy( |
| 88 | + rmm::exec_policy(stream), rhs.point_begin(), rhs.point_end(), rhs_point_sorted.begin()); |
| 89 | + |
| 90 | + auto rhs_with_keys = |
| 91 | + thrust::make_zip_iterator(thrust::make_tuple(rhs_keys.begin(), rhs_point_sorted.begin())); |
| 92 | + |
| 93 | + thrust::sort(rmm::exec_policy(stream), rhs_with_keys, rhs_with_keys + rhs.num_points()); |
| 94 | + |
| 95 | + auto rhs_sorted = multipoint_range{ |
| 96 | + rhs.offsets_begin(), rhs.offsets_end(), rhs_point_sorted.begin(), rhs_point_sorted.end()}; |
| 97 | + |
| 98 | + detail::zero_data_async(output, output + lhs.size(), stream); |
| 99 | + auto [tpb, n_blocks] = grid_1d(lhs.num_points()); |
| 100 | + detail::pairwise_multipoint_equals_count_kernel<<<n_blocks, tpb, 0, stream.value()>>>( |
| 101 | + lhs, rhs_sorted, output); |
| 102 | + |
| 103 | + CUSPATIAL_CHECK_CUDA(stream.value()); |
| 104 | + |
| 105 | + return output + lhs.size(); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace cuspatial |
0 commit comments