42 #ifndef TPETRA_DETAILS_UNPACKCRSGRAPHANDCOMBINE_DEF_HPP
43 #define TPETRA_DETAILS_UNPACKCRSGRAPHANDCOMBINE_DEF_HPP
45 #include "TpetraCore_config.h"
46 #include "Teuchos_Array.hpp"
47 #include "Teuchos_ArrayView.hpp"
56 #include "Kokkos_Core.hpp"
81 #ifndef DOXYGEN_SHOULD_SKIP_THIS
84 #endif // DOXYGEN_SHOULD_SKIP_THIS
91 namespace UnpackAndCombineCrsGraphImpl {
102 template<
class Packet,
class GO,
class Device,
class BufferDevice>
104 unpackRow(
typename Kokkos::View<GO*,Device,Kokkos::MemoryUnmanaged>& gids_out,
105 typename Kokkos::View<int*,Device,Kokkos::MemoryUnmanaged>& pids_out,
106 const Kokkos::View<const Packet*,BufferDevice>& imports,
108 const size_t num_ent)
110 using size_type =
typename Kokkos::View<GO*,Device>::size_type;
118 for (size_type k=0; k<num_ent; k++)
119 gids_out(k) = imports(offset+k);
122 if (pids_out.size() > 0) {
123 for (size_type k=0; k<num_ent; k++)
124 pids_out(k) =
static_cast<int>(imports(offset+num_ent+k));
140 template<
class LocalOrdinal,
class Packet,
class RowView,
141 class IndicesView,
class Device,
class BufferDevice>
144 using LO = LocalOrdinal;
145 using GO =
typename IndicesView::value_type;
146 using packet_type = Packet;
147 using row_ptrs_type = RowView;
148 using indices_type = IndicesView;
149 using buffer_device_type = BufferDevice;
151 using device_type = Device;
152 using execution_space =
typename device_type::execution_space;
154 using num_packets_per_lid_type = Kokkos::View<const size_t*, buffer_device_type>;
155 using offsets_type = Kokkos::View<const size_t*, device_type>;
156 using input_buffer_type = Kokkos::View<const packet_type*, buffer_device_type>;
157 using import_lids_type = Kokkos::View<const LO*, device_type>;
159 using gids_scratch_type = Kokkos::View<GO*, device_type>;
160 using pids_scratch_type = Kokkos::View<int*,device_type>;
162 row_ptrs_type row_ptrs_beg;
163 row_ptrs_type row_ptrs_end;
164 indices_type indices;
165 input_buffer_type imports;
166 num_packets_per_lid_type num_packets_per_lid;
167 import_lids_type import_lids;
168 offsets_type offsets;
171 Kokkos::Experimental::UniqueToken<execution_space,
172 Kokkos::Experimental::UniqueTokenScope::Global> tokens;
173 gids_scratch_type gids_scratch;
174 pids_scratch_type pids_scratch;
177 using value_type = Kokkos::pair<int, LO>;
180 const row_ptrs_type& row_ptrs_beg_in,
181 row_ptrs_type& row_ptrs_end_in,
182 indices_type& indices_in,
183 const input_buffer_type& imports_in,
184 const num_packets_per_lid_type& num_packets_per_lid_in,
185 const import_lids_type& import_lids_in,
186 const offsets_type& offsets_in,
187 const size_t max_num_ent_in,
188 const bool unpack_pids_in) :
189 row_ptrs_beg(row_ptrs_beg_in),
190 row_ptrs_end(row_ptrs_end_in),
193 num_packets_per_lid(num_packets_per_lid_in),
194 import_lids(import_lids_in),
196 max_num_ent(max_num_ent_in),
197 unpack_pids(unpack_pids_in),
198 tokens(execution_space()),
199 gids_scratch(
"gids_scratch", tokens.size() * max_num_ent),
200 pids_scratch(
"pids_scratch", tokens.size() * max_num_ent)
203 KOKKOS_INLINE_FUNCTION
void init(value_type& dst)
const
205 using Tpetra::Details::OrdinalTraits;
206 dst = Kokkos::make_pair(0, OrdinalTraits<LO>::invalid());
209 KOKKOS_INLINE_FUNCTION
void
210 join(
volatile value_type& dst,
const volatile value_type& src)
const
216 using Tpetra::Details::OrdinalTraits;
217 if (src.second != OrdinalTraits<LO>::invalid()) {
222 if (dst.second == OrdinalTraits<LO>::invalid() ||
223 src.second < dst.second) {
229 KOKKOS_INLINE_FUNCTION
230 void operator()(
const LO i, value_type& dst)
const
233 using Kokkos::subview;
234 using Kokkos::MemoryUnmanaged;
235 using size_type =
typename execution_space::size_type;
236 using slice =
typename Kokkos::pair<size_type, size_type>;
238 using pids_out_type = View<int*,device_type, MemoryUnmanaged>;
239 using gids_out_type = View<GO*, device_type, MemoryUnmanaged>;
241 const size_t num_packets_this_lid = num_packets_per_lid(i);
242 const size_t num_ent = (unpack_pids) ? num_packets_this_lid/2
243 : num_packets_this_lid;
244 if (unpack_pids && num_packets_this_lid%2 != 0) {
247 dst = Kokkos::make_pair(1, i);
257 const size_t buf_size = imports.size();
258 const size_t offset = offsets(i);
260 if (offset > buf_size || offset + num_packets_this_lid > buf_size) {
261 dst = Kokkos::make_pair(2, i);
268 const size_type token = tokens.acquire();
269 const size_t a =
static_cast<size_t>(token) * max_num_ent;
270 const size_t b = a + num_ent;
271 gids_out_type gids_out = subview(gids_scratch, slice(a, b));
272 pids_out_type pids_out = subview(pids_scratch, slice(a, (unpack_pids ? b : a)));
275 int err = unpackRow<packet_type,GO,device_type,buffer_device_type>(
276 gids_out, pids_out, imports, offset, num_ent);
279 dst = Kokkos::make_pair(3, i);
280 tokens.release(token);
284 auto import_lid = import_lids(i);
285 for (
size_t k = 0; k < num_ent; ++k) {
286 indices(row_ptrs_end(import_lid)) = gids_out(k);
288 row_ptrs_end(import_lid) += 1;
291 tokens.release(token);
296 template<
class NumPackets,
class ImportL
ids,
class Device>
297 Kokkos::UnorderedMap<
typename ImportLids::non_const_value_type,
298 typename NumPackets::non_const_value_type,
300 computeCrsPadding(
const NumPackets& num_packets_per_lid,
301 const ImportLids& import_lids,
302 const bool unpack_pids)
306 using key_type =
typename ImportLids::non_const_value_type;
307 using val_type =
typename NumPackets::non_const_value_type;
308 Kokkos::UnorderedMap<key_type, val_type, Device> padding(import_lids.size());
309 auto policy = Kokkos::RangePolicy<typename Device::execution_space>(0, import_lids.size());
310 Kokkos::parallel_for(
"Fill padding", policy,
311 KOKKOS_LAMBDA(
typename ImportLids::size_type i) {
312 auto how_much_padding = (unpack_pids) ? num_packets_per_lid(i)/2
313 : num_packets_per_lid(i);
314 padding.insert(import_lids(i), how_much_padding);
317 TEUCHOS_TEST_FOR_EXCEPTION(padding.failed_insert(), std::runtime_error,
318 "computeCrsPadding: failed to insert one or more indices in to padding map");
328 template<
class LocalOrdinal,
class Packet,
class RowView,
329 class IndicesView,
class Device,
class BufferDevice>
332 RowView& row_ptrs_beg,
333 RowView& row_ptrs_end,
334 IndicesView& indices,
335 const Kokkos::View<const Packet*, BufferDevice, Kokkos::MemoryUnmanaged>& imports,
336 const Kokkos::View<const size_t*, BufferDevice, Kokkos::MemoryUnmanaged>& num_packets_per_lid,
337 const Kokkos::View<const LocalOrdinal*, Device, Kokkos::MemoryUnmanaged>& import_lids,
338 const bool unpack_pids)
341 using ImportLidsView = Kokkos::View<const LocalOrdinal*, Device, Kokkos::MemoryUnmanaged>;
342 using NumPacketsView = Kokkos::View<const size_t*, BufferDevice, Kokkos::MemoryUnmanaged>;
343 using LO = LocalOrdinal;
344 using device_type = Device;
345 using execution_space =
typename device_type::execution_space;
346 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<LO>>;
347 using unpack_functor_type = UnpackAndCombineFunctor<LO,Packet,RowView,IndicesView,Device,BufferDevice>;
349 const char prefix[] =
350 "Tpetra::Details::UnpackAndCombineCrsGraphImpl::unpackAndCombine: ";
352 const size_t num_import_lids =
static_cast<size_t>(import_lids.extent(0));
353 if (num_import_lids == 0) {
359 auto padding = computeCrsPadding<NumPacketsView,ImportLidsView,Device>(
360 num_packets_per_lid, import_lids, unpack_pids);
361 padCrsArrays(row_ptrs_beg, row_ptrs_end, indices, padding);
364 Kokkos::View<size_t*, device_type> offsets(
"offsets", num_import_lids+1);
371 Kokkos::parallel_reduce(
"MaxReduce",
372 range_policy(0, static_cast<LO>(num_packets_per_lid.size())),
373 KOKKOS_LAMBDA(
const LO& i,
size_t& running_max_num_ent) {
374 size_t num_packets_this_lid = num_packets_per_lid(i);
375 size_t num_ent = (unpack_pids) ? num_packets_this_lid/2
376 : num_packets_this_lid;
377 if (num_ent > running_max_num_ent) running_max_num_ent = num_ent;
378 }, Kokkos::Max<size_t>(max_num_ent));
381 unpack_functor_type f(row_ptrs_beg, row_ptrs_end, indices,
382 imports, num_packets_per_lid, import_lids, offsets,
383 max_num_ent, unpack_pids);
385 typename unpack_functor_type::value_type x;
386 Kokkos::parallel_reduce(range_policy(0, static_cast<LO>(num_import_lids)), f, x);
387 auto x_h = x.to_std_pair();
388 TEUCHOS_TEST_FOR_EXCEPTION(x_h.first != 0, std::runtime_error,
389 prefix <<
"UnpackAndCombineFunctor reported error code "
390 << x_h.first <<
" for the first bad row " << x_h.second);
395 template<
class Packet,
class LocalGraph,
class BufferDevice>
398 const LocalGraph& local_graph,
399 const Kokkos::View<
const typename LocalGraph::data_type*,
400 typename LocalGraph::device_type,
401 Kokkos::MemoryUnmanaged> permute_from_lids,
402 const Kokkos::View<const Packet*, BufferDevice>& ,
403 const Kokkos::View<const size_t*, BufferDevice>& num_packets_per_lid,
404 const size_t num_same_ids)
406 using Kokkos::parallel_reduce;
407 using local_graph_type = LocalGraph;
408 using LO =
typename local_graph_type::data_type;
409 using device_type =
typename local_graph_type::device_type;
410 using execution_space =
typename device_type::execution_space;
411 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<LO>>;
417 num_items =
static_cast<LO
>(num_same_ids);
421 range_policy(0, num_items),
422 KOKKOS_LAMBDA(
const LO lid,
size_t& update) {
423 update +=
static_cast<size_t>(local_graph.row_map[lid+1]
424 -local_graph.row_map[lid]);
430 num_items =
static_cast<LO
>(permute_from_lids.extent(0));
434 range_policy(0, num_items),
435 KOKKOS_LAMBDA(
const LO i,
size_t& update) {
436 const LO lid = permute_from_lids(i);
437 update +=
static_cast<size_t>(local_graph.row_map[lid+1]
438 - local_graph.row_map[lid]);
445 size_t tot_num_ent = 0;
446 parallel_reduce(
"SumReduce",
447 num_packets_per_lid.size(),
448 KOKKOS_LAMBDA(
const int& i,
size_t& lsum) {
449 lsum += num_packets_per_lid(i) / 2;
450 }, Kokkos::Sum<size_t>(tot_num_ent));
451 count += tot_num_ent;
458 template<
class Packet,
class LO,
class Device,
class BufferDevice>
460 setupRowPointersForRemotes(
461 const Kokkos::View<size_t*, Device>& tgt_rowptr,
462 const Kokkos::View<const LO*, Device>& import_lids,
463 const Kokkos::View<const Packet*, BufferDevice>& ,
464 const Kokkos::View<const size_t*, BufferDevice>& num_packets_per_lid)
466 using Kokkos::parallel_reduce;
467 using device_type = Device;
468 using execution_space =
typename device_type::execution_space;
469 using size_type =
typename Kokkos::View<size_t*,device_type>::size_type;
470 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_type>>;
472 const size_type N = num_packets_per_lid.extent(0);
473 parallel_for(
"Setup row pointers for remotes",
475 KOKKOS_LAMBDA(
const size_t i){
476 using atomic_incr_type =
typename std::remove_reference<decltype(tgt_rowptr(0))>::type;
477 const size_t num_packets_this_lid = num_packets_per_lid(i);
478 const size_t num_ent = num_packets_this_lid / 2;
479 Kokkos::atomic_fetch_add(&tgt_rowptr(import_lids(i)), atomic_incr_type(num_ent));
484 template<
class Device>
486 makeCrsRowPtrFromLengths(
487 const Kokkos::View<size_t*,Device,Kokkos::MemoryUnmanaged>& tgt_rowptr,
488 const Kokkos::View<size_t*,Device>& new_start_row)
490 using Kokkos::parallel_scan;
491 using device_type = Device;
492 using execution_space =
typename device_type::execution_space;
493 using size_type =
typename Kokkos::View<size_t*,device_type>::size_type;
494 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_type>>;
495 const size_type N = new_start_row.extent(0);
498 KOKKOS_LAMBDA(
const size_t& i,
size_t& update,
const bool&
final) {
499 auto cur_val = tgt_rowptr(i);
501 tgt_rowptr(i) = update;
502 new_start_row(i) = tgt_rowptr(i);
509 template<
class LocalGraph,
class LocalMap>
512 const Kokkos::View<
typename LocalMap::global_ordinal_type*,
513 typename LocalMap::device_type>& tgt_colind,
514 const Kokkos::View<int*, typename LocalMap::device_type>& tgt_pids,
515 const Kokkos::View<size_t*,typename LocalMap::device_type>& new_start_row,
516 const Kokkos::View<size_t*, typename LocalMap::device_type>& tgt_rowptr,
517 const Kokkos::View<const int*, typename LocalMap::device_type>& src_pids,
518 const LocalGraph& local_graph,
519 const LocalMap& local_col_map,
520 const size_t num_same_ids,
523 using Kokkos::parallel_for;
524 using device_type =
typename LocalMap::device_type;
525 using LO =
typename LocalMap::local_ordinal_type;
526 using execution_space =
typename device_type::execution_space;
527 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_t>>;
530 range_policy(0, num_same_ids),
531 KOKKOS_LAMBDA(
const size_t i) {
532 using atomic_incr_type =
typename std::remove_reference<decltype(new_start_row(0))>::type;
534 const LO src_lid =
static_cast<LO
>(i);
535 size_t src_row = local_graph.row_map(src_lid);
537 const LO tgt_lid =
static_cast<LO
>(i);
538 const size_t tgt_row = tgt_rowptr(tgt_lid);
540 const size_t nsr = local_graph.row_map(src_lid+1)
541 - local_graph.row_map(src_lid);
542 Kokkos::atomic_fetch_add(&new_start_row(tgt_lid), atomic_incr_type(nsr));
544 for (
size_t j=local_graph.row_map(src_lid);
545 j<local_graph.row_map(src_lid+1); ++j) {
546 LO src_col = local_graph.entries(j);
547 tgt_colind(tgt_row + j - src_row) = local_col_map.getGlobalElement(src_col);
548 tgt_pids(tgt_row + j - src_row) = (src_pids(src_col) != my_pid) ? src_pids(src_col) : -1;
554 template<
class LocalGraph,
class LocalMap>
556 copyDataFromPermuteIDs(
557 const Kokkos::View<
typename LocalMap::global_ordinal_type*,
558 typename LocalMap::device_type>& tgt_colind,
559 const Kokkos::View<
int*,
560 typename LocalMap::device_type>& tgt_pids,
561 const Kokkos::View<
size_t*,
562 typename LocalMap::device_type>& new_start_row,
563 const Kokkos::View<
size_t*,
564 typename LocalMap::device_type>& tgt_rowptr,
565 const Kokkos::View<
const int*,
566 typename LocalMap::device_type>& src_pids,
567 const Kokkos::View<
const typename LocalMap::local_ordinal_type*,
568 typename LocalMap::device_type>& permute_to_lids,
569 const Kokkos::View<
const typename LocalMap::local_ordinal_type*,
570 typename LocalMap::device_type>& permute_from_lids,
571 const LocalGraph& local_graph,
572 const LocalMap& local_col_map,
575 using Kokkos::parallel_for;
576 using device_type =
typename LocalMap::device_type;
577 using LO =
typename LocalMap::local_ordinal_type;
578 using execution_space =
typename device_type::execution_space;
579 using size_type =
typename Kokkos::View<LO*,device_type>::size_type;
580 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_type>>;
582 const size_type num_permute_to_lids = permute_to_lids.extent(0);
585 range_policy(0, num_permute_to_lids),
586 KOKKOS_LAMBDA(
const size_t i) {
587 using atomic_incr_type =
typename std::remove_reference<decltype(new_start_row(0))>::type;
589 const LO src_lid = permute_from_lids(i);
590 const size_t src_row = local_graph.row_map(src_lid);
592 const LO tgt_lid = permute_to_lids(i);
593 const size_t tgt_row = tgt_rowptr(tgt_lid);
595 size_t nsr = local_graph.row_map(src_lid+1)
596 - local_graph.row_map(src_lid);
597 Kokkos::atomic_fetch_add(&new_start_row(tgt_lid), atomic_incr_type(nsr));
599 for (
size_t j=local_graph.row_map(src_lid);
600 j<local_graph.row_map(src_lid+1); ++j) {
601 LO src_col = local_graph.entries(j);
602 tgt_colind(tgt_row + j - src_row) = local_col_map.getGlobalElement(src_col);
603 tgt_pids(tgt_row + j - src_row) = (src_pids(src_col) != my_pid) ? src_pids(src_col) : -1;
609 template<
class Packet,
class LocalGraph,
class LocalMap,
class BufferDevice>
611 unpackAndCombineIntoCrsArrays2(
612 const Kokkos::View<typename LocalMap::global_ordinal_type*, typename LocalMap::device_type>& tgt_colind,
613 const Kokkos::View<int*, typename LocalMap::device_type>& tgt_pids,
614 const Kokkos::View<size_t*,typename LocalMap::device_type>& new_start_row,
615 const Kokkos::View<const size_t*, typename LocalMap::device_type>& offsets,
616 const Kokkos::View<const typename LocalMap::local_ordinal_type*, typename LocalMap::device_type>& import_lids,
617 const Kokkos::View<const Packet*, BufferDevice>& imports,
618 const Kokkos::View<const size_t*, BufferDevice>& num_packets_per_lid,
624 using Kokkos::subview;
625 using Kokkos::MemoryUnmanaged;
626 using Kokkos::parallel_reduce;
627 using Kokkos::atomic_fetch_add;
629 using packet_type = Packet;
630 using buffer_device_type = BufferDevice;
631 using device_type =
typename LocalMap::device_type;
632 using LO =
typename LocalMap::local_ordinal_type;
633 using GO =
typename LocalMap::global_ordinal_type;
634 using execution_space =
typename device_type::execution_space;
635 using size_type =
typename Kokkos::View<LO*, device_type>::size_type;
636 using slice =
typename Kokkos::pair<size_type, size_type>;
637 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_type>>;
639 using pids_out_type = View<int*,device_type, MemoryUnmanaged>;
640 using gids_out_type = View<GO*, device_type, MemoryUnmanaged>;
642 const size_type num_import_lids = import_lids.size();
643 const char prefix[] =
"UnpackAndCombineCrsGraphImpl::unpackAndCombineIntoCrsArrays2: ";
647 parallel_reduce(
"Unpack and combine into CRS",
648 range_policy(0, num_import_lids),
649 KOKKOS_LAMBDA(
const size_t i,
int& err) {
650 using atomic_incr_type =
typename std::remove_reference< decltype( new_start_row(0) )>::type;
651 const size_t num_packets_this_lid = num_packets_per_lid(i);
652 const size_t num_ent = num_packets_this_lid / 2;
653 const size_t offset = offsets(i);
654 const LO lcl_row = import_lids(i);
655 const size_t start_row = atomic_fetch_add(&new_start_row(lcl_row), atomic_incr_type(num_ent));
656 const size_t end_row = start_row + num_ent;
658 gids_out_type gids_out = subview(tgt_colind, slice(start_row, end_row));
659 pids_out_type pids_out = subview(tgt_pids, slice(start_row, end_row));
661 err += unpackRow<packet_type,GO,device_type,buffer_device_type>(
662 gids_out, pids_out, imports, offset, num_ent);
665 for (
size_t j = 0; j < static_cast<size_t>(num_ent); ++j) {
666 const int pid = pids_out(j);
667 pids_out(j) = (pid != my_pid) ? pid : -1;
671 TEUCHOS_TEST_FOR_EXCEPTION(gbl_err_count != 0,
672 std::invalid_argument, prefix <<
673 "Attempting to unpack PIDs, but num_ent is not even; this should never "
674 "happen! Please report this bug to the Tpetra developers.");
679 template<
class Packet,
class LocalGraph,
class LocalMap,
class BufferDevice>
682 const LocalGraph & local_graph,
683 const LocalMap & local_col_map,
684 const Kokkos::View<
const typename LocalMap::local_ordinal_type*,
685 typename LocalMap::device_type,
686 Kokkos::MemoryUnmanaged>& import_lids,
687 const Kokkos::View<const Packet*, BufferDevice>& imports,
688 const Kokkos::View<const size_t*, BufferDevice>& num_packets_per_lid,
689 const Kokkos::View<
const typename LocalMap::local_ordinal_type*,
690 typename LocalMap::device_type,
691 Kokkos::MemoryUnmanaged>& permute_to_lids,
692 const Kokkos::View<
const typename LocalMap::local_ordinal_type*,
693 typename LocalMap::device_type,
694 Kokkos::MemoryUnmanaged>& permute_from_lids,
695 const Kokkos::View<
size_t*,
696 typename LocalMap::device_type,
697 Kokkos::MemoryUnmanaged>& tgt_rowptr,
698 const Kokkos::View<
typename LocalMap::global_ordinal_type*,
699 typename LocalMap::device_type,
700 Kokkos::MemoryUnmanaged>& tgt_colind,
701 const Kokkos::View<
const int*,
702 typename LocalMap::device_type,
703 Kokkos::MemoryUnmanaged>& src_pids,
704 const Kokkos::View<
int*,
705 typename LocalMap::device_type,
706 Kokkos::MemoryUnmanaged>& tgt_pids,
707 const size_t num_same_ids,
708 const size_t tgt_num_rows,
709 const size_t tgt_num_nonzeros,
710 const int my_tgt_pid)
713 using Kokkos::subview;
714 using Kokkos::parallel_for;
715 using Kokkos::MemoryUnmanaged;
716 using packet_type = Packet;
717 using local_map_type = LocalMap;
718 using local_graph_type = LocalGraph;
719 using buffer_device_type = BufferDevice;
720 using device_type =
typename LocalMap::device_type;
721 using LO =
typename LocalMap::local_ordinal_type;
722 using execution_space =
typename device_type::execution_space;
723 using size_type =
typename Kokkos::View<LO*, device_type>::size_type;
724 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<size_t>>;
726 const char prefix[] =
"UnpackAndCombineCrsGraphImpl::unpackAndCombineIntoCrsArrays: ";
728 const size_t N = tgt_num_rows;
729 const size_t mynnz = tgt_num_nonzeros;
733 const int my_pid = my_tgt_pid;
737 range_policy(0, N+1),
738 KOKKOS_LAMBDA(
const size_t i) {
745 range_policy(0, num_same_ids),
746 KOKKOS_LAMBDA(
const size_t i) {
747 const LO tgt_lid =
static_cast<LO
>(i);
748 const LO src_lid =
static_cast<LO
>(i);
749 tgt_rowptr(tgt_lid) = local_graph.row_map(src_lid+1)
750 - local_graph.row_map(src_lid);
755 const size_type num_permute_to_lids = permute_to_lids.extent(0);
757 range_policy(0, num_permute_to_lids),
758 KOKKOS_LAMBDA(
const size_t i) {
759 const LO tgt_lid = permute_to_lids(i);
760 const LO src_lid = permute_from_lids(i);
761 tgt_rowptr(tgt_lid) = local_graph.row_map(src_lid+1)
762 - local_graph.row_map(src_lid);
767 const size_type num_import_lids = import_lids.extent(0);
768 View<size_t*, device_type> offsets(
"offsets", num_import_lids+1);
771 #ifdef HAVE_TPETRA_DEBUG
773 auto nth_offset_h = getEntryOnHost(offsets, num_import_lids);
774 const bool condition =
775 nth_offset_h !=
static_cast<size_t>(imports.extent(0));
776 TEUCHOS_TEST_FOR_EXCEPTION
777 (condition, std::logic_error, prefix
778 <<
"The final offset in bytes " << nth_offset_h
779 <<
" != imports.size() = " << imports.extent(0)
780 <<
". Please report this bug to the Tpetra developers.");
782 #endif // HAVE_TPETRA_DEBUG
785 setupRowPointersForRemotes<packet_type,LO,device_type,buffer_device_type>(
786 tgt_rowptr, import_lids, imports, num_packets_per_lid);
790 View<size_t*, device_type> new_start_row(
"new_start_row", N+1);
793 makeCrsRowPtrFromLengths(tgt_rowptr, new_start_row);
795 auto nth_tgt_rowptr_h = getEntryOnHost(tgt_rowptr, N);
796 bool condition = nth_tgt_rowptr_h != mynnz;
797 TEUCHOS_TEST_FOR_EXCEPTION(condition, std::invalid_argument,
798 prefix <<
"CRS_rowptr[last] = " <<
799 nth_tgt_rowptr_h <<
"!= mynnz = " << mynnz <<
".");
803 copyDataFromSameIDs<LocalGraph,LocalMap>(tgt_colind, tgt_pids, new_start_row,
804 tgt_rowptr, src_pids, local_graph, local_col_map, num_same_ids, my_pid);
806 copyDataFromPermuteIDs<LocalGraph,LocalMap>(tgt_colind, tgt_pids, new_start_row,
807 tgt_rowptr, src_pids, permute_to_lids, permute_from_lids,
808 local_graph, local_col_map, my_pid);
810 if (imports.extent(0) <= 0) {
814 unpackAndCombineIntoCrsArrays2<
815 packet_type,local_graph_type,local_map_type,buffer_device_type>(
816 tgt_colind, tgt_pids, new_start_row, offsets, import_lids, imports,
817 num_packets_per_lid, local_graph, local_col_map, my_pid);
857 template<
class LO,
class GO,
class Node>
862 const Teuchos::ArrayView<const size_t>& numPacketsPerLID,
863 const Teuchos::ArrayView<const LO>& importLIDs,
870 "Graph must be globally indexed!");
874 using UnpackAndCombineCrsGraphImpl::unpackAndCombine;
876 using device_type =
typename Node::device_type;
877 using packet_type =
typename graph_type::packet_type;
878 using buffer_device_type =
typename graph_type::buffer_device_type;
879 using execution_space =
typename device_type::execution_space;
880 typename execution_space::device_type outputDevice;
881 using buffer_execution_space =
typename buffer_device_type::execution_space;
882 typename buffer_execution_space::device_type bufferOutputDevice;
883 using range_policy = Kokkos::RangePolicy<execution_space, Kokkos::IndexType<LO>>;
885 using row_ptrs_type =
typename graph_type::local_graph_type::row_map_type::non_const_type;
886 using indices_type =
typename graph_type::t_GlobalOrdinal_1D;
895 imports.getRawPtr(), imports.size(),
898 auto num_packets_per_lid_d =
900 numPacketsPerLID.getRawPtr(), numPacketsPerLID.size(),
901 true,
"num_packets_per_lid");
905 importLIDs.getRawPtr(), importLIDs.size(),
906 true,
"import_lids");
910 indices_type indices(
"indices", graph.
k_gblInds1D_.extent(0));
913 row_ptrs_type row_ptrs_beg(
"row_ptrs_beg", graph.
k_rowPtrs_.extent(0));
916 const size_t N = (row_ptrs_beg.extent(0) == 0 ? 0 : row_ptrs_beg.extent(0) - 1);
917 row_ptrs_type row_ptrs_end(
"row_ptrs_end", N);
919 bool refill_num_row_entries =
false;
922 refill_num_row_entries =
true;
924 Kokkos::parallel_for(
"Fill end row pointers", range_policy(0, N),
925 KOKKOS_LAMBDA(
const size_t i){
926 row_ptrs_end(i) = row_ptrs_beg(i) + num_row_entries(i);
934 Kokkos::parallel_for(
"Fill end row pointers",
935 range_policy(0, N), KOKKOS_LAMBDA(
const size_t i){
936 row_ptrs_end(i) = row_ptrs_beg(i+1);
941 unpackAndCombine<LO,packet_type,row_ptrs_type,indices_type,device_type,buffer_device_type>(
942 row_ptrs_beg, row_ptrs_end, indices, imports_d,
943 num_packets_per_lid_d, import_lids_d,
false);
947 if (refill_num_row_entries) {
948 Kokkos::parallel_for(
"Fill num entries",
949 range_policy(0, N), KOKKOS_LAMBDA(
const size_t i){
959 template<
class LO,
class GO,
class Node>
961 unpackCrsGraphAndCombineNew(
965 const Kokkos::DualView<
const size_t*,
967 const Kokkos::DualView<
const LO*,
973 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"METHOD NOT COMPLETE");
975 using UnpackAndCombineCrsGraphImpl::unpackAndCombine;
978 using device_type =
typename Node::device_type;
980 using packet_type =
typename graph_type::packet_type;
981 using local_graph_type =
typename graph_type::local_graph_type;
982 using buffer_device_type =
typename graph_type::buffer_device_type;
983 using buffer_memory_space =
typename buffer_device_type::memory_space;
984 using memory_space =
typename device_type::memory_space;
986 using row_ptrs_type =
typename graph_type::local_graph_type::row_map_type::non_const_type;
987 using execution_space =
typename device_type::execution_space;
988 using indices_type = Kokkos::View<GO*, execution_space>;
990 static_assert(std::is_same<device_type, typename local_graph_type::device_type>::value,
991 "Node::device_type and LocalGraph::device_type must be "
996 numPacketsPerLID_nc.sync_device ();
998 auto num_packets_per_lid_d = numPacketsPerLID.view_device ();
1000 TEUCHOS_ASSERT( ! importLIDs.need_sync_device () );
1001 auto import_lids_d = importLIDs.view_device ();
1005 imports_nc.sync_device ();
1007 auto imports_d = imports.view_device ();
1011 indices_type indices;
1012 row_ptrs_type row_ptrs_beg;
1013 row_ptrs_type row_ptrs_end;
1014 unpackAndCombine<LO,packet_type,row_ptrs_type,indices_type,device_type,buffer_device_type>(
1015 row_ptrs_beg, row_ptrs_end, indices, imports_d,
1016 num_packets_per_lid_d, import_lids_d,
false);
1069 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
1073 const Teuchos::ArrayView<const LocalOrdinal> &importLIDs,
1075 const Teuchos::ArrayView<const size_t>& numPacketsPerLID,
1080 const Teuchos::ArrayView<const LocalOrdinal>& permuteToLIDs,
1081 const Teuchos::ArrayView<const LocalOrdinal>& permuteFromLIDs)
1083 using Kokkos::MemoryUnmanaged;
1085 using device_type =
typename Node::device_type;
1089 const char prefix[] =
"unpackAndCombineWithOwningPIDsCount: ";
1091 TEUCHOS_TEST_FOR_EXCEPTION
1092 (permuteToLIDs.size() != permuteFromLIDs.size(), std::invalid_argument,
1093 prefix <<
"permuteToLIDs.size() = " << permuteToLIDs.size() <<
" != "
1094 "permuteFromLIDs.size() = " << permuteFromLIDs.size() <<
".");
1098 TEUCHOS_TEST_FOR_EXCEPTION
1099 (! locallyIndexed, std::invalid_argument, prefix <<
"The input "
1100 "CrsGraph 'sourceGraph' must be locally indexed.");
1101 TEUCHOS_TEST_FOR_EXCEPTION
1102 (importLIDs.size() != numPacketsPerLID.size(), std::invalid_argument,
1103 prefix <<
"importLIDs.size() = " << importLIDs.size() <<
" != "
1104 "numPacketsPerLID.size() = " << numPacketsPerLID.size() <<
".");
1107 auto permute_from_lids_d =
1109 permuteFromLIDs.getRawPtr(),
1110 permuteFromLIDs.size(),
true,
1111 "permute_from_lids");
1114 imports.getRawPtr(),
1115 imports.size(),
true,
1117 auto num_packets_per_lid_d =
1119 numPacketsPerLID.getRawPtr(),
1120 numPacketsPerLID.size(),
true,
1121 "num_packets_per_lid");
1124 packet_type,local_graph_type,buffer_device_type>(
1125 local_graph, permute_from_lids_d, imports_d, num_packets_per_lid_d, numSameIDs);
1141 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
1145 const Teuchos::ArrayView<const LocalOrdinal>& importLIDs,
1147 const Teuchos::ArrayView<const size_t>& numPacketsPerLID,
1151 const size_t numSameIDs,
1152 const Teuchos::ArrayView<const LocalOrdinal>& permuteToLIDs,
1153 const Teuchos::ArrayView<const LocalOrdinal>& permuteFromLIDs,
1154 size_t TargetNumRows,
1155 size_t TargetNumNonzeros,
1156 const int MyTargetPID,
1157 const Teuchos::ArrayView<size_t>& CRS_rowptr,
1158 const Teuchos::ArrayView<GlobalOrdinal>& CRS_colind,
1159 const Teuchos::ArrayView<const int>& SourcePids,
1160 Teuchos::Array<int>& TargetPids)
1164 using Teuchos::ArrayView;
1165 using Teuchos::outArg;
1166 using Teuchos::REDUCE_MAX;
1167 using Teuchos::reduceAll;
1168 using LO = LocalOrdinal;
1172 using device_type =
typename Node::device_type;
1173 using execution_space =
typename device_type::execution_space;
1174 using buffer_execution_space =
typename buffer_device_type::execution_space;
1175 using size_type =
typename ArrayView<const LO>::size_type;
1177 const char prefix[] =
"Tpetra::Details::unpackAndCombineIntoCrsArrays: ";
1179 TEUCHOS_TEST_FOR_EXCEPTION(
1180 TargetNumRows + 1 != static_cast<size_t>(CRS_rowptr.size()),
1181 std::invalid_argument, prefix <<
"CRS_rowptr.size() = " <<
1182 CRS_rowptr.size() <<
"!= TargetNumRows+1 = " << TargetNumRows+1 <<
".");
1184 TEUCHOS_TEST_FOR_EXCEPTION(
1185 permuteToLIDs.size() != permuteFromLIDs.size(), std::invalid_argument,
1186 prefix <<
"permuteToLIDs.size() = " << permuteToLIDs.size()
1187 <<
"!= permuteFromLIDs.size() = " << permuteFromLIDs.size() <<
".");
1188 const size_type numImportLIDs = importLIDs.size();
1190 TEUCHOS_TEST_FOR_EXCEPTION(
1191 numImportLIDs != numPacketsPerLID.size(), std::invalid_argument,
1192 prefix <<
"importLIDs.size() = " << numImportLIDs <<
" != "
1193 "numPacketsPerLID.size() = " << numPacketsPerLID.size() <<
".");
1196 if (static_cast<size_t>(TargetPids.size()) != TargetNumNonzeros) {
1197 TargetPids.resize(TargetNumNonzeros);
1199 TargetPids.assign(TargetNumNonzeros, -1);
1203 auto local_col_map = sourceGraph.
getColMap()->getLocalMap();
1206 typename execution_space::device_type outputDevice;
1207 typename buffer_execution_space::device_type bufferOutputDevice;
1210 importLIDs.getRawPtr(), importLIDs.size(),
1211 true,
"import_lids");
1214 imports.getRawPtr(), imports.size(),
1218 numPacketsPerLID.getRawPtr(), numPacketsPerLID.size(),
1219 true,
"num_packets_per_lid");
1222 permuteFromLIDs.getRawPtr(), permuteFromLIDs.size(),
1223 true,
"permute_from_lids");
1226 permuteToLIDs.getRawPtr(), permuteToLIDs.size(),
1227 true,
"permute_to_lids");
1230 CRS_rowptr.getRawPtr(), CRS_rowptr.size(),
1231 true,
"crs_rowptr");
1234 CRS_colind.getRawPtr(), CRS_colind.size(),
1235 true,
"crs_colidx");
1238 SourcePids.getRawPtr(), SourcePids.size(),
1242 TargetPids.getRawPtr(), TargetPids.size(),
1245 using local_map_type = decltype(local_col_map);
1247 packet_type,local_graph_type,local_map_type,buffer_device_type>(
1248 local_graph, local_col_map, import_lids_d, imports_d, num_packets_per_lid_d,
1249 permute_to_lids_d, permute_from_lids_d, crs_rowptr_d, crs_colind_d, src_pids_d,
1250 tgt_pids_d, numSameIDs, TargetNumRows, TargetNumNonzeros, MyTargetPID);
1253 typename decltype(crs_rowptr_d)::HostMirror crs_rowptr_h(
1254 CRS_rowptr.getRawPtr(), CRS_rowptr.size());
1257 typename decltype(crs_colind_d)::HostMirror crs_colind_h(
1258 CRS_colind.getRawPtr(), CRS_colind.size());
1261 typename decltype(tgt_pids_d)::HostMirror tgt_pids_h(
1262 TargetPids.getRawPtr(), TargetPids.size());
1270 #define TPETRA_DETAILS_UNPACKCRSGRAPHANDCOMBINE_INSTANT( LO, GO, NT ) \
1272 Details::unpackCrsGraphAndCombine<LO, GO, NT>( \
1273 CrsGraph<LO, GO, NT>&, \
1274 const Teuchos::ArrayView<const typename CrsGraph<LO,GO,NT>::packet_type>&, \
1275 const Teuchos::ArrayView<const size_t>&, \
1276 const Teuchos::ArrayView<const LO>&, \
1281 Details::unpackCrsGraphAndCombineNew<LO, GO, NT>( \
1282 CrsGraph<LO, GO, NT>&, \
1283 const Kokkos::DualView<const CrsGraph<LO, GO, NT>::packet_type*, \
1284 CrsGraph<LO, GO, NT>::buffer_device_type>&, \
1285 const Kokkos::DualView<const size_t*, \
1286 CrsGraph<LO, GO, NT>::buffer_device_type>&, \
1287 const Kokkos::DualView<const LO*, \
1288 CrsGraph<LO, GO, NT>::buffer_device_type>&, \
1291 const CombineMode); \
1293 Details::unpackAndCombineIntoCrsArrays<LO, GO, NT>( \
1294 const CrsGraph<LO, GO, NT> &, \
1295 const Teuchos::ArrayView<const LO>&, \
1296 const Teuchos::ArrayView<const typename CrsGraph<LO,GO,NT>::packet_type>&, \
1297 const Teuchos::ArrayView<const size_t>&, \
1300 const CombineMode, \
1302 const Teuchos::ArrayView<const LO>&, \
1303 const Teuchos::ArrayView<const LO>&, \
1307 const Teuchos::ArrayView<size_t>&, \
1308 const Teuchos::ArrayView<GO>&, \
1309 const Teuchos::ArrayView<const int>&, \
1310 Teuchos::Array<int>&); \
1312 Details::unpackAndCombineWithOwningPIDsCount<LO, GO, NT>( \
1313 const CrsGraph<LO, GO, NT> &, \
1314 const Teuchos::ArrayView<const LO> &, \
1315 const Teuchos::ArrayView<const typename CrsGraph<LO,GO,NT>::packet_type> &, \
1316 const Teuchos::ArrayView<const size_t>&, \
1321 const Teuchos::ArrayView<const LO>&, \
1322 const Teuchos::ArrayView<const LO>&);
1324 #endif // TPETRA_DETAILS_UNPACKCRSGRAPHANDCOMBINE_DEF_HPP
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.
t_GlobalOrdinal_1D k_gblInds1D_
Global column indices for all rows.
Declaration of the Tpetra::CrsGraph class.
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, Distributor &distor, 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...
bool isGloballyIndexed() const override
Whether the graph's column indices are stored as global indices.
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.
num_row_entries_type k_numRowEntries_
The number of local entries in each locally owned row.
Functions for manipulating CRS arrays.
Declare and define the function Tpetra::Details::computeOffsetsFromCounts, an implementation detail o...
Kokkos::StaticCrsGraph< local_ordinal_type, Kokkos::LayoutLeft, execution_space > local_graph_type
The type of the part of the sparse graph on each MPI process.
Sets up and executes a communication plan for a Tpetra DistObject.
local_graph_type::row_map_type::const_type k_rowPtrs_
Row offsets for "1-D" storage.
CombineMode
Rule for combining data in an Import or Export.
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, Distributor &distor, 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
OffsetsViewType::non_const_value_type computeOffsetsFromCounts(const OffsetsViewType &ptr, const CountsViewType &counts)
Compute offsets from counts.
Unpacks and combines a single row of the CrsGraph.
void padCrsArrays(RowPtr &rowPtrBeg, RowPtr &rowPtrEnd, Indices &indices, const Padding &padding)
Determine if the row pointers and indices arrays need to be resized to accommodate new entries...
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.
bool isLocallyIndexed() const override
Whether the graph's column indices are stored as local indices.
void unpackCrsGraphAndCombine(CrsGraph< LO, GO, NT > &sourceGraph, const Teuchos::ArrayView< const typename CrsGraph< LO, GO, NT >::packet_type > &imports, const Teuchos::ArrayView< const size_t > &numPacketsPerLID, const Teuchos::ArrayView< const LO > &importLIDs, size_t constantNumPackets, Distributor &distor, CombineMode combineMode)
Unpack the imported column indices and combine into graph.
Declaration and definition of Tpetra::Details::castAwayConstDualView, an implementation detail of Tpe...
Kokkos::DualView< ValueType *, DeviceType > castAwayConstDualView(const Kokkos::DualView< const ValueType *, DeviceType > &input_dv)
Cast away const-ness of a 1-D Kokkos::DualView.
local_graph_type getLocalGraph() const
Get the local graph.
Declaration and definition of Tpetra::Details::getEntryOnHost.
global_ordinal_type packet_type
Type of each entry of the DistObject communication buffer.
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.