| 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 <string> |
| 36 |
#include <gtest/gtest.h> |
| 37 |
#include <stk_mesh/base/BulkData.hpp> |
| 38 |
#include <stk_mesh/base/GetEntities.hpp> |
| 39 |
#include <stk_util/environment/WallTime.hpp> |
| 40 |
#include <stk_util/environment/perf_util.hpp> |
| 41 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 42 |
#include <stk_unit_test_utils/timer.hpp> |
| 43 |
#include <cstdlib> |
| 44 |
|
| 45 |
using EntityIdPair = std::pair<stk::mesh::EntityId,stk::mesh::EntityId>; |
| 46 |
|
| 47 |
class StressEntityKeyMapping : public stk::unit_test_util::MeshFixture |
| 48 |
{ |
| 49 |
public: |
| 50 |
StressEntityKeyMapping() |
| 51 |
{ } |
| 52 |
|
| 53 |
void setup_host_mesh() |
| 54 |
{ |
| 55 |
setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA); |
| 56 |
get_meta().declare_part_with_topology("block_2", stk::topology::HEX_8); |
| 57 |
stk::io::fill_mesh("generated:500x500x10", get_bulk()); |
| 58 |
} |
| 59 |
|
| 60 |
EntityIdPair get_min_max_elem_ids_on_local_proc() |
| 61 |
{ |
| 62 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stk::topology::ELEM_RANK, get_meta().locally_owned_part()); |
| 63 |
stk::mesh::EntityId minElemId = std::numeric_limits<stk::mesh::EntityId>::max(); |
| 64 |
stk::mesh::EntityId maxElemId = 0; |
| 65 |
|
| 66 |
for(const stk::mesh::Bucket* bucket : buckets) { |
| 67 |
minElemId = std::min(minElemId, get_bulk().identifier((*bucket)[0])); |
| 68 |
stk::mesh::Entity lastElem = *std::prev(bucket->end()); |
| 69 |
maxElemId = std::max(maxElemId, get_bulk().identifier(lastElem)); |
| 70 |
} |
| 71 |
return std::make_pair(minElemId, maxElemId); |
| 72 |
} |
| 73 |
|
| 74 |
stk::mesh::Entity get_owned_element(stk::mesh::Entity node) |
| 75 |
{ |
| 76 |
const unsigned numElems = get_bulk().num_elements(node); |
| 77 |
const stk::mesh::Entity* elems = get_bulk().begin_elements(node); |
| 78 |
for(unsigned i=0; i<numElems; ++i) { |
| 79 |
if (get_bulk().bucket(elems[i]).owned()) { |
| 80 |
return elems[i]; |
| 81 |
} |
| 82 |
} |
| 83 |
return stk::mesh::Entity(); |
| 84 |
} |
| 85 |
|
| 86 |
void add_1_element_to_block_2() |
| 87 |
{ |
| 88 |
stk::mesh::Part& block2 = *get_meta().get_part("block_2"); |
| 89 |
stk::mesh::Selector sharedButNotBlock2 = get_meta().globally_shared_part() & !block2; |
| 90 |
const stk::mesh::BucketVector& nodeBuckets = get_bulk().get_buckets(stk::topology::NODE_RANK, sharedButNotBlock2); |
| 91 |
STK_ThrowRequire(!nodeBuckets.empty()); |
| 92 |
STK_ThrowRequire(nodeBuckets[0]->size() > 0); |
| 93 |
|
| 94 |
stk::mesh::Entity node = (*nodeBuckets[0])[0]; |
| 95 |
stk::mesh::Entity elem = get_owned_element(node); |
| 96 |
STK_ThrowRequire(get_bulk().is_valid(elem)); |
| 97 |
get_bulk().change_entity_parts(elem, stk::mesh::ConstPartVector{&block2}); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
stk::mesh::EntityId get_random_id(const EntityIdPair& minMaxElemIds) |
| 102 |
{ |
| 103 |
unsigned numIds = minMaxElemIds.second - minMaxElemIds.first; |
| 104 |
return minMaxElemIds.first + std::rand()%numIds; |
| 105 |
} |
| 106 |
|
| 107 |
TEST_F( StressEntityKeyMapping, Timing ) |
| 108 |
{ |
| 109 |
if (get_parallel_size() != 2) return; |
| 110 |
|
| 111 |
unsigned arbitrarySeed = 1919; |
| 112 |
std::srand(arbitrarySeed); |
| 113 |
|
| 114 |
const unsigned NUM_RUNS = 5; |
| 115 |
const int numElementsToChange = 10; |
| 116 |
const int numGetEntityQueries = 1000000; |
| 117 |
|
| 118 |
stk::unit_test_util::BatchTimer batchTimer(get_comm()); |
| 119 |
batchTimer.initialize_batch_timer(); |
| 120 |
for (unsigned j = 0; j < NUM_RUNS; j++) { |
| 121 |
setup_host_mesh(); |
| 122 |
batchTimer.start_batch_timer(); |
| 123 |
|
| 124 |
for(int i=0; i<numElementsToChange; ++i) { |
| 125 |
get_bulk().modification_begin(); |
| 126 |
add_1_element_to_block_2(); |
| 127 |
get_bulk().modification_end(); |
| 128 |
} |
| 129 |
|
| 130 |
std::pair<stk::mesh::EntityId,stk::mesh::EntityId> minMaxElemIds = get_min_max_elem_ids_on_local_proc(); |
| 131 |
|
| 132 |
for (int i=0; i<numGetEntityQueries; i++) { |
| 133 |
stk::mesh::EntityId id = get_random_id(minMaxElemIds); |
| 134 |
get_bulk().get_entity(stk::topology::ELEM_RANK, id); |
| 135 |
} |
| 136 |
|
| 137 |
batchTimer.stop_batch_timer(); |
| 138 |
reset_mesh(); |
| 139 |
} |
| 140 |
batchTimer.print_batch_timing(numGetEntityQueries); |
| 141 |
} |