Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_FECrsMatrix_def.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_FECRSMATRIX_DEF_HPP
43 #define TPETRA_FECRSMATRIX_DEF_HPP
44 #include <sstream>
45 #include "Tpetra_CrsMatrix.hpp"
46 
47 namespace Tpetra {
48 
49 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
50 FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
51 FECrsMatrix(const Teuchos::RCP<const fe_crs_graph_type>& graph,
52  const Teuchos::RCP<Teuchos::ParameterList>& params) :
53  // We want the OWNED_PLUS_SHARED graph here
54  // NOTE: The casts below are terrible, but necesssary
55  crs_matrix_type( graph->inactiveCrsGraph_.is_null() ? Teuchos::rcp_const_cast<crs_graph_type>(Teuchos::rcp_dynamic_cast<const crs_graph_type>(graph)) : graph->inactiveCrsGraph_,params),
56  feGraph_(graph)
57 
58 {
59  const char tfecfFuncName[] = "FECrsMatrix(RCP<const FECrsGraph>[, RCP<ParameterList>]): ";
60 
61  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
62  (graph.is_null (), std::runtime_error, "Input graph is null.");
63  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
64  (!graph->isFillComplete (), std::runtime_error, "Input graph is not "
65  "fill complete. You must call fillComplete on the graph before using "
66  "it to construct a FECrsMatrix. Note that calling resumeFill on the "
67  "graph makes it not fill complete, even if you had previously called "
68  "fillComplete. In that case, you must call fillComplete on the graph "
69  "again.");
70  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
71  ( *graph->activeCrsGraph_!= fe_crs_graph_type::FE_ACTIVE_OWNED,std::runtime_error,
72  "Input graph must be in FE_ACTIVE_OWNED mode when this constructor is called.");
73 
74  bool start_owned = false;
75  if (! params.is_null ()) {
76  if (params->isParameter ("start owned")) {
77  start_owned = params->get<bool>("start owned", start_owned);
78  }
79  }
80  if(start_owned) {
81  activeCrsMatrix_ = Teuchos::rcp(new FEWhichActive(FE_ACTIVE_OWNED));
82  } else {
83  activeCrsMatrix_ = Teuchos::rcp(new FEWhichActive(FE_ACTIVE_OWNED_PLUS_SHARED));
84  }
85 
86  // Make an "inactive" matrix, if we need to
87  if(!graph->inactiveCrsGraph_.is_null() ) {
88  // We are *requiring* memory aliasing here, so we'll grab the first chunk of the Owned+Shared matrix's values array to make the
89  // guy for the Owned matrix.
90  inactiveCrsMatrix_ = Teuchos::rcp(new crs_matrix_type(*this,graph));
91  }
92 
93  fillState_ = Teuchos::rcp(new FillState(FillState::closed));
94 }
95 
96 
97 
98 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
99 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::doOwnedPlusSharedToOwned(const CombineMode CM) {
100  if(!inactiveCrsMatrix_.is_null() && *activeCrsMatrix_ == FE_ACTIVE_OWNED_PLUS_SHARED) {
101  // Do a self-export in "restricted mode"
102  this->doExport(*this,*feGraph_->ownedRowsImporter_,CM,true);
103  inactiveCrsMatrix_->fillComplete();
104  }
105  crs_matrix_type::fillComplete();
106 }//end doOverlapToLocal
107 
108 
109 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
110 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::doOwnedToOwnedPlusShared(const CombineMode /* CM */) {
111  // This should be a no-op for all of our purposes
112 }//end doLocalToOverlap
113 
114 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
115 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::switchActiveCrsMatrix() {
116  if(*activeCrsMatrix_ == FE_ACTIVE_OWNED_PLUS_SHARED)
117  *activeCrsMatrix_ = FE_ACTIVE_OWNED;
118  else
119  *activeCrsMatrix_ = FE_ACTIVE_OWNED_PLUS_SHARED;
120 
121  if(inactiveCrsMatrix_.is_null()) return;
122 
123  this->swap(*inactiveCrsMatrix_);
124 
125 }//end switchActiveCrsMatrix
126 
127 
128 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
129 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::endFill() {
130  if(*activeCrsMatrix_ == FE_ACTIVE_OWNED_PLUS_SHARED) {
131  doOwnedPlusSharedToOwned(Tpetra::ADD);
132  switchActiveCrsMatrix();
133  }
134  else
135  throw std::runtime_error("FECrsMatrix: Local CrsMatrix already active. Cannot endFill()");
136 }
137 
138 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
139 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::beginFill() {
140  // Note: This does not throw an error since the on construction, the FECRS is in overlap mode. Ergo, calling beginFill(),
141  // like one should expect to do in a rational universe, should not cause an error.
142  if(*activeCrsMatrix_ == FE_ACTIVE_OWNED) {
143  this->resumeFill();
144  switchActiveCrsMatrix();
145  }
146  this->resumeFill();
147 }
148 
149 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
150 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::beginAssembly() {
151  const char tfecfFuncName[] = "FECrsMatrix::beginAssembly: ";
152  if (*fillState_ != FillState::closed)
153  {
154  std::ostringstream errmsg;
155  errmsg << "Cannot begin assembly, matrix is not in a closed state "
156  << "but is currently open for "
157  << (*fillState_ == FillState::open ? "assembly" : "modification");
158  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
159  }
160  *fillState_ = FillState::open;
161  this->beginFill();
162 }
163 
164 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
165 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::endAssembly() {
166  const char tfecfFuncName[] = "FECrsMatrix::endAssembly: ";
167  if (*fillState_ != FillState::open)
168  {
169  std::ostringstream errmsg;
170  errmsg << "Cannot end assembly, matrix is not open for assembly "
171  << "but is currently "
172  << (*fillState_ == FillState::closed ? "closed" : "open for modification");
173  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
174  }
175  *fillState_ = FillState::closed;
176  this->endFill();
177 }
178 
179 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
180 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::beginModify() {
181  const char tfecfFuncName[] = "FECrsMatrix::beginModify: ";
182  if (*fillState_ != FillState::closed)
183  {
184  std::ostringstream errmsg;
185  errmsg << "Cannot begin modifying, matrix is not in a closed state "
186  << "but is currently open for "
187  << (*fillState_ == FillState::open ? "assembly" : "modification");
188  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
189  }
190  *fillState_ = FillState::modify;
191  this->resumeFill();
192 }
193 
194 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
195 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::endModify() {
196  const char tfecfFuncName[] = "FECrsMatrix::endModify: ";
197  if (*fillState_ != FillState::modify)
198  {
199  std::ostringstream errmsg;
200  errmsg << "Cannot end modifying, matrix is not open to modify but is currently "
201  << (*fillState_ == FillState::open ? "open for assembly" : "closed");
202  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
203  }
204  *fillState_ = FillState::closed;
205  this->fillComplete();
206 }
207 
208 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
209 LocalOrdinal
210 FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::replaceGlobalValuesImpl(
211  impl_scalar_type rowVals[],
212  const crs_graph_type& graph,
213  const RowInfo& rowInfo,
214  const GlobalOrdinal inds[],
215  const impl_scalar_type newVals[],
216  const LocalOrdinal numElts)
217 {
218  const char tfecfFuncName[] = "FECrsMatrix::replaceGlobalValues: ";
219  if (*fillState_ != FillState::open)
220  {
221  std::ostringstream errmsg;
222  errmsg << "Cannot replace global values, matrix is not open for assembly "
223  << "but is currently "
224  << (*fillState_ == FillState::modify ? "open for modification" : "closed");
225  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
226  }
227  return crs_matrix_type::replaceGlobalValuesImpl(rowVals, graph, rowInfo, inds, newVals, numElts);
228 }
229 
230 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
231 LocalOrdinal
232 FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::replaceLocalValuesImpl(
233  impl_scalar_type rowVals[],
234  const crs_graph_type& graph,
235  const RowInfo& rowInfo,
236  const LocalOrdinal inds[],
237  const impl_scalar_type newVals[],
238  const LocalOrdinal numElts)
239 {
240  const char tfecfFuncName[] = "FECrsMatrix::replaceLocalValues: ";
241  if (*fillState_ != FillState::open && *fillState_ != FillState::modify)
242  {
243  std::ostringstream errmsg;
244  errmsg << "Cannot replace local values, matrix is not open to fill/modify. "
245  << "The matrix is currently closed";
246  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
247  }
248  return crs_matrix_type::replaceLocalValuesImpl(rowVals, graph, rowInfo, inds, newVals, numElts);
249 }
250 
251 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
252 LocalOrdinal
253 FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::sumIntoGlobalValuesImpl(
254  impl_scalar_type rowVals[],
255  const crs_graph_type& graph,
256  const RowInfo& rowInfo,
257  const GlobalOrdinal inds[],
258  const impl_scalar_type newVals[],
259  const LocalOrdinal numElts,
260  const bool atomic)
261 {
262  const char tfecfFuncName[] = "FECrsMatrix::sumIntoGlobalValues: ";
263  if (*fillState_ != FillState::open)
264  {
265  std::ostringstream errmsg;
266  errmsg << "Cannot sum in to global values, matrix is not open for assembly. "
267  << "The matrix is currently "
268  << (*fillState_ == FillState::modify ? "open for modification" : "closed");
269  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
270  }
271  return crs_matrix_type::sumIntoGlobalValuesImpl(
272  rowVals, graph, rowInfo, inds, newVals, numElts, atomic
273  );
274 }
275 
276 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
277 LocalOrdinal
278 FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::sumIntoLocalValuesImpl(
279  impl_scalar_type rowVals[],
280  const crs_graph_type& graph,
281  const RowInfo& rowInfo,
282  const LocalOrdinal inds[],
283  const impl_scalar_type newVals[],
284  const LocalOrdinal numElts,
285  const bool atomic)
286 {
287  const char tfecfFuncName[] = "FECrsMatrix::sumIntoLocalValues: ";
288  if (*fillState_ != FillState::open)
289  {
290  std::ostringstream errmsg;
291  errmsg << "Cannot sum in to local values, matrix is not open for assembly. "
292  << "The matrix is currently "
293  << (*fillState_ == FillState::modify ? "open for modification" : "closed");
294  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
295  }
296  return crs_matrix_type::sumIntoLocalValuesImpl(
297  rowVals, graph, rowInfo, inds, newVals, numElts, atomic
298  );
299 }
300 
301 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
302 void
303 FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::insertGlobalValuesImpl(
304  crs_graph_type& graph,
305  RowInfo& rowInfo,
306  const GlobalOrdinal gblColInds[],
307  const impl_scalar_type vals[],
308  const size_t numInputEnt)
309 {
310  const char tfecfFuncName[] = "FECrsMatrix::insertGlobalValues: ";
311  if (*fillState_ != FillState::open)
312  {
313  std::ostringstream errmsg;
314  errmsg << "Cannot insert global values, matrix is not open for assembly. "
315  << "The matrix is currently "
316  << (*fillState_ == FillState::modify ? "open for modification" : "closed");
317  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true, std::logic_error, errmsg.str());
318  }
319  return crs_matrix_type::insertGlobalValuesImpl(graph, rowInfo, gblColInds, vals, numInputEnt);
320 }
321 
322 } // end namespace Tpetra
323 
324 
325 //
326 // Explicit instantiation macro
327 //
328 // Must be expanded from within the Tpetra namespace!
329 //
330 #define TPETRA_FECRSMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
331  template class FECrsMatrix<SCALAR, LO, GO, NODE>;
332 
333 
334 
335 #endif // TPETRA_FECRSMATRIX_DEF
CombineMode
Rule for combining data in an Import or Export.
Sum new values.