| 1 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 2 |
#include <stddef.h> // for size_t |
| 3 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData, etc |
| 4 |
#include <stk_mesh/base/ModificationObserver.hpp> |
| 5 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 6 |
#include <stk_unit_test_utils/ioUtils.hpp> // for fill_mesh_using_stk_io |
| 7 |
#include <stk_util/parallel/Parallel.hpp> // for ParallelMachine, etc |
| 8 |
#include <stk_util/parallel/ParallelReduce.hpp> // for all_reduce_max |
| 9 |
#include <string> // for string |
| 10 |
#include <stk_unit_test_utils/BulkDataTester.hpp> |
| 11 |
#include <vector> // for vector |
| 12 |
#include "mpi.h" // for ompi_communicator_t, etc |
| 13 |
#include "stk_mesh/base/Entity.hpp" // for Entity |
| 14 |
#include "stk_mesh/base/MetaData.hpp" // for MetaData |
| 15 |
#include "stk_mesh/base/Types.hpp" // for EntityRank |
| 16 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 17 |
namespace stk { namespace mesh { class Part; } } |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
void reduce_max(std::vector<size_t> &localVector, stk::ParallelMachine comm) |
| 22 |
{ |
| 23 |
std::vector<size_t> globalVector(localVector.size()); |
| 24 |
stk::all_reduce_max(comm, localVector.data(), globalVector.data(), globalVector.size()); |
| 25 |
localVector.swap(globalVector); |
| 26 |
} |
| 27 |
|
| 28 |
class TestListener : public stk::mesh::ModificationObserver { |
| 29 |
public: |
| 30 |
TestListener(stk::ParallelMachine communicator) |
| 31 |
: stk::mesh::ModificationObserver(stk::mesh::ModificationObserverPriority::APPLICATION), |
| 32 |
comm(communicator), |
| 33 |
buckets_changed(5, 0) |
| 34 |
{} |
| 35 |
|
| 36 |
virtual void local_buckets_changed_notification(stk::mesh::EntityRank rank) |
| 37 |
{ |
| 38 |
buckets_changed[rank]++; |
| 39 |
} |
| 40 |
|
| 41 |
virtual void finished_modification_end_notification() |
| 42 |
{ |
| 43 |
reduce_max(buckets_changed, comm); |
| 44 |
} |
| 45 |
|
| 46 |
size_t get_buckets_changed(stk::mesh::EntityRank rank) const |
| 47 |
{ return buckets_changed[rank]; } |
| 48 |
|
| 49 |
private: |
| 50 |
stk::ParallelMachine comm; |
| 51 |
std::vector<size_t> buckets_changed; |
| 52 |
}; |
| 53 |
|
| 54 |
TEST(BulkDataMod, synchronized_count_basic_fill_mesh) |
| 55 |
{ |
| 56 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 57 |
if (stk::parallel_machine_size(comm) > 2) { GTEST_SKIP(); } |
| 58 |
|
| 59 |
std::shared_ptr<stk::mesh::BulkData> bulk = stk::mesh::MeshBuilder(comm).create(); |
| 60 |
|
| 61 |
const std::string generatedMeshSpec = "generated:1x1x2"; |
| 62 |
stk::io::fill_mesh(generatedMeshSpec, *bulk); |
| 63 |
|
| 64 |
EXPECT_EQ(1u, bulk->synchronized_count()); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(BulkDataMod, synchronized_count_empty_mod_cycle) |
| 68 |
{ |
| 69 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 70 |
if (stk::parallel_machine_size(comm) > 2) { GTEST_SKIP(); } |
| 71 |
|
| 72 |
std::shared_ptr<stk::mesh::BulkData> bulk = stk::mesh::MeshBuilder(comm).create(); |
| 73 |
|
| 74 |
const std::string generatedMeshSpec = "generated:1x1x2"; |
| 75 |
stk::io::fill_mesh(generatedMeshSpec, *bulk); |
| 76 |
|
| 77 |
unsigned modCount = bulk->synchronized_count(); |
| 78 |
|
| 79 |
bulk->modification_begin(); |
| 80 |
bulk->modification_end(); |
| 81 |
|
| 82 |
EXPECT_EQ(modCount, bulk->synchronized_count()); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(BulkDataNotifications, test_listener_buckets_changed) |
| 86 |
{ |
| 87 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 88 |
int numProcs = stk::parallel_machine_size(comm); |
| 89 |
if (numProcs==2) |
| 90 |
{ |
| 91 |
stk::mesh::MetaData meta(3); |
| 92 |
stk::unit_test_util::BulkDataTester mesh(meta, comm, stk::mesh::BulkData::NO_AUTO_AURA); |
| 93 |
|
| 94 |
const std::string generatedMeshSpec = "generated:1x1x2"; |
| 95 |
stk::io::fill_mesh(generatedMeshSpec, mesh); |
| 96 |
|
| 97 |
std::shared_ptr<TestListener> listener = std::make_shared<TestListener>(comm); |
| 98 |
mesh.register_observer(listener); |
| 99 |
|
| 100 |
int procId = stk::parallel_machine_rank(comm); |
| 101 |
|
| 102 |
stk::mesh::Part& newPart = meta.declare_part("new part", stk::topology::NODE_RANK); |
| 103 |
stk::mesh::Entity node1 = mesh.get_entity(stk::topology::NODE_RANK, 1); |
| 104 |
|
| 105 |
if (procId == 0) { |
| 106 |
EXPECT_TRUE(mesh.is_valid(node1)); |
| 107 |
} |
| 108 |
else { |
| 109 |
EXPECT_FALSE(mesh.is_valid(node1)); |
| 110 |
} |
| 111 |
|
| 112 |
mesh.modification_begin(); |
| 113 |
if (procId == 0) { |
| 114 |
mesh.change_entity_parts(node1, stk::mesh::ConstPartVector{&newPart}); |
| 115 |
} |
| 116 |
mesh.modification_end(); |
| 117 |
|
| 118 |
EXPECT_EQ(1u, listener->get_buckets_changed(stk::topology::NODE_RANK)); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
class TestOrderingObserver : public stk::mesh::ModificationObserver { |
| 123 |
public: |
| 124 |
TestOrderingObserver(int id, std::vector<int> & evaluationOrder) |
| 125 |
: stk::mesh::ModificationObserver(stk::mesh::ModificationObserverPriority::APPLICATION), |
| 126 |
m_id(id), |
| 127 |
m_evaluationOrder(evaluationOrder) |
| 128 |
{} |
| 129 |
|
| 130 |
~TestOrderingObserver() = default; |
| 131 |
|
| 132 |
virtual void finished_modification_end_notification() |
| 133 |
{ |
| 134 |
m_evaluationOrder.push_back(m_id); |
| 135 |
} |
| 136 |
|
| 137 |
private: |
| 138 |
int m_id; |
| 139 |
std::vector<int> & m_evaluationOrder; |
| 140 |
}; |
| 141 |
|
| 142 |
TEST(BulkDataNotifications, test_ordering_different_priorities) |
| 143 |
{ |
| 144 |
stk::mesh::ModificationNotifier notifier; |
| 145 |
|
| 146 |
std::vector<int> evaluationOrder; |
| 147 |
std::shared_ptr<TestOrderingObserver> observer1 = std::make_shared<TestOrderingObserver>(1, evaluationOrder); |
| 148 |
std::shared_ptr<TestOrderingObserver> observer2 = std::make_shared<TestOrderingObserver>(2, evaluationOrder); |
| 149 |
std::shared_ptr<TestOrderingObserver> observer3 = std::make_shared<TestOrderingObserver>(3, evaluationOrder); |
| 150 |
|
| 151 |
observer3->set_priority(stk::mesh::ModificationObserverPriority::APPLICATION); |
| 152 |
observer1->set_priority(stk::mesh::ModificationObserverPriority::STK_INTERNAL_LOW_PRIORITY); |
| 153 |
observer2->set_priority(stk::mesh::ModificationObserverPriority::STK_TRANSITION); |
| 154 |
|
| 155 |
notifier.register_observer(observer3); |
| 156 |
notifier.register_observer(observer1); |
| 157 |
notifier.register_observer(observer2); |
| 158 |
|
| 159 |
notifier.notify_finished_modification_end(MPI_COMM_WORLD); |
| 160 |
|
| 161 |
EXPECT_EQ(evaluationOrder, (std::vector<int>{1, 2, 3})); |
| 162 |
} |
| 163 |
|
| 164 |
TEST(BulkDataNotifications, test_ordering_duplicate_priorities) |
| 165 |
{ |
| 166 |
stk::mesh::ModificationNotifier notifier; |
| 167 |
|
| 168 |
std::vector<int> evaluationOrder; |
| 169 |
std::shared_ptr<TestOrderingObserver> observer1 = std::make_shared<TestOrderingObserver>(1, evaluationOrder); |
| 170 |
std::shared_ptr<TestOrderingObserver> observer2 = std::make_shared<TestOrderingObserver>(2, evaluationOrder); |
| 171 |
std::shared_ptr<TestOrderingObserver> observer3 = std::make_shared<TestOrderingObserver>(3, evaluationOrder); |
| 172 |
|
| 173 |
observer3->set_priority(stk::mesh::ModificationObserverPriority::STK_INTERNAL_LOW_PRIORITY); |
| 174 |
observer1->set_priority(stk::mesh::ModificationObserverPriority::STK_INTERNAL_LOW_PRIORITY); |
| 175 |
observer2->set_priority(stk::mesh::ModificationObserverPriority::STK_INTERNAL_LOW_PRIORITY); |
| 176 |
|
| 177 |
notifier.register_observer(observer3); |
| 178 |
notifier.register_observer(observer1); |
| 179 |
notifier.register_observer(observer2); |
| 180 |
|
| 181 |
notifier.notify_finished_modification_end(MPI_COMM_WORLD); |
| 182 |
|
| 183 |
EXPECT_EQ(evaluationOrder, (std::vector<int>{3, 1, 2})); |
| 184 |
} |
| 185 |
|
| 186 |
} |