Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_TpetraCrsMatrix_MatrixAdapter_def.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Amesos2: Templated Direct Sparse Solver Package
4 //
5 // Copyright 2011 NTESS and the Amesos2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 
11 #ifndef AMESOS2_TPETRACRSMATRIX_MATRIXADAPTER_DEF_HPP
12 #define AMESOS2_TPETRACRSMATRIX_MATRIXADAPTER_DEF_HPP
13 
15 #include "Amesos2_TpetraRowMatrix_AbstractMatrixAdapter_def.hpp"
16 #include "Amesos2_MatrixAdapter_def.hpp"
17 
18 namespace Amesos2 {
19 
20  template <typename Scalar,
21  typename LocalOrdinal,
22  typename GlobalOrdinal,
23  typename Node>
24  ConcreteMatrixAdapter<
25  Tpetra::CrsMatrix<Scalar,
26  LocalOrdinal,
27  GlobalOrdinal,
28  Node>
29  >::ConcreteMatrixAdapter(Teuchos::RCP<matrix_t> m)
30  : AbstractConcreteMatrixAdapter<Tpetra::RowMatrix<Scalar,
31  LocalOrdinal,
32  GlobalOrdinal,
33  Node>,
34  Tpetra::CrsMatrix<Scalar,
35  LocalOrdinal,
36  GlobalOrdinal,
37  Node> >(m) // with implicit cast
38  {}
39 
40  template <typename Scalar,
41  typename LocalOrdinal,
42  typename GlobalOrdinal,
43  typename Node>
44  Teuchos::RCP<const MatrixAdapter<Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > >
45  ConcreteMatrixAdapter<
46  Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>
47  >::get_impl(const Teuchos::Ptr<const map_t> map, EDistribution distribution) const
48  {
49  using Teuchos::RCP;
50  using Teuchos::rcp;
51  using Teuchos::rcpFromPtr;
52  typedef Tpetra::Import<local_ordinal_t, global_ordinal_t, node_t> import_t;
53 
54  RCP<import_t> importer =
55  rcp (new import_t (this->getRowMap(), rcpFromPtr (map)));
56 
57  RCP<matrix_t> t_mat;
58 
59  t_mat = Tpetra::importAndFillCompleteCrsMatrix<matrix_t>( (this->mat_), *importer ); // DomainMap, RangeMap, params inputs default to Teuchos::null
60 
61  // Case for non-contiguous GIDs
62  if ( distribution == CONTIGUOUS_AND_ROOTED ) {
63 
64  auto myRank = map->getComm()->getRank();
65 
66  auto local_matrix = t_mat->getLocalMatrixDevice();
67  const size_t global_num_contiguous_entries = t_mat->getGlobalNumRows();
68  const size_t local_num_contiguous_entries = (myRank == 0) ? t_mat->getGlobalNumRows() : 0;
69 
70  //create maps
71  //typedef Tpetra::Map< local_ordinal_t, global_ordinal_t, node_t> contiguous_map_type;
72  RCP<const map_t> contiguousRowMap = rcp( new map_t(global_num_contiguous_entries, local_num_contiguous_entries, 0, (t_mat->getComm() ) ) );
73  RCP<const map_t> contiguousColMap = rcp( new map_t(global_num_contiguous_entries, local_num_contiguous_entries, 0, (t_mat->getComm() ) ) );
74  RCP<const map_t> contiguousDomainMap = rcp( new map_t(global_num_contiguous_entries, local_num_contiguous_entries, 0, (t_mat->getComm() ) ) );
75  RCP<const map_t> contiguousRangeMap = rcp( new map_t(global_num_contiguous_entries, local_num_contiguous_entries, 0, (t_mat->getComm() ) ) );
76 
77  RCP<matrix_t> contiguous_t_mat = rcp( new matrix_t(contiguousRowMap, contiguousColMap, local_matrix) );
78  contiguous_t_mat->resumeFill();
79  contiguous_t_mat->expertStaticFillComplete(contiguousDomainMap, contiguousRangeMap);
80 
81  return rcp (new ConcreteMatrixAdapter<matrix_t> (contiguous_t_mat));
82  } //end if not contiguous
83 
84  return rcp (new ConcreteMatrixAdapter<matrix_t> (t_mat));
85  }
86 
87 
88 
89  template <typename Scalar,
90  typename LocalOrdinal,
91  typename GlobalOrdinal,
92  typename Node>
93  Teuchos::RCP<const MatrixAdapter<Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > >
94  ConcreteMatrixAdapter<
95  Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>
96  >::reindex_impl(Teuchos::RCP<const map_t> &contigRowMap,
97  Teuchos::RCP<const map_t> &contigColMap) const
98  {
99  typedef Tpetra::Map< local_ordinal_t, global_ordinal_t, node_t> contiguous_map_type;
100  auto rowMap = this->mat_->getRowMap();
101  auto colMap = this->mat_->getColMap();
102  auto rowComm = rowMap->getComm();
103  auto colComm = colMap->getComm();
104 
105 #ifdef HAVE_AMESOS2_TIMERS
106  auto reindexTimer = Teuchos::TimeMonitor::getNewTimer("Time to re-index matrix gids");
107  Teuchos::TimeMonitor ReindexTimer(*reindexTimer);
108 #endif
109 
110  global_ordinal_t indexBase = rowMap->getIndexBase();
111  global_ordinal_t numDoFs = this->mat_->getGlobalNumRows();
112  local_ordinal_t nRows = this->mat_->getLocalNumRows();
113  local_ordinal_t nCols = colMap->getLocalNumElements();
114 
115  RCP<matrix_t> contiguous_t_mat;
116  // if-checks when to recompute contigRowMap & contigColMap
117  // TODO: this is currentlly based on the global matrix dimesions
118  if (contigRowMap->getGlobalNumElements() != numDoFs || contigColMap->getGlobalNumElements() != numDoFs) {
119  auto tmpMap = rcp (new contiguous_map_type (numDoFs, nRows, indexBase, rowComm));
120  global_ordinal_t frow = tmpMap->getMinGlobalIndex();
121 
122  // Create new GID list for RowMap
123  typedef Kokkos::DefaultHostExecutionSpace HostExecSpaceType;
124  Kokkos::View<global_ordinal_t*, HostExecSpaceType> rowIndexList ("indexList", nRows);
125  for (local_ordinal_t k = 0; k < nRows; k++) {
126  rowIndexList(k) = frow+k;
127  }
128  // Create new GID list for ColMap
129  Kokkos::View<global_ordinal_t*, HostExecSpaceType> colIndexList ("indexList", nCols);
130  typedef Tpetra::MultiVector<global_ordinal_t,
131  local_ordinal_t,
132  global_ordinal_t,
133  node_t> gid_mv_t;
134  Teuchos::ArrayView<const global_ordinal_t> rowIndexArray(rowIndexList.data(), nRows);
135  Teuchos::ArrayView<const global_ordinal_t> colIndexArray(colIndexList.data(), nCols);
136  gid_mv_t row_mv (rowMap, rowIndexArray, nRows, 1);
137  gid_mv_t col_mv (colMap, colIndexArray, nCols, 1);
138  typedef Tpetra::Import<local_ordinal_t, global_ordinal_t, node_t> import_t;
139  RCP<import_t> importer = rcp (new import_t (rowMap, colMap));
140  col_mv.doImport (row_mv, *importer, Tpetra::INSERT);
141  {
142  auto col_view = col_mv.getLocalViewHost(Tpetra::Access::ReadOnly);
143  for(int i=0; i<nCols; i++) colIndexList(i) = col_view(i,0);
144  }
145  // Create new Row & Col Maps
146  contigRowMap = rcp (new contiguous_map_type (numDoFs, rowIndexList.data(), nRows, indexBase, rowComm));
147  contigColMap = rcp (new contiguous_map_type (numDoFs, colIndexList.data(), nCols, indexBase, colComm));
148 
149  // Create contiguous Matrix
150  auto lclMatrix = this->mat_->getLocalMatrixDevice();
151  contiguous_t_mat = rcp( new matrix_t(contigRowMap, contigColMap, lclMatrix));
152  } else {
153  // Build Matrix with contiguous Maps
154  auto lclMatrix = this->mat_->getLocalMatrixDevice();
155  auto importer = this->mat_->getCrsGraph()->getImporter();
156  auto exporter = this->mat_->getCrsGraph()->getExporter();
157  contiguous_t_mat = rcp( new matrix_t(lclMatrix, contigRowMap, contigColMap, contigRowMap, contigColMap, importer,exporter));
158  }
159  return rcp (new ConcreteMatrixAdapter<matrix_t> (contiguous_t_mat));
160  }
161 
162  template <typename Scalar,
163  typename LocalOrdinal,
164  typename GlobalOrdinal,
165  typename Node>
166  void
167  ConcreteMatrixAdapter<
168  Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>
169  >::describe (Teuchos::FancyOStream& os,
170  const Teuchos::EVerbosityLevel verbLevel) const
171  {
172  this->mat_->describe(os, verbLevel);
173  }
174 } // end namespace Amesos2
175 
176 #endif // AMESOS2_TPETRACRSMATRIX_MATRIXADAPTER_DEF_HPP
Specialization of the ConcreteMatrixAdapter for Tpetra::CrsMatrix. Inherits all its functionality fro...
EDistribution
Definition: Amesos2_TypeDecl.hpp:89