All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
Xpetra_EpetraCrsGraph.cpp
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 #include "Xpetra_EpetraCrsGraph.hpp"
00047 
00048 #include "Xpetra_Exceptions.hpp"
00049 #include "Xpetra_Utils.hpp"
00050 #include "Xpetra_EpetraExport.hpp"
00051 #include "Xpetra_EpetraImport.hpp"
00052 
00053 namespace Xpetra {
00054 
00055   // TODO: move that elsewhere
00056   template<class GlobalOrdinal>
00057   const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph<int, GlobalOrdinal> > &graph) {
00058     XPETRA_RCP_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, graph, epetraGraph, "toEpetra");
00059     return *(epetraGraph->getEpetra_CrsGraph());
00060   }
00061 
00062   template<class EpetraGlobalOrdinal>
00063   EpetraCrsGraphT<EpetraGlobalOrdinal>::EpetraCrsGraphT(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, size_t maxNumEntriesPerRow, ProfileType pftype, const Teuchos::RCP< Teuchos::ParameterList > &plist)
00064     : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), maxNumEntriesPerRow, toEpetra(pftype)))) { }
00065 
00066   // TODO: convert array size_t to int
00067   //   template<class EpetraGlobalOrdinal>
00068   //   EpetraCrsGraphT<EpetraGlobalOrdinal>::EpetraCrsGraphT(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, ProfileType pftype)
00069   //     : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), NumEntriesPerRowToAlloc.getRawPtr(), toEpetra(pftype)))) { }
00070 
00071   template<class EpetraGlobalOrdinal>
00072   EpetraCrsGraphT<EpetraGlobalOrdinal>::EpetraCrsGraphT(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &colMap, size_t maxNumEntriesPerRow, ProfileType pftype, const Teuchos::RCP< Teuchos::ParameterList > &plist)
00073     : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), toEpetra(colMap), maxNumEntriesPerRow, toEpetra(pftype)))) { }
00074 
00075   // TODO: convert array size_t to int
00076   //   template<class EpetraGlobalOrdinal>
00077   //   EpetraCrsGraphT<EpetraGlobalOrdinal>::EpetraCrsGraphT(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &colMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, ProfileType pftype)
00078   //     : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), toEpetra(colMap), NumEntriesPerRowToAlloc.getRawPtr(), toEpetra(pftype)))) { }
00079 
00080   template<class EpetraGlobalOrdinal>
00081   void EpetraCrsGraphT<EpetraGlobalOrdinal>::insertGlobalIndices(GlobalOrdinal globalRow, const ArrayView<const GlobalOrdinal> &indices) {
00082     XPETRA_MONITOR("EpetraCrsGraphT::insertGlobalIndices");
00083 
00084     GlobalOrdinal* indices_rawPtr = const_cast<GlobalOrdinal*>(indices.getRawPtr()); // there is no const in the Epetra interface :(
00085     XPETRA_ERR_CHECK(graph_->InsertGlobalIndices(globalRow, indices.size(), indices_rawPtr));
00086   }
00087 
00088   template<class EpetraGlobalOrdinal>
00089   void EpetraCrsGraphT<EpetraGlobalOrdinal>::insertLocalIndices(int localRow, const ArrayView<const int> &indices) {
00090     XPETRA_MONITOR("EpetraCrsGraphT::insertLocalIndices");
00091 
00092     int* indices_rawPtr = const_cast<int*>(indices.getRawPtr()); // there is no const in the Epetra interface :(
00093     XPETRA_ERR_CHECK(graph_->InsertMyIndices(localRow, indices.size(), indices_rawPtr));
00094   }
00095 
00096   template<class EpetraGlobalOrdinal>
00097   void EpetraCrsGraphT<EpetraGlobalOrdinal>::getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView<const GlobalOrdinal> &Indices) const {
00098     XPETRA_MONITOR("EpetraCrsGraphT::getGlobalRowView");
00099 
00100     int      numEntries;
00101     GlobalOrdinal    * eIndices;
00102 
00103     XPETRA_ERR_CHECK(graph_->ExtractGlobalRowView(GlobalRow, numEntries, eIndices));
00104     if (numEntries == 0) { eIndices = NULL; } // Cf. TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 ) in Teuchos ArrayView constructor.
00105 
00106     Indices = ArrayView<const GlobalOrdinal>(eIndices, numEntries);
00107   }
00108 
00109   template<class EpetraGlobalOrdinal>
00110   void EpetraCrsGraphT<EpetraGlobalOrdinal>::getLocalRowView(int LocalRow, ArrayView<const int> &indices) const {
00111     XPETRA_MONITOR("EpetraCrsGraphT::getLocalRowView");
00112 
00113     int      numEntries;
00114     int    * eIndices;
00115 
00116     XPETRA_ERR_CHECK(graph_->ExtractMyRowView(LocalRow, numEntries, eIndices));
00117     if (numEntries == 0) { eIndices = NULL; } // Cf. TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 ) in Teuchos ArrayView constructor.
00118 
00119     indices = ArrayView<const int>(eIndices, numEntries);
00120   }
00121 
00122   template<class EpetraGlobalOrdinal>
00123   void EpetraCrsGraphT<EpetraGlobalOrdinal>::fillComplete(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &domainMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rangeMap, const RCP< ParameterList > &params){
00124     XPETRA_MONITOR("EpetraCrsGraphT::fillComplete");
00125 
00126     graph_->FillComplete(toEpetra(domainMap), toEpetra(rangeMap));
00127     bool doOptimizeStorage = true;
00128     if (params != null && params->get("Optimize Storage",true) == false) doOptimizeStorage = false;
00129     if (doOptimizeStorage) graph_->OptimizeStorage();
00130   }
00131 
00132   template<class EpetraGlobalOrdinal>
00133   void EpetraCrsGraphT<EpetraGlobalOrdinal>::fillComplete(const RCP< ParameterList > &params) {
00134     XPETRA_MONITOR("EpetraCrsGraphT::fillComplete");
00135 
00136     graph_->FillComplete();
00137     bool doOptimizeStorage = true;
00138     if (params != null && params->get("Optimize Storage",true) == false) doOptimizeStorage = false;
00139     if (doOptimizeStorage) graph_->OptimizeStorage();
00140   }
00141 
00142   template<class EpetraGlobalOrdinal>
00143   std::string EpetraCrsGraphT<EpetraGlobalOrdinal>::description() const { XPETRA_MONITOR("EpetraCrsGraphT::description"); return "NotImplemented"; }
00144 
00145   template<class EpetraGlobalOrdinal>
00146   void EpetraCrsGraphT<EpetraGlobalOrdinal>::describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const {
00147     XPETRA_MONITOR("EpetraCrsGraphT::describe");
00148 
00149     out << "EpetraCrsGraphT::describe : Warning, verbosity level is ignored by this method." << std::endl;
00150     const Epetra_BlockMap rowmap = graph_->RowMap();
00151     if (rowmap.Comm().MyPID() == 0) out << "** EpetraCrsGraphT **\n\nrowmap" << std::endl;
00152     rowmap.Print(out);
00153     graph_->Print(out);
00154   }
00155 
00156   // TODO: move that elsewhere
00157   template<class GlobalOrdinal>
00158   RCP<const CrsGraph<int, GlobalOrdinal> >
00159   toXpetra (const Epetra_CrsGraph &g)
00160   {
00161     RCP<const Epetra_CrsGraph> const_graph = rcp (new Epetra_CrsGraph (g));
00162     RCP<Epetra_CrsGraph> graph =
00163       Teuchos::rcp_const_cast<Epetra_CrsGraph> (const_graph);
00164     return rcp (new Xpetra::EpetraCrsGraphT<GlobalOrdinal>(graph));
00165   }
00166   //
00167 
00168   // TODO: use toEpetra()
00169   template<class EpetraGlobalOrdinal>
00170   void EpetraCrsGraphT<EpetraGlobalOrdinal>::doImport(const DistObject<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node> &source,
00171                                  const Import<LocalOrdinal, GlobalOrdinal, Node> &importer, CombineMode CM) {
00172     XPETRA_MONITOR("EpetraCrsGraphT::doImport");
00173 
00174     XPETRA_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, source, tSource, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraCrsGraphT as input arguments.");
00175     XPETRA_DYNAMIC_CAST(const EpetraImportT<GlobalOrdinal>, importer, tImporter, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraImportT as input arguments.");
00176 
00177     RCP<const Epetra_CrsGraph> v = tSource.getEpetra_CrsGraph();
00178     int err = graph_->Import(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
00179     TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
00180   }
00181 
00182   template<class EpetraGlobalOrdinal>
00183   void EpetraCrsGraphT<EpetraGlobalOrdinal>::doExport(const DistObject<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node> &dest,
00184                                  const Import<LocalOrdinal, GlobalOrdinal, Node>& importer, CombineMode CM) {
00185     XPETRA_MONITOR("EpetraCrsGraphT::doExport");
00186 
00187     XPETRA_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, dest, tDest, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraCrsGraphT as input arguments.");
00188     XPETRA_DYNAMIC_CAST(const EpetraImportT<GlobalOrdinal>, importer, tImporter, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraImportT as input arguments.");
00189 
00190     RCP<const Epetra_CrsGraph> v = tDest.getEpetra_CrsGraph();
00191     int err = graph_->Export(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
00192     TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
00193   }
00194 
00195   template<class EpetraGlobalOrdinal>
00196   void EpetraCrsGraphT<EpetraGlobalOrdinal>::doImport(const DistObject<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node> &source,
00197                                  const Export<LocalOrdinal, GlobalOrdinal, Node>& exporter, CombineMode CM) {
00198     XPETRA_MONITOR("EpetraCrsGraphT::doImport");
00199 
00200     XPETRA_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, source, tSource, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraCrsGraphT as input arguments.");
00201     XPETRA_DYNAMIC_CAST(const EpetraExportT<GlobalOrdinal>, exporter, tExporter, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraImportT as input arguments.");
00202 
00203     RCP<const Epetra_CrsGraph> v = tSource.getEpetra_CrsGraph();
00204     int err = graph_->Import(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
00205     TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
00206 
00207   }
00208 
00209   template<class EpetraGlobalOrdinal>
00210   void EpetraCrsGraphT<EpetraGlobalOrdinal>::doExport(const DistObject<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node> &dest,
00211                                  const Export<LocalOrdinal, GlobalOrdinal, Node>& exporter, CombineMode CM) {
00212     XPETRA_MONITOR("EpetraCrsGraphT::doExport");
00213 
00214     XPETRA_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, dest, tDest, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraCrsGraphT as input arguments.");
00215     XPETRA_DYNAMIC_CAST(const EpetraExportT<GlobalOrdinal>, exporter, tExporter, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraImportT as input arguments.");
00216 
00217     RCP<const Epetra_CrsGraph> v = tDest.getEpetra_CrsGraph();
00218     int err = graph_->Export(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
00219     TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
00220   }
00221 
00222 #ifndef XPETRA_EPETRA_NO_32BIT_GLOBAL_INDICES
00223 template class EpetraCrsGraphT<int>;
00224 template RCP< const CrsGraph<int, int> > toXpetra<int>(const Epetra_CrsGraph &g);
00225 template const Epetra_CrsGraph & toEpetra<int>(const RCP< const CrsGraph<int, int> > &graph);
00226 #endif
00227 
00228 #ifndef XPETRA_EPETRA_NO_64BIT_GLOBAL_INDICES
00229 template class EpetraCrsGraphT<long long>;
00230 template RCP< const CrsGraph<int, long long> > toXpetra<long long>(const Epetra_CrsGraph &g);
00231 template const Epetra_CrsGraph & toEpetra<long long>(const RCP< const CrsGraph<int, long long> > &graph);
00232 #endif
00233 
00234 } // namespace Xpetra
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines