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 
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  typedef typename local_matrix_type::values_type values_type;
61 
62  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
63  (graph.is_null (), std::runtime_error, "Input graph is null.");
64  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
65  (!graph->isFillComplete (), std::runtime_error, "Input graph is not "
66  "fill complete. You must call fillComplete on the graph before using "
67  "it to construct a FECrsMatrix. Note that calling resumeFill on the "
68  "graph makes it not fill complete, even if you had previously called "
69  "fillComplete. In that case, you must call fillComplete on the graph "
70  "again.");
71  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
72  ( *graph->activeCrsGraph_!= fe_crs_graph_type::FE_ACTIVE_OWNED,std::runtime_error,
73  "Input graph must be in FE_ACTIVE_OWNED mode when this constructor is called.");
74 
75  activeCrsMatrix_ = Teuchos::rcp(new FEWhichActive(FE_ACTIVE_OWNED_PLUS_SHARED));
76 
77  // Make an "inactive" matrix, if we need to
78  if(!graph->inactiveCrsGraph_.is_null() ) {
79  // We are *requiring* memory aliasing here, so we'll grab the first chunk of the Owned+Shared matrix's values array to make the
80  // guy for the Owned matrix.
81  values_type myvals = this->getLocalMatrix().values;
82 
83  size_t numOwnedVals = graph->getLocalGraph().entries.extent(0); // OwnedVals
84  inactiveCrsMatrix_ = Teuchos::rcp(new crs_matrix_type(graph,Kokkos::subview(myvals,Kokkos::pair<size_t,size_t>(0,numOwnedVals))));
85  }
86 }
87 
88 
89 
90 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
91 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::doOwnedPlusSharedToOwned(const CombineMode CM) {
92  if(!inactiveCrsMatrix_.is_null() && *activeCrsMatrix_ == FE_ACTIVE_OWNED_PLUS_SHARED) {
93  // Do a self-export in "restricted mode"
94  this->doExport(*this,*feGraph_->importer_,CM,true);
95  inactiveCrsMatrix_->fillComplete();
96  }
97  crs_matrix_type::fillComplete();
98 }//end doOverlapToLocal
99 
100 
101 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
102 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::doOwnedToOwnedPlusShared(const CombineMode /* CM */) {
103  // This should be a no-op for all of our purposes
104 }//end doLocalToOverlap
105 
106 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
107 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::switchActiveCrsMatrix() {
108  if(*activeCrsMatrix_ == FE_ACTIVE_OWNED_PLUS_SHARED)
109  *activeCrsMatrix_ = FE_ACTIVE_OWNED;
110  else
111  *activeCrsMatrix_ = FE_ACTIVE_OWNED_PLUS_SHARED;
112 
113  if(inactiveCrsMatrix_.is_null()) return;
114 
115  this->swap(*inactiveCrsMatrix_);
116 
117 }//end switchActiveCrsMatrix
118 
119 
120 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
121 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::endFill() {
122  if(*activeCrsMatrix_ == FE_ACTIVE_OWNED_PLUS_SHARED) {
123  doOwnedPlusSharedToOwned(Tpetra::ADD);
124  switchActiveCrsMatrix();
125  }
126  else
127  throw std::runtime_error("FECrsMatrix: Local CrsMatrix already active. Cannot endFill()");
128 }
129 
130 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
131 void FECrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::beginFill() {
132  // Note: This does not throw an error since the on construction, the FECRS is in overlap mode. Ergo, calling beginFill(),
133  // like one should expect to do in a rational universe, should not cause an error.
134  if(*activeCrsMatrix_ == FE_ACTIVE_OWNED) {
135  this->resumeFill();
136  switchActiveCrsMatrix();
137  }
138  this->resumeFill();
139 }
140 
141 
142 
143 
144 } // end namespace Tpetra
145 
146 
147 //
148 // Explicit instantiation macro
149 //
150 // Must be expanded from within the Tpetra namespace!
151 //
152 #define TPETRA_FECRSMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
153  template class FECrsMatrix<SCALAR, LO, GO, NODE>;
154 
155 
156 
157 #endif // TPETRA_FECRSMATRIX_DEF
CombineMode
Rule for combining data in an Import or Export.
Sum new values into existing values.