| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 36 |
#include "mpi.h" // for MPI_Bcast, MPI_Comm, etc |
| 37 |
#include <stddef.h> // for size_t |
| 38 |
#include <stk_unit_test_utils/getOption.h> // for getOption |
| 39 |
#include <stk_unit_test_utils/BuildMesh.hpp> // for getOption |
| 40 |
#include <stk_util/stk_config.h> // for STK_HAS_MPI |
| 41 |
#include <algorithm> // for binary_search, sort |
| 42 |
#include <cstdint> // for uint64_t |
| 43 |
#include <iostream> // for operator<<, basic_ostream, etc |
| 44 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 45 |
#include <stk_mesh/base/Comm.hpp> // for comm_mesh_counts |
| 46 |
#include <stk_mesh/base/GetEntities.hpp> // for count_entities |
| 47 |
#include <stk_util/util/ReportHandler.hpp> // for ThrowRequireMsg |
| 48 |
#include <stk_util/environment/WallTime.hpp> // for wall_time |
| 49 |
#include <stk_util/parallel/MPI.hpp> // for Datatype |
| 50 |
#include <string> // for string |
| 51 |
#include <vector> // for vector |
| 52 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH |
| 53 |
#include "stk_io/StkMeshIoBroker.hpp" // for StkMeshIoBroker |
| 54 |
#include "stk_mesh/base/Bucket.hpp" // for Bucket |
| 55 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 56 |
#include "stk_mesh/base/EntityKey.hpp" // for EntityKey |
| 57 |
#include "stk_mesh/base/MetaData.hpp" // for MetaData |
| 58 |
#include "stk_mesh/base/Selector.hpp" // for Selector |
| 59 |
#include "stk_mesh/base/Types.hpp" // for BucketVector, EntityId |
| 60 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 61 |
|
| 62 |
//==================== |
| 63 |
|
| 64 |
#if defined(STK_HAS_MPI) // this means MPI is available |
| 65 |
|
| 66 |
namespace { |
| 67 |
|
| 68 |
typedef int INTMPI; |
| 69 |
using stk::unit_test_util::build_mesh; |
| 70 |
|
| 71 |
class MpiInfo |
| 72 |
{ |
| 73 |
public: |
| 74 |
MpiInfo(MPI_Comm comm) : mProcId(-1), mNumProcs(-1), mComm(comm) |
| 75 |
{ |
| 76 |
MPI_Comm_rank(mComm, &mProcId); |
| 77 |
MPI_Comm_size(mComm, &mNumProcs); |
| 78 |
} |
| 79 |
~MpiInfo() {} |
| 80 |
|
| 81 |
INTMPI getProcId() const { return mProcId; } |
| 82 |
INTMPI getNumProcs() const { return mNumProcs; } |
| 83 |
MPI_Comm getMpiComm() const { return mComm; } |
| 84 |
|
| 85 |
private: |
| 86 |
INTMPI mProcId; |
| 87 |
INTMPI mNumProcs; |
| 88 |
MPI_Comm mComm; |
| 89 |
}; |
| 90 |
|
| 91 |
void retrieveIds(const INTMPI root, uint64_t id, MPI_Comm comm, uint64_t numIdsToGetPerProc, std::vector<int>& areIdsBeingUsed); |
| 92 |
bool sendIdToCheck(const INTMPI root, uint64_t id, MPI_Comm comm); |
| 93 |
|
| 94 |
// not stkmesh |
| 95 |
void respondToRootProcessorAboutIdsOwnedOnThisProc(const int root, const uint64_t maxId, const std::vector<uint64_t> &idsInUse, MPI_Comm comm); |
| 96 |
void receiveIdAndCheck(const int root, const std::vector<uint64_t> &idsInUse, MPI_Comm comm); |
| 97 |
void checkUniqueIds(const std::vector<uint64_t> &myIds, const std::vector<uint64_t> &uniqueIds, const MpiInfo& mpiInfo); |
| 98 |
void getBatchesOfIdsFromOtherProcessorsUntilRequestOnThisProcIsFulfilled(INTMPI root, uint64_t &startingIdToSearchForNewIds, std::vector<uint64_t> &idsObtained, uint64_t numIdsNeeded, int scaleFactorForNumIds, |
| 99 |
std::vector<uint64_t> &sortedIds, const uint64_t maxId, const MpiInfo& mpiInfo); |
| 100 |
|
| 101 |
// is stkmesh |
| 102 |
void respondToRootProcessorAboutIdsOwnedOnThisProc(const int root, const uint64_t maxId, stk::mesh::BulkData& stkMeshBulkData, MPI_Comm comm); |
| 103 |
void receiveIdAndCheck(const int root, stk::mesh::BulkData &stkMeshBulkData, MPI_Comm comm); |
| 104 |
void checkUniqueIds(stk::mesh::BulkData &stkMeshBulkData, const std::vector<uint64_t> &uniqueIds, const MpiInfo& mpiInfo); |
| 105 |
void getBatchesOfIdsFromOtherProcessorsUntilRequestOnThisProcIsFulfilled(INTMPI root, uint64_t &startingIdToSearchForNewIds, std::vector<uint64_t> &idsObtained, uint64_t numIdsNeeded, int scaleFactorForNumIds, |
| 106 |
stk::mesh::BulkData& stkMeshBulkData, const uint64_t maxId, const MpiInfo& mpiInfo); |
| 107 |
|
| 108 |
void writeIdsToFile(const std::string &filename, const INTMPI myProcId, const std::vector<uint64_t> &uniqueIds); |
| 109 |
void terminateIdRequestForThisProc(INTMPI root, MPI_Comm comm); |
| 110 |
void getAvailableIds_exp(const std::vector<uint64_t> &myIds, uint64_t numIdsNeeded, std::vector<uint64_t> &idsObtained, uint64_t &startingIdToSearchForNewIds, const uint64_t maxId, const MpiInfo& mpiInfo); |
| 111 |
void getAvailableIds_exp(stk::mesh::BulkData &stkMeshBulkData, uint64_t numIdsNeeded, std::vector<uint64_t> &idsObtained, uint64_t &startingIdToSearchForNewIds, const uint64_t maxId, const MpiInfo& mpiInfo); |
| 112 |
|
| 113 |
//////////////////////////////////////////////////////////////////// |
| 114 |
|
| 115 |
TEST(GeneratedIds, StkMeshApproach1) |
| 116 |
{ |
| 117 |
MpiInfo mpiInfo(MPI_COMM_WORLD); |
| 118 |
|
| 119 |
std::string exodusFileName = stk::unit_test_util::get_option("-i", "generated:10x10x10"); |
| 120 |
const int spatialDim = 3; |
| 121 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, mpiInfo.getMpiComm()); |
| 122 |
stk::mesh::MetaData& stkMeshMetaData = bulkPtr->mesh_meta_data(); |
| 123 |
stk::mesh::BulkData& stkMeshBulkData = *bulkPtr; |
| 124 |
|
| 125 |
// STK IO module will be described in separate chapter. |
| 126 |
// It is used here to read the mesh data from the Exodus file and populate an STK Mesh. |
| 127 |
// The order of the following lines in {} are important |
| 128 |
{ |
| 129 |
stk::io::StkMeshIoBroker exodusFileReader(mpiInfo.getMpiComm()); |
| 130 |
|
| 131 |
// Inform STK IO which STK Mesh objects to populate later |
| 132 |
exodusFileReader.set_bulk_data(stkMeshBulkData); |
| 133 |
|
| 134 |
exodusFileReader.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 135 |
|
| 136 |
// Populate the MetaData which has the descriptions of the Parts and Fields. |
| 137 |
exodusFileReader.create_input_mesh(); |
| 138 |
|
| 139 |
// Populate entities in STK Mesh from Exodus file |
| 140 |
exodusFileReader.populate_bulk_data(); |
| 141 |
} |
| 142 |
|
| 143 |
stk::mesh::Selector select_owned( stkMeshMetaData.locally_owned_part() ); |
| 144 |
|
| 145 |
std::vector<size_t> count; |
| 146 |
stk::mesh::count_entities( select_owned , stkMeshBulkData , count ); |
| 147 |
size_t numIdsThisProc = count[stk::topology::NODE_RANK]; |
| 148 |
|
| 149 |
std::vector<size_t> count1; |
| 150 |
stk::mesh::comm_mesh_counts(stkMeshBulkData, count1); |
| 151 |
size_t totalIdsInUse = count1[stk::topology::NODE_RANK]; |
| 152 |
|
| 153 |
std::vector<uint64_t> myIds(numIdsThisProc,0); |
| 154 |
|
| 155 |
const stk::mesh::BucketVector& buckets = stkMeshBulkData.buckets(stk::topology::NODE_RANK); |
| 156 |
|
| 157 |
size_t counter = 0; |
| 158 |
for (size_t i=0;i<buckets.size();i++) |
| 159 |
{ |
| 160 |
const stk::mesh::Bucket &bucket = *buckets[i]; |
| 161 |
if ( bucket.owned() ) |
| 162 |
{ |
| 163 |
for (size_t j=0;j<bucket.size();j++) |
| 164 |
{ |
| 165 |
stk::mesh::EntityKey entityKey = stkMeshBulkData.entity_key(bucket[j]); |
| 166 |
myIds[counter] = entityKey; |
| 167 |
counter++; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
EXPECT_EQ(counter, numIdsThisProc); |
| 173 |
|
| 174 |
uint64_t startingIdToSearchForNewIds = 1; |
| 175 |
uint64_t numIdsNeeded = 100; |
| 176 |
std::vector<uint64_t> idsObtained; |
| 177 |
uint64_t maxId = 72057594037927935; |
| 178 |
|
| 179 |
double startTime = stk::wall_time(); |
| 180 |
|
| 181 |
getAvailableIds_exp(myIds, numIdsNeeded, idsObtained, startingIdToSearchForNewIds, maxId, mpiInfo); |
| 182 |
|
| 183 |
double endTime = stk::wall_time(); |
| 184 |
|
| 185 |
if ( mpiInfo.getProcId() == 0 ) |
| 186 |
{ |
| 187 |
std::cerr << "Took " << endTime-startTime << " seconds." << std::endl; |
| 188 |
} |
| 189 |
|
| 190 |
checkUniqueIds(myIds, idsObtained, mpiInfo); |
| 191 |
writeIdsToFile("ids_", mpiInfo.getProcId(), idsObtained); |
| 192 |
|
| 193 |
for (size_t i=0;i<idsObtained.size();i++) |
| 194 |
{ |
| 195 |
uint64_t goldId = totalIdsInUse + numIdsNeeded*mpiInfo.getProcId()+1 + i; |
| 196 |
EXPECT_EQ(goldId, idsObtained[i]); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
//////////////////////////////////////////////////////////////////// |
| 201 |
|
| 202 |
TEST(GeneratedIds, StkMeshApproach2) |
| 203 |
{ |
| 204 |
MpiInfo mpiInfo(MPI_COMM_WORLD); |
| 205 |
|
| 206 |
std::string exodusFileName = stk::unit_test_util::get_option("-i", "generated:10x10x10"); |
| 207 |
const int spatialDim = 3; |
| 208 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, mpiInfo.getMpiComm()); |
| 209 |
stk::mesh::BulkData& stkMeshBulkData = *bulkPtr; |
| 210 |
|
| 211 |
// STK IO module will be described in separate chapter. |
| 212 |
// It is used here to read the mesh data from the Exodus file and populate an STK Mesh. |
| 213 |
// The order of the following lines in {} are important |
| 214 |
{ |
| 215 |
stk::io::StkMeshIoBroker exodusFileReader(mpiInfo.getMpiComm()); |
| 216 |
|
| 217 |
// Inform STK IO which STK Mesh objects to populate later |
| 218 |
exodusFileReader.set_bulk_data(stkMeshBulkData); |
| 219 |
|
| 220 |
exodusFileReader.add_mesh_database(exodusFileName, stk::io::READ_MESH); |
| 221 |
|
| 222 |
// Populate the MetaData which has the descriptions of the Parts and Fields. |
| 223 |
exodusFileReader.create_input_mesh(); |
| 224 |
|
| 225 |
// Populate entities in STK Mesh from Exodus file |
| 226 |
exodusFileReader.populate_bulk_data(); |
| 227 |
} |
| 228 |
|
| 229 |
//////////////////////////////////////////////////// |
| 230 |
|
| 231 |
std::vector<size_t> count1; |
| 232 |
stk::mesh::comm_mesh_counts(stkMeshBulkData, count1); |
| 233 |
size_t totalIdsInUse = count1[stk::topology::NODE_RANK]; |
| 234 |
|
| 235 |
uint64_t startingIdToSearchForNewIds = 1; |
| 236 |
uint64_t numIdsNeeded = 100; |
| 237 |
std::vector<uint64_t> idsObtained; |
| 238 |
uint64_t maxId = 72057594037927935; |
| 239 |
|
| 240 |
double startTime = stk::wall_time(); |
| 241 |
|
| 242 |
getAvailableIds_exp(stkMeshBulkData, numIdsNeeded, idsObtained, startingIdToSearchForNewIds, maxId, mpiInfo); |
| 243 |
|
| 244 |
double endTime = stk::wall_time(); |
| 245 |
|
| 246 |
//////////////////////////////////////////////////// |
| 247 |
|
| 248 |
if ( mpiInfo.getProcId() == 0 ) |
| 249 |
{ |
| 250 |
std::cerr << "Took " << endTime-startTime << " seconds." << std::endl; |
| 251 |
} |
| 252 |
|
| 253 |
checkUniqueIds(stkMeshBulkData, idsObtained, mpiInfo); |
| 254 |
writeIdsToFile("ids_", mpiInfo.getProcId(), idsObtained); |
| 255 |
|
| 256 |
for (size_t i=0;i<idsObtained.size();i++) |
| 257 |
{ |
| 258 |
uint64_t goldId = totalIdsInUse + numIdsNeeded*mpiInfo.getProcId()+1 + i; |
| 259 |
EXPECT_EQ(goldId, idsObtained[i]); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
//////////////////////////////////////////////////////////////////// |
| 264 |
|
| 265 |
void checkUniqueIds(const std::vector<uint64_t> &myIds, const std::vector<uint64_t> &uniqueIds, const MpiInfo &mpiInfo) |
| 266 |
{ |
| 267 |
std::vector<uint64_t> sortedIds(uniqueIds.begin(), uniqueIds.end()); |
| 268 |
std::sort(sortedIds.begin(), sortedIds.end()); |
| 269 |
|
| 270 |
for (INTMPI i=0;i<mpiInfo.getNumProcs();i++) |
| 271 |
{ |
| 272 |
if (mpiInfo.getNumProcs() == i) |
| 273 |
{ |
| 274 |
for (size_t j=0;j<uniqueIds.size();j++) |
| 275 |
{ |
| 276 |
STK_ThrowRequireMsg(uniqueIds[j]>0, "Id generation error. Please contact sierra-help for support."); |
| 277 |
EXPECT_FALSE(std::binary_search(myIds.begin(), myIds.end(), uniqueIds[j])); |
| 278 |
EXPECT_EQ(true, sendIdToCheck(i, uniqueIds[j], mpiInfo.getMpiComm())); |
| 279 |
} |
| 280 |
terminateIdRequestForThisProc(i, mpiInfo.getMpiComm()); |
| 281 |
} |
| 282 |
else |
| 283 |
{ |
| 284 |
receiveIdAndCheck(i, sortedIds, mpiInfo.getMpiComm()); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
void checkUniqueIds(stk::mesh::BulkData &stkMeshBulkData, const std::vector<uint64_t> &uniqueIds, const MpiInfo &mpiInfo) |
| 290 |
{ |
| 291 |
for (INTMPI i=0;i<mpiInfo.getNumProcs();i++) |
| 292 |
{ |
| 293 |
if (mpiInfo.getNumProcs() == i) |
| 294 |
{ |
| 295 |
for (size_t j=0;j<uniqueIds.size();j++) |
| 296 |
{ |
| 297 |
STK_ThrowRequireMsg(uniqueIds[j]>0, "Id generation error. Please contact sierra-help for support."); |
| 298 |
|
| 299 |
stk::mesh::Entity entity = stkMeshBulkData.get_entity(stk::topology::NODE_RANK, uniqueIds[j]); |
| 300 |
EXPECT_FALSE(stkMeshBulkData.is_valid(entity)); |
| 301 |
EXPECT_EQ(true, sendIdToCheck(i, uniqueIds[j], mpiInfo.getMpiComm())); |
| 302 |
} |
| 303 |
terminateIdRequestForThisProc(i, mpiInfo.getMpiComm()); |
| 304 |
} |
| 305 |
else |
| 306 |
{ |
| 307 |
receiveIdAndCheck(i, stkMeshBulkData, mpiInfo.getMpiComm()); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
//////////////////////////////////////////////////////////////////// |
| 313 |
|
| 314 |
void writeIdsToFile(const std::string & /*filename*/, |
| 315 |
[[maybe_unused]] const INTMPI myProcId, |
| 316 |
[[maybe_unused]] const std::vector<uint64_t> &uniqueIds) |
| 317 |
{ |
| 318 |
#ifdef DEBUG_THIS_ |
| 319 |
std::ostringstream os; |
| 320 |
os << "ids_" << myProcId << ".m"; |
| 321 |
std::ofstream out(os.str().c_str()); |
| 322 |
// for (size_t i=0;i<myIds.size();i++) |
| 323 |
// { |
| 324 |
// out << "myids(" << i+1 << ") = " << myIds[i] << ";" << std::endl; |
| 325 |
// } |
| 326 |
for (size_t i=0;i<uniqueIds.size();i++) |
| 327 |
{ |
| 328 |
out << "unique_ids(" << i+1 << ") = " << uniqueIds[i] << ";" << std::endl; |
| 329 |
} |
| 330 |
out.close(); |
| 331 |
#endif |
| 332 |
} |
| 333 |
|
| 334 |
//////////////////////////////////////////////////////////////////// |
| 335 |
|
| 336 |
bool sendIdToCheck(const INTMPI root, uint64_t id, MPI_Comm comm) |
| 337 |
{ |
| 338 |
MPI_Bcast(&id, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 339 |
bool goodId = true; |
| 340 |
if ( id != 0 ) |
| 341 |
{ |
| 342 |
uint64_t good = 0; |
| 343 |
uint64_t received = 0; |
| 344 |
MPI_Reduce(&good, &received, 1, sierra::MPI::Datatype<uint64_t>::type(), MPI_SUM, root, comm); |
| 345 |
|
| 346 |
if ( received > 0 ) |
| 347 |
{ |
| 348 |
goodId = false; |
| 349 |
} |
| 350 |
} |
| 351 |
return goodId; |
| 352 |
} |
| 353 |
|
| 354 |
//////////////////////////////////////////////////////////////////// |
| 355 |
|
| 356 |
void receiveIdAndCheck(const int root, const std::vector<uint64_t> &idsInUse, MPI_Comm comm) |
| 357 |
{ |
| 358 |
uint64_t id=0; |
| 359 |
|
| 360 |
while( true ) |
| 361 |
{ |
| 362 |
MPI_Bcast(&id, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 363 |
if ( id == 0) break; |
| 364 |
|
| 365 |
bool found = std::binary_search(idsInUse.begin(), idsInUse.end(), id); |
| 366 |
uint64_t result = 0; |
| 367 |
if ( found ) |
| 368 |
{ |
| 369 |
result = 1; |
| 370 |
} |
| 371 |
MPI_Reduce(&result, &result, 1, sierra::MPI::Datatype<uint64_t>::type(), MPI_SUM, root, comm); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
//////////////////////////////////////////////////////////////////// |
| 376 |
void receiveIdAndCheck(const int root, stk::mesh::BulkData &stkMeshBulkData, MPI_Comm comm) |
| 377 |
{ |
| 378 |
uint64_t id=0; |
| 379 |
|
| 380 |
while( true ) |
| 381 |
{ |
| 382 |
MPI_Bcast(&id, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 383 |
if ( id == 0) break; |
| 384 |
|
| 385 |
stk::mesh::Entity entity = stkMeshBulkData.get_entity(stk::topology::NODE_RANK, id); |
| 386 |
bool found = stkMeshBulkData.is_valid(entity); |
| 387 |
uint64_t result = 0; |
| 388 |
if ( found ) |
| 389 |
{ |
| 390 |
result = 1; |
| 391 |
} |
| 392 |
MPI_Reduce(&result, &result, 1, sierra::MPI::Datatype<uint64_t>::type(), MPI_SUM, root, comm); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
//////////////////////////////////////////////////////////////////// |
| 397 |
|
| 398 |
void respondToRootProcessorAboutIdsOwnedOnThisProc(const int root, const uint64_t /*maxId*/, const std::vector<uint64_t> &idsInUse, MPI_Comm comm) |
| 399 |
{ |
| 400 |
uint64_t id=0; |
| 401 |
|
| 402 |
while( true ) |
| 403 |
{ |
| 404 |
MPI_Bcast(&id, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 405 |
if ( id == 0) break; |
| 406 |
uint64_t numIdsToGet=0; |
| 407 |
MPI_Bcast(&numIdsToGet, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 408 |
|
| 409 |
std::vector<int> areIdsBeingused(numIdsToGet,0); |
| 410 |
for (size_t i=0;i<areIdsBeingused.size();i++) |
| 411 |
{ |
| 412 |
if ( std::binary_search(idsInUse.begin(), idsInUse.end(), id+i) ) |
| 413 |
{ |
| 414 |
areIdsBeingused[i] = 1; |
| 415 |
} |
| 416 |
} |
| 417 |
int *rbuff = 0; |
| 418 |
MPI_Reduce(areIdsBeingused.data(), rbuff, numIdsToGet, MPI_INT, MPI_SUM, root, comm); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
//////////////////////////////////////////////////////////////////// |
| 423 |
|
| 424 |
void respondToRootProcessorAboutIdsOwnedOnThisProc(const int root, const uint64_t /*maxId*/, stk::mesh::BulkData& stkMeshBulkData, MPI_Comm comm) |
| 425 |
{ |
| 426 |
uint64_t id=0; |
| 427 |
|
| 428 |
while( true ) |
| 429 |
{ |
| 430 |
MPI_Bcast(&id, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 431 |
if ( id == 0) break; |
| 432 |
uint64_t numIdsToGet=0; |
| 433 |
MPI_Bcast(&numIdsToGet, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 434 |
|
| 435 |
std::vector<int> areIdsBeingused(numIdsToGet,0); |
| 436 |
for (size_t i=0;i<areIdsBeingused.size();i++) |
| 437 |
{ |
| 438 |
stk::mesh::Entity entity = stkMeshBulkData.get_entity(stk::topology::NODE_RANK, id+i); |
| 439 |
if ( stkMeshBulkData.is_valid(entity) ) |
| 440 |
{ |
| 441 |
areIdsBeingused[i] = 1; |
| 442 |
} |
| 443 |
} |
| 444 |
int *rbuff = 0; |
| 445 |
MPI_Reduce(areIdsBeingused.data(), rbuff, numIdsToGet, MPI_INT, MPI_SUM, root, comm); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
//////////////////////////////////////////////////////////////////// |
| 450 |
|
| 451 |
void retrieveIds(const INTMPI root, uint64_t id, MPI_Comm comm, uint64_t numIdsToGetPerProc, std::vector<int>& areIdsBeingUsed) |
| 452 |
{ |
| 453 |
MPI_Bcast(&id, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 454 |
if ( id != 0 ) |
| 455 |
{ |
| 456 |
MPI_Bcast(&numIdsToGetPerProc, 1, sierra::MPI::Datatype<uint64_t>::type(), root, comm); |
| 457 |
std::vector<uint64_t> zeroids(numIdsToGetPerProc,0); |
| 458 |
MPI_Reduce(zeroids.data(), areIdsBeingUsed.data(), numIdsToGetPerProc, MPI_INT, MPI_SUM, root, comm); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
//////////////////////////////////////////////////////////////////// |
| 463 |
|
| 464 |
|
| 465 |
void getBatchesOfIdsFromOtherProcessorsUntilRequestOnThisProcIsFulfilled(INTMPI root, uint64_t &startingIdToSearchForNewIds, std::vector<uint64_t> &idsObtained, uint64_t numIdsNeeded, int scaleFactorForNumIds, |
| 466 |
std::vector<uint64_t> &sortedIds, const uint64_t maxId, const MpiInfo& mpiInfo) |
| 467 |
{ |
| 468 |
while ( startingIdToSearchForNewIds < maxId && idsObtained.size() < numIdsNeeded ) |
| 469 |
{ |
| 470 |
uint64_t requestNumIds = numIdsNeeded - idsObtained.size(); |
| 471 |
std::vector<int> areIdsBeingUsed(scaleFactorForNumIds*requestNumIds,0); |
| 472 |
if ( !std::binary_search(sortedIds.begin(), sortedIds.end(), startingIdToSearchForNewIds) ) |
| 473 |
{ |
| 474 |
retrieveIds(root, startingIdToSearchForNewIds, mpiInfo.getMpiComm(), scaleFactorForNumIds*requestNumIds, areIdsBeingUsed); |
| 475 |
uint64_t numIdsChecked=0; |
| 476 |
for (size_t i=0;i<areIdsBeingUsed.size();i++) |
| 477 |
{ |
| 478 |
numIdsChecked=i; |
| 479 |
if ( areIdsBeingUsed[i] == 0 ) |
| 480 |
{ |
| 481 |
idsObtained.push_back(startingIdToSearchForNewIds+i); |
| 482 |
if ( idsObtained.size() == numIdsNeeded ) break; |
| 483 |
} |
| 484 |
} |
| 485 |
startingIdToSearchForNewIds += numIdsChecked+1; |
| 486 |
} |
| 487 |
else |
| 488 |
{ |
| 489 |
startingIdToSearchForNewIds++; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
//////////////////////////////////////////////////////////////////// |
| 495 |
|
| 496 |
|
| 497 |
void getBatchesOfIdsFromOtherProcessorsUntilRequestOnThisProcIsFulfilled(INTMPI root, uint64_t &startingIdToSearchForNewIds, std::vector<uint64_t> &idsObtained, uint64_t numIdsNeeded, int scaleFactorForNumIds, |
| 498 |
stk::mesh::BulkData &stkMeshBulkData, const uint64_t maxId, const MpiInfo& mpiInfo) |
| 499 |
{ |
| 500 |
while ( startingIdToSearchForNewIds < maxId && idsObtained.size() < numIdsNeeded ) |
| 501 |
{ |
| 502 |
uint64_t requestNumIds = numIdsNeeded - idsObtained.size(); |
| 503 |
std::vector<int> areIdsBeingUsed(scaleFactorForNumIds*requestNumIds,0); |
| 504 |
stk::mesh::Entity entity = stkMeshBulkData.get_entity(stk::topology::NODE_RANK, startingIdToSearchForNewIds); |
| 505 |
if ( !stkMeshBulkData.is_valid(entity) ) |
| 506 |
{ |
| 507 |
retrieveIds(root, startingIdToSearchForNewIds, mpiInfo.getMpiComm(), scaleFactorForNumIds*requestNumIds, areIdsBeingUsed); |
| 508 |
uint64_t numIdsChecked=0; |
| 509 |
for (size_t i=0;i<areIdsBeingUsed.size();i++) |
| 510 |
{ |
| 511 |
numIdsChecked=i; |
| 512 |
if ( areIdsBeingUsed[i] == 0 ) |
| 513 |
{ |
| 514 |
idsObtained.push_back(startingIdToSearchForNewIds+i); |
| 515 |
if ( idsObtained.size() == numIdsNeeded ) break; |
| 516 |
} |
| 517 |
} |
| 518 |
startingIdToSearchForNewIds += numIdsChecked+1; |
| 519 |
} |
| 520 |
else |
| 521 |
{ |
| 522 |
startingIdToSearchForNewIds++; |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
//////////////////////////////////////////////////////////////////// |
| 528 |
|
| 529 |
void terminateIdRequestForThisProc(INTMPI root, MPI_Comm comm) |
| 530 |
{ |
| 531 |
sendIdToCheck(root, 0, comm); // a zero terminates communication |
| 532 |
} |
| 533 |
|
| 534 |
void getAvailableIds_exp(const std::vector<uint64_t> &myIds, uint64_t numIdsNeeded, std::vector<uint64_t> &idsObtained, uint64_t &startingIdToSearchForNewIds, const uint64_t maxId, const MpiInfo& mpiInfo) |
| 535 |
{ |
| 536 |
INTMPI numprocs = mpiInfo.getNumProcs(); |
| 537 |
std::vector<uint64_t> receivedInfo(numprocs,0); |
| 538 |
MPI_Allgather(&numIdsNeeded, 1, sierra::MPI::Datatype<uint64_t>::type(), receivedInfo.data(), 1, |
| 539 |
sierra::MPI::Datatype<uint64_t>::type(), mpiInfo.getMpiComm()); |
| 540 |
|
| 541 |
std::vector<uint64_t> sortedIds(myIds.begin(), myIds.end()); |
| 542 |
std::sort(sortedIds.begin(), sortedIds.end()); |
| 543 |
|
| 544 |
uint64_t largestIdHere = sortedIds.back(); |
| 545 |
uint64_t largestIdEverywhere = 0; |
| 546 |
|
| 547 |
MPI_Allreduce(&largestIdHere, &largestIdEverywhere, 1, sierra::MPI::Datatype<uint64_t>::type(), MPI_MAX, mpiInfo.getMpiComm()); |
| 548 |
|
| 549 |
uint64_t totalNumberOfIdsNeeded = 0; |
| 550 |
uint64_t offsetId=0; |
| 551 |
for (INTMPI i=0;i<numprocs;i++) |
| 552 |
{ |
| 553 |
totalNumberOfIdsNeeded += receivedInfo[i]; |
| 554 |
if ( i < mpiInfo.getProcId() ) |
| 555 |
{ |
| 556 |
offsetId += receivedInfo[i]; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
if ( maxId - totalNumberOfIdsNeeded > largestIdHere ) |
| 561 |
{ |
| 562 |
startingIdToSearchForNewIds = largestIdHere; |
| 563 |
idsObtained.resize(numIdsNeeded); |
| 564 |
for (size_t i=0;i<idsObtained.size();i++) |
| 565 |
{ |
| 566 |
idsObtained[i] = largestIdEverywhere + offsetId + i + 1; |
| 567 |
} |
| 568 |
} |
| 569 |
else |
| 570 |
{ |
| 571 |
int scaleFactorForNumIds = 1; |
| 572 |
|
| 573 |
for (INTMPI procIndex=0;procIndex<numprocs;procIndex++) |
| 574 |
{ |
| 575 |
if ( receivedInfo[procIndex] != 0 ) |
| 576 |
{ |
| 577 |
if ( procIndex == mpiInfo.getProcId() ) |
| 578 |
{ |
| 579 |
getBatchesOfIdsFromOtherProcessorsUntilRequestOnThisProcIsFulfilled(procIndex, startingIdToSearchForNewIds, idsObtained, numIdsNeeded, scaleFactorForNumIds, sortedIds, maxId, mpiInfo); |
| 580 |
STK_ThrowRequireMsg(idsObtained.size()==numIdsNeeded, "Id generation error. Ran out of ids. Please contact sierra-help for support."); |
| 581 |
terminateIdRequestForThisProc(procIndex, mpiInfo.getMpiComm()); |
| 582 |
} |
| 583 |
else |
| 584 |
{ |
| 585 |
respondToRootProcessorAboutIdsOwnedOnThisProc(procIndex, maxId, sortedIds, mpiInfo.getMpiComm()); |
| 586 |
} |
| 587 |
// updated starting id across all procs |
| 588 |
MPI_Bcast(&startingIdToSearchForNewIds, 1, sierra::MPI::Datatype<uint64_t>::type(), procIndex, mpiInfo.getMpiComm()); |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
void getAvailableIds_exp(stk::mesh::BulkData &stkMeshBulkData, uint64_t numIdsNeeded, std::vector<uint64_t> &idsObtained, uint64_t &startingIdToSearchForNewIds, const uint64_t maxId, const MpiInfo& mpiInfo) |
| 595 |
{ |
| 596 |
INTMPI numprocs = mpiInfo.getNumProcs(); |
| 597 |
std::vector<uint64_t> receivedInfo(numprocs,0); |
| 598 |
MPI_Allgather(&numIdsNeeded, 1, sierra::MPI::Datatype<uint64_t>::type(), receivedInfo.data(), 1, |
| 599 |
sierra::MPI::Datatype<uint64_t>::type(), mpiInfo.getMpiComm()); |
| 600 |
|
| 601 |
stk::mesh::EntityId largestIdHere = 0; |
| 602 |
|
| 603 |
const stk::mesh::BucketVector& buckets = stkMeshBulkData.buckets(stk::topology::NODE_RANK); |
| 604 |
|
| 605 |
for (size_t i=0;i<buckets.size();i++) |
| 606 |
{ |
| 607 |
const stk::mesh::Bucket &bucket = *buckets[i]; |
| 608 |
if ( bucket.owned() ) |
| 609 |
{ |
| 610 |
for (size_t j=0;j<bucket.size();j++) |
| 611 |
{ |
| 612 |
stk::mesh::EntityKey entityKey = stkMeshBulkData.entity_key(bucket[j]); |
| 613 |
if ( entityKey.id() > largestIdHere ) |
| 614 |
{ |
| 615 |
largestIdHere = entityKey.id(); |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
uint64_t largestIdEverywhere = 0; |
| 622 |
MPI_Allreduce(&largestIdHere, &largestIdEverywhere, 1, sierra::MPI::Datatype<uint64_t>::type(), MPI_MAX, mpiInfo.getMpiComm()); |
| 623 |
|
| 624 |
uint64_t totalNumberOfIdsNeeded = 0; |
| 625 |
uint64_t offsetId=0; |
| 626 |
for (INTMPI i=0;i<numprocs;i++) |
| 627 |
{ |
| 628 |
totalNumberOfIdsNeeded += receivedInfo[i]; |
| 629 |
if ( i < mpiInfo.getProcId() ) |
| 630 |
{ |
| 631 |
offsetId += receivedInfo[i]; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
if ( maxId - totalNumberOfIdsNeeded > largestIdHere ) |
| 636 |
{ |
| 637 |
startingIdToSearchForNewIds = largestIdHere; |
| 638 |
idsObtained.resize(numIdsNeeded); |
| 639 |
for (size_t i=0;i<idsObtained.size();i++) |
| 640 |
{ |
| 641 |
idsObtained[i] = largestIdEverywhere + offsetId + i + 1; |
| 642 |
} |
| 643 |
} |
| 644 |
else |
| 645 |
{ |
| 646 |
int scaleFactorForNumIds = 1; |
| 647 |
|
| 648 |
for (INTMPI procIndex=0;procIndex<numprocs;procIndex++) |
| 649 |
{ |
| 650 |
if ( receivedInfo[procIndex] != 0 ) |
| 651 |
{ |
| 652 |
if ( procIndex == mpiInfo.getProcId() ) |
| 653 |
{ |
| 654 |
getBatchesOfIdsFromOtherProcessorsUntilRequestOnThisProcIsFulfilled(procIndex, startingIdToSearchForNewIds, idsObtained, numIdsNeeded, scaleFactorForNumIds, stkMeshBulkData, maxId, mpiInfo); |
| 655 |
STK_ThrowRequireMsg(idsObtained.size()==numIdsNeeded, "Id generation error. Ran out of ids. Please contact sierra-help for support."); |
| 656 |
terminateIdRequestForThisProc(procIndex, mpiInfo.getMpiComm()); |
| 657 |
} |
| 658 |
else |
| 659 |
{ |
| 660 |
respondToRootProcessorAboutIdsOwnedOnThisProc(procIndex, maxId, stkMeshBulkData, mpiInfo.getMpiComm()); |
| 661 |
} |
| 662 |
// updated starting id across all procs |
| 663 |
MPI_Bcast(&startingIdToSearchForNewIds, 1, sierra::MPI::Datatype<uint64_t>::type(), procIndex, mpiInfo.getMpiComm()); |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
} |
| 670 |
|
| 671 |
#endif // STK_HAS_MPI |
| 672 |
|