All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Xpetra_StridedEpetraMap.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 
47 // WARNING: This code is experimental. Backwards compatibility should not be expected.
48 
49 #include "Xpetra_ConfigDefs.hpp"
50 
51 #ifdef HAVE_XPETRA_EPETRA
52 
54 #include "Xpetra_EpetraUtils.hpp"
55 
57 
58 // MPI helper
59 #define sumAll(rcpComm, in, out) \
60  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_SUM, in, Teuchos::outArg(out));
61 #define minAll(rcpComm, in, out) \
62  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MIN, in, Teuchos::outArg(out));
63 #define maxAll(rcpComm, in, out) \
64  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MAX, in, Teuchos::outArg(out));
65 
66 namespace Xpetra {
67 
68  // 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.
69  // TODO: use toEpetra() function here.
70  StridedEpetraMap::StridedEpetraMap(global_size_t numGlobalElements, int indexBase, std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
71  LocalOrdinal stridedBlockId, GlobalOrdinal offset, LocalGlobal lg, const Teuchos::RCP<Node> &node)
72  : EpetraMap(numGlobalElements, indexBase, comm, lg, node), StridedMap<int, int>(numGlobalElements, indexBase, stridingInfo, comm, stridedBlockId, offset)
73  {
74  // check input data and reorganize map
76  if(numGlobalElements != Teuchos::OrdinalTraits<global_size_t>::invalid())
77  numGlobalNodes = numGlobalElements / getFixedBlockSize(); // number of nodes (over all processors)
78 
79  // build an equally distributed node map
80  RCP<Epetra_Map> nodeMap = Teuchos::null;
81  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((nodeMap = (rcp(new Epetra_Map(static_cast<int>(numGlobalNodes), indexBase, *toEpetra(comm))))));
82 
83  // translate local node ids to local dofs
84  int nStridedOffset = 0;
85  int nDofsPerNode = Teuchos::as<int>(getFixedBlockSize()); // dofs per node for local striding block
86  if(stridedBlockId > -1) {
87  // determine nStridedOffset
88  for(int j=0; j<stridedBlockId; j++) {
89  nStridedOffset += stridingInfo[j];
90  }
91  nDofsPerNode = stridingInfo[stridedBlockId];
92 
93  numGlobalElements = nodeMap->NumGlobalElements()*nDofsPerNode;
94  }
95  std::vector<int> dofgids;
96  for(int i = 0; i<nodeMap->NumMyElements(); i++) {
97  int gid = nodeMap->GID(i);
98  for(int dof = 0; dof < nDofsPerNode; ++dof) {
99  // dofs are calculated by
100  // global indexBase + offset + (node_GID - nodeIndexBase) * full size of strided map + striding offset of current striding block + dof id of current striding block
101  dofgids.push_back(indexBase + offset_ + (gid-indexBase)*getFixedBlockSize() + nStridedOffset + dof);
102  }
103  }
104 
105  if (numGlobalElements == Teuchos::OrdinalTraits<global_size_t>::invalid()) {
106  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(-1, dofgids.size(), &dofgids[0], 1, indexBase, *toEpetra(comm))))));
107  } else {
108  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(numGlobalElements, dofgids.size(), &dofgids[0], 1, indexBase, *toEpetra(comm))))));
109  }
110 
111  TEUCHOS_TEST_FOR_EXCEPTION(map_->NumMyPoints() % nDofsPerNode != 0, Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
112  if(stridedBlockId == -1) {
113  TEUCHOS_TEST_FOR_EXCEPTION(getNodeNumElements() != Teuchos::as<size_t>(nodeMap->NumMyElements()*nDofsPerNode), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
114  TEUCHOS_TEST_FOR_EXCEPTION(getGlobalNumElements() != Teuchos::as<size_t>(nodeMap->NumGlobalElements()*nDofsPerNode), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
115  } else {
116  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo.size() < Teuchos::as<size_t>(stridedBlockId), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: stridedBlockId > stridingInfo.size()");
117  int nDofsInStridedBlock = stridingInfo[stridedBlockId];
118  TEUCHOS_TEST_FOR_EXCEPTION(getNodeNumElements() != Teuchos::as<size_t>(nodeMap->NumMyElements()*nDofsInStridedBlock), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
119  TEUCHOS_TEST_FOR_EXCEPTION(getGlobalNumElements() != Teuchos::as<size_t>(nodeMap->NumGlobalElements()*nDofsInStridedBlock), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
120  }
121 
122  TEUCHOS_TEST_FOR_EXCEPTION(CheckConsistency() == false, Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: CheckConsistency() == false");
123  }
124 
125  StridedEpetraMap::StridedEpetraMap(global_size_t numGlobalElements, size_t numLocalElements, int indexBase,
126  std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, LocalOrdinal stridedBlockId, GlobalOrdinal offset, const Teuchos::RCP<Node> &node)
127  : EpetraMap(numGlobalElements, numLocalElements, indexBase, comm, node), StridedMap<int, int>(numGlobalElements, numLocalElements, indexBase, stridingInfo, comm, stridedBlockId, offset)
128  {
129  // check input data and reorganize map
130 
132  if(numGlobalElements != Teuchos::OrdinalTraits<global_size_t>::invalid())
133  numGlobalNodes = numGlobalElements / getFixedBlockSize(); // number of nodes (over all processors)
134  size_t blockSize = getFixedBlockSize();
135  //if(stridedBlockId > -1) {
136  // blockSize = stridingInfo[stridedBlockId];
137  //}
138  size_t numLocalNodes = numLocalElements / blockSize; // number of nodes (on each processor)
139 
140  // build an equally distributed node map
141  RCP<Epetra_Map> nodeMap = Teuchos::null;
142  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((nodeMap = (rcp(new Epetra_Map(static_cast<int>(numGlobalNodes), numLocalNodes, indexBase, *toEpetra(comm))))));
143 
144  // translate local node ids to local dofs
145  int nStridedOffset = 0;
146  int nDofsPerNode = Teuchos::as<int>(getFixedBlockSize()); // dofs per node for local striding block
147  if(stridedBlockId > -1) {
148  // determine nStridedOffset
149  for(int j=0; j<stridedBlockId; j++) {
150  nStridedOffset += stridingInfo[j];
151  }
152  nDofsPerNode = stridingInfo[stridedBlockId];
153 
154  numGlobalElements = nodeMap->NumGlobalElements()*nDofsPerNode;
155  }
156  std::vector<int> dofgids;
157  for(int i = 0; i<nodeMap->NumMyElements(); i++) {
158  int gid = nodeMap->GID(i);
159  for(int dof = 0; dof < nDofsPerNode; ++dof) {
160  // dofs are calculated by
161  // indexBase + global offset + (node_GID-nodeIndexBase) * full size of strided map + striding offset of current striding block + dof id of current striding block
162  dofgids.push_back(indexBase + offset_ + (gid-indexBase)*getFixedBlockSize() + nStridedOffset + dof);
163  }
164  }
165 
166  if (numGlobalElements == Teuchos::OrdinalTraits<global_size_t>::invalid()) {
167  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(-1, dofgids.size(), &dofgids[0], 1, indexBase, *toEpetra(comm))))));
168  } else {
169  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(numGlobalElements, dofgids.size(), &dofgids[0], 1, indexBase, *toEpetra(comm))))));
170  }
171 
172  TEUCHOS_TEST_FOR_EXCEPTION(map_->NumMyPoints() % nDofsPerNode != 0, Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
173  if(stridedBlockId == -1) {
174  TEUCHOS_TEST_FOR_EXCEPTION(getNodeNumElements() != Teuchos::as<size_t>(nodeMap->NumMyElements()*nDofsPerNode), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
175  TEUCHOS_TEST_FOR_EXCEPTION(getGlobalNumElements() != Teuchos::as<size_t>(nodeMap->NumGlobalElements()*nDofsPerNode), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
176  } else {
177  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo.size() < Teuchos::as<size_t>(stridedBlockId), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: stridedBlockId > stridingInfo.size()");
178  int nDofsInStridedBlock = stridingInfo[stridedBlockId];
179  TEUCHOS_TEST_FOR_EXCEPTION(getNodeNumElements() != Teuchos::as<size_t>(nodeMap->NumMyElements()*nDofsInStridedBlock), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
180  TEUCHOS_TEST_FOR_EXCEPTION(getGlobalNumElements() != Teuchos::as<size_t>(nodeMap->NumGlobalElements()*nDofsInStridedBlock), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
181  }
182 
183  TEUCHOS_TEST_FOR_EXCEPTION(CheckConsistency() == false, Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: CheckConsistency() == false");
184  }
185 
186 
187  StridedEpetraMap::StridedEpetraMap(global_size_t numGlobalElements, const Teuchos::ArrayView<const int> &elementList, int indexBase,
188  std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, LocalOrdinal stridedBlockId, const Teuchos::RCP<Node> &node)
189  : EpetraMap(numGlobalElements, elementList, indexBase, comm, node), Xpetra::StridedMap<int,int>(numGlobalElements, elementList, indexBase, stridingInfo, comm, stridedBlockId)
190  {
191  int nDofsPerNode = Teuchos::as<int>(getFixedBlockSize());
192  if(stridedBlockId != -1) {
193  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo.size() < Teuchos::as<size_t>(stridedBlockId), Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: stridedBlockId > stridingInfo.size()");
194  nDofsPerNode = Teuchos::as<int>(stridingInfo[stridedBlockId]);
195  }
196  TEUCHOS_TEST_FOR_EXCEPTION(map_->NumMyPoints() % nDofsPerNode != 0, Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: wrong distribution of dofs among processors.");
197 
198  // create EpetraMap using the dofs from ElementList
199  IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG((map_ = (rcp(new Epetra_BlockMap(numGlobalElements, elementList.size(), &elementList[0], 1, indexBase, *toEpetra(comm))))));
200 
201  // set parameters for striding information
202  TEUCHOS_TEST_FOR_EXCEPTION(CheckConsistency() == false, Exceptions::RuntimeError, "StridedEpetraMap::StridedEpetraMap: CheckConsistency() == false");
203  }
204 
205 
206  std::string StridedEpetraMap::description() const {
207  std::ostringstream oss;
208  oss << EpetraMap::description();
209  oss << "{getGlobalNumElements() = " << EpetraMap::getGlobalNumElements()
210  << ", getNodeNumElements() = " << EpetraMap::getNodeNumElements()
211  << ", isContiguous() = " << EpetraMap::isContiguous()
212  << ", isDistributed() = " << EpetraMap::isDistributed()
213  << "}";
214  return oss.str();
215  }
216 
218 
220 
221  // This implementation come from Tpetra_Map_def.hpp (without modification)
222  using std::endl;
223  using std::setw;
224  using Teuchos::VERB_DEFAULT;
225  using Teuchos::VERB_NONE;
226  using Teuchos::VERB_LOW;
227  using Teuchos::VERB_MEDIUM;
228  using Teuchos::VERB_HIGH;
229  using Teuchos::VERB_EXTREME;
230 
231  const size_t nME = EpetraMap::getNodeNumElements();
233  int myImageID = comm_->getRank();
234  int numImages = comm_->getSize();
235 
236  Teuchos::EVerbosityLevel vl = verbLevel;
237  if (vl == VERB_DEFAULT) vl = VERB_LOW;
238 
239  size_t width = 1;
240  for (size_t dec=10; dec<EpetraMap::getGlobalNumElements(); dec *= 10) {
241  ++width;
242  }
243  width = std::max<size_t>(width,12) + 2;
244 
245  Teuchos::OSTab tab(out);
246 
247  if (vl == VERB_NONE) {
248  // do nothing
249  }
250  else if (vl == VERB_LOW) {
251  out << this->description() << endl;
252  }
253  else { // MEDIUM, HIGH or EXTREME
254  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
255  if (myImageID == imageCtr) {
256  if (myImageID == 0) { // this is the root node (only output this info once)
257  out << endl
258  << "Number of Global Entries = " << EpetraMap::getGlobalNumElements() << endl
259  << "Maximum of all GIDs = " << EpetraMap::getMaxAllGlobalIndex() << endl
260  << "Minimum of all GIDs = " << EpetraMap::getMinAllGlobalIndex() << endl
261  << "Index Base = " << EpetraMap::getIndexBase() << endl;
262  }
263  out << endl;
264  if (vl == VERB_HIGH || vl == VERB_EXTREME) {
265  out << "Number of Local Elements = " << nME << endl
266  << "Maximum of my GIDs = " << EpetraMap::getMaxGlobalIndex() << endl
267  << "Minimum of my GIDs = " << EpetraMap::getMinGlobalIndex() << endl;
268  out << endl;
269  }
270  if (vl == VERB_EXTREME) {
271  out << std::setw(width) << "Node ID"
272  << std::setw(width) << "Local Index"
273  << std::setw(width) << "Global Index"
274  << endl;
275  for (size_t i=0; i < nME; i++) {
276  out << std::setw(width) << myImageID
277  << std::setw(width) << i
278  << std::setw(width) << myEntries[i]
279  << endl;
280  }
281  out << std::flush;
282  }
283  }
284  // Do a few global ops to give I/O a chance to complete
285  comm_->barrier();
286  comm_->barrier();
287  comm_->barrier();
288  }
289  }
290  }
291 
292 }
293 
294 #endif
std::string description() const
Return a simple one-line description of this object.
bool isContiguous() const
True if this Map is distributed contiguously, else false.
GlobalOrdinal getMaxAllGlobalIndex() const
The maximum global index over all processes in the communicator.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_type size() const
Exception throws to report errors in the internal logical of the program.
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
Returns the number of elements belonging to the calling node.
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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
global_size_t getGlobalNumElements() const
Returns the number of elements in this Map.
std::string description() const
Return a simple one-line description of this object.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to a FancyOStream object.
#define IF_EPETRA_EXCEPTION_THEN_THROW_GLOBAL_INVALID_ARG(sourceCode)
int offset_
offset for gids in map (default = 0)
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.
StridedEpetraMap(global_size_t numGlobalElements, GlobalOrdinal indexBase, std::vector< size_t > &stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset=0, LocalGlobal lg=GloballyDistributed, const Teuchos::RCP< Node > &node=Kokkos::DefaultNode::getDefaultNode())
Map constructor with Tpetra-defined contiguous uniform distribution. The elements are distributed amo...
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.
Class that stores a strided map.