42 #ifndef TPETRA_BLOCKMAP_DEF_HPP 43 #define TPETRA_BLOCKMAP_DEF_HPP 45 #include "Tpetra_ConfigDefs.hpp" 46 #include "Tpetra_Distributor.hpp" 48 #ifdef DOXYGEN_USE_ONLY 54 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
57 LocalOrdinal blockSize,
58 GlobalOrdinal indexBase,
62 globalNumBlocks_(numGlobalBlocks),
64 pbuf_firstPointInBlock_(),
65 view_firstPointInBlock_(),
66 blockIDsAreContiguous_(true),
67 constantBlockSize_(blockSize)
70 "Tpetra::BlockMap::BlockMap ERROR: blockSize must be greater than 0.");
78 size_t numLocalPoints = numGlobalBlocks/comm->getSize();
79 int remainder = numGlobalBlocks%comm->getSize();
80 int localProc = comm->getRank();
81 if (localProc < remainder) ++numLocalPoints;
82 numLocalPoints *= blockSize;
87 size_t numLocalBlocks = pointMap_->getNodeNumElements()/blockSize;
88 size_t checkLocalBlocks = numLocalPoints/blockSize;
91 "Tpetra::BlockMap::BlockMap ERROR: internal failure, numLocalBlocks not consistent with point-map.");
93 myGlobalBlockIDs_.
resize(numLocalBlocks);
94 pbuf_firstPointInBlock_ = node->template allocBuffer<LocalOrdinal>(numLocalBlocks+1);
95 Teuchos::ArrayRCP<LocalOrdinal> v_firstPoints = node->template viewBufferNonConst<LocalOrdinal>(Kokkos::WriteOnly, numLocalBlocks+1, pbuf_firstPointInBlock_);
97 LocalOrdinal firstPoint = pointMap_->getMinLocalIndex();
98 GlobalOrdinal blockID = pointMap_->getMinGlobalIndex()/blockSize;
99 for(
size_t i=0; i<numLocalBlocks; ++i) {
100 myGlobalBlockIDs_[i] = blockID++;
101 v_firstPoints[i] = firstPoint;
102 firstPoint += blockSize;
104 v_firstPoints[numLocalBlocks] = firstPoint;
105 v_firstPoints = Teuchos::null;
106 view_firstPointInBlock_ = node->template viewBuffer<LocalOrdinal>(numLocalBlocks+1, pbuf_firstPointInBlock_);
109 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
112 size_t numLocalBlocks,
113 LocalOrdinal blockSize,
114 GlobalOrdinal indexBase,
118 globalNumBlocks_(numGlobalBlocks),
120 pbuf_firstPointInBlock_(),
121 view_firstPointInBlock_(),
122 blockIDsAreContiguous_(true),
123 constantBlockSize_(blockSize)
126 "Tpetra::BlockMap::BlockMap ERROR: blockSize must be greater than 0.");
133 size_t numLocalPoints = numLocalBlocks*blockSize;
139 Teuchos::reduceAll<int, global_size_t>(*comm, Teuchos::REDUCE_SUM,
140 1, &numLocalBlocks, &globalNumBlocks_);
143 myGlobalBlockIDs_.
resize(numLocalBlocks);
144 pbuf_firstPointInBlock_ = node->template allocBuffer<LocalOrdinal>(numLocalBlocks+1);
145 Teuchos::ArrayRCP<LocalOrdinal> v_firstPoints = node->template viewBufferNonConst<LocalOrdinal>(Kokkos::WriteOnly, numLocalBlocks+1, pbuf_firstPointInBlock_);
147 LocalOrdinal firstPoint = pointMap_->getMinLocalIndex();
148 GlobalOrdinal blockID = pointMap_->getMinGlobalIndex()/blockSize;
149 for(
size_t i=0; i<numLocalBlocks; ++i) {
150 myGlobalBlockIDs_[i] = blockID++;
151 v_firstPoints[i] = firstPoint;
152 firstPoint += blockSize;
154 v_firstPoints[numLocalBlocks] = firstPoint;
155 v_firstPoints = Teuchos::null;
156 view_firstPointInBlock_ = node->template viewBuffer<LocalOrdinal>(numLocalBlocks+1, pbuf_firstPointInBlock_);
159 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
165 GlobalOrdinal indexBase,
169 globalNumBlocks_(numGlobalBlocks),
170 myGlobalBlockIDs_(myGlobalBlockIDs),
171 pbuf_firstPointInBlock_(),
172 view_firstPointInBlock_(),
173 blockIDsAreContiguous_(false),
174 constantBlockSize_(0)
177 "Tpetra::BlockMap::BlockMap ERROR: input myGlobalBlockIDs and blockSizes arrays must have the same length.");
179 size_t sum_blockSizes = 0;
182 iter = blockSizes.
begin(), iend = blockSizes.
end();
184 for(; iter!=iend; ++iter) {
185 LocalOrdinal bsize = *iter;
186 sum_blockSizes += bsize;
187 GlobalOrdinal firstPoint = myFirstGlobalPointInBlocks[i++];
188 for(LocalOrdinal j=0; j<bsize; ++j) {
196 Teuchos::reduceAll<int,global_size_t>(*comm, Teuchos::REDUCE_SUM,
197 Teuchos::as<global_size_t>(myGlobalBlockIDs.
size()), Teuchos::outArg(global_sum));
198 globalNumBlocks_ = global_sum;
200 iter = blockSizes.
begin();
201 LocalOrdinal firstBlockSize = iter==iend ? 0 : *iter;
202 LocalOrdinal firstPoint = pointMap_->getMinLocalIndex();
203 LocalOrdinal numLocalBlocks = myGlobalBlockIDs.
size();
204 pbuf_firstPointInBlock_ = node->template allocBuffer<LocalOrdinal>(numLocalBlocks+1);
205 Teuchos::ArrayRCP<LocalOrdinal> v_firstPoints = node->template viewBufferNonConst<LocalOrdinal>(Kokkos::WriteOnly, numLocalBlocks+1, pbuf_firstPointInBlock_);
207 bool blockSizesAreConstant =
true;
209 for(; iter!=iend; ++iter, ++i) {
210 v_firstPoints[i] = firstPoint;
212 if (*iter != firstBlockSize) {
213 blockSizesAreConstant =
false;
216 v_firstPoints[i] = firstPoint;
217 v_firstPoints = Teuchos::null;
218 if (blockSizesAreConstant) constantBlockSize_ = firstBlockSize;
220 size_t num_points = pointMap_->getNodeNumElements();
222 "Tpetra::BlockMap::BlockMap ERROR: internal failure, sum of block-sizes must equal pointMap->getNodeNumElements().");
225 b_iter = myGlobalBlockIDs_.
begin(), b_end = myGlobalBlockIDs_.
end();
226 GlobalOrdinal
id = b_iter==b_end ? 0 : *b_iter;
227 if (b_iter != b_end) ++b_iter;
228 blockIDsAreContiguous_ =
true;
229 for(; b_iter != b_end; ++b_iter) {
230 if (*b_iter !=
id+1) {
231 blockIDsAreContiguous_ =
false;
236 if (blockIDsAreContiguous_ ==
false) {
237 setup_noncontig_mapping();
240 view_firstPointInBlock_ = node->template viewBuffer<LocalOrdinal>(numLocalBlocks+1, pbuf_firstPointInBlock_);
243 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
245 : pointMap_(pointMap),
247 myGlobalBlockIDs_(myGlobalBlockIDs),
248 pbuf_firstPointInBlock_(),
249 view_firstPointInBlock_(),
250 blockIDsAreContiguous_(false),
251 constantBlockSize_(0)
254 "Tpetra::BlockMap::BlockMap ERROR: input myGlobalBlockIDs and blockSizes arrays must have the same length.");
257 Teuchos::reduceAll<int,global_size_t>(*pointMap->getComm(), Teuchos::REDUCE_SUM, 1, &numLocalBlocks, &globalNumBlocks_);
259 LocalOrdinal firstPoint = pointMap->getMinLocalIndex();
260 size_t sum_blockSizes = 0;
262 iter = blockSizes.
begin(), iend = blockSizes.
end();
263 LocalOrdinal firstBlockSize = *iter;
265 pbuf_firstPointInBlock_ = node->template allocBuffer<LocalOrdinal>(myGlobalBlockIDs.
size()+1);
266 Teuchos::ArrayRCP<LocalOrdinal> v_firstPoints = node->template viewBufferNonConst<LocalOrdinal>(Kokkos::WriteOnly, numLocalBlocks+1, pbuf_firstPointInBlock_);
268 bool blockSizesAreConstant =
true;
270 for(; iter!=iend; ++iter, ++i) {
271 sum_blockSizes += *iter;
272 v_firstPoints[i] = firstPoint;
274 if (*iter != firstBlockSize) {
275 blockSizesAreConstant =
false;
278 v_firstPoints[i] = firstPoint;
279 v_firstPoints = Teuchos::null;
280 if (blockSizesAreConstant) constantBlockSize_ = firstBlockSize;
282 size_t num_points = pointMap->getNodeNumElements();
284 "Tpetra::BlockMap::BlockMap ERROR: sum of block-sizes must equal pointMap->getNodeNumElements().");
287 b_iter = myGlobalBlockIDs_.
begin(), b_end = myGlobalBlockIDs_.
end();
288 GlobalOrdinal
id = *b_iter;
290 blockIDsAreContiguous_ =
true;
291 for(; b_iter != b_end; ++b_iter) {
292 if (*b_iter !=
id+1) {
293 blockIDsAreContiguous_ =
false;
298 if (blockIDsAreContiguous_ ==
false) {
299 setup_noncontig_mapping();
302 view_firstPointInBlock_ = node->template viewBuffer<LocalOrdinal>(numLocalBlocks+1, pbuf_firstPointInBlock_);
305 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
313 convertBlockMapToPointMap(*
this);
316 pbmap->getRemoteIndexList(GBIDs(), remoteProcs());
323 distor.createFromRecvs(GBIDs(), remoteProcs(), exportGBIDs, exportProcs);
329 iter = exportGBIDs.
begin(),
330 iter_end = exportGBIDs.end();
331 for(
int i=0; iter!=iter_end; ++iter, ++i) {
332 GlobalOrdinal gbid = *iter;
338 size_t numPackets = 1;
339 distor.doPostsAndWaits(exportFirstPointInBlocks().
getConst(),
340 numPackets, firstGlobalPointInBlocks);
342 distor.doPostsAndWaits(exportSizes().
getConst(), numPackets, blockSizes);
345 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
350 for(Tsize_t i=0; i<myGlobalBlockIDs_.
size(); ++i) {
353 map_global_to_local_.insert(std::make_pair(myGlobalBlockIDs_[i],li));
357 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
361 return globalNumBlocks_;
364 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
368 return myGlobalBlockIDs_.
size();
371 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
375 return myGlobalBlockIDs_();
378 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
381 {
return constantBlockSize_ != 0; }
383 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
387 return view_firstPointInBlock_;
390 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
394 return pbuf_firstPointInBlock_;
397 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
402 if (localBlockID < 0 || localBlockID >= myGlobalBlockIDs_.
size()) {
406 return myGlobalBlockIDs_[localBlockID];
409 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
414 if (myGlobalBlockIDs_.
size() == 0) {
418 if (blockIDsAreContiguous_ ==
false) {
420 typename std::map<GlobalOrdinal,LocalOrdinal>::const_iterator iter =
421 map_global_to_local_.find(globalBlockID);
422 if (iter == map_global_to_local_.end())
return invalid;
426 LocalOrdinal localBlockID = globalBlockID - myGlobalBlockIDs_[0];
427 LocalOrdinal numLocalBlocks = myGlobalBlockIDs_.
size();
429 if (localBlockID < 0 || localBlockID >= numLocalBlocks) {
436 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
440 if (constantBlockSize_ != 0) {
441 return constantBlockSize_;
445 if (localBlockID < 0 || localBlockID >= view_firstPointInBlock_.
size()) {
446 throw std::runtime_error(
"Tpetra::BlockMap::getLocalBlockSize ERROR: localBlockID out of range.");
449 return view_firstPointInBlock_[localBlockID+1]-view_firstPointInBlock_[localBlockID];
452 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
457 if (localBlockID < 0 || localBlockID >= view_firstPointInBlock_.
size()) {
458 throw std::runtime_error(
"Tpetra::BlockMap::getFirstLocalPointInLocalBlock ERROR: localBlockID out of range.");
461 return view_firstPointInBlock_[localBlockID];
464 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
469 if (localBlockID < 0 || localBlockID >= view_firstPointInBlock_.
size()) {
470 throw std::runtime_error(
"Tpetra::BlockMap::getFirstGlobalPointInLocalBlock ERROR: localBlockID out of range.");
473 return pointMap_->getGlobalElement(view_firstPointInBlock_[localBlockID]);
484 #define TPETRA_BLOCKMAP_INSTANT(LO,GO,NODE) \ 486 template class BlockMap< LO , GO , NODE >; Block-entry counterpart to Tpetra::Map.
BlockMap(global_size_t numGlobalBlocks, LocalOrdinal blockSize, GlobalOrdinal indexBase, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, const Teuchos::RCP< Node > &node=Kokkos::DefaultNode::getDefaultNode())
BlockMap constructor specifying numGlobalBlocks and constant blockSize.
LocalOrdinal getLocalBlockID(GlobalOrdinal globalBlockID) const
Return the localBlockID corresponding to the given globalBlockID.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
global_size_t getGlobalNumBlocks() const
Return global number of blocks.
Teuchos::ArrayRCP< const LocalOrdinal > getNodeFirstPointInBlocks() const
Return ArrayRCP of first-local-point in local blocks.
bool isBlockSizeConstant() const
Return true if all blocks have the same size.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
const_pointer const_iterator
GlobalOrdinal getGlobalBlockID(LocalOrdinal localBlockID) const
Return the globalBlockID corresponding to the given localBlockID.
size_t global_size_t
Global size_t object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
LocalOrdinal getLocalBlockSize(LocalOrdinal localBlockID) const
Return the block-size for localBlockID.
Sets up and executes a communication plan for a Tpetra DistObject.
void getRemoteBlockInfo(const Teuchos::ArrayView< const GlobalOrdinal > &GBIDs, const Teuchos::ArrayView< GlobalOrdinal > &firstGlobalPointInBlocks, const Teuchos::ArrayView< LocalOrdinal > &blockSizes) const
Return the first-global-point-in-block and block-sizes for a list of block-IDs on remote processors...
size_t getNodeNumBlocks() const
Return number of blocks on the local processor.
void resize(size_type new_size, const value_type &x=value_type())
std::vector< T >::const_iterator const_iterator
void push_back(const value_type &x)
Describes a parallel distribution of objects over processes.
Teuchos::ArrayView< const GlobalOrdinal > getNodeBlockIDs() const
Return array-view of block-ids for this local processor.
GlobalOrdinal getFirstGlobalPointInLocalBlock(LocalOrdinal localBlockID) const
Return the first global point-index corresponding to localBlockID.
LocalOrdinal getFirstLocalPointInLocalBlock(LocalOrdinal localBlockID) const
Return the first local point-index corresponding to localBlockID.
Teuchos::ArrayRCP< const LocalOrdinal > getNodeFirstPointInBlocks_Device() const
Return device-resident ArrayRCP of first-local-point in local blocks.