1 #ifndef _COMPADRE_NEIGHBORLISTS_HPP_
2 #define _COMPADRE_NEIGHBORLISTS_HPP_
5 #include <Kokkos_Core.hpp>
10 template <
typename view_type>
46 NeighborLists(view_type cr_neighbor_lists, view_type number_of_neighbors_list,
49 "cr_neighbor_lists and number_neighbors_list and neighbor_lists_row_offsets must be a 1D Kokkos view.");
74 &&
"neighbor_lists is not large enough to store all neighbors.");
82 NeighborLists(view_type cr_neighbor_lists, view_type number_of_neighbors_list) {
84 &&
"cr_neighbor_lists and number_neighbors_list must be a 1D Kokkos view.");
106 &&
"neighbor_lists is not large enough to store all neighbors.");
117 &&
"cr_neighbor_lists and number_neighbors_list must be a 1D Kokkos view.");
148 KOKKOS_INLINE_FUNCTION
162 Kokkos::parallel_reduce(
"max number of neighbors",
164 KOKKOS_LAMBDA(
const int i,
int& t_max_num_neighbors) {
165 t_max_num_neighbors = (number_of_neighbors_list(i) > t_max_num_neighbors) ? number_of_neighbors_list(i) : t_max_num_neighbors;
177 Kokkos::parallel_reduce(
"min number of neighbors",
179 KOKKOS_LAMBDA(
const int i,
int& t_min_num_neighbors) {
180 t_min_num_neighbors = (number_of_neighbors_list(i) < t_min_num_neighbors) ? number_of_neighbors_list(i) : t_min_num_neighbors;
190 Kokkos::parallel_scan(
"number of neighbors offsets",
193 row_offsets(i) = lsum;
194 lsum += number_of_neighbors_list(i);
222 KOKKOS_INLINE_FUNCTION
234 KOKKOS_INLINE_FUNCTION
247 KOKKOS_INLINE_FUNCTION
257 &&
"Stale information in host_cr_neighbor_lists. Call CopyDeviceDataToHost() to refresh.");
259 &&
"neighor_num exceeds number of neighbors for this target_index.");
264 KOKKOS_INLINE_FUNCTION
267 &&
"neighor_num exceeds number of neighbors for this target_index.");
273 KOKKOS_INLINE_FUNCTION
280 KOKKOS_INLINE_FUNCTION
296 KOKKOS_INLINE_FUNCTION
305 template <
typename view_type>
311 template <
typename view_type>
317 template <
typename view_type>
323 template <
typename view_type_2d,
typename view_type_1d = Kokkos::View<
int*,
typename view_type_2d::memory_space,
typename view_type_2d::memory_traits> >
329 Kokkos::parallel_reduce(
"total number of neighbors over all lists", Kokkos::RangePolicy<typename view_type_2d::execution_space>(0, neighbor_lists.extent(0)),
331 t_total_num_neighbors += neighbor_lists(i,0);
332 }, Kokkos::Sum<global_index_type>(total_storage_size));
336 view_type_1d new_cr_neighbor_lists(
"compressed row neighbor lists", total_storage_size);
337 view_type_1d new_number_of_neighbors_list(
"number of neighbors list", neighbor_lists.extent(0));
341 auto d_neighbor_lists = create_mirror_view(
typename view_type_1d::execution_space(), neighbor_lists);
342 Kokkos::deep_copy(d_neighbor_lists, neighbor_lists);
344 Kokkos::parallel_for(
"copy number of neighbors to compressed row",
345 Kokkos::RangePolicy<typename view_type_1d::execution_space>(0, neighbor_lists.extent(0)),
346 KOKKOS_LAMBDA(
const int i) {
347 new_number_of_neighbors_list(i) = d_neighbor_lists(i,0);
354 auto cr_data = nla.getNeighborLists();
357 if (Kokkos::SpaceAccessibility<device_execution_space, typename view_type_1d::memory_space>::accessible==1) {
358 Kokkos::parallel_for(
"copy neighbor lists to compressed row", Kokkos::RangePolicy<typename view_type_1d::execution_space>(0, neighbor_lists.extent(0)),
359 KOKKOS_LAMBDA(
const int i) {
360 for (
int j=0; j<d_neighbor_lists(i,0); ++j) {
361 cr_data(nla.getRowOffsetDevice(i)+j) = d_neighbor_lists(i,j+1);
365 nla.copyDeviceDataToHost();
371 Kokkos::parallel_for(
"copy neighbor lists to compressed row", Kokkos::RangePolicy<host_execution_space>(0, neighbor_lists.extent(0)),
372 KOKKOS_LAMBDA(
const int i) {
373 for (
int j=0; j<neighbor_lists(i,0); ++j) {
374 cr_data(nla.getRowOffsetHost(i)+j) = d_neighbor_lists(i,j+1);
#define compadre_kernel_assert_debug(condition)
std::size_t global_index_type
#define compadre_assert_extreme_debug(condition)
compadre_kernel_assert_debug is similar to compadre_assert_debug, but is a call on the device,...
#define compadre_assert_debug(condition)
compadre_assert_debug is used for assertions that are checked in loops, as these significantly impact...
#define TO_GLOBAL(variable)
#define compadre_kernel_assert_extreme_debug(condition)
#define compadre_assert_release(condition)
compadre_assert_release is used for assertions that should always be checked, but generally are not e...
NeighborLists< view_type_1d > Convert2DToCompressedRowNeighborLists(view_type_2d neighbor_lists)
Converts 2D neighbor lists to compressed row neighbor lists.
NeighborLists< view_type > CreateNeighborLists(view_type number_of_neighbors_list)
CreateNeighborLists allows for the construction of an object of type NeighborLists with template dedu...
NeighborLists assists in accessing entries of compressed row neighborhood lists.
view_type _cr_neighbor_lists
view_type _number_of_neighbors_list
KOKKOS_INLINE_FUNCTION global_index_type getTotalNeighborsOverAllListsDevice() const
Get the sum of the number of neighbors of all targets' neighborhoods (device)
view_type::HostMirror _host_number_of_neighbors_list
void computeMinNumNeighbors()
Calculate the minimum number of neighbors of all targets' neighborhoods (host)
KOKKOS_INLINE_FUNCTION int getMinNumNeighbors() const
Get the minimum number of neighbors of all targets' neighborhoods (host/device)
view_type getNeighborLists() const
Device view into neighbor lists data (use with caution)
global_index_type getRowOffsetHost(int target_index) const
Get offset into compressed row neighbor lists (host)
KOKKOS_INLINE_FUNCTION global_index_type getRowOffsetDevice(int target_index) const
Get offset into compressed row neighbor lists (device)
NeighborLists(view_type number_of_neighbors_list)
Constructor for when number_of_neighbors_list is already populated. Will allocate space for compresse...
view_type internal_view_type
int _max_neighbor_list_row_storage_size
internal_row_offsets_view_type::HostMirror _host_row_offsets
int getNeighborHost(int target_index, int neighbor_num) const
Offers N(i,j) indexing where N(i,j) is the index of the jth neighbor of i (host)
Kokkos::View< global_index_type *, typename view_type::array_layout, typename view_type::memory_space, typename view_type::memory_traits > internal_row_offsets_view_type
view_type getNumberOfNeighborsList() const
Device view into number of neighbors list (use with caution)
KOKKOS_INLINE_FUNCTION int getNumberOfNeighborsDevice(int target_index) const
Get number of neighbors for a given target (device)
KOKKOS_INLINE_FUNCTION int getNeighborDevice(int target_index, int neighbor_num) const
Offers N(i,j) indexing where N(i,j) is the index of the jth neighbor of i (device)
view_type::HostMirror _host_cr_neighbor_lists
global_index_type getTotalNeighborsOverAllListsHost() const
Get the sum of the number of neighbors of all targets' neighborhoods (host)
NeighborLists(view_type cr_neighbor_lists, view_type number_of_neighbors_list, internal_row_offsets_view_type neighbor_lists_row_offsets, bool compute_max=true)
Constructor for when compressed row cr_neighbor_lists is preallocated/populated, number_of_neighbors_...
void computeRowOffsets()
Calculate the row offsets for each target's neighborhood (host)
NeighborLists(view_type cr_neighbor_lists, view_type number_of_neighbors_list)
Constructor for when compressed row cr_neighbor_lists is preallocated/populated, and number_of_neighb...
KOKKOS_INLINE_FUNCTION int getMaxNumNeighbors() const
Get the maximum number of neighbors of all targets' neighborhoods (host/device)
int _min_neighbor_list_row_storage_size
internal_row_offsets_view_type _row_offsets
KOKKOS_INLINE_FUNCTION int getNumberOfTargets() const
Get number of total targets having neighborhoods (host/device).
KOKKOS_INLINE_FUNCTION void setNeighborDevice(int target_index, int neighbor_num, int new_value)
Setter function for N(i,j) indexing where N(i,j) is the index of the jth neighbor of i.
NeighborLists()
Constructor for the purpose of classes who have NeighborLists as a member object.
void copyDeviceDataToHost()
Sync the host from the device (copy device data to host)
int getNumberOfNeighborsHost(int target_index) const
Get number of neighbors for a given target (host)
void computeMaxNumNeighbors()
Calculate the maximum number of neighbors of all targets' neighborhoods (host)