/
ArborX_NeighborList.hpp
149 lines (123 loc) · 5.3 KB
/
ArborX_NeighborList.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/****************************************************************************
* Copyright (c) 2017-2023 by the ArborX authors *
* All rights reserved. *
* *
* This file is part of the ArborX library. ArborX is *
* distributed under a BSD 3-clause license. For the licensing terms see *
* the LICENSE file in the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
#ifndef ARBORX_NEIGHBOR_LIST_HPP
#define ARBORX_NEIGHBOR_LIST_HPP
#include <ArborX_DetailsHalfTraversal.hpp>
#include <ArborX_DetailsKokkosExtViewHelpers.hpp> // reallocWithoutInitializing
#include <ArborX_DetailsUtils.hpp> // exclusivePrefixSum
#include <ArborX_LinearBVH.hpp>
#include <ArborX_Sphere.hpp>
#include <Kokkos_Core.hpp>
namespace ArborX::Experimental
{
struct NeighborListPredicateGetter
{
float _radius;
KOKKOS_FUNCTION auto operator()(Box b) const
{
return intersects(Sphere{b.minCorner(), _radius});
}
};
template <class ExecutionSpace, class Primitives, class Offsets, class Indices>
void findHalfNeighborList(ExecutionSpace const &space,
Primitives const &primitives, float radius,
Offsets &offsets, Indices &indices)
{
Kokkos::Profiling::pushRegion("ArborX::Experimental::HalfNeighborList");
using Details::HalfTraversal;
using MemorySpace =
typename AccessTraits<Primitives, PrimitivesTag>::memory_space;
BVH<MemorySpace> bvh(space, primitives);
int const n = bvh.size();
Kokkos::Profiling::pushRegion(
"ArborX::Experimental::HalfNeighborList::Count");
KokkosBlah::reallocWithoutInitializing(space, offsets, n + 1);
Kokkos::deep_copy(space, offsets, 0);
HalfTraversal(
space, bvh,
KOKKOS_LAMBDA(int, int j) { Kokkos::atomic_increment(&offsets(j)); },
NeighborListPredicateGetter{radius});
exclusivePrefixSum(space, offsets);
KokkosBlah::reallocWithoutInitializing(
space, indices, KokkosBlah::lastElement(space, offsets));
Kokkos::Profiling::popRegion();
Kokkos::Profiling::pushRegion("ArborX::Experimental::HalfNeighborList::Fill");
auto counts =
KokkosBlah::clone(space, Kokkos::subview(offsets, std::make_pair(0, n)),
"ArborX::Experimental::HalfNeighborList::counts");
HalfTraversal(
space, bvh,
KOKKOS_LAMBDA(int i, int j) {
indices(Kokkos::atomic_fetch_inc(&counts(j))) = i;
},
NeighborListPredicateGetter{radius});
Kokkos::Profiling::popRegion();
Kokkos::Profiling::popRegion();
}
template <class ExecutionSpace, class Primitives, class Offsets, class Indices>
void findFullNeighborList(ExecutionSpace const &space,
Primitives const &primitives, float radius,
Offsets &offsets, Indices &indices)
{
Kokkos::Profiling::pushRegion("ArborX::Experimental::FullNeighborList");
using Details::HalfTraversal;
using MemorySpace =
typename AccessTraits<Primitives, PrimitivesTag>::memory_space;
BVH<MemorySpace> bvh(space, primitives);
int const n = bvh.size();
Kokkos::Profiling::pushRegion(
"ArborX::Experimental::FullNeighborList::Count");
KokkosBlah::reallocWithoutInitializing(space, offsets, n + 1);
Kokkos::deep_copy(space, offsets, 0);
HalfTraversal(
space, bvh,
KOKKOS_LAMBDA(int i, int j) {
Kokkos::atomic_increment(&offsets(i));
Kokkos::atomic_increment(&offsets(j));
},
NeighborListPredicateGetter{radius});
exclusivePrefixSum(space, offsets);
KokkosBlah::reallocWithoutInitializing(
space, indices, KokkosBlah::lastElement(space, offsets));
Kokkos::Profiling::popRegion();
Kokkos::Profiling::pushRegion("ArborX::Experimental::FullNeighborList::Fill");
auto counts =
KokkosBlah::clone(space, Kokkos::subview(offsets, std::make_pair(0, n)),
"ArborX::Experimental::FullNeighborList::counts");
HalfTraversal(
space, bvh,
KOKKOS_LAMBDA(int i, int j) {
indices(Kokkos::atomic_fetch_inc(&counts(j))) = i;
},
NeighborListPredicateGetter{radius});
Kokkos::Profiling::popRegion();
Kokkos::Profiling::pushRegion("ArborX::Experimental::FullNeighborList::Copy");
auto counts_copy = KokkosBlah::clone(space, counts, counts.label() + "_copy");
Kokkos::parallel_for(
"ArborX::Experimental::FullNeighborList::Copy",
Kokkos::TeamPolicy<ExecutionSpace>(space, n, Kokkos::AUTO, 1),
KOKKOS_LAMBDA(
typename Kokkos::TeamPolicy<ExecutionSpace>::member_type const
&member) {
auto const i = member.league_rank();
auto const first = offsets(i);
auto const last = counts_copy(i);
Kokkos::parallel_for(
Kokkos::TeamVectorRange(member, last - first), [&](int j) {
int const k = indices(first + j);
indices(Kokkos::atomic_fetch_inc(&counts(k))) = i;
});
});
Kokkos::Profiling::popRegion();
Kokkos::Profiling::popRegion();
}
} // namespace ArborX::Experimental
#endif