10 #ifndef TPETRA_DETAILS_UNPACKCRSGRAPHANDCOMBINE_DEF_HPP
11 #define TPETRA_DETAILS_UNPACKCRSGRAPHANDCOMBINE_DEF_HPP
13 #include "TpetraCore_config.h"
14 #include "Teuchos_Array.hpp"
15 #include "Teuchos_ArrayView.hpp"
24 #include "Kokkos_Core.hpp"
54 namespace UnpackAndCombineCrsGraphImpl {
65 template<
class Packet,
class GO,
class Device,
class BufferDevice>
67 unpackRow (
const Kokkos::View<GO*,Device,Kokkos::MemoryUnmanaged>& gids_out,
68 const Kokkos::View<int*,Device,Kokkos::MemoryUnmanaged>& pids_out,
69 const Kokkos::View<const Packet*,BufferDevice>& imports,
73 using size_type =
typename Kokkos::View<GO*,Device>::size_type;
81 for (size_type k=0; k<num_ent; k++)
82 gids_out(k) = imports(offset+k);
85 if (pids_out.size() > 0) {
86 for (size_type k=0; k<num_ent; k++) {
87 pids_out(k) =
static_cast<int>(imports(offset+num_ent+k));
104 template<
class LocalOrdinal,
111 using LO = LocalOrdinal;
112 using GO =
typename IndicesView::value_type;
113 using packet_type = Packet;
114 using row_ptrs_type = RowView;
115 using indices_type = IndicesView;
116 using buffer_device_type = BufferDevice;
118 using device_type =
typename IndicesView::device_type;
119 using execution_space =
typename device_type::execution_space;
121 using num_packets_per_lid_type = Kokkos::View<const size_t*, buffer_device_type>;
122 using offsets_type = Kokkos::View<const size_t*, device_type>;
123 using input_buffer_type = Kokkos::View<const packet_type*, buffer_device_type>;
124 using import_lids_type = Kokkos::View<const LO*, buffer_device_type>;
126 using gids_scratch_type = Kokkos::View<GO*, device_type>;
127 using pids_scratch_type = Kokkos::View<int*,device_type>;
129 row_ptrs_type row_ptrs_beg;
130 row_ptrs_type row_ptrs_end;
131 indices_type indices;
132 input_buffer_type imports;
133 num_packets_per_lid_type num_packets_per_lid;
134 import_lids_type import_lids;
135 offsets_type offsets;
138 Kokkos::Experimental::UniqueToken<execution_space,
139 Kokkos::Experimental::UniqueTokenScope::Global> tokens;
140 gids_scratch_type gids_scratch;
141 pids_scratch_type pids_scratch;
144 using value_type = Kokkos::pair<int, LO>;
147 const row_ptrs_type& row_ptrs_beg_in,
148 const row_ptrs_type& row_ptrs_end_in,
149 const indices_type& indices_in,
150 const input_buffer_type& imports_in,
151 const num_packets_per_lid_type& num_packets_per_lid_in,
152 const import_lids_type& import_lids_in,
153 const offsets_type& offsets_in,
154 const size_t max_num_ent_in,
155 const bool unpack_pids_in) :
156 row_ptrs_beg(row_ptrs_beg_in),
157 row_ptrs_end(row_ptrs_end_in),
160 num_packets_per_lid(num_packets_per_lid_in),
161 import_lids(import_lids_in),
163 max_num_ent(max_num_ent_in),
164 unpack_pids(unpack_pids_in),
165 tokens(execution_space()),
166 gids_scratch(
"gids_scratch", tokens.size() * max_num_ent),
167 pids_scratch(
"pids_scratch", tokens.size() * max_num_ent)
170 KOKKOS_INLINE_FUNCTION
void init(value_type& dst)
const
172 using Tpetra::Details::OrdinalTraits;
173 dst = Kokkos::make_pair(0, OrdinalTraits<LO>::invalid());
176 KOKKOS_INLINE_FUNCTION
void
177 join(value_type& dst,
const value_type& src)
const
183 using Tpetra::Details::OrdinalTraits;
184 if (src.second != OrdinalTraits<LO>::invalid()) {
189 if (dst.second == OrdinalTraits<LO>::invalid() ||
190 src.second < dst.second) {
196 KOKKOS_INLINE_FUNCTION
197 void operator()(
const LO i, value_type& dst)
const
200 using Kokkos::subview;
201 using Kokkos::MemoryUnmanaged;
202 using size_type =
typename execution_space::size_type;
203 using slice =
typename Kokkos::pair<size_type, size_type>;
205 using pids_out_type = View<int*,device_type, MemoryUnmanaged>;
206 using gids_out_type = View<GO*, device_type, MemoryUnmanaged>;
208 const size_t num_packets_this_lid = num_packets_per_lid(i);
209 const size_t num_ent = (unpack_pids) ? num_packets_this_lid/2
210 : num_packets_this_lid;
211 if (unpack_pids && num_packets_this_lid%2 != 0) {
214 dst = Kokkos::make_pair(1, i);
224 const size_t buf_size = imports.size();
225 const size_t offset = offsets(i);
227 if (offset > buf_size || offset + num_packets_this_lid > buf_size) {
228 dst = Kokkos::make_pair(2, i);
235 const size_type token = tokens.acquire();
236 const size_t a =
static_cast<size_t>(token) * max_num_ent;
237 const size_t b = a + num_ent;
238 gids_out_type gids_out = subview(gids_scratch, slice(a, b));
239 pids_out_type pids_out = subview(pids_scratch, slice(a, (unpack_pids ? b : a)));
241 const int err = unpackRow (gids_out, pids_out, imports, offset, num_ent);
244 dst = Kokkos::make_pair(3, i);
245 tokens.release(token);
249 auto import_lid = import_lids(i);
250 for (
size_t k = 0; k < num_ent; ++k) {
251 indices(row_ptrs_end(import_lid)) = gids_out(k);
253 row_ptrs_end(import_lid) += 1;
256 tokens.release(token);
267 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node,
268 class RowView,
class IndicesView,
class BufferDevice>
271 (
const RowView& row_ptrs_beg,
272 const RowView& row_ptrs_end,
273 IndicesView& indices,
274 const Kokkos::View<
const GlobalOrdinal*, BufferDevice,
275 Kokkos::MemoryUnmanaged>& imports,
276 const Kokkos::View<
const size_t*, BufferDevice,
277 Kokkos::MemoryUnmanaged>& num_packets_per_lid,
278 const Kokkos::View<
const LocalOrdinal*, BufferDevice,
279 Kokkos::MemoryUnmanaged>& import_lids,
280 const typename CrsGraph<LocalOrdinal, GlobalOrdinal,
281 Node>::padding_type& padding,
282 const bool unpack_pids,
286 using LO = LocalOrdinal;
287 using GO = GlobalOrdinal;
288 using device_type =
typename Node::device_type;
289 using execution_space =
typename BufferDevice::execution_space;
291 Kokkos::RangePolicy<execution_space, Kokkos::IndexType<LO>>;
292 using unpack_functor_type =
295 const char prefix[] =
296 "Tpetra::Details::UnpackAndCombineCrsGraphImpl::unpackAndCombine: ";
298 const size_t num_import_lids =
static_cast<size_t>(import_lids.extent(0));
299 if (num_import_lids == 0) {
305 padCrsArrays(row_ptrs_beg, row_ptrs_end, indices, padding,
309 Kokkos::View<size_t*, device_type> offsets(
"offsets", num_import_lids+1);
316 Kokkos::parallel_reduce
318 range_policy (0, LO (num_packets_per_lid.size ())),
319 KOKKOS_LAMBDA (
const LO i,
size_t& running_max_num_ent) {
320 const size_t num_packets_this_lid = num_packets_per_lid(i);
321 const size_t num_ent = (unpack_pids) ? num_packets_this_lid/2 :
322 num_packets_this_lid;
323 if (num_ent > running_max_num_ent) {
324 running_max_num_ent = num_ent;
326 }, Kokkos::Max<size_t> (max_num_ent));
329 unpack_functor_type f (row_ptrs_beg, row_ptrs_end, indices, imports,
330 num_packets_per_lid, import_lids, offsets,
331 max_num_ent, unpack_pids);
333 typename unpack_functor_type::value_type x;
334 Kokkos::parallel_reduce(range_policy(0, static_cast<LO>(num_import_lids)), f, x);
335 auto x_h = x.to_std_pair();
336 TEUCHOS_TEST_FOR_EXCEPTION(x_h.first != 0, std::runtime_error,
337 prefix <<
"UnpackAndCombineFunctor reported error code "
338 << x_h.first <<
" for the first bad row " << x_h.second);
341 template<
class Packet,
class LocalGraph,
class BufferDevice>
344 const LocalGraph& local_graph,
345 const Kokkos::View<
const typename LocalGraph::data_type*,
346 typename LocalGraph::device_type,
347 Kokkos::MemoryUnmanaged> permute_from_lids,
348 const Kokkos::View<const Packet*, BufferDevice>& ,
349 const Kokkos::View<const size_t*, BufferDevice>& num_packets_per_lid,
350 const size_t num_same_ids)
352 using Kokkos::parallel_reduce;
353 using local_graph_type = LocalGraph;
354 using LO =
typename local_graph_type::data_type;
355 using device_type =
typename local_graph_type::device_type;
356 using execution_space =
typename device_type::execution_space;
357 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<LO>>;
363 num_items =
static_cast<LO
>(num_same_ids);
367 range_policy(0, num_items),
368 KOKKOS_LAMBDA(
const LO lid,
size_t& update) {
369 update +=
static_cast<size_t>(local_graph.row_map[lid+1]
370 -local_graph.row_map[lid]);
376 num_items =
static_cast<LO
>(permute_from_lids.extent(0));
380 range_policy(0, num_items),
381 KOKKOS_LAMBDA(
const LO i,
size_t& update) {
382 const LO lid = permute_from_lids(i);
383 update +=
static_cast<size_t>(local_graph.row_map[lid+1]
384 - local_graph.row_map[lid]);
391 size_t tot_num_ent = 0;
392 parallel_reduce(
"SumReduce",
393 range_policy(0,num_packets_per_lid.size()),
394 KOKKOS_LAMBDA(
const int& i,
size_t& lsum) {
395 lsum += num_packets_per_lid(i) / 2;
396 }, Kokkos::Sum<size_t>(tot_num_ent));
397 count += tot_num_ent;
404 template<
class Packet,
class LO,
class Device,
class BufferDevice>
406 setupRowPointersForRemotes(
407 const Kokkos::View<size_t*, Device>& tgt_rowptr,
408 const Kokkos::View<const LO*, BufferDevice>& import_lids,
409 const Kokkos::View<const Packet*, BufferDevice>& ,
410 const Kokkos::View<const size_t*, BufferDevice>& num_packets_per_lid)
412 using Kokkos::parallel_reduce;
413 using device_type = Device;
414 using execution_space =
typename device_type::execution_space;
415 using size_type =
typename Kokkos::View<size_t*,device_type>::size_type;
416 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_type>>;
418 const size_type N = num_packets_per_lid.extent(0);
419 parallel_for(
"Setup row pointers for remotes",
421 KOKKOS_LAMBDA(
const size_t i){
422 using atomic_incr_type =
typename std::remove_reference<decltype(tgt_rowptr(0))>::type;
423 const size_t num_packets_this_lid = num_packets_per_lid(i);
424 const size_t num_ent = num_packets_this_lid / 2;
425 Kokkos::atomic_fetch_add(&tgt_rowptr(import_lids(i)), atomic_incr_type(num_ent));
430 template<
class Device>
432 makeCrsRowPtrFromLengths(
433 const Kokkos::View<size_t*,Device,Kokkos::MemoryUnmanaged>& tgt_rowptr,
434 const Kokkos::View<size_t*,Device>& new_start_row)
436 using Kokkos::parallel_scan;
437 using device_type = Device;
438 using execution_space =
typename device_type::execution_space;
439 using size_type =
typename Kokkos::View<size_t*,device_type>::size_type;
440 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_type>>;
441 const size_type N = new_start_row.extent(0);
444 KOKKOS_LAMBDA(
const size_t& i,
size_t& update,
const bool&
final) {
445 auto cur_val = tgt_rowptr(i);
447 tgt_rowptr(i) = update;
448 new_start_row(i) = tgt_rowptr(i);
455 template<
class LocalGraph,
class LocalMap>
460 const Kokkos::View<int*, typename LocalMap::device_type>& tgt_pids,
461 const Kokkos::View<size_t*,typename LocalMap::device_type>& new_start_row,
462 const Kokkos::View<size_t*, typename LocalMap::device_type>& tgt_rowptr,
463 const Kokkos::View<const int*, typename LocalMap::device_type>& src_pids,
464 const LocalGraph& local_graph,
465 const LocalMap& local_col_map,
466 const size_t num_same_ids,
469 using Kokkos::parallel_for;
472 using execution_space =
typename device_type::execution_space;
473 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_t>>;
476 range_policy(0, num_same_ids),
477 KOKKOS_LAMBDA(
const size_t i) {
478 using atomic_incr_type =
typename std::remove_reference<decltype(new_start_row(0))>::type;
480 const LO src_lid =
static_cast<LO
>(i);
481 size_t src_row = local_graph.row_map(src_lid);
483 const LO tgt_lid =
static_cast<LO
>(i);
484 const size_t tgt_row = tgt_rowptr(tgt_lid);
486 const size_t nsr = local_graph.row_map(src_lid+1)
487 - local_graph.row_map(src_lid);
488 Kokkos::atomic_fetch_add(&new_start_row(tgt_lid), atomic_incr_type(nsr));
490 for (
size_t j=local_graph.row_map(src_lid);
491 j<local_graph.row_map(src_lid+1); ++j) {
492 LO src_col = local_graph.entries(j);
493 tgt_colind(tgt_row + j - src_row) = local_col_map.getGlobalElement(src_col);
494 tgt_pids(tgt_row + j - src_row) = (src_pids(src_col) != my_pid) ? src_pids(src_col) : -1;
500 template<
class LocalGraph,
class LocalMap,
class BufferDevice>
502 copyDataFromPermuteIDs(
505 const Kokkos::View<
int*,
507 const Kokkos::View<
size_t*,
509 const Kokkos::View<
size_t*,
511 const Kokkos::View<
const int*,
514 BufferDevice, Kokkos::MemoryUnmanaged>& permute_to_lids,
516 BufferDevice, Kokkos::MemoryUnmanaged>& permute_from_lids,
517 const LocalGraph& local_graph,
518 const LocalMap& local_col_map,
521 using Kokkos::parallel_for;
524 using execution_space =
typename device_type::execution_space;
525 using size_type =
typename Kokkos::View<LO*,device_type>::size_type;
526 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_type>>;
528 const size_type num_permute_to_lids = permute_to_lids.extent(0);
531 range_policy(0, num_permute_to_lids),
532 KOKKOS_LAMBDA(
const size_t i) {
533 using atomic_incr_type =
typename std::remove_reference<decltype(new_start_row(0))>::type;
535 const LO src_lid = permute_from_lids(i);
536 const size_t src_row = local_graph.row_map(src_lid);
538 const LO tgt_lid = permute_to_lids(i);
539 const size_t tgt_row = tgt_rowptr(tgt_lid);
541 size_t nsr = local_graph.row_map(src_lid+1)
542 - local_graph.row_map(src_lid);
543 Kokkos::atomic_fetch_add(&new_start_row(tgt_lid), atomic_incr_type(nsr));
545 for (
size_t j=local_graph.row_map(src_lid);
546 j<local_graph.row_map(src_lid+1); ++j) {
547 LO src_col = local_graph.entries(j);
548 tgt_colind(tgt_row + j - src_row) = local_col_map.getGlobalElement(src_col);
549 tgt_pids(tgt_row + j - src_row) = (src_pids(src_col) != my_pid) ? src_pids(src_col) : -1;
555 template<
class Packet,
class LocalGraph,
class LocalMap,
class BufferDevice>
557 unpackAndCombineIntoCrsArrays2(
558 const Kokkos::View<typename LocalMap::global_ordinal_type*, typename LocalMap::device_type>& tgt_colind,
559 const Kokkos::View<int*, typename LocalMap::device_type>& tgt_pids,
560 const Kokkos::View<size_t*,typename LocalMap::device_type>& new_start_row,
561 const Kokkos::View<const size_t*, typename LocalMap::device_type>& offsets,
565 Kokkos::MemoryUnmanaged>& import_lids,
566 const Kokkos::View<const Packet*, BufferDevice>& imports,
567 const Kokkos::View<const size_t*, BufferDevice>& num_packets_per_lid,
573 using Kokkos::subview;
574 using Kokkos::MemoryUnmanaged;
575 using Kokkos::parallel_reduce;
576 using Kokkos::atomic_fetch_add;
581 using execution_space =
typename device_type::execution_space;
582 using size_type =
typename Kokkos::View<LO*, device_type>::size_type;
583 using slice =
typename Kokkos::pair<size_type, size_type>;
584 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_type>>;
586 using pids_out_type = View<int*,device_type, MemoryUnmanaged>;
587 using gids_out_type = View<GO*, device_type, MemoryUnmanaged>;
589 const size_type num_import_lids = import_lids.size();
590 const char prefix[] =
"UnpackAndCombineCrsGraphImpl::unpackAndCombineIntoCrsArrays2: ";
594 parallel_reduce(
"Unpack and combine into CRS",
595 range_policy(0, num_import_lids),
596 KOKKOS_LAMBDA(
const size_t i,
int& err) {
597 using atomic_incr_type =
typename std::remove_reference< decltype( new_start_row(0) )>::type;
598 const size_t num_packets_this_lid = num_packets_per_lid(i);
599 const size_t num_ent = num_packets_this_lid / 2;
600 const size_t offset = offsets(i);
601 const LO lcl_row = import_lids(i);
602 const size_t start_row = atomic_fetch_add(&new_start_row(lcl_row), atomic_incr_type(num_ent));
603 const size_t end_row = start_row + num_ent;
605 gids_out_type gids_out = subview(tgt_colind, slice(start_row, end_row));
606 pids_out_type pids_out = subview(tgt_pids, slice(start_row, end_row));
608 err += unpackRow (gids_out, pids_out, imports, offset, num_ent);
611 for (
size_t j = 0; j < static_cast<size_t>(num_ent); ++j) {
612 const int pid = pids_out(j);
613 pids_out(j) = (pid != my_pid) ? pid : -1;
617 TEUCHOS_TEST_FOR_EXCEPTION(gbl_err_count != 0,
618 std::invalid_argument, prefix <<
619 "Attempting to unpack PIDs, but num_ent is not even; this should never "
620 "happen! Please report this bug to the Tpetra developers.");
625 template<
class Packet,
class LocalGraph,
class LocalMap,
class BufferDevice>
628 const LocalGraph & local_graph,
629 const LocalMap & local_col_map,
632 Kokkos::MemoryUnmanaged>& import_lids,
633 const Kokkos::View<const Packet*, BufferDevice>& imports,
634 const Kokkos::View<const size_t*, BufferDevice>& num_packets_per_lid,
637 Kokkos::MemoryUnmanaged>& permute_to_lids,
640 Kokkos::MemoryUnmanaged>& permute_from_lids,
641 const Kokkos::View<
size_t*,
643 Kokkos::MemoryUnmanaged>& tgt_rowptr,
646 Kokkos::MemoryUnmanaged>& tgt_colind,
647 const Kokkos::View<
const int*,
649 Kokkos::MemoryUnmanaged>& src_pids,
650 const Kokkos::View<
int*,
652 Kokkos::MemoryUnmanaged>& tgt_pids,
653 const size_t num_same_ids,
654 const size_t tgt_num_rows,
655 const size_t tgt_num_nonzeros,
656 const int my_tgt_pid)
659 using Kokkos::subview;
660 using Kokkos::parallel_for;
661 using Kokkos::MemoryUnmanaged;
662 using packet_type = Packet;
663 using local_map_type = LocalMap;
664 using local_graph_type = LocalGraph;
665 using buffer_device_type = BufferDevice;
668 using execution_space =
typename device_type::execution_space;
669 using size_type =
typename Kokkos::View<LO*, device_type>::size_type;
670 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_t>>;
672 const char prefix[] =
"UnpackAndCombineCrsGraphImpl::unpackAndCombineIntoCrsArrays: ";
674 const size_t N = tgt_num_rows;
675 const size_t mynnz = tgt_num_nonzeros;
679 const int my_pid = my_tgt_pid;
688 range_policy(0, N+1),
689 KOKKOS_LAMBDA(
const size_t i) {
696 range_policy(0, num_same_ids),
697 KOKKOS_LAMBDA(
const size_t i) {
698 const LO tgt_lid =
static_cast<LO
>(i);
699 const LO src_lid =
static_cast<LO
>(i);
700 tgt_rowptr(tgt_lid) = local_graph.row_map(src_lid+1)
701 - local_graph.row_map(src_lid);
706 const size_type num_permute_to_lids = permute_to_lids.extent(0);
708 range_policy(0, num_permute_to_lids),
709 KOKKOS_LAMBDA(
const size_t i) {
710 const LO tgt_lid = permute_to_lids(i);
711 const LO src_lid = permute_from_lids(i);
712 tgt_rowptr(tgt_lid) = local_graph.row_map(src_lid+1)
713 - local_graph.row_map(src_lid);
718 const size_type num_import_lids = import_lids.extent(0);
719 View<size_t*, device_type> offsets(
"offsets", num_import_lids+1);
722 #ifdef HAVE_TPETRA_DEBUG
724 auto nth_offset_h = getEntryOnHost(offsets, num_import_lids);
725 const bool condition =
726 nth_offset_h !=
static_cast<size_t>(imports.extent(0));
727 TEUCHOS_TEST_FOR_EXCEPTION
728 (condition, std::logic_error, prefix
729 <<
"The final offset in bytes " << nth_offset_h
730 <<
" != imports.size() = " << imports.extent(0)
731 <<
". Please report this bug to the Tpetra developers.");
733 #endif // HAVE_TPETRA_DEBUG
736 setupRowPointersForRemotes<packet_type,LO,device_type,buffer_device_type>(
737 tgt_rowptr, import_lids, imports, num_packets_per_lid);
741 View<size_t*, device_type> new_start_row(
"new_start_row", N+1);
744 makeCrsRowPtrFromLengths(tgt_rowptr, new_start_row);
746 auto nth_tgt_rowptr_h = getEntryOnHost(tgt_rowptr, N);
747 bool condition = nth_tgt_rowptr_h != mynnz;
748 TEUCHOS_TEST_FOR_EXCEPTION(condition, std::invalid_argument,
749 prefix <<
"CRS_rowptr[last] = " <<
750 nth_tgt_rowptr_h <<
"!= mynnz = " << mynnz <<
".");
754 copyDataFromSameIDs<LocalGraph,LocalMap>(tgt_colind, tgt_pids, new_start_row,
755 tgt_rowptr, src_pids, local_graph, local_col_map, num_same_ids, my_pid);
757 copyDataFromPermuteIDs<LocalGraph,LocalMap>(tgt_colind, tgt_pids, new_start_row,
758 tgt_rowptr, src_pids, permute_to_lids, permute_from_lids,
759 local_graph, local_col_map, my_pid);
761 if (imports.extent(0) <= 0) {
765 unpackAndCombineIntoCrsArrays2<
766 packet_type,local_graph_type,local_map_type,buffer_device_type>(
767 tgt_colind, tgt_pids, new_start_row, offsets, import_lids, imports,
768 num_packets_per_lid, local_graph, local_col_map, my_pid);
822 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
826 const Teuchos::ArrayView<const LocalOrdinal> &importLIDs,
828 const Teuchos::ArrayView<const size_t>& numPacketsPerLID,
832 const Teuchos::ArrayView<const LocalOrdinal>& permuteToLIDs,
833 const Teuchos::ArrayView<const LocalOrdinal>& permuteFromLIDs)
835 using Kokkos::MemoryUnmanaged;
837 using device_type =
typename Node::device_type;
841 const char prefix[] =
"unpackAndCombineWithOwningPIDsCount: ";
843 TEUCHOS_TEST_FOR_EXCEPTION
844 (permuteToLIDs.size() != permuteFromLIDs.size(), std::invalid_argument,
845 prefix <<
"permuteToLIDs.size() = " << permuteToLIDs.size() <<
" != "
846 "permuteFromLIDs.size() = " << permuteFromLIDs.size() <<
".");
850 TEUCHOS_TEST_FOR_EXCEPTION
851 (! locallyIndexed, std::invalid_argument, prefix <<
"The input "
852 "CrsGraph 'sourceGraph' must be locally indexed.");
853 TEUCHOS_TEST_FOR_EXCEPTION
854 (importLIDs.size() != numPacketsPerLID.size(), std::invalid_argument,
855 prefix <<
"importLIDs.size() = " << importLIDs.size() <<
" != "
856 "numPacketsPerLID.size() = " << numPacketsPerLID.size() <<
".");
859 auto permute_from_lids_d =
861 permuteFromLIDs.getRawPtr(),
862 permuteFromLIDs.size(),
true,
863 "permute_from_lids");
867 imports.size(),
true,
869 auto num_packets_per_lid_d =
871 numPacketsPerLID.getRawPtr(),
872 numPacketsPerLID.size(),
true,
873 "num_packets_per_lid");
876 packet_type,local_graph_device_type,buffer_device_type>(
877 local_graph, permute_from_lids_d, imports_d, num_packets_per_lid_d, numSameIDs);
893 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
897 const Teuchos::ArrayView<const LocalOrdinal>& importLIDs,
899 const Teuchos::ArrayView<const size_t>& numPacketsPerLID,
902 const size_t numSameIDs,
903 const Teuchos::ArrayView<const LocalOrdinal>& permuteToLIDs,
904 const Teuchos::ArrayView<const LocalOrdinal>& permuteFromLIDs,
905 size_t TargetNumRows,
906 size_t TargetNumNonzeros,
907 const int MyTargetPID,
908 const Teuchos::ArrayView<size_t>& CRS_rowptr,
909 const Teuchos::ArrayView<GlobalOrdinal>& CRS_colind,
910 const Teuchos::ArrayView<const int>& SourcePids,
911 Teuchos::Array<int>& TargetPids)
915 using Teuchos::outArg;
916 using Teuchos::REDUCE_MAX;
917 using Teuchos::reduceAll;
918 using LO = LocalOrdinal;
919 using GO = GlobalOrdinal;
921 using packet_type =
typename crs_graph_type::packet_type;
922 using local_graph_device_type =
typename crs_graph_type::local_graph_device_type;
923 using buffer_device_type =
typename crs_graph_type::buffer_device_type;
924 using device_type =
typename Node::device_type;
925 using size_type =
typename Teuchos::ArrayView<const LO>::size_type;
927 const char prefix[] =
"Tpetra::Details::unpackAndCombineIntoCrsArrays: ";
929 TEUCHOS_TEST_FOR_EXCEPTION(
930 TargetNumRows + 1 != static_cast<size_t>(CRS_rowptr.size()),
931 std::invalid_argument, prefix <<
"CRS_rowptr.size() = " <<
932 CRS_rowptr.size() <<
"!= TargetNumRows+1 = " << TargetNumRows+1 <<
".");
934 TEUCHOS_TEST_FOR_EXCEPTION(
935 permuteToLIDs.size() != permuteFromLIDs.size(), std::invalid_argument,
936 prefix <<
"permuteToLIDs.size() = " << permuteToLIDs.size()
937 <<
"!= permuteFromLIDs.size() = " << permuteFromLIDs.size() <<
".");
938 const size_type numImportLIDs = importLIDs.size();
940 TEUCHOS_TEST_FOR_EXCEPTION(
941 numImportLIDs != numPacketsPerLID.size(), std::invalid_argument,
942 prefix <<
"importLIDs.size() = " << numImportLIDs <<
" != "
943 "numPacketsPerLID.size() = " << numPacketsPerLID.size() <<
".");
946 if (static_cast<size_t>(TargetPids.size()) != TargetNumNonzeros) {
947 TargetPids.resize(TargetNumNonzeros);
949 TargetPids.assign(TargetNumNonzeros, -1);
953 auto local_col_map = sourceGraph.
getColMap()->getLocalMap();
956 device_type outputDevice;
957 buffer_device_type bufferOutputDevice;
959 Kokkos::View<const LO*, buffer_device_type> import_lids_d =
961 (bufferOutputDevice, importLIDs.getRawPtr(),
962 importLIDs.size(),
true,
"import_lids");
964 Kokkos::View<const packet_type*, buffer_device_type> imports_d =
966 (bufferOutputDevice, imports.getRawPtr(),
967 imports.size(),
true,
"imports");
969 Kokkos::View<const size_t*, buffer_device_type> num_packets_per_lid_d =
971 numPacketsPerLID.getRawPtr(), numPacketsPerLID.size(),
972 true,
"num_packets_per_lid");
974 Kokkos::View<const LO*, buffer_device_type> permute_to_lids_d =
976 permuteToLIDs.getRawPtr(), permuteToLIDs.size(),
977 true,
"permute_to_lids");
979 Kokkos::View<const LO*, buffer_device_type> permute_from_lids_d =
981 permuteFromLIDs.getRawPtr(), permuteFromLIDs.size(),
982 true,
"permute_from_lids");
984 Kokkos::View<size_t*, device_type> crs_rowptr_d =
986 CRS_rowptr.getRawPtr(), CRS_rowptr.size(),
989 Kokkos::View<GO*, device_type> crs_colind_d =
991 CRS_colind.getRawPtr(), CRS_colind.size(),
994 Kokkos::View<const int*, device_type> src_pids_d =
996 SourcePids.getRawPtr(), SourcePids.size(),
999 Kokkos::View<int*, device_type> tgt_pids_d =
1001 TargetPids.getRawPtr(), TargetPids.size(),
1004 using local_map_type = decltype(local_col_map);
1006 packet_type,local_graph_device_type,local_map_type,buffer_device_type>(
1007 local_graph, local_col_map, import_lids_d, imports_d, num_packets_per_lid_d,
1008 permute_to_lids_d, permute_from_lids_d, crs_rowptr_d, crs_colind_d, src_pids_d,
1009 tgt_pids_d, numSameIDs, TargetNumRows, TargetNumNonzeros, MyTargetPID);
1014 typename decltype(crs_rowptr_d)::HostMirror crs_rowptr_h(
1015 CRS_rowptr.getRawPtr(), CRS_rowptr.size());
1018 typename decltype(crs_colind_d)::HostMirror crs_colind_h(
1019 CRS_colind.getRawPtr(), CRS_colind.size());
1022 typename decltype(tgt_pids_d)::HostMirror tgt_pids_h(
1023 TargetPids.getRawPtr(), TargetPids.size());
1031 #define TPETRA_DETAILS_UNPACKCRSGRAPHANDCOMBINE_INSTANT( LO, GO, NT ) \
1033 Details::unpackAndCombineIntoCrsArrays<LO, GO, NT>( \
1034 const CrsGraph<LO, GO, NT> &, \
1035 const Teuchos::ArrayView<const LO>&, \
1036 const Teuchos::ArrayView<const typename CrsGraph<LO,GO,NT>::packet_type>&, \
1037 const Teuchos::ArrayView<const size_t>&, \
1039 const CombineMode, \
1041 const Teuchos::ArrayView<const LO>&, \
1042 const Teuchos::ArrayView<const LO>&, \
1046 const Teuchos::ArrayView<size_t>&, \
1047 const Teuchos::ArrayView<GO>&, \
1048 const Teuchos::ArrayView<const int>&, \
1049 Teuchos::Array<int>&); \
1051 Details::unpackAndCombineWithOwningPIDsCount<LO, GO, NT>( \
1052 const CrsGraph<LO, GO, NT> &, \
1053 const Teuchos::ArrayView<const LO> &, \
1054 const Teuchos::ArrayView<const typename CrsGraph<LO,GO,NT>::packet_type> &, \
1055 const Teuchos::ArrayView<const size_t>&, \
1059 const Teuchos::ArrayView<const LO>&, \
1060 const Teuchos::ArrayView<const LO>&);
1062 #endif // TPETRA_DETAILS_UNPACKCRSGRAPHANDCOMBINE_DEF_HPP
GlobalOrdinal global_ordinal_type
The type of global indices.
Impl::CreateMirrorViewFromUnmanagedHostArray< ValueType, OutputDeviceType >::output_view_type create_mirror_view_from_raw_host_array(const OutputDeviceType &, ValueType *inPtr, const size_t inSize, const bool copy=true, const char label[]="")
Variant of Kokkos::create_mirror_view that takes a raw host 1-d array as input.
Import KokkosSparse::OrdinalTraits, a traits class for "invalid" (flag) values of integer types...
Teuchos::RCP< const map_type > getColMap() const override
Returns the Map that describes the column distribution in this graph.
Kokkos::StaticCrsGraph< local_ordinal_type, Kokkos::LayoutLeft, device_type, void, size_t > local_graph_device_type
The type of the part of the sparse graph on each MPI process.
void padCrsArrays(const RowPtr &rowPtrBeg, const RowPtr &rowPtrEnd, Indices &indices_wdv, const Padding &padding, const int my_rank, const bool verbose)
Determine if the row pointers and indices arrays need to be resized to accommodate new entries...
Declaration of the Tpetra::CrsGraph class.
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
Functions for manipulating CRS arrays.
Declare and define the functions Tpetra::Details::computeOffsetsFromCounts and Tpetra::computeOffsets...
void unpackAndCombineIntoCrsArrays(const CrsGraph< LO, GO, NT > &sourceGraph, const Teuchos::ArrayView< const LO > &importLIDs, const Teuchos::ArrayView< const typename CrsGraph< LO, GO, NT >::packet_type > &imports, const Teuchos::ArrayView< const size_t > &numPacketsPerLID, const size_t constantNumPackets, const CombineMode combineMode, const size_t numSameIDs, const Teuchos::ArrayView< const LO > &permuteToLIDs, const Teuchos::ArrayView< const LO > &permuteFromLIDs, size_t TargetNumRows, size_t TargetNumNonzeros, const int MyTargetPID, const Teuchos::ArrayView< size_t > &CRS_rowptr, const Teuchos::ArrayView< GO > &CRS_colind, const Teuchos::ArrayView< const int > &SourcePids, Teuchos::Array< int > &TargetPids)
unpackAndCombineIntoCrsArrays
CombineMode
Rule for combining data in an Import or Export.
Unpacks and combines a single row of the CrsGraph.
typename dist_object_type::buffer_device_type buffer_device_type
Kokkos::Device specialization for communication buffers.
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
size_t unpackAndCombineWithOwningPIDsCount(const CrsGraph< LO, GO, NT > &sourceGraph, const Teuchos::ArrayView< const LO > &importLIDs, const Teuchos::ArrayView< const typename CrsGraph< LO, GO, NT >::packet_type > &imports, const Teuchos::ArrayView< const size_t > &numPacketsPerLID, size_t constantNumPackets, CombineMode combineMode, size_t numSameIDs, const Teuchos::ArrayView< const LO > &permuteToLIDs, const Teuchos::ArrayView< const LO > &permuteFromLIDs)
Special version of Tpetra::Details::unpackCrsGraphAndCombine that also unpacks owning process ranks...
DeviceType device_type
The device type.
bool isLocallyIndexed() const override
Whether the graph's column indices are stored as local indices.
Declaration and definition of Tpetra::Details::castAwayConstDualView, an implementation detail of Tpe...
OffsetsViewType::non_const_value_type computeOffsetsFromCounts(const ExecutionSpace &execSpace, const OffsetsViewType &ptr, const CountsViewType &counts)
Compute offsets from counts.
Declaration and definition of Tpetra::Details::getEntryOnHost.
local_graph_device_type getLocalGraphDevice() const
Get the local graph.
global_ordinal_type packet_type
Type of each entry of the DistObject communication buffer.
LocalOrdinal local_ordinal_type
The type of local indices.
Functions that wrap Kokkos::create_mirror_view, in order to avoid deep copies when not necessary...
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.