All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // Jeremie Gaidamour (jngaida@sandia.gov)
40 // Jonathan Hu (jhu@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  const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph<int, int> > &graph) {
57  XPETRA_RCP_DYNAMIC_CAST(const EpetraCrsGraph, graph, epetraGraph, "toEpetra");
58  return *(epetraGraph->getEpetra_CrsGraph());
59  }
60 
61  EpetraCrsGraph::EpetraCrsGraph(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, size_t maxNumEntriesPerRow, ProfileType pftype, const Teuchos::RCP< Teuchos::ParameterList > &plist)
62  : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), maxNumEntriesPerRow, toEpetra(pftype)))) { }
63 
64  // TODO: convert array size_t to int
65  // EpetraCrsGraph::EpetraCrsGraph(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, ProfileType pftype)
66  // : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), NumEntriesPerRowToAlloc.getRawPtr(), toEpetra(pftype)))) { }
67 
68  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)
69  : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), toEpetra(colMap), maxNumEntriesPerRow, toEpetra(pftype)))) { }
70 
71  // TODO: convert array size_t to int
72  // 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)
73  // : graph_(Teuchos::rcp(new Epetra_CrsGraph(Copy, toEpetra(rowMap), toEpetra(colMap), NumEntriesPerRowToAlloc.getRawPtr(), toEpetra(pftype)))) { }
74 
75  void EpetraCrsGraph::insertGlobalIndices(int globalRow, const ArrayView<const int> &indices) {
76  XPETRA_MONITOR("EpetraCrsGraph::insertGlobalIndices");
77 
78  int* indices_rawPtr = const_cast<int*>(indices.getRawPtr()); // there is no const in the Epetra interface :(
79  XPETRA_ERR_CHECK(graph_->InsertGlobalIndices(globalRow, indices.size(), indices_rawPtr));
80  }
81 
82  void EpetraCrsGraph::insertLocalIndices(int localRow, const ArrayView<const int> &indices) {
83  XPETRA_MONITOR("EpetraCrsGraph::insertLocalIndices");
84 
85  int* indices_rawPtr = const_cast<int*>(indices.getRawPtr()); // there is no const in the Epetra interface :(
86  XPETRA_ERR_CHECK(graph_->InsertMyIndices(localRow, indices.size(), indices_rawPtr));
87  }
88 
89  void EpetraCrsGraph::getGlobalRowView(int GlobalRow, ArrayView<const int> &Indices) const {
90  XPETRA_MONITOR("EpetraCrsGraph::getGlobalRowView");
91 
92  int numEntries;
93  int * eIndices;
94 
95  XPETRA_ERR_CHECK(graph_->ExtractGlobalRowView(GlobalRow, numEntries, eIndices));
96  if (numEntries == 0) { eIndices = NULL; } // Cf. TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 ) in Teuchos ArrayView constructor.
97 
98  Indices = ArrayView<const int>(eIndices, numEntries);
99  }
100 
101  void EpetraCrsGraph::getLocalRowView(int LocalRow, ArrayView<const int> &indices) const {
102  XPETRA_MONITOR("EpetraCrsGraph::getLocalRowView");
103 
104  int numEntries;
105  int * eIndices;
106 
107  XPETRA_ERR_CHECK(graph_->ExtractMyRowView(LocalRow, numEntries, eIndices));
108  if (numEntries == 0) { eIndices = NULL; } // Cf. TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 ) in Teuchos ArrayView constructor.
109 
110  indices = ArrayView<const int>(eIndices, numEntries);
111  }
112 
114  XPETRA_MONITOR("EpetraCrsGraph::fillComplete");
115 
116  graph_->FillComplete(toEpetra(domainMap), toEpetra(rangeMap));
117  bool doOptimizeStorage = true;
118  if (params != null && params->get("Optimize Storage",true) == false) doOptimizeStorage = false;
119  if (doOptimizeStorage) graph_->OptimizeStorage();
120  }
121 
123  XPETRA_MONITOR("EpetraCrsGraph::fillComplete");
124 
125  graph_->FillComplete();
126  bool doOptimizeStorage = true;
127  if (params != null && params->get("Optimize Storage",true) == false) doOptimizeStorage = false;
128  if (doOptimizeStorage) graph_->OptimizeStorage();
129  }
130 
131  std::string EpetraCrsGraph::description() const { XPETRA_MONITOR("EpetraCrsGraph::description"); return "NotImplemented"; }
132 
134  XPETRA_MONITOR("EpetraCrsGraph::describe");
135 
136  out << "EpetraCrsGraph::describe : Warning, verbosity level is ignored by this method." << std::endl;
137  const Epetra_BlockMap rowmap = graph_->RowMap();
138  if (rowmap.Comm().MyPID() == 0) out << "** EpetraCrsGraph **\n\nrowmap" << std::endl;
139  rowmap.Print(out);
140  graph_->Print(out);
141  }
142 
143  // TODO: move that elsewhere
144  RCP< const CrsGraph<int, int> > toXpetra(const Epetra_CrsGraph &g) {
145  RCP<const Epetra_CrsGraph> const_graph = rcp(new Epetra_CrsGraph(g));
146 
147  RCP<Epetra_CrsGraph> graph = Teuchos::rcp_const_cast<Epetra_CrsGraph>(const_graph); //TODO: can I avoid the const_cast ?
148  return rcp( new Xpetra::EpetraCrsGraph(graph) );
149  }
150  //
151 
152  // TODO: use toEpetra()
153  void EpetraCrsGraph::doImport(const DistObject<int, int, int> &source,
154  const Import<int, int> &importer, CombineMode CM) {
155  XPETRA_MONITOR("EpetraCrsGraph::doImport");
156 
157  XPETRA_DYNAMIC_CAST(const EpetraCrsGraph, source, tSource, "Xpetra::EpetraCrsGraph::doImport only accept Xpetra::EpetraCrsGraph as input arguments.");
158  XPETRA_DYNAMIC_CAST(const EpetraImport, importer, tImporter, "Xpetra::EpetraCrsGraph::doImport only accept Xpetra::EpetraImport as input arguments.");
159 
160  RCP<const Epetra_CrsGraph> v = tSource.getEpetra_CrsGraph();
161  int err = graph_->Import(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
162  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
163  }
164 
165  void EpetraCrsGraph::doExport(const DistObject<int, int, int> &dest,
166  const Import<int, int>& importer, CombineMode CM) {
167  XPETRA_MONITOR("EpetraCrsGraph::doExport");
168 
169  XPETRA_DYNAMIC_CAST(const EpetraCrsGraph, dest, tDest, "Xpetra::EpetraCrsGraph::doImport only accept Xpetra::EpetraCrsGraph as input arguments.");
170  XPETRA_DYNAMIC_CAST(const EpetraImport, importer, tImporter, "Xpetra::EpetraCrsGraph::doImport only accept Xpetra::EpetraImport as input arguments.");
171 
172  RCP<const Epetra_CrsGraph> v = tDest.getEpetra_CrsGraph();
173  int err = graph_->Export(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
174  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
175  }
176 
177  void EpetraCrsGraph::doImport(const DistObject<int, int, int> &source,
178  const Export<int, int>& exporter, CombineMode CM) {
179  XPETRA_MONITOR("EpetraCrsGraph::doImport");
180 
181  XPETRA_DYNAMIC_CAST(const EpetraCrsGraph, source, tSource, "Xpetra::EpetraCrsGraph::doImport only accept Xpetra::EpetraCrsGraph as input arguments.");
182  XPETRA_DYNAMIC_CAST(const EpetraExport, exporter, tExporter, "Xpetra::EpetraCrsGraph::doImport only accept Xpetra::EpetraImport as input arguments.");
183 
184  RCP<const Epetra_CrsGraph> v = tSource.getEpetra_CrsGraph();
185  int err = graph_->Import(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
186  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
187 
188  }
189 
190  void EpetraCrsGraph::doExport(const DistObject<int, int, int> &dest,
191  const Export<int, int>& exporter, CombineMode CM) {
192  XPETRA_MONITOR("EpetraCrsGraph::doExport");
193 
194  XPETRA_DYNAMIC_CAST(const EpetraCrsGraph, dest, tDest, "Xpetra::EpetraCrsGraph::doImport only accept Xpetra::EpetraCrsGraph as input arguments.");
195  XPETRA_DYNAMIC_CAST(const EpetraExport, exporter, tExporter, "Xpetra::EpetraCrsGraph::doImport only accept Xpetra::EpetraImport as input arguments.");
196 
197  RCP<const Epetra_CrsGraph> v = tDest.getEpetra_CrsGraph();
198  int err = graph_->Export(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
199  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
200  }
201 
202 } // namespace Xpetra
void insertGlobalIndices(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &indices)
Insert graph indices, using global IDs.
void getLocalRowView(LocalOrdinal LocalRow, ArrayView< const LocalOrdinal > &indices) const
Extract a const, non-persisting view of local indices in a specified row of the graph.
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void insertLocalIndices(LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &indices)
Insert graph indices, using local IDs.
size_type size() const
std::string description() const
Return a simple one-line description of this object.
void doImport(const DistObject< GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node > &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Import.
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.
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, int > > &graph)
RCP< Epetra_CrsGraph > graph_
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define XPETRA_ERR_CHECK(arg)
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.
T * getRawPtr() const
#define XPETRA_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
EpetraCrsGraph(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 XPETRA_RCP_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
void doExport(const DistObject< GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node > &dest, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Export.
CombineMode
Xpetra::Combine Mode enumerable type.
#define XPETRA_MONITOR(funcName)
RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > > toXpetra(RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > >)
Convert an Epetra_Comm.to a Teuchos_Comm.
void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView< const GlobalOrdinal > &Indices) const
Extract a const, non-persisting view of global indices in a specified row of the graph.