All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Xpetra_StridedMap.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 // 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 #ifndef XPETRA_STRIDEDMAP_HPP
50 #define XPETRA_STRIDEDMAP_HPP
51 
52 /* this file is automatically generated - do not edit (see script/interfaces.py) */
53 
54 #include <Kokkos_DefaultNode.hpp>
55 #include <Teuchos_Describable.hpp>
56 #include "Xpetra_Map.hpp"
57 #include "Xpetra_ConfigDefs.hpp"
58 
59 #include "Xpetra_Exceptions.hpp"
60 
61 // MPI helper
62 #define sumAll(rcpComm, in, out) \
63  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_SUM, in, Teuchos::outArg(out));
64 #define minAll(rcpComm, in, out) \
65  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MIN, in, Teuchos::outArg(out));
66 #define maxAll(rcpComm, in, out) \
67  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MAX, in, Teuchos::outArg(out));
68 
69 namespace Xpetra {
70 
99  template <class LocalOrdinal, class GlobalOrdinal = LocalOrdinal, class Node = Kokkos::DefaultNode::DefaultNodeType>
100  class StridedMap
101  : public virtual Map<LocalOrdinal, GlobalOrdinal, Node>
102  {
103 
104  public:
105 
107 
108 
109  // FIXME FIXME FIXME
110  // Do we need both offset and indexBase? If we do, we need to set up indexBase properly
111  StridedMap(global_size_t numGlobalElements, GlobalOrdinal indexBase, std::vector<size_t>& stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId, GlobalOrdinal offset = 0)
112  : stridingInfo_(stridingInfo), stridedBlockId_(stridedBlockId), offset_(offset)
113  {
114  indexBase_ = indexBase;
115 
116  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo.size() == 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: stridingInfo.size() = 0?");
117  //TEUCHOS_TEST_FOR_EXCEPTION(numGlobalElements % getFixedBlockSize() != 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: getFixedBlockSize is not an integer multiple of numGlobalElements.");
118 
119  }
120 
121  StridedMap(global_size_t numGlobalElements, size_t numLocalElements, GlobalOrdinal indexBase, std::vector<size_t>& stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId, GlobalOrdinal offset = 0)
122  : stridingInfo_(stridingInfo), stridedBlockId_(stridedBlockId), offset_(offset)
123  {
124  indexBase_ = indexBase;
125 
126  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo.size() == 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: stridingInfo.size() = 0?");
127  //TEUCHOS_TEST_FOR_EXCEPTION(numGlobalElements % getFixedBlockSize() != 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: getFixedBlockSize is not an integer multiple of numGlobalElements.");
128  //TEUCHOS_TEST_FOR_EXCEPTION(numLocalElements % getFixedBlockSize() != 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: getFixedBlockSize is not an integer multiple of numLocalElements.");
129 
130  }
131 
132  StridedMap(std::vector<size_t>& stridingInfo, GlobalOrdinal indexBase, LocalOrdinal stridedBlockId, GlobalOrdinal offset = 0)
133  : stridingInfo_(stridingInfo), stridedBlockId_(stridedBlockId), offset_(offset)
134  {
135  indexBase_ = indexBase;
136  }
137 
138  StridedMap(global_size_t numGlobalElements, const Teuchos::ArrayView< const GlobalOrdinal > &elementList, GlobalOrdinal indexBase, std::vector<size_t>& stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId=-1)
139  : stridingInfo_(stridingInfo), stridedBlockId_(stridedBlockId)
140  {
141  indexBase_ = indexBase;
142 
143  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo.size() == 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: stridingInfo.size() = 0?");
144  TEUCHOS_TEST_FOR_EXCEPTION(stridedBlockId < -1, Exceptions::RuntimeError, "StridedMap::StridedMap: stridedBlockId must not be smaller than -1.");
145 
146  // the following tests are not valid if stridedBlockId != 1
147  if (stridedBlockId == -1) {
148  TEUCHOS_TEST_FOR_EXCEPTION(numGlobalElements % getFixedBlockSize() != 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: getFixedBlockSize is not an integer multiple of numGlobalElements.");
149  TEUCHOS_TEST_FOR_EXCEPTION(elementList.size() % getFixedBlockSize() != 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: getFixedBlockSize is not an integer multiple of elementList.size().");
150  } else {
151  // numGlobalElements can be -1! FIXME
152  //TEUCHOS_TEST_FOR_EXCEPTION(numGlobalElements % stridingInfo[stridedBlockId] != 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: stridingBlockInfo[stridedBlockId] is not an integer multiple of numGlobalElements.");
153  TEUCHOS_TEST_FOR_EXCEPTION(elementList.size() % stridingInfo[stridedBlockId] != 0, Exceptions::RuntimeError, "StridedMap::StridedMap: stridingInfo not valid: stridingBlockInfo[stridedBlockId] is not an integer multiple of elementList.size().");
154  }
155 
156  // calculate offset_
157 
158  // find minimum GID over all procs
159  GlobalOrdinal minGidOnCurProc = 999999; // TODO use scalar traits for max possible gid.
160  for(Teuchos_Ordinal k=0; k<elementList.size(); ++k) { // TODO fix occurence of Teuchos_Ordinal
161  if(elementList[k] < minGidOnCurProc) minGidOnCurProc = elementList[k];
162  }
163  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MIN, minGidOnCurProc, Teuchos::outArg(offset_));
164 
165  // calculate striding index
166  size_t nStridedOffset = 0;
167  for(int j=0; j<stridedBlockId; j++) {
168  nStridedOffset += stridingInfo[j];
169  }
170  const GlobalOrdinal goStridedOffset = Teuchos::as<GlobalOrdinal>(nStridedOffset);
171 
172  // adapt offset_
173  offset_ -= goStridedOffset - indexBase_; // TODO check me
174  }
175 
177  virtual ~StridedMap() { }
178 
180 
182 
183 
184  std::vector<size_t> getStridingData() const { return stridingInfo_; }
185 
186  void setStridingData(std::vector<size_t> stridingInfo) { stridingInfo_ = stridingInfo; }
187 
188  size_t getFixedBlockSize() const {
189  size_t blkSize = 0;
190  for (std::vector<size_t>::const_iterator it = stridingInfo_.begin(); it != stridingInfo_.end(); ++it)
191  blkSize += *it;
192  return blkSize;
193  }
194 
197  LocalOrdinal getStridedBlockId() const { return stridedBlockId_; }
198 
200  bool isStrided() { return stridingInfo_.size() > 1 ? true : false; }
201 
204  bool isBlocked() { return getFixedBlockSize() > 1 ? true : false; }
205 
206  GlobalOrdinal getOffset() const { return offset_; }
207 
208  void setOffset(GlobalOrdinal offset) { offset_ = offset; }
209 
210  // returns number of strided block id which gid belongs to.
211  size_t GID2StridingBlockId( GlobalOrdinal gid ) const {
212  GlobalOrdinal tgid = gid - offset_;
213  tgid = tgid % getFixedBlockSize();
214 
215  size_t nStridedOffset = 0;
216  size_t stridedBlockId = 0;
217  for(size_t j=0; j<stridingInfo_.size(); j++) {
218  nStridedOffset += stridingInfo_[j];
219  if(Teuchos::as<size_t>(tgid) < nStridedOffset) {
220  stridedBlockId = j;
221  break;
222  }
223  }
224  return stridedBlockId;
225  }
226 
227  /* // function currently not needed but maybe useful
228  std::vector<GlobalOrdinal> NodeId2GlobalDofIds(GlobalOrdinal nodeId) const {
229  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo_.size() == 0, Exceptions::RuntimeError, "StridedMap::NodeId2GlobalDofIds: stridingInfo not valid: stridingInfo.size() = 0?");
230  std::vector<GlobalOrdinal> dofs;
231  if(stridedBlockId_ > -1) {
232  TEUCHOS_TEST_FOR_EXCEPTION(stridingInfo_[stridedBlockId_] == 0, Exceptions::RuntimeError, "StridedMap::NodeId2GlobalDofIds: stridingInfo not valid: stridingInfo[stridedBlockId] = 0?");
233 
234  // determine nStridedOffset
235  size_t nStridedOffset = 0;
236  for(int j=0; j<stridedBlockId_; j++) {
237  nStridedOffset += stridingInfo_[j];
238  }
239 
240  for(size_t i = 0; i<stridingInfo_[stridedBlockId_]; i++) {
241  GlobalOrdinal gid =
242  nodeId * Teuchos::as<GlobalOrdinal>(getFixedBlockSize()) +
243  offset_ +
244  Teuchos::as<GlobalOrdinal>(nStridedOffset) +
245  Teuchos::as<GlobalOrdinal>(i);
246  dofs.push_back(gid);
247  }
248  } else {
249  for(size_t i = 0; i<getFixedBlockSize(); i++) {
250  GlobalOrdinal gid =
251  nodeId * Teuchos::as<GlobalOrdinal>(getFixedBlockSize()) +
252  offset_ +
253  Teuchos::as<GlobalOrdinal>(i);
254  dofs.push_back(gid);
255  }
256  }
257  return dofs;
258  }*/
260 
261  private:
262  virtual bool CheckConsistency() = 0;
263 
264  protected:
265  std::vector<size_t> stridingInfo_;
266  LocalOrdinal stridedBlockId_;
267  // stridedBlock == -1: the full map (with all strided block dofs)
268  // stridedBlock > -1: only dofs of strided block with index "stridedBlockId" are stored in this map
269  GlobalOrdinal offset_;
270  GlobalOrdinal indexBase_;
271 
272  }; // StridedMap class
273 
274 } // Xpetra namespace
275 
276 #define XPETRA_STRIDEDMAP_SHORT
277 #endif // XPETRA_STRIDEDMAP_HPP
LocalOrdinal getStridedBlockId() const
size_t GID2StridingBlockId(GlobalOrdinal gid) const
StridedMap(std::vector< size_t > &stridingInfo, GlobalOrdinal indexBase, LocalOrdinal stridedBlockId, GlobalOrdinal offset=0)
void setStridingData(std::vector< size_t > stridingInfo)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
GlobalOrdinal indexBase_
index base for the strided map (default = 0)
size_type size() const
GlobalOrdinal getOffset() const
Exception throws to report errors in the internal logical of the program.
LocalOrdinal stridedBlockId_
member variable denoting which dofs are stored in map
std::vector< size_t > stridingInfo_
vector with size of strided blocks (dofs)
virtual ~StridedMap()
Destructor.
size_t getFixedBlockSize() const
StridedMap(global_size_t numGlobalElements, GlobalOrdinal indexBase, std::vector< size_t > &stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId, GlobalOrdinal offset=0)
StridedMap(global_size_t numGlobalElements, size_t numLocalElements, GlobalOrdinal indexBase, std::vector< size_t > &stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId, GlobalOrdinal offset=0)
GlobalOrdinal offset_
offset for gids in map (default = 0)
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
std::vector< size_t > getStridingData() const
size_t global_size_t
Global size_t object.
virtual bool CheckConsistency()=0
StridedMap(global_size_t numGlobalElements, const Teuchos::ArrayView< const GlobalOrdinal > &elementList, GlobalOrdinal indexBase, std::vector< size_t > &stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId=-1)
bool isStrided()
returns true, if this is a strided map (i.e. more than 1 strided blocks)
void setOffset(GlobalOrdinal offset)