Xpetra_MatrixFactory.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Xpetra: A linear algebra interface package
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 
47 // WARNING: This code is experimental. Backwards compatibility should not be expected.
48 
49 #ifndef XPETRA_MATRIXFACTORY_HPP
50 #define XPETRA_MATRIXFACTORY_HPP
51 
52 #include "Xpetra_ConfigDefs.hpp"
53 #include "Xpetra_Matrix.hpp"
54 #include "Xpetra_CrsMatrixWrap.hpp"
55 #include "Xpetra_Map.hpp"
56 #include "Xpetra_Vector.hpp"
57 #include "Xpetra_Exceptions.hpp"
58 
59 namespace Xpetra {
60  template <class Scalar = Matrix<>::scalar_type,
61  class LocalOrdinal = typename Matrix<Scalar>::local_ordinal_type,
62  class GlobalOrdinal =
64  class Node =
66  class MatrixFactory {
67 #undef XPETRA_MATRIXFACTORY_SHORT
68 #include "Xpetra_UseShortNames.hpp"
69 
70  private:
73 
74  public:
75 
77  static RCP<Matrix> Build(const RCP<const Map>& rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype = Xpetra::DynamicProfile) {
78  return rcp(new CrsMatrixWrap(rowMap, maxNumEntriesPerRow, pftype));
79  }
80 
82  static RCP<Matrix> Build(const RCP<const Map>& rowMap, const RCP<const Map>& colMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype = Xpetra::DynamicProfile) {
83  return rcp(new CrsMatrixWrap(rowMap, colMap, maxNumEntriesPerRow, pftype));
84  }
85 
87  static RCP<Matrix> Build(const RCP<const Map>& rowMap, const RCP<const Map>& colMap, const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, Xpetra::ProfileType pftype = Xpetra::DynamicProfile) {
88  return rcp(new CrsMatrixWrap(rowMap, colMap, NumEntriesPerRowToAlloc, pftype));
89  }
90 
91 #ifdef HAVE_XPETRA_KOKKOS_REFACTOR
92  static RCP<Matrix> Build (
94  const Teuchos::RCP<const Map>& rowMap,
95  const Teuchos::RCP<const Map>& colMap,
97  const Teuchos::RCP<Teuchos::ParameterList>& params = null) {
98  XPETRA_MONITOR("MatrixFactory::Build");
99  return rcp(new CrsMatrixWrap(rowMap, colMap, lclMatrix, params));
100  }
101 #endif
102 
104  static RCP<Matrix> Build(const RCP<const Map> &rowMap, const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, ProfileType pftype = Xpetra::DynamicProfile) {
105  return rcp( new CrsMatrixWrap(rowMap, NumEntriesPerRowToAlloc, pftype) );
106  }
107 
109  static RCP<Matrix> Build(const RCP<const CrsGraph>& graph, const RCP<ParameterList>& paramList = Teuchos::null) {
110  return rcp(new CrsMatrixWrap(graph, paramList));
111  }
112 
114  static RCP<Matrix> Build(const RCP<const Vector>& diagonal) {
115  Teuchos::ArrayRCP<const Scalar> vals = diagonal->getData(0);
116  LocalOrdinal NumMyElements = diagonal->getMap()->getNodeNumElements();
117  Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = diagonal->getMap()->getNodeElementList();
118 
120 
121  for (LocalOrdinal i = 0; i < NumMyElements; ++i) {
122  mtx->insertGlobalValues(MyGlobalElements[i],
123  Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]),
124  Teuchos::tuple<Scalar>(vals[i]) );
125  }
126  mtx->fillComplete();
127  return mtx;
128  }
129 
131  static RCP<Matrix> Build(const RCP<const Matrix>& sourceMatrix, const Import& importer, const RCP<const Map>& domainMap = Teuchos::null, const RCP<const Map>& rangeMap = Teuchos::null, const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null) {
132  RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(sourceMatrix);
133  if (crsOp == Teuchos::null)
134  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
135 
136  RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
137  RCP<CrsMatrix> newCrs = CrsMatrixFactory::Build(originalCrs, importer, domainMap, rangeMap, params);
138  if (newCrs->hasMatrix())
139  return rcp(new CrsMatrixWrap(newCrs));
140  else
141  return Teuchos::null;
142  }
143 
145  static RCP<Matrix> Build(const RCP<const Matrix> & sourceMatrix, const Export &exporter, const RCP<const Map> & domainMap = Teuchos::null, const RCP<const Map> & rangeMap = Teuchos::null,const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null) {
146  RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(sourceMatrix);
147  if (crsOp == Teuchos::null)
148  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
149 
150  RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
151  return rcp(new CrsMatrixWrap(CrsMatrixFactory::Build(originalCrs, exporter, domainMap, rangeMap, params)));
152  }
153  };
154 #define XPETRA_MATRIXFACTORY_SHORT
155 
156 
157  template <class Scalar = Matrix<>::scalar_type,
158  class LocalOrdinal = typename Matrix<Scalar>::local_ordinal_type,
159  class GlobalOrdinal =
161  class Node =
163  class MatrixFactory2 {
164 #undef XPETRA_MATRIXFACTORY2_SHORT
165 #include "Xpetra_UseShortNames.hpp"
166 
167  public:
169  RCP<const CrsMatrixWrap> oldOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(A);
170  if (oldOp == Teuchos::null)
171  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
172 
173  RCP<const CrsMatrix> oldCrsOp = oldOp->getCrsMatrix();
174 
175  UnderlyingLib lib = A->getRowMap()->lib();
176 
178  "Not Epetra or Tpetra matrix");
179 
180 #ifdef HAVE_XPETRA_EPETRA
181  if (lib == UseEpetra) {
182  // NOTE: The proper Epetra conversion in Xpetra_MatrixFactory.cpp
183  throw Exceptions::RuntimeError("Xpetra::BuildCopy(): matrix templates are incompatible with Epetra");
184  }
185 #endif
186 
187 #ifdef HAVE_XPETRA_TPETRA
188  if (lib == UseTpetra) {
189  // Underlying matrix is Tpetra
190  RCP<const TpetraCrsMatrix> oldTCrsOp = Teuchos::rcp_dynamic_cast<const TpetraCrsMatrix>(oldCrsOp);
191 
192  if (oldTCrsOp != Teuchos::null) {
193  RCP<TpetraCrsMatrix> newTCrsOp(new TpetraCrsMatrix(*oldTCrsOp));
195 
196  return newOp;
197  } else {
198  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::TpetraCrsMatrix failed");
199  }
200  }
201 #endif
202 
203  return Teuchos::null;
204  }
205  };
206 #define XPETRA_MATRIXFACTORY2_SHORT
207 
208  template<>
209  class MatrixFactory2<double,int,int> {
210  typedef double Scalar;
211  typedef int LocalOrdinal;
212  typedef int GlobalOrdinal;
214 #undef XPETRA_MATRIXFACTORY2_SHORT
215 #include "Xpetra_UseShortNames.hpp"
216 
217  public:
218  static RCP<Matrix> BuildCopy(const RCP<const Matrix> A);
219  };
220 
221 #define XPETRA_MATRIXFACTORY2_SHORT
222 
223 #ifdef HAVE_XPETRA_INT_LONG_LONG
224  template<>
225  class MatrixFactory2<double,int,long long> {
226  typedef double Scalar;
227  typedef int LocalOrdinal;
228  typedef long long GlobalOrdinal;
230 #undef XPETRA_MATRIXFACTORY2_SHORT
231 #include "Xpetra_UseShortNames.hpp"
232 
233  public:
234  static RCP<Matrix> BuildCopy(const RCP<const Matrix> A);
235  };
236 #endif // HAVE_XPETRA_INT_LONG_LONG
237 
238 #define XPETRA_MATRIXFACTORY2_SHORT
239 
240 }
241 
242 #define XPETRA_MATRIXFACTORY_SHORT
243 #define XPETRA_MATRIXFACTORY2_SHORT
244 #endif
static RCP< Matrix > Build(const RCP< const Vector > &diagonal)
Constructor for creating a diagonal Xpetra::Matrix using the entries of a given vector for the diagon...
static RCP< Matrix > Build(const RCP< const Matrix > &sourceMatrix, const Import &importer, const RCP< const Map > &domainMap=Teuchos::null, const RCP< const Map > &rangeMap=Teuchos::null, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Constructor to create a Matrix using a fusedImport-style construction. The originalMatrix must be a X...
static RCP< Matrix > Build(const RCP< const CrsGraph > &graph, const RCP< ParameterList > &paramList=Teuchos::null)
Constructor specifying graph.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static RCP< CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype=Xpetra::DynamicProfile, const Teuchos::RCP< Teuchos::ParameterList > &plist=Teuchos::null)
Constructor specifying fixed number of entries for each row.
Xpetra namespace
LocalOrdinal local_ordinal_type
Exception throws to report errors in the internal logical of the program.
static RCP< Matrix > Build(const RCP< const Map > &rowMap, const RCP< const Map > &colMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, Xpetra::ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying the (possibly different) number of entries per row and providing column map...
static RCP< Matrix > Build(const RCP< const Map > &rowMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying (possibly different) number of entries in each row.
Exception indicating invalid cast attempted.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static RCP< Matrix > Build(const RCP< const Map > &rowMap, const RCP< const Map > &colMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying the max number of non-zeros per row and providing column map.
Matrix< double, int, GlobalOrdinal >::node_type Node
TypeTo as(const TypeFrom &t)
static RCP< Matrix > BuildCopy(const RCP< const Matrix > A)
Concrete implementation of Xpetra::Matrix.
#define XPETRA_MONITOR(funcName)
static RCP< Matrix > Build(const RCP< const Map > &rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying the number of non-zeros for all rows.
MatrixFactory()
Private constructor. This is a static class.
static RCP< Matrix > Build(const RCP< const Matrix > &sourceMatrix, const Export &exporter, const RCP< const Map > &domainMap=Teuchos::null, const RCP< const Map > &rangeMap=Teuchos::null, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Constructor to create a Matrix using a fusedExport-style construction. The originalMatrix must be a X...
GlobalOrdinal global_ordinal_type