15#ifdef DEAL_II_TRILINOS_WITH_TPETRA
25# include <Teuchos_FancyOStream.hpp>
34#ifdef DEAL_II_TRILINOS_WITH_TPETRA
39 namespace TpetraWrappers
43 template <
typename MemorySpace>
49 if (
static_cast<std::size_t
>(this->a_row) == sparsity_pattern->n_rows())
56 if (!sparsity_pattern->is_compressed())
57 sparsity_pattern->compress();
60 std::make_shared<std::vector<::types::signed_global_dof_index>>(
61 sparsity_pattern->row_length(this->a_row));
63 if (colnum_cache->size() > 0)
69 column_indices_view(colnum_cache->data(), colnum_cache->size());
71 sparsity_pattern->graph->getGlobalRowCopy(this->a_row,
85 template <
typename MemorySpace>
97 graph->fillComplete();
102 template <
typename MemorySpace>
108 reinit(m, n, n_entries_per_row);
113 template <
typename MemorySpace>
117 const std::vector<size_type> &n_entries_per_row)
119 reinit(m, n, n_entries_per_row);
124 template <
typename MemorySpace>
128 , column_space_map(std::move(other.column_space_map))
129 , graph(std::move(other.graph))
130 , nonlocal_graph(std::move(other.nonlocal_graph))
136 template <
typename MemorySpace>
144 Utilities::Trilinos::tpetra_comm_self()))
146 TpetraTypes::GraphType<
MemorySpace>>(column_space_map,
150 (void)input_sparsity;
153 "Copy constructor only works for empty sparsity patterns."));
158 template <
typename MemorySpace>
160 const IndexSet ¶llel_partitioning,
164 reinit(parallel_partitioning,
165 parallel_partitioning,
172 template <
typename MemorySpace>
174 const IndexSet ¶llel_partitioning,
176 const std::vector<size_type> &n_entries_per_row)
178 reinit(parallel_partitioning,
179 parallel_partitioning,
186 template <
typename MemorySpace>
188 const IndexSet &row_parallel_partitioning,
189 const IndexSet &col_parallel_partitioning,
193 reinit(row_parallel_partitioning,
194 col_parallel_partitioning,
201 template <
typename MemorySpace>
203 const IndexSet &row_parallel_partitioning,
204 const IndexSet &col_parallel_partitioning,
206 const std::vector<size_type> &n_entries_per_row)
208 reinit(row_parallel_partitioning,
209 col_parallel_partitioning,
216 template <
typename MemorySpace>
218 const IndexSet &row_parallel_partitioning,
219 const IndexSet &col_parallel_partitioning,
224 reinit(row_parallel_partitioning,
225 col_parallel_partitioning,
228 n_max_entries_per_row);
233 template <
typename MemorySpace>
247 template <
typename MemorySpace>
252 const std::vector<size_type> &n_entries_per_row)
262 namespace SparsityPatternImpl
264 template <
typename MemorySpace>
267 template <
typename MemorySpace>
277 Assert(row_map->isOneToOne(),
278 ExcMessage(
"Row map must be 1-to-1, i.e., no overlap between "
279 "the maps of different processors."));
280 Assert(col_map->isOneToOne(),
281 ExcMessage(
"Column map must be 1-to-1, i.e., no overlap between "
282 "the maps of different processors."));
284 nonlocal_graph.reset();
286 column_space_map = col_map;
300 template <
typename MemorySpace>
310 Assert(row_map->isOneToOne(),
311 ExcMessage(
"Row map must be 1-to-1, i.e., no overlap between "
312 "the maps of different processors."));
313 Assert(col_map->isOneToOne(),
314 ExcMessage(
"Column map must be 1-to-1, i.e., no overlap between "
315 "the maps of different processors."));
318 nonlocal_graph.reset();
321 row_map->getGlobalNumElements());
323 column_space_map = col_map;
327 Kokkos::DualView<size_t *, typename MemorySpace::kokkos_space>
328 local_entries_per_row(
"local_entries_per_row",
329 row_map->getMaxGlobalIndex() -
330 row_map->getMinGlobalIndex());
332 auto local_entries_per_row_host =
333 local_entries_per_row
334 .template view<Kokkos::DefaultHostExecutionSpace>();
336 std::uint64_t total_size = 0;
337 for (
unsigned int i = 0; i < local_entries_per_row.extent(0); ++i)
339 local_entries_per_row_host(i) =
340 n_entries_per_row[row_map->getMinGlobalIndex() + i];
341 total_size += local_entries_per_row_host[i];
343 local_entries_per_row
344 .template modify<Kokkos::DefaultHostExecutionSpace>();
345 local_entries_per_row
346 .template sync<typename MemorySpace::kokkos_space>();
349 total_size <
static_cast<std::uint64_t
>(
353 "You are requesting to store more elements than global ordinal type allows."));
361 template <
typename SparsityPatternType,
typename MemorySpace>
366 const SparsityPatternType &sp,
367 [[maybe_unused]]
const bool exchange_data,
372 nonlocal_graph.reset();
381 Assert(row_map->isContiguous() ==
true,
383 "This function only works if the row map is contiguous."));
387 row_map->getMaxGlobalIndex() + 1;
389 Teuchos::Array<size_t> n_entries_per_row(
390 row_map->getLocalNumElements());
392 if (row_map->getLocalNumElements() > 0)
395 n_entries_per_row[row - first_row] = sp.row_length(row);
399 std::accumulate(n_entries_per_row.begin(),
400 n_entries_per_row.end(),
402 static_cast<std::uint64_t
>(std::numeric_limits<int>::max()),
404 "The TrilinosWrappers use Tpetra internally, and "
405 "Trilinos/Tpetra was compiled with 'local ordinate = int'. "
406 "Therefore, 'signed int' is used to represent local indices, "
407 "and only 2,147,483,647 nonzero matrix entries can be stored "
408 "on a single process, but you are requesting more than "
409 "that. Either use more MPI processes or recompile Trilinos "
410 "with 'local ordinate = long long' "));
420 std::vector<TrilinosWrappers::types::int_type> row_indices;
422 if (row_map->getLocalNumElements() > 0)
431 row_indices.resize(row_length, -1);
433 typename SparsityPatternType::iterator p = sp.begin(row);
436 for (
int col = 0; col < row_length;)
438 row_indices[col++] = p->column();
439 if (col < row_length)
443 graph->insertGlobalIndices(row, row_length, row_indices.data());
447 graph->globalAssemble();
452 template <
typename MemorySpace>
459 parallel_partitioning.
size());
460 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> map =
461 parallel_partitioning
462 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
463 communicator,
false);
464 SparsityPatternImpl::reinit_sp<MemorySpace>(
465 map, map, n_entries_per_row, column_space_map, graph, nonlocal_graph);
470 template <
typename MemorySpace>
473 const IndexSet ¶llel_partitioning,
475 const std::vector<size_type> &n_entries_per_row)
478 parallel_partitioning.
size());
479 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> map =
480 parallel_partitioning
481 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
482 communicator,
false);
483 SparsityPatternImpl::reinit_sp<MemorySpace>(
484 map, map, n_entries_per_row, column_space_map, graph, nonlocal_graph);
489 template <
typename MemorySpace>
492 const IndexSet &row_parallel_partitioning,
493 const IndexSet &col_parallel_partitioning,
498 col_parallel_partitioning.
size());
499 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> row_map =
500 row_parallel_partitioning
501 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
502 communicator,
false);
503 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> col_map =
504 col_parallel_partitioning
505 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
506 communicator,
false);
507 SparsityPatternImpl::reinit_sp<MemorySpace>(row_map,
517 template <
typename MemorySpace>
520 const IndexSet &row_parallel_partitioning,
521 const IndexSet &col_parallel_partitioning,
523 const std::vector<size_type> &n_entries_per_row)
526 col_parallel_partitioning.
size());
527 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> row_map =
528 row_parallel_partitioning
529 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
530 communicator,
false);
531 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> col_map =
532 col_parallel_partitioning
533 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
534 communicator,
false);
535 SparsityPatternImpl::reinit_sp<MemorySpace>(row_map,
545 template <
typename MemorySpace>
549 const IndexSet &col_parallel_partitioning,
555 col_parallel_partitioning.
size());
556 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> row_map =
557 row_parallel_partitioning
558 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
559 communicator,
false);
560 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> col_map =
561 col_parallel_partitioning
562 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
563 communicator,
false);
564 SparsityPatternImpl::reinit_sp<MemorySpace>(row_map,
571 IndexSet nonlocal_partitioner = writable_rows;
573 row_parallel_partitioning.size());
577 IndexSet tmp = writable_rows & row_parallel_partitioning;
578 Assert(tmp == row_parallel_partitioning,
580 "The set of writable rows passed to this method does not "
581 "contain the locally owned rows, which is not allowed."));
584 nonlocal_partitioner.
subtract_set(row_parallel_partitioning);
587 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> nonlocal_map =
589 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
592 TpetraTypes::GraphType<MemorySpace>>(nonlocal_map,
601 template <
typename MemorySpace>
602 template <
typename SparsityPatternType>
605 const IndexSet &row_parallel_partitioning,
606 const IndexSet &col_parallel_partitioning,
607 const SparsityPatternType &nontrilinos_sparsity_pattern,
609 const bool exchange_data)
612 col_parallel_partitioning.
size());
613 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> row_map =
614 row_parallel_partitioning
615 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
616 communicator,
false);
617 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> col_map =
618 col_parallel_partitioning
619 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
620 communicator,
false);
621 SparsityPatternImpl::reinit_sp<SparsityPatternType, MemorySpace>(
624 nontrilinos_sparsity_pattern,
633 template <
typename MemorySpace>
634 template <
typename SparsityPatternType>
637 const IndexSet ¶llel_partitioning,
638 const SparsityPatternType &nontrilinos_sparsity_pattern,
640 const bool exchange_data)
645 parallel_partitioning.size());
647 parallel_partitioning.size());
648 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> map =
649 parallel_partitioning
650 .template make_tpetra_map_rcp<TpetraTypes::NodeType<MemorySpace>>(
651 communicator,
false);
652 SparsityPatternImpl::reinit_sp<SparsityPatternType, MemorySpace>(
655 nontrilinos_sparsity_pattern,
664 template <
typename MemorySpace>
675 template <
typename MemorySpace>
690 nonlocal_graph.reset();
695 template <
typename MemorySpace>
696 template <
typename SparsityPatternType>
701 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> rows =
707 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> columns =
714 SparsityPatternImpl::reinit_sp<SparsityPatternType, MemorySpace>(
715 rows, columns, sp,
false, column_space_map, graph, nonlocal_graph);
720 template <
typename MemorySpace>
737 graph->fillComplete();
739 nonlocal_graph.reset();
744 template <
typename MemorySpace>
749 if (nonlocal_graph.get() !=
nullptr)
753 nonlocal_graph->fillComplete(column_space_map, graph->getRowMap());
757 nonlocal_graph->getRowMap(), graph->getRowMap());
758 graph->doExport(*nonlocal_graph, exporter, Tpetra::ADD);
760 graph->fillComplete(column_space_map, graph->getRowMap());
771 template <
typename MemorySpace>
775 return graph->getRowMap()->getLocalElement(i) !=
776 Teuchos::OrdinalTraits<int>::invalid();
781 template <
typename MemorySpace>
786 if (!row_is_stored_locally(i))
790 const auto trilinos_i = graph->getRowMap()->getLocalElement(i);
791 const auto trilinos_j = graph->getColMap()->getLocalElement(j);
797 graph->getLocalRowView(trilinos_i, col_indices);
801 std::find(col_indices.data(),
802 col_indices.data() + col_indices.size(),
806 return static_cast<std::size_t
>(local_col_index) != col_indices.size();
811 template <
typename MemorySpace>
816 for (
int i = 0; i < static_cast<int>(local_size()); ++i)
821 graph->getLocalRowView(i, indices);
822 const auto num_entries = indices.size();
823 for (
unsigned int j = 0; j < static_cast<unsigned int>(num_entries);
835 return static_cast<size_type
>(global_b);
840 template <
typename MemorySpace>
844# if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
845 return graph->getLocalNumRows();
847 return graph->getNodeNumRows();
853 template <
typename MemorySpace>
854 std::pair<typename SparsityPattern<MemorySpace>::size_type,
859 const size_type end = graph->getRowMap()->getMaxGlobalIndex() + 1;
866 template <
typename MemorySpace>
870 return graph->getGlobalNumEntries();
875 template <
typename MemorySpace>
879# if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
880 return graph->getLocalMaxNumRowEntries();
882 return graph->getNodeMaxNumRowEntries();
888 template <
typename MemorySpace>
897 graph->getRowMap()->getLocalElement(row);
902 return graph->getNumEntriesInLocalRow(local_row);
909 template <
typename MemorySpace>
912 const ::types::global_dof_index &row,
914 const bool indices_are_sorted)
916 add_entries(row, columns.
begin(), columns.
end(), indices_are_sorted);
921 template <
typename MemorySpace>
922 Teuchos::RCP<const typename TpetraTypes::MapType<MemorySpace>>
925 return graph->getDomainMap();
930 template <
typename MemorySpace>
931 Teuchos::RCP<const typename TpetraTypes::MapType<MemorySpace>>
934 return graph->getRangeMap();
939 template <
typename MemorySpace>
944 graph->getRangeMap()->getComm());
949 template <
typename MemorySpace>
950 Teuchos::RCP<const Teuchos::Comm<int>>
953 return graph->getRangeMap()->getComm();
961 template <
typename MemorySpace>
965 const bool write_extended_trilinos_info)
const
967 if (write_extended_trilinos_info)
970 Teuchos::getFancyOStream(Teuchos::RCP(&out,
false));
971 graph->describe(*fancy_stream);
975# if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
976 for (
unsigned int i = 0; i < graph->getLocalNumRows(); ++i)
978 for (
unsigned int i = 0; i < graph->getNodeNumRows(); ++i)
983 graph->getLocalRowView(i, indices);
984 int num_entries = indices.size();
985 for (
int j = 0; j < num_entries; ++j)
986 out <<
"(" << graph->getRowMap()->getGlobalElement(i) <<
","
987 << graph->getColMap()->getGlobalElement(indices[j]) <<
") "
997 template <
typename MemorySpace>
1003 for (
unsigned int row = 0; row < local_size(); ++row)
1008 graph->getLocalRowView(row, indices);
1009 int num_entries = indices.size();
1013 const ::types::signed_global_dof_index num_entries_ =
1022 out <<
static_cast<int>(
1023 graph->getColMap()->getGlobalElement(indices[j]))
1025 << -
static_cast<int>(graph->getRowMap()->getGlobalElement(row))
1033 template <
typename MemorySpace>
1048 const ::SparsityPattern &);
1051 const ::DynamicSparsityPattern &);
1056 const ::SparsityPattern &,
1062 const ::DynamicSparsityPattern &,
1071 const ::SparsityPattern &,
1078 const ::DynamicSparsityPattern &,
1087 const ::SparsityPattern &);
1090 const ::DynamicSparsityPattern &);
1095 const ::SparsityPattern &,
1101 const ::DynamicSparsityPattern &,
1110 const ::SparsityPattern &,
1117 const ::DynamicSparsityPattern &,
size_type n_elements() const
void subtract_set(const IndexSet &other)
Teuchos::RCP< TpetraTypes::GraphType< MemorySpace > > graph
Teuchos::RCP< const Teuchos::Comm< int > > get_teuchos_mpi_communicator() const
std::pair< size_type, size_type > local_range() const
Teuchos::RCP< const TpetraTypes::MapType< MemorySpace > > range_partitioner() const
void copy_from(const SparsityPattern< MemorySpace > &input_sparsity_pattern)
SparsityPattern< MemorySpace > & operator=(const SparsityPattern< MemorySpace > &input_sparsity_pattern)
void print(std::ostream &out, const bool write_extended_trilinos_info=false) const
unsigned int max_entries_per_row() const
Teuchos::RCP< TpetraTypes::GraphType< MemorySpace > > nonlocal_graph
void print_gnuplot(std::ostream &out) const
MPI_Comm get_mpi_communicator() const
Teuchos::RCP< const TpetraTypes::MapType< MemorySpace > > domain_partitioner() const
std::uint64_t n_nonzero_elements() const
size_type bandwidth() const
bool exists(const size_type i, const size_type j) const
Teuchos::RCP< TpetraTypes::MapType< MemorySpace > > column_space_map
void reinit(const size_type m, const size_type n, const size_type n_entries_per_row)
unsigned int local_size() const
virtual void add_row_entries(const ::types::global_dof_index &row, const ArrayView< const ::types::global_dof_index > &columns, const bool indices_are_sorted=false) override
size_type row_length(const size_type row) const
std::size_t memory_consumption() const
bool row_is_stored_locally(const size_type i) const
virtual void resize(const size_type rows, const size_type cols)
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NOT_IMPLEMENTED()
static ::ExceptionBase & ExcIO()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
IndexSet complete_index_set(const IndexSet::size_type N)
void reinit_sp(const Teuchos::RCP< TpetraTypes::MapType< MemorySpace > > &row_map, const Teuchos::RCP< TpetraTypes::MapType< MemorySpace > > &col_map, const size_type< MemorySpace > n_entries_per_row, Teuchos::RCP< TpetraTypes::MapType< MemorySpace > > &column_space_map, Teuchos::RCP< TpetraTypes::GraphType< MemorySpace > > &graph, Teuchos::RCP< TpetraTypes::GraphType< MemorySpace > > &nonlocal_graph)
typename SparsityPattern< MemorySpace >::size_type size_type
Tpetra::CrsGraph< LO, GO, NodeType< MemorySpace > > GraphType
Tpetra::Map< LO, GO, NodeType< MemorySpace > > MapType
Tpetra::Export< LO, GO, NodeType< MemorySpace > > ExportType
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
Teuchos::RCP< T > make_rcp(Args &&...args)
MPI_Comm teuchos_comm_to_mpi_comm(const Teuchos::RCP< const Teuchos::Comm< int > > &teuchos_comm)
const Teuchos::RCP< const Teuchos::Comm< int > > & tpetra_comm_self()
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)