| 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 <stddef.h> // for size_t |
| 37 |
#include <stk_mesh/baseImpl/BucketRepository.hpp> // for BucketRepository |
| 38 |
#include <stk_mesh/baseImpl/EntityKeyMapping.hpp> |
| 39 |
#include <stk_mesh/baseImpl/Partition.hpp> // for Partition |
| 40 |
#include <stk_util/parallel/Parallel.hpp> // for ParallelMachine |
| 41 |
#include <stk_unit_test_utils/BulkDataTester.hpp> |
| 42 |
#include <utility> // for pair |
| 43 |
#include "mpi.h" // for MPI_COMM_WORLD, etc |
| 44 |
#include "stk_mesh/base/Bucket.hpp" // for Bucket |
| 45 |
#include "stk_mesh/base/MeshBuilder.hpp" |
| 46 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 47 |
#include "stk_mesh/base/EntityKey.hpp" // for EntityKey |
| 48 |
#include "stk_mesh/base/MetaData.hpp" // for MetaData, entity_rank_names |
| 49 |
#include "stk_mesh/base/Part.hpp" // for Part |
| 50 |
#include "stk_mesh/base/Types.hpp" // for BucketVector, OrdinalVector, etc |
| 51 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 52 |
|
| 53 |
TEST(BucketRepositoryTest, createPartitions) |
| 54 |
{ |
| 55 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(stk::parallel_machine_world()).set_spatial_dimension(3).create(); |
| 56 |
stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data(); |
| 57 |
stk::mesh::Part& myUnrankedNodePart = meta.declare_part("myNodePart"); |
| 58 |
stk::mesh::OrdinalVector partOrds = {0, 1, myUnrankedNodePart.mesh_meta_data_ordinal()}; |
| 59 |
stk::mesh::impl::BucketRepository bucketRepo(*bulkPtr, meta.entity_rank_count(), 16, 512); |
| 60 |
|
| 61 |
stk::mesh::EntityRank rank = stk::topology::NODE_RANK; |
| 62 |
stk::mesh::impl::Partition* partition = bucketRepo.get_or_create_partition(rank, partOrds); |
| 63 |
EXPECT_TRUE(partition != nullptr); |
| 64 |
EXPECT_EQ(1u, bucketRepo.get_partitions(rank).size()); |
| 65 |
|
| 66 |
stk::mesh::impl::Partition* samePartition = bucketRepo.get_or_create_partition(rank, partOrds); |
| 67 |
EXPECT_EQ(partition, samePartition); |
| 68 |
EXPECT_EQ(1u, bucketRepo.get_partitions(rank).size()); |
| 69 |
} |
| 70 |
|
| 71 |
TEST(BucketRepositoryTest, createBuckets) |
| 72 |
{ |
| 73 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 74 |
size_t spatialDim = 3; |
| 75 |
stk::mesh::MetaData stkMeshMetaData(spatialDim, stk::mesh::entity_rank_names()); |
| 76 |
|
| 77 |
stk::mesh::OrdinalVector parts, scratch; |
| 78 |
parts.push_back(stkMeshMetaData.universal_part().mesh_meta_data_ordinal()); |
| 79 |
parts.push_back(stkMeshMetaData.locally_owned_part().mesh_meta_data_ordinal()); |
| 80 |
parts.push_back(stkMeshMetaData.declare_part("part1").mesh_meta_data_ordinal()); |
| 81 |
parts.push_back(stkMeshMetaData.declare_part("part2").mesh_meta_data_ordinal()); |
| 82 |
stkMeshMetaData.commit(); |
| 83 |
|
| 84 |
stk::unit_test_util::BulkDataTester stkMeshBulkData(stkMeshMetaData, comm); |
| 85 |
stk::mesh::impl::EntityKeyMapping entityKeyMapping; |
| 86 |
|
| 87 |
stk::mesh::impl::BucketRepository &bucketRepository = stkMeshBulkData.my_get_bucket_repository(); |
| 88 |
stk::mesh::impl::Partition* partition = bucketRepository.get_or_create_partition(stk::topology::NODE_RANK, parts); |
| 89 |
|
| 90 |
size_t numNodes = 1024; |
| 91 |
for(size_t i=0; i<numNodes; i++) |
| 92 |
{ |
| 93 |
stk::mesh::EntityId nodeID = i+1; |
| 94 |
stk::mesh::EntityKey nodeKey(stk::topology::NODE_RANK, nodeID); |
| 95 |
std::pair<stk::mesh::entity_iterator,bool> createResult = entityKeyMapping.internal_create_entity(nodeKey); |
| 96 |
bool aNewEntityWasCreated = createResult.second; |
| 97 |
EXPECT_TRUE(aNewEntityWasCreated); |
| 98 |
stk::mesh::Entity node = stkMeshBulkData.my_generate_new_entity(); |
| 99 |
stkMeshBulkData.my_set_entity_key(node, nodeKey); |
| 100 |
partition->add(node); |
| 101 |
} |
| 102 |
|
| 103 |
const size_t maxBucketCapacity = stk::mesh::get_default_maximum_bucket_capacity(); |
| 104 |
size_t expectedNumBuckets = (numNodes+maxBucketCapacity-1)/maxBucketCapacity; |
| 105 |
EXPECT_EQ(expectedNumBuckets, partition->num_buckets()); |
| 106 |
|
| 107 |
const stk::mesh::BucketVector & nodeBuckets = bucketRepository.buckets(stk::topology::NODE_RANK); |
| 108 |
EXPECT_EQ(expectedNumBuckets, nodeBuckets.size()); |
| 109 |
|
| 110 |
size_t numRemainingNodes = numNodes; |
| 111 |
for (const stk::mesh::Bucket * bucket : nodeBuckets) { |
| 112 |
size_t expectedSize = std::min(maxBucketCapacity, numRemainingNodes); |
| 113 |
EXPECT_EQ(expectedSize, bucket->size()); |
| 114 |
numRemainingNodes -= bucket->size(); |
| 115 |
} |
| 116 |
const size_t lastBucketSize = nodeBuckets.back()->size(); |
| 117 |
const bool lastBucketMaxedOut = (lastBucketSize >= maxBucketCapacity); |
| 118 |
|
| 119 |
stk::mesh::EntityId nodeID = numNodes+1; |
| 120 |
stk::mesh::EntityKey nodeKey(stk::topology::NODE_RANK, nodeID); |
| 121 |
std::pair<stk::mesh::entity_iterator,bool> createResult = entityKeyMapping.internal_create_entity(nodeKey); |
| 122 |
bool aNewEntityWasCreated = createResult.second; |
| 123 |
EXPECT_TRUE(aNewEntityWasCreated); |
| 124 |
stk::mesh::Entity node = stkMeshBulkData.my_generate_new_entity(); |
| 125 |
stkMeshBulkData.my_set_entity_key(node, nodeKey); |
| 126 |
partition->add(node); |
| 127 |
|
| 128 |
expectedNumBuckets = (lastBucketMaxedOut) ? expectedNumBuckets + 1 : expectedNumBuckets; |
| 129 |
|
| 130 |
const stk::mesh::BucketVector & newNodeBuckets = bucketRepository.buckets(stk::topology::NODE_RANK); |
| 131 |
EXPECT_EQ(expectedNumBuckets, newNodeBuckets.size()); |
| 132 |
EXPECT_EQ(expectedNumBuckets, partition->num_buckets()); |
| 133 |
|
| 134 |
const size_t newLastBucketSize = (lastBucketMaxedOut) ? 1 : lastBucketSize + 1; |
| 135 |
EXPECT_EQ(newLastBucketSize, newNodeBuckets.back()->size()); |
| 136 |
} |