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