Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TpetraExt_MMHelpers_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TPETRA_MMHELPERS_DEF_HPP
11 #define TPETRA_MMHELPERS_DEF_HPP
12 
14 #include "Teuchos_VerboseObject.hpp"
15 
20 namespace Tpetra {
21 
22 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
23 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CrsMatrixStruct()
24 {
25 }
26 
27 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
28 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::~CrsMatrixStruct()
29 {
30  deleteContents();
31 }
32 
33 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
34 void CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
35 deleteContents ()
36 {
37  importMatrix.reset();
38  origMatrix = Teuchos::null;
39 }
40 
41 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
42 BlockCrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BlockCrsMatrixStruct(const LocalOrdinal blocksize_)
43  : blocksize(blocksize_)
44 {
45 }
46 
47 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
48 BlockCrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::~BlockCrsMatrixStruct()
49 {
50  deleteContents();
51 }
52 
53 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
54 void BlockCrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
55 deleteContents ()
56 {
57  importMatrix.reset();
58  origMatrix = Teuchos::null;
59 }
60 
61 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
62 int dumpCrsMatrixStruct (const CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& M)
63 {
64  std::cout << "proc " << M.rowMap->Comm().MyPID()<<std::endl;
65  std::cout << "numRows: " << M.numRows<<std::endl;
66  for(LocalOrdinal i=0; i<M.numRows; ++i) {
67  for(LocalOrdinal j=0; j<M.numEntriesPerRow[i]; ++j) {
68  std::cout << " "<<M.rowMap->GID(i)<<" "
69  <<M.colMap->GID(M.indices[i][j])<<" "<<M.values[i][j]<<std::endl;
70  }
71  }
72 
73  return 0;
74 }
75 
76 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
77 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
78 CrsWrapper_CrsMatrix (CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& crsmatrix)
79  : crsmat_ (crsmatrix)
80 {
81 }
82 
83 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
84 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::~CrsWrapper_CrsMatrix()
85 {
86 }
87 
88 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
89 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
90 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::getRowMap() const
91 {
92  return crsmat_.getRowMap();
93 }
94 
95 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
96 bool CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
97 isFillComplete ()
98 {
99  return crsmat_.isFillComplete ();
100 }
101 
102 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
103 void
104 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
105 insertGlobalValues (GlobalOrdinal globalRow,
106  const Teuchos::ArrayView<const GlobalOrdinal> &indices,
107  const Teuchos::ArrayView<const Scalar> &values)
108 {
109  crsmat_.insertGlobalValues (globalRow, indices, values);
110 }
111 
112 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
113 void
114 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
115 sumIntoGlobalValues (GlobalOrdinal globalRow,
116  const Teuchos::ArrayView<const GlobalOrdinal> &indices,
117  const Teuchos::ArrayView<const Scalar> &values)
118 {
119  crsmat_.sumIntoGlobalValues (globalRow, indices, values);
120 }
121 
122 
123 
124 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
125 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
126 CrsWrapper_GraphBuilder (const Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >& map)
127  : graph_(),
128  rowmap_(map),
129  max_row_length_(0)
130 {
131  Teuchos::ArrayView<const GlobalOrdinal> rows = map->getLocalElementList ();
132  const LocalOrdinal numRows = static_cast<LocalOrdinal> (rows.size ());
133  for (LocalOrdinal i = 0; i < numRows; ++i) {
134  graph_[rows[i]] = new std::set<GlobalOrdinal>;
135  }
136 }
137 
138 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
139 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
140 ~CrsWrapper_GraphBuilder ()
141 {
142  typename std::map<GlobalOrdinal,std::set<GlobalOrdinal>*>::iterator
143  iter = graph_.begin(), iter_end = graph_.end();
144  for (; iter != iter_end; ++iter) {
145  delete iter->second;
146  }
147  graph_.clear ();
148 }
149 
150 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
151 bool CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::isFillComplete()
152 {
153  return false;
154 }
155 
156 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
157 void
158 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
159 insertGlobalValues (GlobalOrdinal globalRow,
160  const Teuchos::ArrayView<const GlobalOrdinal> &indices,
161  const Teuchos::ArrayView<const Scalar> &/* values */)
162 {
163  typename std::map<GlobalOrdinal,std::set<GlobalOrdinal>*>::iterator
164  iter = graph_.find (globalRow);
165 
166  TEUCHOS_TEST_FOR_EXCEPTION(
167  iter == graph_.end(), std::runtime_error,
168  "Tpetra::CrsWrapper_GraphBuilder::insertGlobalValues could not find row "
169  << globalRow << " in the graph. Super bummer man. Hope you figure it out.");
170 
171  std::set<GlobalOrdinal>& cols = * (iter->second);
172 
173  for (typename Teuchos::ArrayView<const GlobalOrdinal>::size_type i = 0;
174  i < indices.size (); ++i) {
175  cols.insert (indices[i]);
176  }
177 
178  const global_size_t row_length = static_cast<global_size_t> (cols.size ());
179  if (row_length > max_row_length_) {
180  max_row_length_ = row_length;
181  }
182 }
183 
184 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
185 void
186 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
187 sumIntoGlobalValues (GlobalOrdinal globalRow,
188  const Teuchos::ArrayView<const GlobalOrdinal> &indices,
189  const Teuchos::ArrayView<const Scalar> &values)
190 {
191  insertGlobalValues (globalRow, indices, values);
192 }
193 
194 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
195 std::map<GlobalOrdinal,std::set<GlobalOrdinal>*>&
196 CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>::get_graph ()
197 {
198  return graph_;
199 }
200 
201 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
202 void
203 insert_matrix_locations (CrsWrapper_GraphBuilder<Scalar, LocalOrdinal, GlobalOrdinal, Node>& graphbuilder,
204  CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& C)
205 {
206  global_size_t max_row_length = graphbuilder.get_max_row_length();
207  if (max_row_length < 1) return;
208 
209  Teuchos::Array<GlobalOrdinal> indices(max_row_length);
210  Teuchos::Array<Scalar> zeros(max_row_length, Teuchos::ScalarTraits<Scalar>::zero());
211 
212  typedef std::map<GlobalOrdinal,std::set<GlobalOrdinal>*> Graph;
213  typedef typename Graph::iterator GraphIter;
214  Graph& graph = graphbuilder.get_graph ();
215 
216  const GraphIter iter_end = graph.end ();
217  for (GraphIter iter = graph.begin (); iter != iter_end; ++iter) {
218  const GlobalOrdinal row = iter->first;
219  const std::set<GlobalOrdinal>& cols = * (iter->second);
220  // "copy" entries out of set into contiguous array storage
221  const size_t num_entries = std::copy (cols.begin (), cols.end (), indices.begin ()) - indices.begin ();
222  // insert zeros into the result matrix at the appropriate locations
223  C.insertGlobalValues (row, indices (0, num_entries), zeros (0, num_entries));
224  }
225 }
226 
227 } // namespace Tpetra
228 
229 //
230 // Explicit instantiation macro
231 //
232 // Must be expanded from within the Tpetra namespace!
233 //
234 
235 #define TPETRA_CRSMATRIXSTRUCT_INSTANT(SCALAR,LO,GO,NODE) \
236  \
237  template class CrsMatrixStruct< SCALAR , LO , GO , NODE >;
238 
239 #define TPETRA_BLOCKCRSMATRIXSTRUCT_INSTANT(SCALAR,LO,GO,NODE) \
240  \
241  template class BlockCrsMatrixStruct< SCALAR , LO , GO , NODE >;
242 
243 #define TPETRA_CRSWRAPPER_INSTANT(SCALAR,LO,GO,NODE) \
244  \
245  template class CrsWrapper< SCALAR , LO , GO , NODE >;
246 
247 #define TPETRA_CRSWRAPPER_CRSMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
248  \
249  template class CrsWrapper_CrsMatrix< SCALAR , LO , GO , NODE >;
250 
251 #define TPETRA_CRSWRAPPER_GRAPHBUILDER_INSTANT(SCALAR,LO,GO,NODE) \
252  \
253  template class CrsWrapper_GraphBuilder< SCALAR , LO , GO , NODE >;
254 
255 #endif // TPETRA_MMHELPERS_DEF_HPP
size_t global_size_t
Global size_t object.
Declaration of Tpetra::MMMultiMultiply and nonmember constructors.