19#ifdef DEAL_II_WITH_TRILINOS
27# include <Epetra_Export.h>
66 AssertThrow(
static_cast<std::vector<size_type>::size_type
>(ncols) ==
91 graph = std::make_unique<Epetra_FECrsGraph>(View,
95 graph->FillComplete();
104 reinit(m, n, n_entries_per_row);
112 const std::vector<size_type> &n_entries_per_row)
114 reinit(m, n, n_entries_per_row);
121 , column_space_map(std::move(other.column_space_map))
122 , graph(std::move(other.graph))
123 , nonlocal_graph(std::move(other.nonlocal_graph))
136 new Epetra_FECrsGraph(View, *column_space_map, *column_space_map, 0))
138 (void)input_sparsity;
141 "Copy constructor only works for empty sparsity patterns."));
150 reinit(parallel_partitioning,
151 parallel_partitioning,
159 const IndexSet & parallel_partitioning,
161 const std::vector<size_type> &n_entries_per_row)
163 reinit(parallel_partitioning,
164 parallel_partitioning,
172 const IndexSet &col_parallel_partitioning,
176 reinit(row_parallel_partitioning,
177 col_parallel_partitioning,
185 const IndexSet & row_parallel_partitioning,
186 const IndexSet & col_parallel_partitioning,
188 const std::vector<size_type> &n_entries_per_row)
190 reinit(row_parallel_partitioning,
191 col_parallel_partitioning,
199 const IndexSet &col_parallel_partitioning,
204 reinit(row_parallel_partitioning,
205 col_parallel_partitioning,
208 n_max_entries_per_row);
229 const std::vector<size_type> &n_entries_per_row)
244 reinit_sp(
const Epetra_Map & row_map,
245 const Epetra_Map & col_map,
246 const size_type n_entries_per_row,
247 std::unique_ptr<Epetra_Map> & column_space_map,
248 std::unique_ptr<Epetra_FECrsGraph> &graph,
249 std::unique_ptr<Epetra_CrsGraph> & nonlocal_graph)
251 Assert(row_map.IsOneToOne(),
252 ExcMessage(
"Row map must be 1-to-1, i.e., no overlap between "
253 "the maps of different processors."));
254 Assert(col_map.IsOneToOne(),
255 ExcMessage(
"Column map must be 1-to-1, i.e., no overlap between "
256 "the maps of different processors."));
258 nonlocal_graph.reset();
260 column_space_map = std::make_unique<Epetra_Map>(col_map);
264 std::uint64_t(n_entries_per_row) <
265 static_cast<std::uint64_t
>(std::numeric_limits<int>::max()),
266 ExcMessage(
"The TrilinosWrappers use Epetra internally which "
267 "uses 'signed int' to represent local indices. "
268 "Therefore, only 2,147,483,647 nonzero matrix "
269 "entries can be stored on a single process, "
270 "but you are requesting more than that. "
271 "If possible, use more MPI processes."));
282 if (row_map.Comm().NumProc() > 1)
283 graph = std::make_unique<Epetra_FECrsGraph>(
284 Copy, row_map, n_entries_per_row,
false
292 graph = std::make_unique<Epetra_FECrsGraph>(
293 Copy, row_map, col_map, n_entries_per_row,
false);
299 reinit_sp(
const Epetra_Map & row_map,
300 const Epetra_Map & col_map,
301 const std::vector<size_type> & n_entries_per_row,
302 std::unique_ptr<Epetra_Map> & column_space_map,
303 std::unique_ptr<Epetra_FECrsGraph> &graph,
304 std::unique_ptr<Epetra_CrsGraph> & nonlocal_graph)
306 Assert(row_map.IsOneToOne(),
307 ExcMessage(
"Row map must be 1-to-1, i.e., no overlap between "
308 "the maps of different processors."));
309 Assert(col_map.IsOneToOne(),
310 ExcMessage(
"Column map must be 1-to-1, i.e., no overlap between "
311 "the maps of different processors."));
314 nonlocal_graph.reset();
319 column_space_map = std::make_unique<Epetra_Map>(col_map);
323 std::vector<int> local_entries_per_row(
326 for (
unsigned int i = 0; i < local_entries_per_row.size(); ++i)
327 local_entries_per_row[i] =
330 AssertThrow(std::accumulate(local_entries_per_row.begin(),
331 local_entries_per_row.end(),
333 static_cast<std::uint64_t
>(std::numeric_limits<int>::max()),
334 ExcMessage(
"The TrilinosWrappers use Epetra internally which "
335 "uses 'signed int' to represent local indices. "
336 "Therefore, only 2,147,483,647 nonzero matrix "
337 "entries can be stored on a single process, "
338 "but you are requesting more than that. "
339 "If possible, use more MPI processes."));
341 if (row_map.Comm().NumProc() > 1)
342 graph = std::make_unique<Epetra_FECrsGraph>(
343 Copy, row_map, local_entries_per_row.data(),
false
351 graph = std::make_unique<Epetra_FECrsGraph>(
352 Copy, row_map, col_map, local_entries_per_row.data(),
false);
357 template <
typename SparsityPatternType>
359 reinit_sp(
const Epetra_Map & row_map,
360 const Epetra_Map & col_map,
361 const SparsityPatternType & sp,
362 const bool exchange_data,
363 std::unique_ptr<Epetra_Map> & column_space_map,
364 std::unique_ptr<Epetra_FECrsGraph> &graph,
365 std::unique_ptr<Epetra_CrsGraph> & nonlocal_graph)
367 nonlocal_graph.reset();
375 column_space_map = std::make_unique<Epetra_Map>(col_map);
377 Assert(row_map.LinearMap() ==
true,
379 "This function only works if the row map is contiguous."));
383 std::vector<int> n_entries_per_row(last_row - first_row);
387 for (size_type row = first_row; row < last_row; ++row)
388 n_entries_per_row[row - first_row] =
389 static_cast<int>(sp.row_length(row));
391 AssertThrow(std::accumulate(n_entries_per_row.begin(),
392 n_entries_per_row.end(),
394 static_cast<std::uint64_t
>(std::numeric_limits<int>::max()),
395 ExcMessage(
"The TrilinosWrappers use Epetra internally which "
396 "uses 'signed int' to represent local indices. "
397 "Therefore, only 2,147,483,647 nonzero matrix "
398 "entries can be stored on a single process, "
399 "but you are requesting more than that. "
400 "If possible, use more MPI processes."));
402 if (row_map.Comm().NumProc() > 1)
403 graph = std::make_unique<Epetra_FECrsGraph>(Copy,
405 n_entries_per_row.data(),
408 graph = std::make_unique<Epetra_FECrsGraph>(
409 Copy, row_map, col_map, n_entries_per_row.data(),
false);
413 std::vector<TrilinosWrappers::types::int_type> row_indices;
417 if (exchange_data ==
false)
418 for (size_type row = first_row; row < last_row; ++row)
425 row_indices.resize(row_length, -1);
427 typename SparsityPatternType::iterator p = sp.begin(row);
430 for (
int col = 0; col < row_length;)
432 row_indices[col++] = p->column();
433 if (col < row_length)
437 graph->Epetra_CrsGraph::InsertGlobalIndices(row,
442 for (size_type row = 0; row < sp.n_rows(); ++row)
449 row_indices.resize(row_length, -1);
451 typename SparsityPatternType::iterator p = sp.begin(row);
454 for (
int col = 0; col < row_length;)
456 row_indices[col++] = p->column();
457 if (col < row_length)
462 graph->InsertGlobalIndices(1,
469 const auto &range_map =
470 static_cast<const Epetra_Map &
>(graph->RangeMap());
471 int ierr = graph->GlobalAssemble(*column_space_map, range_map,
true);
474 ierr = graph->OptimizeStorage();
487 parallel_partitioning.
size());
499 const std::vector<size_type> &n_entries_per_row)
502 parallel_partitioning.
size());
513 const IndexSet &col_parallel_partitioning,
518 col_parallel_partitioning.
size());
535 const IndexSet &col_parallel_partitioning,
537 const std::vector<size_type> &n_entries_per_row)
540 col_parallel_partitioning.
size());
557 const IndexSet &col_parallel_partitioning,
563 col_parallel_partitioning.
size());
575 IndexSet nonlocal_partitioner = writable_rows;
577 row_parallel_partitioning.
size());
580 IndexSet tmp = writable_rows & row_parallel_partitioning;
581 Assert(tmp == row_parallel_partitioning,
583 "The set of writable rows passed to this method does not "
584 "contain the locally owned rows, which is not allowed."));
587 nonlocal_partitioner.
subtract_set(row_parallel_partitioning);
590 Epetra_Map nonlocal_map =
593 std::make_unique<Epetra_CrsGraph>(Copy, nonlocal_map, 0);
601 template <
typename SparsityPatternType>
604 const IndexSet & row_parallel_partitioning,
605 const IndexSet & col_parallel_partitioning,
606 const SparsityPatternType &nontrilinos_sparsity_pattern,
608 const bool exchange_data)
611 col_parallel_partitioning.
size());
618 nontrilinos_sparsity_pattern,
627 template <
typename SparsityPatternType>
630 const IndexSet & parallel_partitioning,
631 const SparsityPatternType &nontrilinos_sparsity_pattern,
633 const bool exchange_data)
636 parallel_partitioning.
size());
638 parallel_partitioning.
size());
640 parallel_partitioning.
size());
645 nontrilinos_sparsity_pattern,
668 graph = std::make_unique<Epetra_FECrsGraph>(*sp.
graph);
678 template <
typename SparsityPatternType>
707 graph = std::make_unique<Epetra_FECrsGraph>(View,
711 graph->FillComplete();
767 const auto &range_map =
768 static_cast<const Epetra_Map &
>(
graph->RangeMap());
775 ierr =
graph->OptimizeStorage();
777 catch (
const int error_code)
782 "The Epetra_CrsGraph::OptimizeStorage() function "
783 "has thrown an error with code " +
784 std::to_string(error_code) +
785 ". You will have to look up the exact meaning of this error "
786 "in the Trilinos source code, but oftentimes, this function "
787 "throwing an error indicates that you are trying to allocate "
788 "more than 2,147,483,647 nonzero entries in the sparsity "
789 "pattern on the local process; this will not work because "
790 "Epetra indexes entries with a simple 'signed int'."));
808 return graph->RowMap().LID(
833 if (
graph->Filled() ==
false)
835 int nnz_present =
graph->NumGlobalIndices(i);
845 int ierr =
graph->ExtractGlobalRowView(trilinos_i,
850 Assert(nnz_present == nnz_extracted,
854 const std::ptrdiff_t local_col_index =
855 std::find(col_indices, col_indices + nnz_present, trilinos_j) -
858 if (local_col_index == nnz_present)
865 int nnz_present =
graph->NumGlobalIndices(i);
873 graph->ExtractMyRowView(trilinos_i, nnz_extracted, col_indices);
877 Assert(nnz_present == nnz_extracted,
881 const std::ptrdiff_t local_col_index =
882 std::find(col_indices, col_indices + nnz_present, trilinos_j) -
885 if (local_col_index == nnz_present)
900 for (
int i = 0; i < static_cast<int>(
local_size()); ++i)
904 graph->ExtractMyRowView(i, num_entries, indices);
905 for (
unsigned int j = 0; j < static_cast<unsigned int>(num_entries);
931 std::pair<SparsityPattern::size_type, SparsityPattern::size_type>
948 return static_cast<std::uint64_t
>(nnz);
956 int nnz =
graph->MaxNumIndices();
958 return static_cast<unsigned int>(nnz);
976 return graph->NumMyIndices(local_row);
986 const bool indices_are_sorted)
997 const auto &domain_map =
998 static_cast<const Epetra_Map &
>(
graph->DomainMap());
1008 const auto &range_map =
1009 static_cast<const Epetra_Map &
>(
graph->RangeMap());
1018 const Epetra_MpiComm *mpi_comm =
1019 dynamic_cast<const Epetra_MpiComm *
>(&
graph->RangeMap().Comm());
1021 return mpi_comm->Comm();
1039 const bool write_extended_trilinos_info)
const
1041 if (write_extended_trilinos_info)
1048 for (
int i = 0; i <
graph->NumMyRows(); ++i)
1050 graph->ExtractMyRowView(i, num_entries, indices);
1051 for (
int j = 0; j < num_entries; ++j)
1055 <<
") " << std::endl;
1072 graph->ExtractMyRowView(row, num_entries, indices);
1076 const ::types::global_dof_index num_entries_ = num_entries;
1083 out <<
static_cast<int>(
1086 << -
static_cast<int>(
1113 const ::SparsityPattern &,
1118 const ::DynamicSparsityPattern &,
1126 const ::SparsityPattern &,
1132 const ::DynamicSparsityPattern &,
size_type n_elements() const
Epetra_Map make_trilinos_map(const MPI_Comm communicator=MPI_COMM_WORLD, const bool overlapping=false) const
void subtract_set(const IndexSet &other)
virtual void resize(const size_type rows, const size_type cols)
std::shared_ptr< const std::vector< size_type > > colnum_cache
SparsityPattern * sparsity_pattern
size_type row_length(const size_type row) const
std::unique_ptr< Epetra_FECrsGraph > graph
void print(std::ostream &out, const bool write_extended_trilinos_info=false) const
unsigned int max_entries_per_row() const
size_type bandwidth() const
void print_gnuplot(std::ostream &out) const
const_iterator end() const
MPI_Comm get_mpi_communicator() const
virtual void add_row_entries(const size_type &row, const ArrayView< const size_type > &columns, const bool indices_are_sorted=false) override
std::uint64_t n_nonzero_elements() const
std::unique_ptr< Epetra_CrsGraph > nonlocal_graph
bool exists(const size_type i, const size_type j) const
const Epetra_Map & domain_partitioner() const
std::pair< size_type, size_type > local_range() const
::types::global_dof_index size_type
void add_entries(const size_type row, ForwardIterator begin, ForwardIterator end, const bool indices_are_sorted=false)
bool is_compressed() const
unsigned int local_size() const
void copy_from(const SparsityPattern &input_sparsity_pattern)
std::unique_ptr< Epetra_Map > column_space_map
const Epetra_Map & range_partitioner() const
const_iterator begin() const
std::size_t memory_consumption() const
SparsityPattern & operator=(const SparsityPattern &input_sparsity_pattern)
bool row_is_stored_locally(const size_type i) const
void reinit(const size_type m, const size_type n, const size_type n_entries_per_row=0)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcTrilinosError(int arg1)
static ::ExceptionBase & ExcTrilinosError(int arg1)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
IndexSet complete_index_set(const IndexSet::size_type N)
types::global_dof_index size_type
TrilinosWrappers::types::int_type global_index(const Epetra_BlockMap &map, const ::types::global_dof_index i)
TrilinosWrappers::types::int_type n_global_rows(const Epetra_CrsGraph &graph)
TrilinosWrappers::types::int_type min_my_gid(const Epetra_BlockMap &map)
TrilinosWrappers::types::int64_type n_global_entries(const Epetra_CrsGraph &graph)
TrilinosWrappers::types::int64_type n_global_elements(const Epetra_BlockMap &map)
TrilinosWrappers::types::int_type max_my_gid(const Epetra_BlockMap &map)
TrilinosWrappers::types::int_type n_global_cols(const Epetra_CrsGraph &graph)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
const Epetra_Comm & comm_self()
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)