244 neighbor_lists_view_type neighbor_lists, epsilons_view_type epsilons,
245 const double uniform_radius = 0.0,
double max_search_radius = 0.0) {
249 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename trg_view_type::memory_space>::accessible==1) &&
250 "Target coordinates view passed to generate2DNeighborListsFromRadiusSearch should be accessible from the host.");
252 "Target coordinates view passed to generate2DNeighborListsFromRadiusSearch must have \
253 second dimension as large as _dim.");
254 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename neighbor_lists_view_type::memory_space>::accessible==1) &&
255 "Views passed to generate2DNeighborListsFromRadiusSearch should be accessible from the host.");
256 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename epsilons_view_type::memory_space>::accessible==1) &&
257 "Views passed to generate2DNeighborListsFromRadiusSearch should be accessible from the host.");
260 const size_t num_target_sites = trg_pts_view.extent(0);
268 && neighbor_lists.extent(1)>=1)
269 &&
"neighbor lists View does not have large enough dimensions");
273 &&
"epsilons View does not have the correct dimension");
275 typedef Kokkos::View<double*, host_scratch, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
278 typedef Kokkos::View<size_t*, host_scratch, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
282 size_t team_scratch_size = 0;
283 team_scratch_size += scratch_double_view::shmem_size(neighbor_lists.extent(1));
284 team_scratch_size += scratch_int_view::shmem_size(neighbor_lists.extent(1));
285 team_scratch_size += scratch_double_view::shmem_size(
_dim);
289 size_t max_num_neighbors = 0;
292 auto scratch_level = (team_scratch_size > 32000) ? 1 : 0;
296 .set_scratch_size(scratch_level , Kokkos::PerTeam(team_scratch_size));
297 auto radius_search = [=,*
this](
const host_member_type& teamMember,
size_t& t_max_num_neighbors) {
300 scratch_double_view neighbor_distances(teamMember.team_scratch(scratch_level ), neighbor_lists.extent(1));
301 scratch_int_view neighbor_indices(teamMember.team_scratch(scratch_level ), neighbor_lists.extent(1));
302 scratch_double_view this_target_coord(teamMember.team_scratch(scratch_level ), dim);
304 size_t neighbors_found = 0;
306 const int i = teamMember.league_rank();
309 if (uniform_radius > 0) epsilons(i) = uniform_radius;
312 compadre_kernel_assert_release((epsilons(i)<=max_search_radius || max_search_radius==0) &&
"max_search_radius given (generally derived from the size of a halo region), and search radius needed would exceed this max_search_radius.");
314 Kokkos::parallel_for(Kokkos::TeamThreadRange(teamMember, neighbor_lists.extent(1)), [&](
const int j) {
315 neighbor_indices(j) = 0;
316 neighbor_distances(j) = -1.0;
318 teamMember.team_barrier();
320 Kokkos::single(Kokkos::PerTeam(teamMember), [&] () {
324 for (
int j=0; j<dim; ++j) {
325 this_target_coord(j) = trg_pts_view(i,j);
328 nanoflann::SearchParams sp;
331 neighbors_found =
_tree_1d->template radiusSearchCustomCallback<Compadre::RadiusResultSet<double> >(this_target_coord.data(), rrs, sp) ;
333 neighbors_found =
_tree_2d->template radiusSearchCustomCallback<Compadre::RadiusResultSet<double> >(this_target_coord.data(), rrs, sp) ;
335 neighbors_found =
_tree_3d->template radiusSearchCustomCallback<Compadre::RadiusResultSet<double> >(this_target_coord.data(), rrs, sp) ;
338 t_max_num_neighbors = (neighbors_found > t_max_num_neighbors) ? neighbors_found : t_max_num_neighbors;
341 neighbor_lists(i,0) = neighbors_found;
345 teamMember.team_barrier();
348 int loop_bound = (neighbors_found < neighbor_lists.extent(1)-1) ? neighbors_found : neighbor_lists.extent(1)-1;
351 Kokkos::parallel_for(Kokkos::TeamThreadRange(teamMember, loop_bound), [&](
const int j) {
353 neighbor_lists(i,j+1) =
static_cast<typename std::remove_pointer<typename std::remove_pointer<typename neighbor_lists_view_type::data_type>::type
>::type>(neighbor_indices(j));
355 teamMember.team_barrier();
359 Kokkos::parallel_reduce(
"radius search 2D", policy, radius_search, Kokkos::Max<size_t>(max_num_neighbors));
364 &&
"neighbor_lists does not contain enough columns for the maximum number of neighbors needing to be stored.");
366 return max_num_neighbors;
383 neighbor_lists_view_type neighbor_lists, neighbor_lists_view_type number_of_neighbors_list,
384 epsilons_view_type epsilons,
const double uniform_radius = 0.0,
double max_search_radius = 0.0) {
388 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename trg_view_type::memory_space>::accessible==1) &&
389 "Target coordinates view passed to generateCRNeighborListsFromRadiusSearch should be accessible from the host.");
391 "Target coordinates view passed to generateCRNeighborListsFromRadiusSearch must have \
392 second dimension as large as _dim.");
393 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename neighbor_lists_view_type::memory_space>::accessible==1) &&
394 "Views passed to generateCRNeighborListsFromRadiusSearch should be accessible from the host.");
395 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename epsilons_view_type::memory_space>::accessible==1) &&
396 "Views passed to generateCRNeighborListsFromRadiusSearch should be accessible from the host.");
399 const size_t num_target_sites = trg_pts_view.extent(0);
406 &&
"number_of_neighbors_list or neighbor lists View does not have large enough dimensions");
409 typedef Kokkos::View<
global_index_type*,
typename neighbor_lists_view_type::array_layout,
410 typename neighbor_lists_view_type::memory_space,
typename neighbor_lists_view_type::memory_traits> row_offsets_view_type;
411 row_offsets_view_type row_offsets;
412 int max_neighbor_list_row_storage_size = 1;
415 max_neighbor_list_row_storage_size = nla.getMaxNumNeighbors();
416 Kokkos::resize(row_offsets, num_target_sites);
418 Kokkos::parallel_for(Kokkos::RangePolicy<host_execution_space>(0,num_target_sites), [&](
const int i) {
419 row_offsets(i) = nla.getRowOffsetHost(i);
425 &&
"epsilons View does not have the correct dimension");
427 typedef Kokkos::View<double*, host_scratch, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
430 typedef Kokkos::View<size_t*, host_scratch, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
434 size_t team_scratch_size = 0;
435 team_scratch_size += scratch_double_view::shmem_size(max_neighbor_list_row_storage_size);
436 team_scratch_size += scratch_int_view::shmem_size(max_neighbor_list_row_storage_size);
437 team_scratch_size += scratch_double_view::shmem_size(
_dim);
442 auto scratch_level = (team_scratch_size > 32000) ? 1 : 0;
446 .set_scratch_size(scratch_level , Kokkos::PerTeam(team_scratch_size));
450 scratch_double_view neighbor_distances(teamMember.team_scratch(scratch_level ), max_neighbor_list_row_storage_size);
451 scratch_int_view neighbor_indices(teamMember.team_scratch(scratch_level ), max_neighbor_list_row_storage_size);
452 scratch_double_view this_target_coord(teamMember.team_scratch(scratch_level ), dim);
454 size_t neighbors_found = 0;
456 const int i = teamMember.league_rank();
459 if (uniform_radius > 0) epsilons(i) = uniform_radius;
462 compadre_kernel_assert_release((epsilons(i)<=max_search_radius || max_search_radius==0) &&
"max_search_radius given (generally derived from the size of a halo region), and search radius needed would exceed this max_search_radius.");
464 Kokkos::parallel_for(Kokkos::TeamThreadRange(teamMember, max_neighbor_list_row_storage_size), [&](
const int j) {
465 neighbor_indices(j) = 0;
466 neighbor_distances(j) = -1.0;
468 teamMember.team_barrier();
470 Kokkos::single(Kokkos::PerTeam(teamMember), [&] () {
474 for (
int j=0; j<dim; ++j) {
475 this_target_coord(j) = trg_pts_view(i,j);
478 nanoflann::SearchParams sp;
481 neighbors_found =
_tree_1d->template radiusSearchCustomCallback<Compadre::RadiusResultSet<double> >(this_target_coord.data(), rrs, sp) ;
483 neighbors_found =
_tree_2d->template radiusSearchCustomCallback<Compadre::RadiusResultSet<double> >(this_target_coord.data(), rrs, sp) ;
485 neighbors_found =
_tree_3d->template radiusSearchCustomCallback<Compadre::RadiusResultSet<double> >(this_target_coord.data(), rrs, sp) ;
490 if (is_dry_run || uniform_radius!=0.0) {
491 number_of_neighbors_list(i) = neighbors_found;
494 &&
"Number of neighbors found changed since dry-run.");
499 teamMember.team_barrier();
502 int loop_bound = neighbors_found;
505 Kokkos::parallel_for(Kokkos::TeamThreadRange(teamMember, loop_bound), [&](
const int j) {
507 neighbor_lists(row_offsets(i)+j) =
static_cast<typename std::remove_pointer<typename std::remove_pointer<typename neighbor_lists_view_type::data_type>::type
>::type>(neighbor_indices(j));
509 teamMember.team_barrier();
513 Kokkos::parallel_for(
"radius search CR", policy, radius_search);
515 size_t total_neighbors = 0;
516 for (
size_t i=0; i<num_target_sites; ++i) {
517 total_neighbors +=
static_cast<size_t>(number_of_neighbors_list(i));
520 return total_neighbors;
537 neighbor_lists_view_type neighbor_lists, epsilons_view_type epsilons,
538 const int neighbors_needed,
const double epsilon_multiplier = 1.6,
539 double max_search_radius = 0.0,
const bool verify_unisolvency =
true) {
543 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename trg_view_type::memory_space>::accessible==1) &&
544 "Target coordinates view passed to generate2DNeighborListsFromKNNSearch should be accessible from the host.");
546 "Target coordinates view passed to generate2DNeighborListsFromRadiusSearch must have \
547 second dimension as large as _dim.");
548 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename neighbor_lists_view_type::memory_space>::accessible==1) &&
549 "Views passed to generate2DNeighborListsFromKNNSearch should be accessible from the host.");
550 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename epsilons_view_type::memory_space>::accessible==1) &&
551 "Views passed to generate2DNeighborListsFromKNNSearch should be accessible from the host.");
554 const size_t num_target_sites = trg_pts_view.extent(0);
562 (neighbor_lists.extent(0)==num_target_sites
563 && neighbor_lists.extent(1)>=(
size_t)(neighbors_needed+1)))
564 &&
"neighbor lists View does not have large enough dimensions");
568 &&
"epsilons View does not have the correct dimension");
570 typedef Kokkos::View<double*, host_scratch, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
573 typedef Kokkos::View<size_t*, host_scratch, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
577 size_t team_scratch_size = 0;
578 team_scratch_size += scratch_double_view::shmem_size(neighbor_lists.extent(1));
579 team_scratch_size += scratch_int_view::shmem_size(neighbor_lists.extent(1));
580 team_scratch_size += scratch_double_view::shmem_size(
_dim);
584 size_t min_num_neighbors = 0;
592 auto scratch_level = (team_scratch_size > 32000) ? 1 : 0;
596 .set_scratch_size(scratch_level , Kokkos::PerTeam(team_scratch_size));
597 auto knn_search = [=,*
this](
const host_member_type& teamMember,
size_t& t_min_num_neighbors) {
600 scratch_double_view neighbor_distances(teamMember.team_scratch(scratch_level ), neighbor_lists.extent(1));
601 scratch_int_view neighbor_indices(teamMember.team_scratch(scratch_level ), neighbor_lists.extent(1));
602 scratch_double_view this_target_coord(teamMember.team_scratch(scratch_level ), dim);
604 size_t neighbors_found = 0;
606 const int i = teamMember.league_rank();
608 Kokkos::parallel_for(Kokkos::TeamThreadRange(teamMember, neighbor_lists.extent(1)), [=](
const int j) {
609 neighbor_indices(j) = 0;
610 neighbor_distances(j) = -1.0;
613 teamMember.team_barrier();
614 Kokkos::single(Kokkos::PerTeam(teamMember), [&] () {
618 for (
int j=0; j<dim; ++j) {
619 this_target_coord(j) = trg_pts_view(i,j);
623 neighbors_found =
_tree_1d->knnSearch(this_target_coord.data(), neighbors_needed,
624 neighbor_indices.data(), neighbor_distances.data()) ;
626 neighbors_found =
_tree_2d->knnSearch(this_target_coord.data(), neighbors_needed,
627 neighbor_indices.data(), neighbor_distances.data()) ;
629 neighbors_found =
_tree_3d->knnSearch(this_target_coord.data(), neighbors_needed,
630 neighbor_indices.data(), neighbor_distances.data()) ;
634 t_min_num_neighbors = (neighbors_found < t_min_num_neighbors) ? neighbors_found : t_min_num_neighbors;
637 epsilons(i) = (neighbor_distances(neighbors_found-1) > 0) ?
638 std::sqrt(neighbor_distances(neighbors_found-1))*epsilon_multiplier : 1e-14*epsilon_multiplier;
646 &&
"Neighbors found exceeded storage capacity in neighbor list.");
648 &&
"max_search_radius given (generally derived from the size of a halo region), \
649 and search radius needed would exceed this max_search_radius.");
653 Kokkos::parallel_reduce(
"knn search 2D", policy, knn_search, Kokkos::Min<size_t>(min_num_neighbors));
657 if (num_target_sites==0) min_num_neighbors = neighbors_needed;
659 if (verify_unisolvency) {
662 &&
"Neighbor search failed to find number of neighbors needed for unisolvency.");
667 epsilons, 0.0 , max_search_radius);
669 return max_num_neighbors;
686 neighbor_lists_view_type neighbor_lists, neighbor_lists_view_type number_of_neighbors_list,
687 epsilons_view_type epsilons,
const int neighbors_needed,
const double epsilon_multiplier = 1.6,
688 double max_search_radius = 0.0,
const bool verify_unisolvency =
true) {
692 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename trg_view_type::memory_space>::accessible==1) &&
693 "Target coordinates view passed to generateCRNeighborListsFromKNNSearch should be accessible from the host.");
695 "Target coordinates view passed to generateCRNeighborListsFromRadiusSearch must have \
696 second dimension as large as _dim.");
697 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename neighbor_lists_view_type::memory_space>::accessible==1) &&
698 "Views passed to generateCRNeighborListsFromKNNSearch should be accessible from the host.");
699 compadre_assert_release((Kokkos::SpaceAccessibility<host_execution_space, typename epsilons_view_type::memory_space>::accessible==1) &&
700 "Views passed to generateCRNeighborListsFromKNNSearch should be accessible from the host.");
703 const size_t num_target_sites = trg_pts_view.extent(0);
711 &&
"number_of_neighbors_list or neighbor lists View does not have large enough dimensions");
715 int max_neighbor_list_row_storage_size = neighbors_needed;
718 max_neighbor_list_row_storage_size = nla.getMaxNumNeighbors();
722 &&
"epsilons View does not have the correct dimension");
724 typedef Kokkos::View<double*, host_scratch, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
727 typedef Kokkos::View<size_t*, host_scratch, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
731 size_t team_scratch_size = 0;
732 team_scratch_size += scratch_double_view::shmem_size(max_neighbor_list_row_storage_size);
733 team_scratch_size += scratch_int_view::shmem_size(max_neighbor_list_row_storage_size);
734 team_scratch_size += scratch_double_view::shmem_size(
_dim);
738 size_t min_num_neighbors = 0;
746 auto scratch_level = (team_scratch_size > 32000) ? 1 : 0;
750 .set_scratch_size(scratch_level , Kokkos::PerTeam(team_scratch_size));
751 auto knn_search = [=,*
this](
const host_member_type& teamMember,
size_t& t_min_num_neighbors) {
754 scratch_double_view neighbor_distances(teamMember.team_scratch(scratch_level ), max_neighbor_list_row_storage_size);
755 scratch_int_view neighbor_indices(teamMember.team_scratch(scratch_level ), max_neighbor_list_row_storage_size);
756 scratch_double_view this_target_coord(teamMember.team_scratch(scratch_level ), dim);
758 size_t neighbors_found = 0;
760 const int i = teamMember.league_rank();
762 Kokkos::parallel_for(Kokkos::TeamThreadRange(teamMember, max_neighbor_list_row_storage_size), [=](
const int j) {
763 neighbor_indices(j) = 0;
764 neighbor_distances(j) = -1.0;
767 teamMember.team_barrier();
768 Kokkos::single(Kokkos::PerTeam(teamMember), [&] () {
772 for (
int j=0; j<dim; ++j) {
773 this_target_coord(j) = trg_pts_view(i,j);
777 neighbors_found =
_tree_1d->knnSearch(this_target_coord.data(), neighbors_needed,
778 neighbor_indices.data(), neighbor_distances.data()) ;
780 neighbors_found =
_tree_2d->knnSearch(this_target_coord.data(), neighbors_needed,
781 neighbor_indices.data(), neighbor_distances.data()) ;
783 neighbors_found =
_tree_3d->knnSearch(this_target_coord.data(), neighbors_needed,
784 neighbor_indices.data(), neighbor_distances.data()) ;
788 t_min_num_neighbors = (neighbors_found < t_min_num_neighbors) ? neighbors_found : t_min_num_neighbors;
792 if (neighbors_found > 0 && neighbor_distances(neighbors_found-1) > 0) {
793 epsilons(i) = std::sqrt(neighbor_distances(neighbors_found-1))*epsilon_multiplier;
795 epsilons(i) = 1e-14*epsilon_multiplier;
803 &&
"max_search_radius given (generally derived from the size of a halo region), \
804 and search radius needed would exceed this max_search_radius.");
808 Kokkos::parallel_reduce(
"knn search CR", policy, knn_search, Kokkos::Min<size_t>(min_num_neighbors));
812 if (num_target_sites==0) min_num_neighbors = neighbors_needed;
814 if (verify_unisolvency) {
817 &&
"Neighbor search failed to find number of neighbors needed for unisolvency.");
822 number_of_neighbors_list, epsilons, 0.0 , max_search_radius);
824 size_t total_neighbors = 0;
825 for (
size_t i=0; i<num_target_sites; ++i) {
826 total_neighbors +=
static_cast<size_t>(number_of_neighbors_list(i));
829 return total_neighbors;