All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Xpetra_MapUtils.hpp
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 // Tobias Wiesner (tawiesn@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef PACKAGES_XPETRA_SUP_MAP_UTILS_HPP_
48 #define PACKAGES_XPETRA_SUP_MAP_UTILS_HPP_
49 
50 #include "Xpetra_ConfigDefs.hpp"
51 
52 #include "Xpetra_Map.hpp"
53 #include "Xpetra_MapFactory.hpp"
54 
55 namespace Xpetra {
56 
57 #ifndef DOXYGEN_SHOULD_SKIP_THIS
58  // forward declaration of BlockedMap, needed to prevent circular inclusions
59  template<class LO, class GO, class N> class BlockedMap;
60 #endif
61 
69 template <class LocalOrdinal,
70  class GlobalOrdinal,
71  class Node>
72 class MapUtils {
73 #undef XPETRA_MAPUTILS_SHORT
75 
76 public:
77 
93 
94  // ToDo Resolve header issues to allow for using this routing in Xpetra::BlockedMap.
95 
96  // merge submaps to global map
97  std::vector<GlobalOrdinal> gids;
98  for(size_t tt = 0; tt<subMaps.size(); ++tt) {
100  Teuchos::ArrayView< const GlobalOrdinal > subMapGids = subMap->getNodeElementList();
101  gids.insert(gids.end(), subMapGids.begin(), subMapGids.end());
102  }
103 
104  const GlobalOrdinal INVALID = Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid();
105  //std::sort(gids.begin(), gids.end());
106  //gids.erase(std::unique(gids.begin(), gids.end()), gids.end());
107  Teuchos::ArrayView<GlobalOrdinal> gidsView(&gids[0], gids.size());
108  Teuchos::RCP<Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > fullMap = Xpetra::MapFactory<LocalOrdinal,GlobalOrdinal,Node>::Build(subMaps[0]->lib(), INVALID, gidsView, subMaps[0]->getIndexBase(), subMaps[0]->getComm());
109  return fullMap;
110  }
111 
130  TEUCHOS_TEST_FOR_EXCEPTION(nonOvlInput.getNodeNumElements() > input.getNodeNumElements(), Xpetra::Exceptions::Incompatible, "Xpetra::MatrixUtils::shrinkMapGIDs: the non-overlapping map must not have more local ids than the overlapping map.")
131  TEUCHOS_TEST_FOR_EXCEPTION(nonOvlInput.getMaxAllGlobalIndex() != input.getMaxAllGlobalIndex(), Xpetra::Exceptions::Incompatible, "Xpetra::MatrixUtils::shrinkMapGIDs: the maximum GIDs of the overlapping and non-overlapping maps must be the same.")
132 
133  RCP< const Teuchos::Comm<int> > comm = input.getComm();
134 
135  // we expect input to be the potentially overlapping map associated with nonOvlInput as the non-overlapping
136  // map with the same GIDs over all processors (e.g. column map and domain map). We use the nonOvlInput map
137  // to determine which GIDs are owned by which processor.
138 
139  // calculate offset for new global Ids
140  std::vector<int> myGIDs(comm->getSize(),0);
141  std::vector<int> numGIDs(comm->getSize(),0);
142  myGIDs[comm->getRank()] = nonOvlInput.getNodeNumElements();
143  Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,comm->getSize(),&myGIDs[0],&numGIDs[0]);
144  size_t gidOffset = 0;
145  for(int p = 0; p < comm->getRank(); p++) gidOffset += numGIDs[p];
146 
147  // we use nonOvlInput to assign the globally unique shrinked GIDs and communicate them to input.
148  std::map<const GlobalOrdinal, GlobalOrdinal> origGID2newGID;
149  for(size_t i = 0; i < nonOvlInput.getNodeNumElements(); i++) {
150  origGID2newGID[nonOvlInput.getGlobalElement(i)] = Teuchos::as<GlobalOrdinal>(i) + Teuchos::as<GlobalOrdinal>(gidOffset);
151  }
152  // build an overlapping version of mySpecialMap
153  Teuchos::Array<GlobalOrdinal> ovlUnknownStatusGids;
154  Teuchos::Array<GlobalOrdinal> ovlFoundStatusGids;
155  // loop over global column map of A and find all GIDs where it is not sure, whether they are special or not
156  for(size_t i = 0; i<input.getNodeNumElements(); i++) {
157  GlobalOrdinal gcid = input.getGlobalElement(i);
158  if( nonOvlInput.isNodeGlobalElement(gcid) == false) {
159  ovlUnknownStatusGids.push_back(gcid);
160  }
161  }
162 
163  // Communicate the number of DOFs on each processor
164  std::vector<int> myUnknownDofGIDs(comm->getSize(),0);
165  std::vector<int> numUnknownDofGIDs(comm->getSize(),0);
166  myUnknownDofGIDs[comm->getRank()] = ovlUnknownStatusGids.size();
167  Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,comm->getSize(),&myUnknownDofGIDs[0],&numUnknownDofGIDs[0]);
168 
169  // create array containing all DOF GIDs
170  size_t cntUnknownDofGIDs = 0;
171  for(int p = 0; p < comm->getSize(); p++) cntUnknownDofGIDs += numUnknownDofGIDs[p];
172  std::vector<GlobalOrdinal> lUnknownDofGIDs(cntUnknownDofGIDs,0); // local version to be filled
173  std::vector<GlobalOrdinal> gUnknownDofGIDs(cntUnknownDofGIDs,0); // global version after communication
174  // calculate the offset and fill chunk of memory with local data on each processor
175  size_t cntUnknownOffset = 0;
176  for(int p = 0; p < comm->getRank(); p++) cntUnknownOffset += numUnknownDofGIDs[p];
177  for(size_t k=0; k < Teuchos::as<size_t>(ovlUnknownStatusGids.size()); k++) {
178  lUnknownDofGIDs[k+cntUnknownOffset] = ovlUnknownStatusGids[k];
179  }
180  if(cntUnknownDofGIDs > 0) // only perform communication if there are unknown DOF GIDs
181  Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,Teuchos::as<int>(cntUnknownDofGIDs),&lUnknownDofGIDs[0],&gUnknownDofGIDs[0]);
182  std::vector<GlobalOrdinal> lTranslatedDofGIDs(cntUnknownDofGIDs,0); // local version to be filled
183  std::vector<GlobalOrdinal> gTranslatedDofGIDs(cntUnknownDofGIDs,0); // global version after communication
184  // loop through all GIDs with unknown status
185  for(size_t k=0; k < gUnknownDofGIDs.size(); k++) {
186  GlobalOrdinal curgid = gUnknownDofGIDs[k];
187  if(nonOvlInput.isNodeGlobalElement(curgid)) {
188  lTranslatedDofGIDs[k] = origGID2newGID[curgid]; // curgid is in special map (on this processor)
189  }
190  }
191  if(cntUnknownDofGIDs > 0) // only perform communication if there are unknown DOF GIDs
192  Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,Teuchos::as<int>(cntUnknownDofGIDs),&lTranslatedDofGIDs[0],&gTranslatedDofGIDs[0]);
193 
194  for(size_t k=0; k < Teuchos::as<size_t>(ovlUnknownStatusGids.size()); k++) {
195  origGID2newGID[ovlUnknownStatusGids[k]] = gTranslatedDofGIDs[k+cntUnknownOffset];
196  }
197  Teuchos::Array<GlobalOrdinal> ovlDomainMapArray;
198  for(size_t i = 0; i<input.getNodeNumElements(); i++) {
199  GlobalOrdinal gcid = input.getGlobalElement(i);
200  ovlDomainMapArray.push_back(origGID2newGID[gcid]);
201  }
204  (nonOvlInput.lib(),Teuchos::OrdinalTraits<GlobalOrdinal>::invalid(),ovlDomainMapArray(),0,comm);
205  return ovlDomainMap;
206  }
207 
227  const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>& nonOvlReferenceInput) {
228  //TEUCHOS_TEST_FOR_EXCEPTION(nonOvlInput.getNodeNumElements() > input.getNodeNumElements(), Xpetra::Exceptions::Incompatible, "Xpetra::MatrixUtils::transformThyra2XpetraGIDs: the non-overlapping map must not have more local ids than the overlapping map.");
229  TEUCHOS_TEST_FOR_EXCEPTION(nonOvlInput.getNodeNumElements() != nonOvlReferenceInput.getNodeNumElements(), Xpetra::Exceptions::Incompatible, "Xpetra::MatrixUtils::transformThyra2XpetraGIDs: the number of local Xpetra reference GIDs and local Thyra GIDs of the non-overlapping maps must be the same!");
230  //TEUCHOS_TEST_FOR_EXCEPTION(nonOvlInput.getMaxAllGlobalIndex() != input.getMaxAllGlobalIndex(), Xpetra::Exceptions::Incompatible, "Xpetra::MatrixUtils::transformThyra2XpetraGIDs: the maximum GIDs of the overlapping and non-overlapping maps must be the same. nonOvlInput.getMaxAllGlobalIndex() = " << nonOvlInput.getMaxAllGlobalIndex() << " ovlInput.getMaxAllGlobalIndex() = " << input.getMaxAllGlobalIndex());
231 
232  RCP< const Teuchos::Comm<int> > comm = input.getComm();
233 
234  // fill translation map as far as possible
235  std::map<const GlobalOrdinal, GlobalOrdinal> thyra2xpetraGID;
236  for(size_t i = 0; i < nonOvlInput.getNodeNumElements(); i++) {
237  thyra2xpetraGID[nonOvlInput.getGlobalElement(i)] =
238  nonOvlReferenceInput.getGlobalElement(i);
239  }
240 
241  // find all GIDs of the overlapping Thyra map which are not owned by this proc
242  Teuchos::Array<GlobalOrdinal> ovlUnknownStatusGids;
243  // loop over global column map of A and find all GIDs where it is not sure, whether they are special or not
244  for(size_t i = 0; i<input.getNodeNumElements(); i++) {
245  GlobalOrdinal gcid = input.getGlobalElement(i);
246  if( nonOvlInput.isNodeGlobalElement(gcid) == false) {
247  ovlUnknownStatusGids.push_back(gcid);
248  }
249  }
250 
251  // Communicate the number of DOFs on each processor
252  std::vector<int> myUnknownDofGIDs(comm->getSize(),0);
253  std::vector<int> numUnknownDofGIDs(comm->getSize(),0);
254  myUnknownDofGIDs[comm->getRank()] = ovlUnknownStatusGids.size();
255  Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,comm->getSize(),&myUnknownDofGIDs[0],&numUnknownDofGIDs[0]);
256 
257  // create array containing all DOF GIDs
258  size_t cntUnknownDofGIDs = 0;
259  for(int p = 0; p < comm->getSize(); p++) cntUnknownDofGIDs += numUnknownDofGIDs[p];
260  std::vector<GlobalOrdinal> lUnknownDofGIDs(cntUnknownDofGIDs,0); // local version to be filled
261  std::vector<GlobalOrdinal> gUnknownDofGIDs(cntUnknownDofGIDs,0); // global version after communication
262  // calculate the offset and fill chunk of memory with local data on each processor
263  size_t cntUnknownOffset = 0;
264  for(int p = 0; p < comm->getRank(); p++) cntUnknownOffset += numUnknownDofGIDs[p];
265  for(size_t k=0; k < Teuchos::as<size_t>(ovlUnknownStatusGids.size()); k++) {
266  lUnknownDofGIDs[k+cntUnknownOffset] = ovlUnknownStatusGids[k];
267  }
268  if(cntUnknownDofGIDs > 0) // only perform communication if there are unknown DOF GIDs
269  Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,Teuchos::as<int>(cntUnknownDofGIDs),&lUnknownDofGIDs[0],&gUnknownDofGIDs[0]);
270  std::vector<GlobalOrdinal> lTranslatedDofGIDs(cntUnknownDofGIDs,0); // local version to be filled
271  std::vector<GlobalOrdinal> gTranslatedDofGIDs(cntUnknownDofGIDs,0); // global version after communication
272  // loop through all GIDs with unknown status
273  for(size_t k=0; k < gUnknownDofGIDs.size(); k++) {
274  GlobalOrdinal curgid = gUnknownDofGIDs[k];
275  if(nonOvlInput.isNodeGlobalElement(curgid)) {
276  lTranslatedDofGIDs[k] = thyra2xpetraGID[curgid];
277  }
278  }
279  if(cntUnknownDofGIDs > 0) // only perform communication if there are unknown DOF GIDs
280  Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,Teuchos::as<int>(cntUnknownDofGIDs),&lTranslatedDofGIDs[0],&gTranslatedDofGIDs[0]);
281 
282  for(size_t k=0; k < Teuchos::as<size_t>(ovlUnknownStatusGids.size()); k++) {
283  thyra2xpetraGID[ovlUnknownStatusGids[k]] = gTranslatedDofGIDs[k+cntUnknownOffset];
284  }
285  Teuchos::Array<GlobalOrdinal> ovlDomainMapArray;
286  for(size_t i = 0; i<input.getNodeNumElements(); i++) {
287  GlobalOrdinal gcid = input.getGlobalElement(i);
288  ovlDomainMapArray.push_back(thyra2xpetraGID[gcid]);
289  }
292  (nonOvlInput.lib(),Teuchos::OrdinalTraits<GlobalOrdinal>::invalid(),ovlDomainMapArray(),0,comm);
293 
294  TEUCHOS_TEST_FOR_EXCEPTION(input.getNodeNumElements() != ovlDomainMap->getNodeNumElements(), Xpetra::Exceptions::Incompatible, "Xpetra::MatrixUtils::transformThyra2XpetraGIDs: the number of local Thyra reference GIDs (overlapping) and local Xpetra GIDs (overlapping) must be the same!");
295  //TEUCHOS_TEST_FOR_EXCEPTION(nonOvlReferenceInput.getMaxAllGlobalIndex() != ovlDomainMap->getMaxAllGlobalIndex(), Xpetra::Exceptions::Incompatible, "Xpetra::MatrixUtils::transformThyra2XpetraGIDs: the maximum GIDs of the overlapping and non-overlapping Xpetra maps must be the same.");
296 
297  return ovlDomainMap;
298  }
299 
307  const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>& input, GlobalOrdinal offset) {
308 
310  RCP< const Teuchos::Comm<int> > comm = input.getComm();
311 
312  RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > rcpInput = Teuchos::rcpFromRef(input);
313 
314  // check whether input map is a BlockedMap or a standard Map
316  if(rcpBlockedInput.is_null() == true) {
317 
318  // create a new map with Xpetra GIDs (may start not from GID = 0)
319  std::vector<GlobalOrdinal> gids;
320  for(LocalOrdinal l = 0; l < Teuchos::as<LocalOrdinal>(rcpInput->getNodeNumElements()); ++l) {
321  GlobalOrdinal gid = rcpInput->getGlobalElement(l) + offset;
322  gids.push_back(gid);
323  }
324  Teuchos::ArrayView<GO> gidsView(&gids[0], gids.size());
325  RCP<Map> fullMap = MapFactory::Build(rcpInput->lib(), INVALID, gidsView, rcpInput->getIndexBase(), comm);
326  return fullMap;
327  }
328 
329  // SPECIAL CASE: input is a blocked map
330  // we have to recursively call this routine to get a BlockedMap containing (unique) Xpetra style GIDs
331 
332  size_t numMaps = rcpBlockedInput->getNumMaps();
333 
334  // first calucale GID offsets in submaps
335  // we need that for generating Xpetra GIDs
336  std::vector<GlobalOrdinal> gidOffsets(numMaps,0);
337  for(size_t i = 1; i < numMaps; ++i) {
338  gidOffsets[i] = rcpBlockedInput->getMap(i-1,true)->getMaxAllGlobalIndex() + gidOffsets[i-1] + 1;
339  }
340 
341  std::vector<RCP<const Map> > mapsXpetra(rcpBlockedInput->getNumMaps(), Teuchos::null);
342  std::vector<RCP<const Map> > mapsThyra (rcpBlockedInput->getNumMaps(), Teuchos::null);
343  for (size_t b = 0; b < rcpBlockedInput->getNumMaps(); ++b){
344  // extract sub map with Thyra style gids
345  // this can be an underlying Map or BlockedMap object
346  RCP<const Map> subMapThyra = rcpBlockedInput->getMap(b,true);
347  RCP<const Map> subMapXpetra = MapUtils::transformThyra2XpetraGIDs(*subMapThyra, gidOffsets[b] + offset); // recursive call
348  mapsXpetra[b] = subMapXpetra; // map can be of type Map or BlockedMap
349  mapsThyra[b] = subMapThyra; // map can be of type Map or BlockedMap
350  }
351 
353  return resultMap;
354  }
355 
356 };
357 
358 } // end namespace Xpetra
359 
360 #define XPETRA_MAPUTILS_SHORT
361 
362 #endif // PACKAGES_XPETRA_SUP_MAP_UTILS_HPP_
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const =0
Get this Map&#39;s Comm object.
GlobalOrdinal GO
iterator begin() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual size_t getNodeNumElements() const =0
The number of elements belonging to the calling process.
static Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > concatenateMaps(const std::vector< Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > > &subMaps)
Helper function to concatenate several maps.
virtual bool isNodeGlobalElement(GlobalOrdinal globalIndex) const =0
Whether the given global index is valid for this Map on this process.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static Teuchos::RCP< Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > transformThyra2XpetraGIDs(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &input, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &nonOvlInput, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &nonOvlReferenceInput)
replace set of global ids by new global ids
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
virtual GlobalOrdinal getMaxAllGlobalIndex() const =0
The maximum global index over all processes in the communicator.
virtual GlobalOrdinal getGlobalElement(LocalOrdinal localIndex) const =0
The global index corresponding to the given local index.
static Teuchos::RCP< Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > shrinkMapGIDs(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &input, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &nonOvlInput)
Helper function to shrink the GIDs and generate a standard map whith GIDs starting at 0...
void push_back(const value_type &x)
iterator end() const
size_type size() const
Exception throws to report incompatible objects (like maps).
static Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > transformThyra2XpetraGIDs(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &input, GlobalOrdinal offset)
replace set of global ids by new global ids
static Teuchos::RCP< Map< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalGlobal lg=Xpetra::GloballyDistributed)
Map constructor with Xpetra-defined contiguous uniform distribution.
virtual UnderlyingLib lib() const =0
Get the library used by this object (Tpetra or Epetra?)
bool is_null() const