Xpetra_EpetraMap.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
46 #include "Xpetra_ConfigDefs.hpp"
47 
48 #ifdef HAVE_XPETRA_EPETRA
49 
50 #include "Xpetra_EpetraMap.hpp"
51 #include "Xpetra_EpetraUtils.hpp"
52 
54 
55 namespace Xpetra {
56 
57  // Implementation note for constructors: the Epetra_Comm is cloned in the constructor of Epetra_BlockMap. We don't need to keep a reference on it.
58  // TODO: use toEpetra() function here.
59  EpetraMap::EpetraMap(global_size_t numGlobalElements, int indexBase, const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
60  LocalGlobal lg, const Teuchos::RCP<Node> &node)
61  {
62  // This test come from Tpetra (Epetra doesn't check if numGlobalElements,indexBase are equivalent across images).
63  // In addition, for the test TEST_THROW(M map((myImageID == 0 ? GSTI : 0),0,comm), std::invalid_argument), only one node throw an exception and there is a dead lock.
64  std::string errPrefix;
65  errPrefix = Teuchos::typeName(*this) + "::constructor(numGlobal,indexBase,comm,lOrG): ";
66 
67  if (lg == GloballyDistributed) {
68  const int myImageID = comm->getRank();
69 
70  // check that numGlobalElements,indexBase is equivalent across images
71  global_size_t rootNGE = numGlobalElements;
72  int rootIB = indexBase;
73  Teuchos::broadcast<int,global_size_t>(*comm,0,&rootNGE);
74  Teuchos::broadcast<int,int>(*comm,0,&rootIB);
75  int localChecks[2], globalChecks[2];
76  localChecks[0] = -1; // fail or pass
77  localChecks[1] = 0; // fail reason
78  if (numGlobalElements != rootNGE) {
79  localChecks[0] = myImageID;
80  localChecks[1] = 1;
81  }
82  else if (indexBase != rootIB) {
83  localChecks[0] = myImageID;
84  localChecks[1] = 2;
85  }
86  // REDUCE_MAX will give us the image ID of the highest rank proc that DID NOT pass, as well as the reason
87  // these will be -1 and 0 if all procs passed
88  Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_MAX,2,localChecks,globalChecks);
89  if (globalChecks[0] != -1) {
90  if (globalChecks[1] == 1) {
91  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
92  errPrefix << "numGlobal must be the same on all nodes (examine node " << globalChecks[0] << ").");
93  }
94  else if (globalChecks[1] == 2) {
95  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
96  errPrefix << "indexBase must be the same on all nodes (examine node " << globalChecks[0] << ").");
97  }
98  else {
99  // logic error on our part
100  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
101  errPrefix << "logic error. Please contact the Tpetra team.");
102  }
103  }
104  }
105 
106  // Note: validity of numGlobalElements checked by Epetra.
107 
108  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(static_cast<int>(numGlobalElements), 1, indexBase, *toEpetra(comm))))));
109  }
110 
111  EpetraMap::EpetraMap(global_size_t numGlobalElements, size_t numLocalElements, int indexBase,
112  const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node)
113  {
114  // This test come from Tpetra
115  using Teuchos::outArg;
116 
117  const size_t L0 = Teuchos::OrdinalTraits<size_t>::zero();
118  const size_t L1 = Teuchos::OrdinalTraits<size_t>::one();
119  const global_size_t GST0 = Teuchos::OrdinalTraits<global_size_t>::zero();
120  const global_size_t GST1 = Teuchos::OrdinalTraits<global_size_t>::one();
121  const global_size_t GSTI = Teuchos::OrdinalTraits<global_size_t>::invalid();
122 
123  std::string errPrefix;
124  errPrefix = Teuchos::typeName(*this) + "::constructor(numGlobal,numLocal,indexBase,platform): ";
125 
126  // get a internodal communicator from the Platform
127  const int myImageID = comm->getRank();
128 
129  global_size_t global_sum;
130  { // begin scoping block
131  // for communicating failures
132  int localChecks[2], globalChecks[2];
133  /* compute the global size
134  we are computing the number of global elements because exactly ONE of the following is true:
135  - the user didn't specify it, and we need it
136  - the user did specify it, but we need to
137  + validate it against the sum of the local sizes, and
138  + ensure that it is the same on all nodes
139  */
140  Teuchos::reduceAll<int,global_size_t>(*comm,Teuchos::REDUCE_SUM,
141  Teuchos::as<global_size_t>(numLocalElements),outArg(global_sum));
142  /* there are three errors we should be detecting:
143  - numGlobalElements != invalid() and it is incorrect/invalid
144  - numLocalElements invalid (<0)
145  */
146  localChecks[0] = -1;
147  localChecks[1] = 0;
148  if (numLocalElements < L1 && numLocalElements != L0) {
149  // invalid
150  localChecks[0] = myImageID;
151  localChecks[1] = 1;
152  }
153  else if (numGlobalElements < GST1 && numGlobalElements != GST0 && numGlobalElements != GSTI) {
154  // invalid
155  localChecks[0] = myImageID;
156  localChecks[1] = 2;
157  }
158  else if (numGlobalElements != GSTI && numGlobalElements != global_sum) {
159  // incorrect
160  localChecks[0] = myImageID;
161  localChecks[1] = 3;
162  }
163  // now check that indexBase is equivalent across images
164  int rootIB = indexBase;
165  Teuchos::broadcast<int,int>(*comm,0,&rootIB); // broadcast one ordinal from node 0
166  if (indexBase != rootIB) {
167  localChecks[0] = myImageID;
168  localChecks[1] = 4;
169  }
170  // REDUCE_MAX will give us the image ID of the highest rank proc that DID NOT pass
171  // this will be -1 if all procs passed
172  Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_MAX,2,localChecks,globalChecks);
173  if (globalChecks[0] != -1) {
174  if (globalChecks[1] == 1) {
175  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
176  errPrefix << "numLocal is not valid on at least one node (possibly node "
177  << globalChecks[0] << ").");
178  }
179  else if (globalChecks[1] == 2) {
180  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
181  errPrefix << "numGlobal is not valid on at least one node (possibly node "
182  << globalChecks[0] << ").");
183  }
184  else if (globalChecks[1] == 3) {
185  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
186  errPrefix << "numGlobal doesn't match sum of numLocal (== "
187  << global_sum << ") on at least one node (possibly node "
188  << globalChecks[0] << ").");
189  }
190  else if (globalChecks[1] == 4) {
191  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
192  errPrefix << "indexBase is not the same on all nodes (examine node "
193  << globalChecks[0] << ").");
194  }
195  else {
196  // logic error on my part
197  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
198  errPrefix << "logic error. Please contact the Tpetra team.");
199  }
200  }
201 
202  }
203 
204  // set numGlobalElements
205  if (numGlobalElements == GSTI) {
206  numGlobalElements = global_sum;}
207 
208  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(static_cast<int>(numGlobalElements), numLocalElements, 1, indexBase, *toEpetra(comm))))));
209  }
210 
211  // TODO: UnitTest FAILED
212  EpetraMap::EpetraMap(global_size_t numGlobalElements, const Teuchos::ArrayView<const int> &elementList, int indexBase,
213  const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node)
214  {
215  if (numGlobalElements == Teuchos::OrdinalTraits<global_size_t>::invalid()) {
216  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(-1, elementList.size(), elementList.getRawPtr(), 1, indexBase, *toEpetra(comm))))));
217  } else {
218  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(numGlobalElements, elementList.size(), elementList.getRawPtr(), 1, indexBase, *toEpetra(comm))))));
219  }
220  }
221 
222 
223 
224 
225 
226 
227 
228  LookupStatus EpetraMap::getRemoteIndexList(const Teuchos::ArrayView< const GlobalOrdinal > &GIDList, const Teuchos::ArrayView< int > &nodeIDList, const Teuchos::ArrayView< int > &LIDList) const { XPETRA_MONITOR("EpetraMap::getRemoteIndexList"); return toXpetra(map_->RemoteIDList(GIDList.size(), GIDList.getRawPtr(), nodeIDList.getRawPtr(), LIDList.getRawPtr())); }
229 
230  LookupStatus EpetraMap::getRemoteIndexList(const Teuchos::ArrayView< const int > &GIDList, const Teuchos::ArrayView< int > &nodeIDList) const { XPETRA_MONITOR("EpetraMap::getRemoteIndexList"); return toXpetra(map_->RemoteIDList(GIDList.size(), GIDList.getRawPtr(), nodeIDList.getRawPtr(), 0)); }
231 
232  Teuchos::ArrayView< const int > EpetraMap::getNodeElementList() const { XPETRA_MONITOR("EpetraMap::getNodeElementList"); return ArrayView< const int >(map_->MyGlobalElements(), map_->NumMyElements()); /* Note: this method return a const array, so it is safe to use directly the internal array. */ }
233 
234  //typedef Kokkos::DefaultNode::DefaultNodeType Node;
235  const Teuchos::RCP<Kokkos::DefaultNode::DefaultNodeType> EpetraMap::getNode() const { XPETRA_MONITOR("EpetraMap::getNode"); return Kokkos::DefaultNode::getDefaultNode(); } //removed &
236 
237 
238 
239 
240 
241 
242  std::string EpetraMap::description() const {
243  XPETRA_MONITOR("EpetraMap::description");
244 
245  // This implementation come from Tpetra_Map_def.hpp (without modification)
246  std::ostringstream oss;
247  oss << Teuchos::Describable::description();
248  oss << "{getGlobalNumElements() = " << getGlobalNumElements()
249  << ", getNodeNumElements() = " << getNodeNumElements()
250  << ", isContiguous() = " << isContiguous()
251  << ", isDistributed() = " << isDistributed()
252  << "}";
253  return oss.str();
254  }
255 
256  void EpetraMap::describe( Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const {
257  XPETRA_MONITOR("EpetraMap::describe");
258 
259  const Teuchos::RCP<const Teuchos::Comm<int> > comm_ = getComm();
260 
261  // This implementation come from Tpetra_Map_def.hpp (without modification)
262  using std::endl;
263  using std::setw;
264  using Teuchos::VERB_DEFAULT;
265  using Teuchos::VERB_NONE;
266  using Teuchos::VERB_LOW;
267  using Teuchos::VERB_MEDIUM;
268  using Teuchos::VERB_HIGH;
269  using Teuchos::VERB_EXTREME;
270 
271  const size_t nME = getNodeNumElements();
272  Teuchos::ArrayView<const int> myEntries = getNodeElementList();
273  int myImageID = comm_->getRank();
274  int numImages = comm_->getSize();
275 
276  Teuchos::EVerbosityLevel vl = verbLevel;
277  if (vl == VERB_DEFAULT) vl = VERB_LOW;
278 
279  size_t width = 1;
280  for (size_t dec=10; dec<getGlobalNumElements(); dec *= 10) {
281  ++width;
282  }
283  width = std::max<size_t>(width,12) + 2;
284 
285  Teuchos::OSTab tab(out);
286 
287  if (vl == VERB_NONE) {
288  // do nothing
289  }
290  else if (vl == VERB_LOW) {
291  out << this->description() << endl;
292  }
293  else { // MEDIUM, HIGH or EXTREME
294  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
295  if (myImageID == imageCtr) {
296  if (myImageID == 0) { // this is the root node (only output this info once)
297  out << endl
298  << "Number of Global Entries = " << getGlobalNumElements() << endl
299  << "Maximum of all GIDs = " << getMaxAllGlobalIndex() << endl
300  << "Minimum of all GIDs = " << getMinAllGlobalIndex() << endl
301  << "Index Base = " << getIndexBase() << endl;
302  }
303  out << endl;
304  if (vl == VERB_HIGH || vl == VERB_EXTREME) {
305  out << "Number of Local Elements = " << nME << endl
306  << "Maximum of my GIDs = " << getMaxGlobalIndex() << endl
307  << "Minimum of my GIDs = " << getMinGlobalIndex() << endl;
308  out << endl;
309  }
310  if (vl == VERB_EXTREME) {
311  out << std::setw(width) << "Node ID"
312  << std::setw(width) << "Local Index"
313  << std::setw(width) << "Global Index"
314  << endl;
315  for (size_t i=0; i < nME; i++) {
316  out << std::setw(width) << myImageID
317  << std::setw(width) << i
318  << std::setw(width) << myEntries[i]
319  << endl;
320  }
321  out << std::flush;
322  }
323  }
324  // Do a few global ops to give I/O a chance to complete
325  comm_->barrier();
326  comm_->barrier();
327  comm_->barrier();
328  }
329  }
330  }
331 
332 
333 
334 
335 
336  const Epetra_Map & toEpetra(const Map<int,int> &map) {
337  // TODO: throw exception
338  const EpetraMap & epetraMap = dynamic_cast<const EpetraMap &>(map);
339  return epetraMap.getEpetra_Map();
340  }
341 
342  const Epetra_Map & toEpetra(const RCP< const Map<int, int> > &map) {
343  XPETRA_RCP_DYNAMIC_CAST(const EpetraMap, map, epetraMap, "toEpetra");
344  return epetraMap->getEpetra_Map();
345  }
346 
347 // const RCP< const Map<int, int> > toXpetra(const RCP< const Epetra_Map > &map) {
348 // return rcp( new EpetraMap(map) );
349 // }
350 
351  const RCP< const Map<int, int> > toXpetra(const Epetra_BlockMap &map) {
352  RCP<const Epetra_BlockMap> m = rcp(new Epetra_BlockMap(map));
353  return rcp( new EpetraMap(m) );
354  }
355  //
356 
357 }
358 
359 #endif
std::string description() const
Return a simple one-line description of this object.
RCP< const CrsGraph< int, int > > toXpetra(const Epetra_CrsGraph &g)
bool isContiguous() const
True if this Map is distributed contiguously, else false.
#define IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG(sourceCode)
LookupStatus getRemoteIndexList(const Teuchos::ArrayView< const GlobalOrdinal > &GIDList, const Teuchos::ArrayView< int > &nodeIDList, const Teuchos::ArrayView< LocalOrdinal > &LIDList) const
Return the process IDs and corresponding local IDs for the given global IDs.
GlobalOrdinal getMaxAllGlobalIndex() const
The maximum global index over all processes in the communicator.
const Teuchos::RCP< Node > getNode() const
Get this Map&#39;s Node object.
Xpetra namespace
static Teuchos::RCP< DefaultNodeType > getDefaultNode()
#define XPETRA_RCP_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
Teuchos::ArrayView< const GlobalOrdinal > getNodeElementList() const
Return a view of the global indices owned by this node.
RCP< const Epetra_BlockMap > map_
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, int > > &graph)
size_t getNodeNumElements() const
The number of elements belonging to the calling node.
GlobalOrdinal getMinGlobalIndex() const
The minimum global index owned by the calling process.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print this object with the given verbosity level to the given FancyOStream.
EpetraMap(global_size_t numGlobalElements, GlobalOrdinal indexBase, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalGlobal lg=GloballyDistributed, const Teuchos::RCP< Node > &node=Kokkos::DefaultNode::getDefaultNode())
Constructor with Tpetra-defined contiguous uniform distribution.
size_t global_size_t
Global size_t object.
const Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Get this Map&#39;s Comm object.
bool isDistributed() const
Whether this Map is globally distributed or locally replicated.
global_size_t getGlobalNumElements() const
The number of elements in this Map.
#define XPETRA_MONITOR(funcName)
GlobalOrdinal getMaxGlobalIndex() const
The maximum global index owned by the calling process.
GlobalOrdinal getMinAllGlobalIndex() const
The minimum global index over all processes in the communicator.
GlobalOrdinal getIndexBase() const
The index base for this Map.
const Epetra_Map & getEpetra_Map() const