19#ifdef DEAL_II_WITH_TRILINOS
31# include <boost/container/small_vector.hpp>
33# ifdef DEAL_II_TRILINOS_WITH_EPETRAEXT
34# include <EpetraExt_MatrixMatrix.h>
36# include <Epetra_Export.h>
37# include <Teuchos_RCP.hpp>
38# include <ml_epetra_utils.h>
39# include <ml_struct.h>
49 template <
typename VectorType>
50 typename VectorType::value_type *
56 template <
typename VectorType>
57 const typename VectorType::value_type *
63 template <
typename VectorType>
64 typename VectorType::value_type *
70 template <
typename VectorType>
71 const typename VectorType::value_type *
72 end(
const VectorType &V)
105# ifdef DEAL_II_TRILINOS_WITH_TPETRA
106 template <
typename Number>
113 template <
typename Number>
120 template <
typename Number>
128 template <
typename Number>
165 std::make_shared<std::vector<TrilinosScalar>>(
matrix->
n());
207 : column_space_map(new Epetra_Map(0, 0,
Utilities::Trilinos::comm_self()))
209 new Epetra_FECrsMatrix(View, *column_space_map, *column_space_map, 0))
220 const unsigned int n_max_entries_per_row)
235 matrix(new Epetra_FECrsMatrix(
241 n_max_entries_per_row,
251 const std::vector<unsigned int> &n_entries_per_row)
256 , matrix(new Epetra_FECrsMatrix(
262 reinterpret_cast<
int *>(
263 const_cast<unsigned
int *>(n_entries_per_row.data())),
273 const unsigned int n_max_entries_per_row)
274 : column_space_map(new Epetra_Map(
275 parallel_partitioning.make_trilinos_map(communicator, false)))
276 , matrix(new Epetra_FECrsMatrix(Copy,
278 n_max_entries_per_row,
288 const std::vector<unsigned int> &n_entries_per_row)
289 : column_space_map(new Epetra_Map(
290 parallel_partitioning.make_trilinos_map(communicator, false)))
291 , matrix(new Epetra_FECrsMatrix(Copy,
293 reinterpret_cast<
int *>(
294 const_cast<unsigned
int *>(
295 n_entries_per_row.data())),
304 const IndexSet &col_parallel_partitioning,
307 : column_space_map(new Epetra_Map(
308 col_parallel_partitioning.make_trilinos_map(communicator, false)))
309 , matrix(new Epetra_FECrsMatrix(
311 row_parallel_partitioning.make_trilinos_map(communicator, false),
312 n_max_entries_per_row,
321 const IndexSet &col_parallel_partitioning,
323 const std::vector<unsigned int> &n_entries_per_row)
324 : column_space_map(new Epetra_Map(
325 col_parallel_partitioning.make_trilinos_map(communicator, false)))
326 , matrix(new Epetra_FECrsMatrix(
328 row_parallel_partitioning.make_trilinos_map(communicator, false),
329 reinterpret_cast<
int *>(
330 const_cast<unsigned
int *>(n_entries_per_row.data())),
339 : column_space_map(new Epetra_Map(sparsity_pattern.domain_partitioner()))
341 new Epetra_FECrsMatrix(Copy,
342 sparsity_pattern.trilinos_sparsity_pattern(),
349 "The Trilinos sparsity pattern has not been compressed."));
356 : column_space_map(std::move(other.column_space_map))
357 , matrix(std::move(other.matrix))
358 , nonlocal_matrix(std::move(other.nonlocal_matrix))
359 , nonlocal_matrix_exporter(std::move(other.nonlocal_matrix_exporter))
360 , last_action(other.last_action)
361 , compressed(other.compressed)
363 other.last_action = Zero;
364 other.compressed =
false;
381 bool needs_deep_copy =
386 if (!needs_deep_copy)
393 const int row_local =
matrix->RowMap().LID(
397 int n_entries, rhs_n_entries;
399 int * index_ptr, *rhs_index_ptr;
400 int ierr = rhs.
matrix->ExtractMyRowView(row_local,
407 ierr =
matrix->ExtractMyRowView(row_local,
413 if (n_entries != rhs_n_entries ||
414 std::memcmp(
static_cast<void *
>(index_ptr),
415 static_cast<void *
>(rhs_index_ptr),
416 sizeof(
int) * n_entries) != 0)
418 needs_deep_copy =
true;
422 for (
int i = 0; i < n_entries; ++i)
423 value_ptr[i] = rhs_value_ptr[i];
433 matrix = std::make_unique<Epetra_FECrsMatrix>(*rhs.
matrix);
440 std::make_unique<Epetra_CrsMatrix>(Copy, rhs.
nonlocal_matrix->Graph());
449 template <
typename SparsityPatternType>
451 reinit_matrix(
const IndexSet & row_parallel_partitioning,
452 const IndexSet & column_parallel_partitioning,
453 const SparsityPatternType & sparsity_pattern,
454 const bool exchange_data,
456 std::unique_ptr<Epetra_Map> &column_space_map,
457 std::unique_ptr<Epetra_FECrsMatrix> &matrix,
458 std::unique_ptr<Epetra_CrsMatrix> & nonlocal_matrix,
459 std::unique_ptr<Epetra_Export> & nonlocal_matrix_exporter)
463 nonlocal_matrix.reset();
464 nonlocal_matrix_exporter.reset();
466 column_space_map = std::make_unique<Epetra_Map>(
469 if (column_space_map->Comm().MyPID() == 0)
472 row_parallel_partitioning.
size());
474 column_parallel_partitioning.
size());
477 Epetra_Map row_space_map =
487 trilinos_sparsity.
reinit(row_parallel_partitioning,
488 column_parallel_partitioning,
492 matrix = std::make_unique<Epetra_FECrsMatrix>(
493 Copy, trilinos_sparsity.trilinos_sparsity_pattern(),
false);
501 std::vector<int> n_entries_per_row(last_row - first_row);
503 for (size_type row = first_row; row < last_row; ++row)
504 n_entries_per_row[row - first_row] = sparsity_pattern.row_length(row);
520 std::unique_ptr<Epetra_CrsGraph> graph;
521 if (row_space_map.Comm().NumProc() > 1)
522 graph = std::make_unique<Epetra_CrsGraph>(Copy,
524 n_entries_per_row.data(),
527 graph = std::make_unique<Epetra_CrsGraph>(Copy,
530 n_entries_per_row.data(),
538 std::vector<TrilinosWrappers::types::int_type> row_indices;
540 for (size_type row = first_row; row < last_row; ++row)
542 const int row_length = sparsity_pattern.row_length(row);
546 row_indices.resize(row_length, -1);
548 typename SparsityPatternType::iterator p =
549 sparsity_pattern.begin(row);
550 for (size_type col = 0; p != sparsity_pattern.end(row); ++p, ++col)
551 row_indices[col] = p->column();
553 graph->Epetra_CrsGraph::InsertGlobalIndices(row,
562 graph->FillComplete(*column_space_map, row_space_map);
563 graph->OptimizeStorage();
571 matrix = std::make_unique<Epetra_FECrsMatrix>(Copy, *graph,
false);
583 class Epetra_CrsGraphMod :
public Epetra_CrsGraph
586 Epetra_CrsGraphMod(
const Epetra_Map &row_map,
587 const int * n_entries_per_row)
588 : Epetra_CrsGraph(Copy, row_map, n_entries_per_row, true)
592 SetIndicesAreGlobal()
594 this->Epetra_CrsGraph::SetIndicesAreGlobal(
true);
604 reinit_matrix(
const IndexSet & row_parallel_partitioning,
605 const IndexSet & column_parallel_partitioning,
607 const bool exchange_data,
609 std::unique_ptr<Epetra_Map> & column_space_map,
610 std::unique_ptr<Epetra_FECrsMatrix> &matrix,
611 std::unique_ptr<Epetra_CrsMatrix> & nonlocal_matrix,
612 std::unique_ptr<Epetra_Export> & nonlocal_matrix_exporter)
615 nonlocal_matrix.reset();
616 nonlocal_matrix_exporter.reset();
618 column_space_map = std::make_unique<Epetra_Map>(
622 row_parallel_partitioning.
size());
624 column_parallel_partitioning.
size());
626 Epetra_Map row_space_map =
631 if (relevant_rows.size() == 0)
633 relevant_rows.set_size(
635 relevant_rows.add_range(
638 relevant_rows.compress();
639 Assert(relevant_rows.n_elements() >=
640 static_cast<unsigned int>(row_space_map.NumMyElements()),
642 "Locally relevant rows of sparsity pattern must contain "
643 "all locally owned rows"));
648 const bool have_ghost_rows = [&]() {
649 const std::vector<::types::global_dof_index> indices =
650 relevant_rows.get_index_vector();
651 Epetra_Map relevant_map(
659 row_space_map.Comm());
660 return !relevant_map.SameAs(row_space_map);
663 const unsigned int n_rows = relevant_rows.n_elements();
664 std::vector<TrilinosWrappers::types::int_type> ghost_rows;
665 std::vector<int> n_entries_per_row(row_space_map.NumMyElements());
666 std::vector<int> n_entries_per_ghost_row;
667 for (
unsigned int i = 0, own = 0; i < n_rows; ++i)
670 relevant_rows.nth_index_in_set(i);
671 if (row_space_map.MyGID(global_row))
672 n_entries_per_row[own++] = sparsity_pattern.
row_length(global_row);
673 else if (sparsity_pattern.
row_length(global_row) > 0)
675 ghost_rows.push_back(global_row);
676 n_entries_per_ghost_row.push_back(
681 Epetra_Map off_processor_map(-1,
683 (ghost_rows.size() > 0) ?
684 (ghost_rows.data()) :
687 row_space_map.Comm());
689 std::unique_ptr<Epetra_CrsGraph> graph;
690 std::unique_ptr<Epetra_CrsGraphMod> nonlocal_graph;
691 if (row_space_map.Comm().NumProc() > 1)
694 std::make_unique<Epetra_CrsGraph>(Copy,
696 (n_entries_per_row.size() > 0) ?
697 (n_entries_per_row.data()) :
699 exchange_data ? false : true);
700 if (have_ghost_rows ==
true)
701 nonlocal_graph = std::make_unique<Epetra_CrsGraphMod>(
702 off_processor_map, n_entries_per_ghost_row.data());
706 std::make_unique<Epetra_CrsGraph>(Copy,
709 (n_entries_per_row.size() > 0) ?
710 (n_entries_per_row.data()) :
715 std::vector<TrilinosWrappers::types::int_type> row_indices;
717 for (
unsigned int i = 0; i < n_rows; ++i)
720 relevant_rows.nth_index_in_set(i);
721 const int row_length = sparsity_pattern.
row_length(global_row);
725 row_indices.resize(row_length, -1);
726 for (
int col = 0; col < row_length; ++col)
727 row_indices[col] = sparsity_pattern.
column_number(global_row, col);
729 if (row_space_map.MyGID(global_row))
730 graph->InsertGlobalIndices(global_row,
736 nonlocal_graph->InsertGlobalIndices(global_row,
743 if (nonlocal_graph.get() !=
nullptr)
749 nonlocal_graph->SetIndicesAreGlobal();
750 Assert(nonlocal_graph->IndicesAreGlobal() ==
true,
752 nonlocal_graph->FillComplete(*column_space_map, row_space_map);
753 nonlocal_graph->OptimizeStorage();
758 Epetra_Export exporter(nonlocal_graph->RowMap(), row_space_map);
759 int ierr = graph->Export(*nonlocal_graph, exporter, Add);
765 std::make_unique<Epetra_CrsMatrix>(Copy, *nonlocal_graph);
768 graph->FillComplete(*column_space_map, row_space_map);
769 graph->OptimizeStorage();
774 matrix = std::make_unique<Epetra_FECrsMatrix>(Copy, *graph,
false);
780 template <
typename SparsityPatternType>
797 template <
typename SparsityPatternType>
798 inline std::enable_if_t<
799 !std::is_same<SparsityPatternType, ::SparseMatrix<double>>::value>
801 const IndexSet & col_parallel_partitioning,
802 const SparsityPatternType &sparsity_pattern,
804 const bool exchange_data)
806 reinit_matrix(row_parallel_partitioning,
807 col_parallel_partitioning,
833 matrix = std::make_unique<Epetra_FECrsMatrix>(
838 std::make_unique<Epetra_CrsMatrix>(Copy,
852 if (
this == &sparse_matrix)
856 std::make_unique<Epetra_Map>(sparse_matrix.
trilinos_matrix().DomainMap());
859 matrix = std::make_unique<Epetra_FECrsMatrix>(
874 template <
typename number>
877 const IndexSet & row_parallel_partitioning,
878 const IndexSet & col_parallel_partitioning,
879 const ::SparseMatrix<number> &dealii_sparse_matrix,
881 const double drop_tolerance,
882 const bool copy_values,
883 const ::SparsityPattern * use_this_sparsity)
885 if (copy_values ==
false)
889 if (use_this_sparsity ==
nullptr)
890 reinit(row_parallel_partitioning,
891 col_parallel_partitioning,
892 dealii_sparse_matrix.get_sparsity_pattern(),
896 reinit(row_parallel_partitioning,
897 col_parallel_partitioning,
904 const size_type n_rows = dealii_sparse_matrix.m();
909 const ::SparsityPattern &sparsity_pattern =
910 (use_this_sparsity !=
nullptr) ?
912 dealii_sparse_matrix.get_sparsity_pattern();
914 if (
matrix.get() ==
nullptr ||
m() != n_rows ||
917 reinit(row_parallel_partitioning,
918 col_parallel_partitioning,
930 std::vector<size_type> row_indices(maximum_row_length);
931 std::vector<TrilinosScalar> values(maximum_row_length);
933 for (
size_type row = 0; row < n_rows; ++row)
935 if (row_parallel_partitioning.
is_element(row) ==
true)
938 sparsity_pattern.begin(row);
939 typename ::SparseMatrix<number>::const_iterator it =
940 dealii_sparse_matrix.begin(row);
942 if (sparsity_pattern.n_rows() == sparsity_pattern.n_cols())
946 if (std::fabs(it->value()) > drop_tolerance)
948 values[col] = it->value();
949 row_indices[col++] = it->column();
955 while (it != dealii_sparse_matrix.end(row) &&
956 select_index != sparsity_pattern.end(row))
958 while (select_index->column() < it->column() &&
959 select_index != sparsity_pattern.end(row))
961 while (it->column() < select_index->column() &&
962 it != dealii_sparse_matrix.end(row))
965 if (it == dealii_sparse_matrix.end(row))
967 if (std::fabs(it->value()) > drop_tolerance)
969 values[col] = it->value();
970 row_indices[col++] = it->column();
977 reinterpret_cast<size_type *
>(row_indices.data()),
986 template <
typename number>
989 const ::SparseMatrix<number> &dealii_sparse_matrix,
990 const double drop_tolerance,
991 const bool copy_values,
992 const ::SparsityPattern * use_this_sparsity)
996 dealii_sparse_matrix,
1007 const bool copy_values)
1009 Assert(input_matrix.Filled() ==
true,
1010 ExcMessage(
"Input CrsMatrix has not called FillComplete()!"));
1014 const Epetra_CrsGraph *graph = &input_matrix.Graph();
1019 matrix = std::make_unique<Epetra_FECrsMatrix>(Copy, *graph,
false);
1023 if (copy_values ==
true)
1029 const size_type my_nonzeros = input_matrix.NumMyNonzeros();
1030 std::memcpy(values, in_values, my_nonzeros *
sizeof(
TrilinosScalar));
1054 "compress() can only be called with VectorOperation add, insert, or unknown"));
1061 ExcMessage(
"Operation and argument to compress() do not match"));
1087 ierr =
matrix->OptimizeStorage();
1132 matrix->ExtractMyRowView(local_row, num_entries, values, col_indices);
1137 const std::ptrdiff_t diag_index =
1138 std::find(col_indices, col_indices + num_entries, local_row) -
1142 if (diag_index != j || new_diag_value == 0)
1145 if (diag_index != num_entries)
1146 values[diag_index] = new_diag_value;
1156 for (
const auto row : rows)
1179 if (trilinos_i == -1)
1194 int nnz_present =
matrix->NumMyEntries(trilinos_i);
1203 int ierr =
matrix->ExtractMyRowView(trilinos_i,
1210 Assert(nnz_present == nnz_extracted,
1216 const std::ptrdiff_t local_col_index =
1217 std::find(col_indices, col_indices + nnz_present, trilinos_j) -
1228 if (local_col_index == nnz_present)
1233 value = values[local_col_index];
1259 if ((trilinos_i == -1) || (trilinos_j == -1))
1270 int nnz_present =
matrix->NumMyEntries(trilinos_i);
1278 int ierr =
matrix->ExtractMyRowView(trilinos_i,
1285 Assert(nnz_present == nnz_extracted,
1291 const std::ptrdiff_t local_col_index =
1292 std::find(col_indices, col_indices + nnz_present, trilinos_j) -
1302 if (local_col_index == nnz_present)
1305 value = values[local_col_index];
1351 int ierr =
matrix->NumMyRowEntries(local_row, ncols);
1355 return static_cast<unsigned int>(ncols);
1362 const std::vector<size_type> & col_indices,
1364 const bool elide_zero_values)
1366 Assert(row_indices.size() == values.m(),
1368 Assert(col_indices.size() == values.n(),
1371 for (
size_type i = 0; i < row_indices.size(); ++i)
1383 const std::vector<size_type> & col_indices,
1384 const std::vector<TrilinosScalar> &values,
1385 const bool elide_zero_values)
1387 Assert(col_indices.size() == values.size(),
1401 SparseMatrix::set<TrilinosScalar>(
const size_type row,
1405 const bool elide_zero_values)
1425 boost::container::small_vector<TrilinosScalar, 200> local_value_array(
1427 boost::container::small_vector<TrilinosWrappers::types::int_type, 200>
1428 local_index_array(n_cols);
1433 if (elide_zero_values ==
false)
1438 col_value_ptr = values;
1445 col_index_ptr = local_index_array.data();
1446 col_value_ptr = local_value_array.data();
1451 const double value = values[j];
1455 local_index_array[n_columns] = col_indices[j];
1456 local_value_array[n_columns] = value;
1473 if (
matrix->RowMap().MyGID(
1476 if (
matrix->Filled() ==
false)
1478 ierr =
matrix->Epetra_CrsMatrix::InsertGlobalValues(
1479 row,
static_cast<int>(n_columns), col_value_ptr, col_index_ptr);
1488 ierr =
matrix->Epetra_CrsMatrix::ReplaceGlobalValues(row,
1503 if (
matrix->Filled() ==
false)
1505 ierr =
matrix->InsertGlobalValues(1,
1510 Epetra_FECrsMatrix::ROW_MAJOR);
1515 ierr =
matrix->ReplaceGlobalValues(1,
1520 Epetra_FECrsMatrix::ROW_MAJOR);
1537 const bool elide_zero_values)
1539 Assert(indices.size() == values.m(),
1543 for (
size_type i = 0; i < indices.size(); ++i)
1555 const std::vector<size_type> & col_indices,
1557 const bool elide_zero_values)
1559 Assert(row_indices.size() == values.m(),
1561 Assert(col_indices.size() == values.n(),
1564 for (
size_type i = 0; i < row_indices.size(); ++i)
1576 const std::vector<size_type> & col_indices,
1577 const std::vector<TrilinosScalar> &values,
1578 const bool elide_zero_values)
1580 Assert(col_indices.size() == values.size(),
1597 const bool elide_zero_values,
1619 boost::container::small_vector<TrilinosScalar, 100> local_value_array(
1621 boost::container::small_vector<TrilinosWrappers::types::int_type, 100>
1622 local_index_array(n_cols);
1627 if (elide_zero_values ==
false)
1632 col_value_ptr = values;
1643 col_index_ptr = local_index_array.data();
1644 col_value_ptr = local_value_array.data();
1649 const double value = values[j];
1654 local_index_array[n_columns] = col_indices[j];
1655 local_value_array[n_columns] = value;
1671 if (
matrix->RowMap().MyGID(
1674 ierr =
matrix->Epetra_CrsMatrix::SumIntoGlobalValues(row,
1688 ExcMessage(
"Attempted to write into off-processor matrix row "
1689 "that has not be specified as being writable upon "
1707 ierr =
matrix->SumIntoGlobalValues(1,
1712 Epetra_FECrsMatrix::ROW_MAJOR);
1719 std::cout <<
"------------------------------------------" << std::endl;
1720 std::cout <<
"Got error " << ierr <<
" in row " << row <<
" of proc "
1721 <<
matrix->RowMap().Comm().MyPID()
1722 <<
" when trying to add the columns:" << std::endl;
1724 std::cout << col_index_ptr[i] <<
" ";
1725 std::cout << std::endl << std::endl;
1726 std::cout <<
"Matrix row "
1727 << (
matrix->RowMap().MyGID(
1732 <<
" has the following indices:" << std::endl;
1733 std::vector<TrilinosWrappers::types::int_type> indices;
1734 const Epetra_CrsGraph * graph =
1741 indices.resize(graph->NumGlobalIndices(row));
1743 graph->ExtractGlobalRowCopy(row,
1750 std::cout << indices[i] <<
" ";
1751 std::cout << std::endl << std::endl;
1767 const int ierr =
matrix->PutScalar(d);
1785 ExcMessage(
"Can only add matrices with same distribution of rows"));
1787 ExcMessage(
"Addition of matrices only allowed if matrices are "
1788 "filled, i.e., compress() has been called"));
1790 const bool same_col_map =
matrix->ColMap().SameAs(rhs.
matrix->ColMap());
1794 const int row_local =
matrix->RowMap().LID(
1802 int n_entries, rhs_n_entries;
1804 int * index_ptr, *rhs_index_ptr;
1805 int ierr = rhs.
matrix->ExtractMyRowView(row_local,
1813 matrix->ExtractMyRowView(row_local, n_entries, value_ptr, index_ptr);
1815 bool expensive_checks = (n_entries != rhs_n_entries || !same_col_map);
1816 if (!expensive_checks)
1820 expensive_checks = std::memcmp(
static_cast<void *
>(index_ptr),
1821 static_cast<void *
>(rhs_index_ptr),
1822 sizeof(
int) * n_entries) != 0;
1823 if (!expensive_checks)
1824 for (
int i = 0; i < n_entries; ++i)
1825 value_ptr[i] += rhs_value_ptr[i] * factor;
1831 if (expensive_checks)
1833 for (
int i = 0; i < rhs_n_entries; ++i)
1835 if (rhs_value_ptr[i] == 0.)
1839 int local_col =
matrix->ColMap().LID(rhs_global_col);
1841 index_ptr + n_entries,
1843 Assert(local_index != index_ptr + n_entries &&
1844 *local_index == local_col,
1846 "Adding the entries from the other matrix "
1847 "failed, because the sparsity pattern "
1848 "of that matrix includes more elements than the "
1849 "calling matrix, which is not allowed."));
1850 value_ptr[local_index - index_ptr] += factor * rhs_value_ptr[i];
1868 if (!
matrix->UseTranspose())
1870 ierr =
matrix->SetUseTranspose(
true);
1875 ierr =
matrix->SetUseTranspose(
false);
1885 const int ierr =
matrix->Scale(a);
1901 const int ierr =
matrix->Scale(factor);
1914 return matrix->NormOne();
1923 return matrix->NormInf();
1932 return matrix->NormFrobenius();
1939 namespace SparseMatrixImplementation
1941 template <
typename VectorType>
1954 ExcMessage(
"The column partitioning of a matrix does not match "
1955 "the partitioning of a vector you are trying to "
1956 "multiply it with. Are you multiplying the "
1957 "matrix with a vector that has ghost elements?"));
1959 ExcMessage(
"The row partitioning of a matrix does not match "
1960 "the partitioning of a vector you are trying to "
1961 "put the result of a matrix-vector product in. "
1962 "Are you trying to put the product of the "
1963 "matrix with a vector into a vector that has "
1964 "ghost elements?"));
1973 template <
typename VectorType>
1975 std::is_same<typename VectorType::value_type, TrilinosScalar>::value>
1991 Epetra_MultiVector tril_dst(
1993 Epetra_MultiVector tril_src(View,
2000 const int ierr =
matrix->Multiply(
false, tril_src, tril_dst);
2007 template <
typename VectorType>
2009 !std::is_same<typename VectorType::value_type, TrilinosScalar>::value>
2017 template <
typename VectorType>
2019 std::is_same<typename VectorType::value_type, TrilinosScalar>::value>
2033 Epetra_MultiVector tril_dst(
2035 Epetra_MultiVector tril_src(View,
2041 const int ierr =
matrix->Multiply(
true, tril_src, tril_dst);
2048 template <
typename VectorType>
2050 !std::is_same<typename VectorType::value_type, TrilinosScalar>::value>
2058 template <
typename VectorType>
2066 VectorType tmp_vector;
2067 tmp_vector.reinit(dst,
true);
2068 vmult(tmp_vector, src);
2074 template <
typename VectorType>
2082 VectorType tmp_vector;
2083 tmp_vector.reinit(dst,
true);
2096 temp_vector.
reinit(v,
true);
2098 vmult(temp_vector, v);
2099 return temp_vector * v;
2111 temp_vector.
reinit(v,
true);
2113 vmult(temp_vector, v);
2114 return u * temp_vector;
2142 const bool transpose_left)
2144 const bool use_vector = (V.
size() == inputright.
m() ? true :
false);
2145 if (transpose_left ==
false)
2147 Assert(inputleft.
n() == inputright.
m(),
2151 ExcMessage(
"Parallel partitioning of A and B does not fit."));
2155 Assert(inputleft.
m() == inputright.
m(),
2159 ExcMessage(
"Parallel partitioning of A and B does not fit."));
2170 Teuchos::RCP<Epetra_CrsMatrix> mod_B;
2171 if (use_vector ==
false)
2173 mod_B = Teuchos::rcp(
const_cast<Epetra_CrsMatrix *
>(
2179 mod_B = Teuchos::rcp(
2185 ExcMessage(
"Parallel distribution of matrix B and vector V "
2186 "does not match."));
2189 for (
int i = 0; i < local_N; ++i)
2192 double *new_data, *B_data;
2193 mod_B->ExtractMyRowView(i, N_entries, new_data);
2199 new_data[j] = value * B_data[j];
2210# ifdef DEAL_II_TRILINOS_WITH_EPETRAEXT
2215 const_cast<Epetra_CrsMatrix &
>(
2216 tmp_result.trilinos_matrix()));
2219 ExcMessage(
"This function requires that the Trilinos "
2220 "installation found while running the deal.II "
2221 "CMake scripts contains the optional Trilinos "
2222 "package 'EpetraExt'. However, this optional "
2223 "part of Trilinos was not found."));
2225 result.
reinit(tmp_result.trilinos_matrix());
2263 const bool print_detailed_trilinos_information)
const
2265 if (print_detailed_trilinos_information ==
true)
2273 for (
int i = 0; i <
matrix->NumMyRows(); ++i)
2276 matrix->ExtractMyRowView(i, num_entries, values, indices);
2284 <<
") " << values[j] << std::endl;
2297 sizeof(*this) +
sizeof(*matrix) +
sizeof(*
matrix->Graph().DataPtr());
2300 matrix->NumMyNonzeros() +
2309 const Epetra_MpiComm *mpi_comm =
2310 dynamic_cast<const Epetra_MpiComm *
>(&
matrix->RangeMap().Comm());
2312 return mpi_comm->Comm();
2321 namespace LinearOperatorImplementation
2324 : use_transpose(false)
2325 , communicator(MPI_COMM_SELF)
2326 , domain_map(
IndexSet().make_trilinos_map(communicator.Comm()))
2327 , range_map(
IndexSet().make_trilinos_map(communicator.Comm()))
2331 ExcMessage(
"Uninitialized TrilinosPayload::vmult called "
2332 "(Default constructor)"));
2337 ExcMessage(
"Uninitialized TrilinosPayload::Tvmult called "
2338 "(Default constructor)"));
2343 ExcMessage(
"Uninitialized TrilinosPayload::inv_vmult called "
2344 "(Default constructor)"));
2349 ExcMessage(
"Uninitialized TrilinosPayload::inv_Tvmult called "
2350 "(Default constructor)"));
2360 matrix.trilinos_matrix()),
2362 matrix_exemplar.trilinos_matrix().UseTranspose(),
2363 matrix_exemplar.get_mpi_communicator(),
2364 matrix_exemplar.locally_owned_domain_indices(),
2365 matrix_exemplar.locally_owned_range_indices())
2375 matrix.trilinos_matrix()),
2377 payload_exemplar.UseTranspose(),
2378 payload_exemplar.get_mpi_communicator(),
2379 payload_exemplar.locally_owned_domain_indices(),
2380 payload_exemplar.locally_owned_range_indices())
2390 matrix_exemplar.trilinos_matrix().UseTranspose(),
2391 matrix_exemplar.get_mpi_communicator(),
2392 matrix_exemplar.locally_owned_domain_indices(),
2393 matrix_exemplar.locally_owned_range_indices())
2402 preconditioner.trilinos_operator(),
2404 preconditioner_exemplar.trilinos_operator().UseTranspose(),
2405 preconditioner_exemplar.get_mpi_communicator(),
2406 preconditioner_exemplar.locally_owned_domain_indices(),
2407 preconditioner_exemplar.locally_owned_range_indices())
2417 payload_exemplar.UseTranspose(),
2418 payload_exemplar.get_mpi_communicator(),
2419 payload_exemplar.locally_owned_domain_indices(),
2420 payload_exemplar.locally_owned_range_indices())
2426 : vmult(payload.vmult)
2427 , Tvmult(payload.Tvmult)
2428 , inv_vmult(payload.inv_vmult)
2429 , inv_Tvmult(payload.inv_Tvmult)
2430 , use_transpose(payload.use_transpose)
2431 , communicator(payload.communicator)
2432 , domain_map(payload.domain_map)
2433 , range_map(payload.range_map)
2442 : use_transpose(false)
2445 communicator(first_op.communicator)
2446 , domain_map(second_op.domain_map)
2447 , range_map(first_op.range_map)
2458 tril_dst = tril_src;
2462 tril_dst = tril_src;
2466 tril_dst = tril_src;
2470 tril_dst = tril_src;
2484 const int ierr = tril_dst.PutScalar(0.0);
2490 const int ierr = tril_dst.PutScalar(0.0);
2497 ExcMessage(
"Cannot compute inverse of null operator"));
2499 const int ierr = tril_dst.PutScalar(0.0);
2506 ExcMessage(
"Cannot compute inverse of null operator"));
2508 const int ierr = tril_dst.PutScalar(0.0);
2608 return "TrilinosPayload";
2666 "Operators are set to work on incompatible IndexSets."));
2670 "Operators are set to work on incompatible IndexSets."));
2675 return_op.
vmult = [first_op, second_op](Range & tril_dst,
2676 const Domain &tril_src) {
2684 i->reinit(
IndexSet(first_op_init_map),
2689 const size_type i_local_size = i->end() - i->begin();
2693 (void)second_op_init_map;
2694 Intermediate tril_int(View,
2702 second_op.
Apply(tril_src, tril_int);
2703 first_op.
Apply(tril_src, tril_dst);
2704 const int ierr = tril_dst.Update(1.0, tril_int, 1.0);
2708 return_op.
Tvmult = [first_op, second_op](Domain & tril_dst,
2709 const Range &tril_src) {
2724 i->reinit(
IndexSet(first_op_init_map),
2729 const size_type i_local_size = i->end() - i->begin();
2733 (void)second_op_init_map;
2734 Intermediate tril_int(View,
2742 second_op.
Apply(tril_src, tril_int);
2743 first_op.
Apply(tril_src, tril_dst);
2744 const int ierr = tril_dst.Update(1.0, tril_int, 1.0);
2752 return_op.
inv_vmult = [first_op, second_op](Domain & tril_dst,
2753 const Range &tril_src) {
2761 i->reinit(
IndexSet(first_op_init_map),
2766 const size_type i_local_size = i->end() - i->begin();
2770 (void)second_op_init_map;
2771 Intermediate tril_int(View,
2781 const int ierr = tril_dst.Update(1.0, tril_int, 1.0);
2785 return_op.
inv_Tvmult = [first_op, second_op](Range & tril_dst,
2786 const Domain &tril_src) {
2801 i->reinit(
IndexSet(first_op_init_map),
2806 const size_type i_local_size = i->end() - i->begin();
2810 (void)second_op_init_map;
2811 Intermediate tril_int(View,
2821 const int ierr = tril_dst.Update(1.0, tril_int, 1.0);
2846 "Operators are set to work on incompatible IndexSets."));
2851 return_op.
vmult = [first_op, second_op](Range & tril_dst,
2852 const Domain &tril_src) {
2860 i->reinit(
IndexSet(first_op_init_map),
2865 const size_type i_local_size = i->end() - i->begin();
2869 (void)second_op_init_map;
2870 Intermediate tril_int(View,
2878 second_op.
Apply(tril_src, tril_int);
2879 first_op.
Apply(tril_int, tril_dst);
2882 return_op.
Tvmult = [first_op, second_op](Domain & tril_dst,
2883 const Range &tril_src) {
2898 i->reinit(
IndexSet(first_op_init_map),
2903 const size_type i_local_size = i->end() - i->begin();
2907 (void)second_op_init_map;
2908 Intermediate tril_int(View,
2915 first_op.
Apply(tril_src, tril_int);
2916 second_op.
Apply(tril_int, tril_dst);
2923 return_op.
inv_vmult = [first_op, second_op](Domain & tril_dst,
2924 const Range &tril_src) {
2932 i->reinit(
IndexSet(first_op_init_map),
2937 const size_type i_local_size = i->end() - i->begin();
2941 (void)second_op_init_map;
2942 Intermediate tril_int(View,
2954 return_op.
inv_Tvmult = [first_op, second_op](Range & tril_dst,
2955 const Domain &tril_src) {
2970 i->reinit(
IndexSet(first_op_init_map),
2975 const size_type i_local_size = i->end() - i->begin();
2979 (void)second_op_init_map;
2980 Intermediate tril_int(View,
3008# include "trilinos_sparse_matrix.inst"
3023 const ::SparsityPattern &,
3039 const ::Vector<double> &)
const;
3044 const ::LinearAlgebra::distributed::Vector<double> &)
const;
3046# ifdef DEAL_II_TRILINOS_WITH_TPETRA
3047# if defined(HAVE_TPETRA_INST_DOUBLE)
3051 const ::LinearAlgebra::TpetraWrappers::Vector<double> &)
const;
3054# if defined(HAVE_TPETRA_INST_FLOAT)
3058 const ::LinearAlgebra::TpetraWrappers::Vector<float> &)
const;
3065 const ::LinearAlgebra::EpetraWrappers::Vector &)
const;
3072 const ::Vector<double> &)
const;
3077 const ::LinearAlgebra::distributed::Vector<double> &)
const;
3079# ifdef DEAL_II_TRILINOS_WITH_TPETRA
3080# if defined(HAVE_TPETRA_INST_DOUBLE)
3084 const ::LinearAlgebra::TpetraWrappers::Vector<double> &)
const;
3087# if defined(HAVE_TPETRA_INST_FLOAT)
3091 const ::LinearAlgebra::TpetraWrappers::Vector<float> &)
const;
3098 const ::LinearAlgebra::EpetraWrappers::Vector &)
const;
3105 const ::Vector<double> &)
const;
3110 const ::LinearAlgebra::distributed::Vector<double> &)
const;
3112# ifdef DEAL_II_TRILINOS_WITH_TPETRA
3113# if defined(HAVE_TPETRA_INST_DOUBLE)
3117 const ::LinearAlgebra::TpetraWrappers::Vector<double> &)
const;
3120# if defined(HAVE_TPETRA_INST_FLOAT)
3124 const ::LinearAlgebra::TpetraWrappers::Vector<float> &)
const;
3131 const ::LinearAlgebra::EpetraWrappers::Vector &)
const;
3138 const ::Vector<double> &)
const;
3143 const ::LinearAlgebra::distributed::Vector<double> &)
const;
3145# ifdef DEAL_II_TRILINOS_WITH_TPETRA
3146# if defined(HAVE_TPETRA_INST_DOUBLE)
3150 const ::LinearAlgebra::TpetraWrappers::Vector<double> &)
const;
3153# if defined(HAVE_TPETRA_INST_FLOAT)
3157 const ::LinearAlgebra::TpetraWrappers::Vector<float> &)
const;
3164 const ::LinearAlgebra::EpetraWrappers::Vector &)
const;
const IndexSet & row_index_set() const
size_type row_length(const size_type row) const
size_type column_number(const size_type row, const size_type index) const
bool is_element(const size_type index) const
Epetra_Map make_trilinos_map(const MPI_Comm communicator=MPI_COMM_WORLD, const bool overlapping=false) const
const Epetra_FEVector & trilinos_vector() const
const Tpetra::Vector< Number, int, types::signed_global_dof_index > & trilinos_vector() const
void reinit(const size_type m, const size_type n, const ArrayView< const unsigned int > &row_lengths)
const Epetra_BlockMap & trilinos_partitioner() const
real_type l2_norm() const
const Epetra_MultiVector & trilinos_vector() const
void reinit(const Vector &v, const bool omit_zeroing_entries=false, const bool allow_different_maps=false)
std::pair< size_type, size_type > local_range() const
std::shared_ptr< std::vector< TrilinosScalar > > value_cache
std::shared_ptr< std::vector< size_type > > colnum_cache
void set(const size_type i, const size_type j, const TrilinosScalar value)
std::unique_ptr< Epetra_Map > column_space_map
MPI_Comm get_mpi_communicator() const
SparseMatrix & operator*=(const TrilinosScalar factor)
void mmult(SparseMatrix &C, const SparseMatrix &B, const MPI::Vector &V=MPI::Vector()) const
std::unique_ptr< Epetra_Export > nonlocal_matrix_exporter
TrilinosScalar l1_norm() const
void compress(VectorOperation::values operation)
std::unique_ptr< Epetra_FECrsMatrix > matrix
::types::global_dof_index size_type
TrilinosScalar linfty_norm() const
const Epetra_CrsMatrix & trilinos_matrix() const
size_type memory_consumption() const
TrilinosScalar matrix_norm_square(const MPI::Vector &v) const
Epetra_CombineMode last_action
IndexSet locally_owned_range_indices() const
void print(std::ostream &out, const bool write_extended_trilinos_info=false) const
void Tmmult(SparseMatrix &C, const SparseMatrix &B, const MPI::Vector &V=MPI::Vector()) const
void clear_row(const size_type row, const TrilinosScalar new_diag_value=0)
void reinit(const SparsityPatternType &sparsity_pattern)
std::enable_if_t< std::is_same< typename VectorType::value_type, TrilinosScalar >::value > Tvmult(VectorType &dst, const VectorType &src) const
void vmult_add(VectorType &dst, const VectorType &src) const
SparseMatrix & operator=(const SparseMatrix &)=delete
SparseMatrix & operator/=(const TrilinosScalar factor)
void Tvmult_add(VectorType &dst, const VectorType &src) const
void clear_rows(const std::vector< size_type > &rows, const TrilinosScalar new_diag_value=0)
TrilinosScalar el(const size_type i, const size_type j) const
IndexSet locally_owned_domain_indices() const
TrilinosScalar residual(MPI::Vector &dst, const MPI::Vector &x, const MPI::Vector &b) const
bool in_local_range(const size_type index) const
void copy_from(const SparseMatrix &source)
unsigned int row_length(const size_type row) const
TrilinosScalar frobenius_norm() const
std::uint64_t n_nonzero_elements() const
const Epetra_CrsGraph & trilinos_sparsity_pattern() const
TrilinosScalar diag_element(const size_type i) const
void add(const size_type i, const size_type j, const TrilinosScalar value)
unsigned int local_size() const
TrilinosScalar operator()(const size_type i, const size_type j) const
TrilinosScalar matrix_scalar_product(const MPI::Vector &u, const MPI::Vector &v) const
std::pair< size_type, size_type > local_range() const
std::enable_if_t< std::is_same< typename VectorType::value_type, TrilinosScalar >::value > vmult(VectorType &dst, const VectorType &src) const
std::unique_ptr< Epetra_CrsMatrix > nonlocal_matrix
std::unique_ptr< Epetra_CrsGraph > nonlocal_graph
const Epetra_Map & domain_partitioner() const
const Epetra_FECrsGraph & trilinos_sparsity_pattern() const
Epetra_MpiComm communicator
std::function< void(VectorType &, const VectorType &)> inv_Tvmult
virtual int SetUseTranspose(bool UseTranspose) override
virtual bool UseTranspose() const override
virtual const Epetra_Map & OperatorDomainMap() const override
IndexSet locally_owned_range_indices() const
TrilinosPayload transpose_payload() const
Epetra_MultiVector VectorType
MPI_Comm get_mpi_communicator() const
virtual int ApplyInverse(const VectorType &Y, VectorType &X) const override
std::function< void(VectorType &, const VectorType &)> Tvmult
virtual int Apply(const VectorType &X, VectorType &Y) const override
std::function< void(VectorType &, const VectorType &)> vmult
virtual const Epetra_Map & OperatorRangeMap() const override
std::function< void(VectorType &, const VectorType &)> inv_vmult
virtual const char * Label() const override
IndexSet locally_owned_domain_indices() const
virtual const Epetra_Comm & Comm() const override
virtual bool HasNormInf() const override
TrilinosPayload identity_payload() const
virtual double NormInf() const override
TrilinosPayload null_payload() const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
static ::ExceptionBase & ExcInvalidIndex(size_type arg1, size_type arg2)
#define Assert(cond, exc)
static ::ExceptionBase & ExcAccessToNonPresentElement(size_type arg1, size_type arg2)
#define AssertIsFinite(number)
static ::ExceptionBase & ExcAccessToNonlocalRow(std::size_t arg1)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDivideByZero()
static ::ExceptionBase & ExcSourceEqualsDestination()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotQuadratic()
static ::ExceptionBase & ExcMatrixNotCompressed()
static ::ExceptionBase & ExcTrilinosError(int arg1)
static ::ExceptionBase & ExcAccessToNonLocalElement(size_type arg1, size_type arg2, size_type arg3, size_type arg4)
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcTrilinosError(int arg1)
#define AssertThrow(cond, exc)
IndexSet complete_index_set(const IndexSet::size_type N)
@ matrix
Contents is actually a matrix.
types::global_dof_index size_type
TrilinosPayload operator+(const TrilinosPayload &first_op, const TrilinosPayload &second_op)
TrilinosPayload operator*(const TrilinosPayload &first_op, const TrilinosPayload &second_op)
void check_vector_map_equality(const Epetra_CrsMatrix &, const VectorType &, const VectorType &)
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
void perform_mmult(const SparseMatrix &inputleft, const SparseMatrix &inputright, SparseMatrix &result, const MPI::Vector &V, const bool transpose_left)
TrilinosWrappers::types::int_type min_my_gid(const Epetra_BlockMap &map)
TrilinosWrappers::types::int64_type n_global_elements(const Epetra_BlockMap &map)
TrilinosWrappers::types::int_type global_column_index(const Epetra_CrsMatrix &matrix, const ::types::global_dof_index i)
TrilinosWrappers::types::int_type max_my_gid(const Epetra_BlockMap &map)
TrilinosWrappers::types::int_type global_row_index(const Epetra_CrsMatrix &matrix, const ::types::global_dof_index i)
TrilinosWrappers::types::int_type n_global_cols(const Epetra_CrsGraph &graph)
const Epetra_Comm & comm_self()
Iterator lower_bound(Iterator first, Iterator last, const T &val)
unsigned int global_dof_index