All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
Xpetra_MatrixFactory.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //             Xpetra: A linear algebra interface package
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact
00039 //                    Jonathan Hu       (jhu@sandia.gov)
00040 //                    Andrey Prokopenko (aprokop@sandia.gov)
00041 //                    Ray Tuminaro      (rstumin@sandia.gov)
00042 //
00043 // ***********************************************************************
00044 //
00045 // @HEADER
00046 
00047 // WARNING: This code is experimental. Backwards compatibility should not be expected.
00048 
00049 #ifndef XPETRA_MATRIXFACTORY_HPP
00050 #define XPETRA_MATRIXFACTORY_HPP
00051 
00052 #include "Xpetra_ConfigDefs.hpp"
00053 #include "Xpetra_Matrix.hpp"
00054 #include "Xpetra_CrsMatrixWrap.hpp"
00055 #include "Xpetra_Map.hpp"
00056 #include "Xpetra_Vector.hpp"
00057 #include "Xpetra_Exceptions.hpp"
00058 
00059 namespace Xpetra {
00060   template <class Scalar = Matrix<>::scalar_type,
00061             class LocalOrdinal = typename Matrix<Scalar>::local_ordinal_type,
00062             class GlobalOrdinal =
00063               typename Matrix<Scalar, LocalOrdinal>::global_ordinal_type,
00064             class Node =
00065               typename Matrix<Scalar, LocalOrdinal, GlobalOrdinal>::node_type>
00066   class MatrixFactory {
00067 #undef XPETRA_MATRIXFACTORY_SHORT
00068 #include "Xpetra_UseShortNames.hpp"
00069 
00070   private:
00072     MatrixFactory() {}
00073 
00074   public:
00075 
00077     static RCP<Matrix> Build(const RCP<const Map>& rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype = Xpetra::DynamicProfile) {
00078       return rcp(new CrsMatrixWrap(rowMap, maxNumEntriesPerRow, pftype));
00079     }
00080 
00082     static RCP<Matrix> Build(const RCP<const Map>& rowMap, const RCP<const Map>& colMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype = Xpetra::DynamicProfile) {
00083       return rcp(new CrsMatrixWrap(rowMap, colMap, maxNumEntriesPerRow, pftype));
00084     }
00085 
00087     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) {
00088       return rcp(new CrsMatrixWrap(rowMap, colMap, NumEntriesPerRowToAlloc, pftype));
00089     }
00090 
00092     static RCP<Matrix> Build(const RCP<const Map> &rowMap, const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, ProfileType pftype = Xpetra::DynamicProfile) {
00093       return rcp( new CrsMatrixWrap(rowMap, NumEntriesPerRowToAlloc, pftype) );
00094     }
00095 
00097     static RCP<Matrix> Build(const RCP<const CrsGraph>& graph, const RCP<ParameterList>& paramList = Teuchos::null) {
00098       return rcp(new CrsMatrixWrap(graph, paramList));
00099     }
00100 
00102     static RCP<Matrix> Build(const RCP<const Vector>& diagonal) {
00103       Teuchos::ArrayRCP<const Scalar>         vals             = diagonal->getData(0);
00104       LocalOrdinal                            NumMyElements    = diagonal->getMap()->getNodeNumElements();
00105       Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = diagonal->getMap()->getNodeElementList();
00106 
00107       Teuchos::RCP<CrsMatrixWrap> mtx = Teuchos::rcp(new CrsMatrixWrap(diagonal->getMap(), 1, Xpetra::StaticProfile));
00108 
00109       for (LocalOrdinal i = 0; i < NumMyElements; ++i) {
00110           mtx->insertGlobalValues(MyGlobalElements[i],
00111                                   Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]),
00112                                   Teuchos::tuple<Scalar>(vals[i]) );
00113       }
00114       mtx->fillComplete();
00115       return mtx;
00116     }
00117 
00119     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) {
00120       RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(sourceMatrix);
00121       if (crsOp == Teuchos::null)
00122         throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
00123 
00124       RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
00125       RCP<CrsMatrix> newCrs      = CrsMatrixFactory::Build(originalCrs, importer, domainMap, rangeMap, params);
00126       if (newCrs->hasMatrix())
00127         return rcp(new CrsMatrixWrap(newCrs));
00128       else
00129         return Teuchos::null;
00130     }
00131 
00133     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) {
00134       RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(sourceMatrix);
00135       if (crsOp == Teuchos::null)
00136         throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
00137 
00138       RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
00139       return rcp(new CrsMatrixWrap(CrsMatrixFactory::Build(originalCrs, exporter, domainMap, rangeMap, params)));
00140     }
00141   };
00142 #define XPETRA_MATRIXFACTORY_SHORT
00143 
00144 
00145   template <class Scalar = Matrix<>::scalar_type,
00146             class LocalOrdinal = typename Matrix<Scalar>::local_ordinal_type,
00147             class GlobalOrdinal =
00148               typename Matrix<Scalar, LocalOrdinal>::global_ordinal_type,
00149             class Node =
00150               typename Matrix<Scalar, LocalOrdinal, GlobalOrdinal>::node_type>
00151   class MatrixFactory2 {
00152 #undef XPETRA_MATRIXFACTORY2_SHORT
00153 #include "Xpetra_UseShortNames.hpp"
00154 
00155   public:
00156     static RCP<Matrix> BuildCopy(const RCP<const Matrix> A) {
00157       RCP<const CrsMatrixWrap> oldOp = Teuchos::rcp_dynamic_cast<const CrsMatrixWrap>(A);
00158       if (oldOp == Teuchos::null)
00159         throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
00160 
00161       RCP<const CrsMatrix> oldCrsOp = oldOp->getCrsMatrix();
00162 
00163       UnderlyingLib lib = A->getRowMap()->lib();
00164 
00165       TEUCHOS_TEST_FOR_EXCEPTION(lib != UseEpetra && lib != UseTpetra, Exceptions::RuntimeError,
00166                                  "Not Epetra or Tpetra matrix");
00167 
00168 #ifdef HAVE_XPETRA_EPETRA
00169       if (lib == UseEpetra) {
00170         // NOTE: The proper Epetra conversion in Xpetra_MatrixFactory.cpp
00171         throw Exceptions::RuntimeError("Xpetra::BuildCopy(): matrix templates are incompatible with Epetra");
00172       }
00173 #endif
00174 
00175 #ifdef HAVE_XPETRA_TPETRA
00176       if (lib == UseTpetra) {
00177         // Underlying matrix is Tpetra
00178         RCP<const TpetraCrsMatrix> oldTCrsOp = Teuchos::rcp_dynamic_cast<const TpetraCrsMatrix>(oldCrsOp);
00179 
00180         if (oldTCrsOp != Teuchos::null) {
00181           RCP<TpetraCrsMatrix> newTCrsOp(new TpetraCrsMatrix(*oldTCrsOp));
00182           RCP<CrsMatrixWrap>   newOp    (new CrsMatrixWrap(Teuchos::as<RCP<CrsMatrix> >(newTCrsOp)));
00183 
00184           return newOp;
00185         } else {
00186           throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::TpetraCrsMatrix failed");
00187         }
00188       }
00189 #endif
00190 
00191       return Teuchos::null;
00192     }
00193   };
00194 #define XPETRA_MATRIXFACTORY2_SHORT
00195 
00196   template<>
00197   class MatrixFactory2<double,int,int> {
00198     typedef double                                        Scalar;
00199     typedef int                                           LocalOrdinal;
00200     typedef int                                           GlobalOrdinal;
00201     typedef Matrix<double, int, GlobalOrdinal>::node_type Node;
00202 #undef XPETRA_MATRIXFACTORY2_SHORT
00203 #include "Xpetra_UseShortNames.hpp"
00204 
00205   public:
00206     static RCP<Matrix> BuildCopy(const RCP<const Matrix> A);
00207   };
00208 
00209 #define XPETRA_MATRIXFACTORY2_SHORT
00210 
00211 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00212   template<>
00213   class MatrixFactory2<double,int,long long> {
00214     typedef double                                        Scalar;
00215     typedef int                                           LocalOrdinal;
00216     typedef long long                                     GlobalOrdinal;
00217     typedef Matrix<double, int, GlobalOrdinal>::node_type Node;
00218 #undef XPETRA_MATRIXFACTORY2_SHORT
00219 #include "Xpetra_UseShortNames.hpp"
00220 
00221   public:
00222     static RCP<Matrix> BuildCopy(const RCP<const Matrix> A);
00223   };
00224 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00225 
00226 #define XPETRA_MATRIXFACTORY2_SHORT
00227 
00228 }
00229 
00230 #define XPETRA_MATRIXFACTORY_SHORT
00231 #define XPETRA_MATRIXFACTORY2_SHORT
00232 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines