Xpetra_EpetraCrsGraph.cpp
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
47 
48 #include "Xpetra_Exceptions.hpp"
49 #include "Xpetra_Utils.hpp"
50 #include "Xpetra_EpetraExport.hpp"
51 #include "Xpetra_EpetraImport.hpp"
52 
53 namespace Xpetra {
54 
55  // TODO: move that elsewhere
56  template<class GlobalOrdinal>
57  const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph<int, GlobalOrdinal> > &graph) {
58  XPETRA_RCP_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, graph, epetraGraph, "toEpetra");
59  return *(epetraGraph->getEpetra_CrsGraph());
60  }
61 
62  template<class EpetraGlobalOrdinal>
64  : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), maxNumEntriesPerRow, toEpetra(pftype)))) { }
65 
66  // TODO: convert array size_t to int
67  // template<class EpetraGlobalOrdinal>
68  // EpetraCrsGraphT<EpetraGlobalOrdinal>::EpetraCrsGraphT(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, ProfileType pftype)
69  // : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), NumEntriesPerRowToAlloc.getRawPtr(), toEpetra(pftype)))) { }
70 
71  template<class EpetraGlobalOrdinal>
73  : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), toEpetra(colMap), maxNumEntriesPerRow, toEpetra(pftype)))) { }
74 
75  // TODO: convert array size_t to int
76  // template<class EpetraGlobalOrdinal>
77  // 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)
78  // : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), toEpetra(colMap), NumEntriesPerRowToAlloc.getRawPtr(), toEpetra(pftype)))) { }
79 
80  template<class EpetraGlobalOrdinal>
82  XPETRA_MONITOR("EpetraCrsGraphT::insertGlobalIndices");
83 
84  GlobalOrdinal* indices_rawPtr = const_cast<GlobalOrdinal*>(indices.getRawPtr()); // there is no const in the Epetra interface :(
85  XPETRA_ERR_CHECK(graph_->InsertGlobalIndices(globalRow, indices.size(), indices_rawPtr));
86  }
87 
88  template<class EpetraGlobalOrdinal>
90  XPETRA_MONITOR("EpetraCrsGraphT::insertLocalIndices");
91 
92  int* indices_rawPtr = const_cast<int*>(indices.getRawPtr()); // there is no const in the Epetra interface :(
93  XPETRA_ERR_CHECK(graph_->InsertMyIndices(localRow, indices.size(), indices_rawPtr));
94  }
95 
96  template<class EpetraGlobalOrdinal>
98  XPETRA_MONITOR("EpetraCrsGraphT::getGlobalRowView");
99 
100  int numEntries;
101  GlobalOrdinal * eIndices;
102 
103  XPETRA_ERR_CHECK(graph_->ExtractGlobalRowView(GlobalRow, numEntries, eIndices));
104  if (numEntries == 0) { eIndices = NULL; } // Cf. TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 ) in Teuchos ArrayView constructor.
105 
106  Indices = ArrayView<const GlobalOrdinal>(eIndices, numEntries);
107  }
108 
109  template<class EpetraGlobalOrdinal>
111  XPETRA_MONITOR("EpetraCrsGraphT::getLocalRowView");
112 
113  int numEntries;
114  int * eIndices;
115 
116  XPETRA_ERR_CHECK(graph_->ExtractMyRowView(LocalRow, numEntries, eIndices));
117  if (numEntries == 0) { eIndices = NULL; } // Cf. TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 ) in Teuchos ArrayView constructor.
118 
119  indices = ArrayView<const int>(eIndices, numEntries);
120  }
121 
122  template<class EpetraGlobalOrdinal>
124  XPETRA_MONITOR("EpetraCrsGraphT::fillComplete");
125 
126  graph_->FillComplete(toEpetra(domainMap), toEpetra(rangeMap));
127  bool doOptimizeStorage = true;
128  if (params != null && params->get("Optimize Storage",true) == false) doOptimizeStorage = false;
129  if (doOptimizeStorage) graph_->OptimizeStorage();
130  }
131 
132  template<class EpetraGlobalOrdinal>
134  XPETRA_MONITOR("EpetraCrsGraphT::fillComplete");
135 
136  graph_->FillComplete();
137  bool doOptimizeStorage = true;
138  if (params != null && params->get("Optimize Storage",true) == false) doOptimizeStorage = false;
139  if (doOptimizeStorage) graph_->OptimizeStorage();
140  }
141 
142  template<class EpetraGlobalOrdinal>
143  std::string EpetraCrsGraphT<EpetraGlobalOrdinal>::description() const { XPETRA_MONITOR("EpetraCrsGraphT::description"); return "NotImplemented"; }
144 
145  template<class EpetraGlobalOrdinal>
147  XPETRA_MONITOR("EpetraCrsGraphT::describe");
148 
149  out << "EpetraCrsGraphT::describe : Warning, verbosity level is ignored by this method." << std::endl;
150  const Epetra_BlockMap rowmap = graph_->RowMap();
151  if (rowmap.Comm().MyPID() == 0) out << "** EpetraCrsGraphT **\n\nrowmap" << std::endl;
152  rowmap.Print(out);
153  graph_->Print(out);
154  }
155 
156  // TODO: move that elsewhere
157  template<class GlobalOrdinal>
159  toXpetra (const Epetra_CrsGraph &g)
160  {
161  RCP<const Epetra_CrsGraph> const_graph = rcp (new Epetra_CrsGraph (g));
162  RCP<Epetra_CrsGraph> graph =
163  Teuchos::rcp_const_cast<Epetra_CrsGraph> (const_graph);
164  return rcp (new Xpetra::EpetraCrsGraphT<GlobalOrdinal>(graph));
165  }
166  //
167 
168  // TODO: use toEpetra()
169  template<class EpetraGlobalOrdinal>
172  XPETRA_MONITOR("EpetraCrsGraphT::doImport");
173 
174  XPETRA_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, source, tSource, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraCrsGraphT as input arguments.");
175  XPETRA_DYNAMIC_CAST(const EpetraImportT<GlobalOrdinal>, importer, tImporter, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraImportT as input arguments.");
176 
177  RCP<const Epetra_CrsGraph> v = tSource.getEpetra_CrsGraph();
178  int err = graph_->Import(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
179  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
180  }
181 
182  template<class EpetraGlobalOrdinal>
185  XPETRA_MONITOR("EpetraCrsGraphT::doExport");
186 
187  XPETRA_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, dest, tDest, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraCrsGraphT as input arguments.");
188  XPETRA_DYNAMIC_CAST(const EpetraImportT<GlobalOrdinal>, importer, tImporter, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraImportT as input arguments.");
189 
190  RCP<const Epetra_CrsGraph> v = tDest.getEpetra_CrsGraph();
191  int err = graph_->Export(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
192  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
193  }
194 
195  template<class EpetraGlobalOrdinal>
198  XPETRA_MONITOR("EpetraCrsGraphT::doImport");
199 
200  XPETRA_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, source, tSource, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraCrsGraphT as input arguments.");
201  XPETRA_DYNAMIC_CAST(const EpetraExportT<GlobalOrdinal>, exporter, tExporter, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraImportT as input arguments.");
202 
203  RCP<const Epetra_CrsGraph> v = tSource.getEpetra_CrsGraph();
204  int err = graph_->Import(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
205  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
206 
207  }
208 
209  template<class EpetraGlobalOrdinal>
212  XPETRA_MONITOR("EpetraCrsGraphT::doExport");
213 
214  XPETRA_DYNAMIC_CAST(const EpetraCrsGraphT<GlobalOrdinal>, dest, tDest, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraCrsGraphT as input arguments.");
215  XPETRA_DYNAMIC_CAST(const EpetraExportT<GlobalOrdinal>, exporter, tExporter, "Xpetra::EpetraCrsGraphT::doImport only accept Xpetra::EpetraImportT as input arguments.");
216 
217  RCP<const Epetra_CrsGraph> v = tDest.getEpetra_CrsGraph();
218  int err = graph_->Export(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
219  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
220  }
221 
222 #ifndef XPETRA_EPETRA_NO_32BIT_GLOBAL_INDICES
223 template class EpetraCrsGraphT<int>;
224 template RCP< const CrsGraph<int, int> > toXpetra<int>(const Epetra_CrsGraph &g);
225 template const Epetra_CrsGraph & toEpetra<int>(const RCP< const CrsGraph<int, int> > &graph);
226 #endif
227 
228 #ifndef XPETRA_EPETRA_NO_64BIT_GLOBAL_INDICES
229 template class EpetraCrsGraphT<long long>;
230 template RCP< const CrsGraph<int, long long> > toXpetra<long long>(const Epetra_CrsGraph &g);
231 template const Epetra_CrsGraph & toEpetra<long long>(const RCP< const CrsGraph<int, long long> > &graph);
232 #endif
233 
234 } // namespace Xpetra
template const Epetra_CrsGraph & toEpetra< long long >(const RCP< const CrsGraph< int, long long > > &graph)
template RCP< const CrsGraph< int, long long > > toXpetra< long long >(const Epetra_CrsGraph &g)
void getLocalRowView(LocalOrdinal LocalRow, ArrayView< const LocalOrdinal > &indices) const
Return a const, nonpersisting view of local indices in the given row.
void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView< const GlobalOrdinal > &Indices) const
Return a const, nonpersisting view of global indices in the given row.
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, GlobalOrdinal > > &graph)
T & get(const std::string &name, T def_value)
EpetraCrsGraphT(const RCP< const map_type > &rowMap, size_t maxNumEntriesPerRow, ProfileType pftype=DynamicProfile, const RCP< ParameterList > &params=null)
Constructor specifying fixed number of entries for each row.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Xpetra namespace
size_type size() const
RCP< const CrsGraph< int, GlobalOrdinal > > toXpetra(const Epetra_CrsGraph &g)
template const Epetra_CrsGraph & toEpetra< int >(const RCP< const CrsGraph< int, int > > &graph)
std::string description() const
Return a simple one-line description of this object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define XPETRA_ERR_CHECK(arg)
void insertGlobalIndices(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &indices)
Insert global indices into the graph.
EpetraGlobalOrdinal GlobalOrdinal
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
template RCP< const CrsGraph< int, int > > toXpetra< int >(const Epetra_CrsGraph &g)
void doImport(const DistObject< GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node > &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Import.
T * getRawPtr() const
#define XPETRA_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
#define XPETRA_RCP_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
void fillComplete(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &domainMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rangeMap, const RCP< ParameterList > &params=null)
Signal that data entry is complete, specifying domain and range maps.
void doExport(const DistObject< GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node > &dest, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Export.
Copy
CombineMode
Xpetra::Combine Mode enumerable type.
#define XPETRA_MONITOR(funcName)
void insertLocalIndices(const LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &indices)
Insert local indices into the graph.