| 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 |
|
| 36 |
|
| 37 |
#include <gtest/gtest.h> // for AssertHelper, EXPECT_EQ, etc |
| 38 |
#include <stddef.h> // for size_t |
| 39 |
#include <ostream> // for operator<<, ostream |
| 40 |
#include <stk_mesh/base/ModificationNotifier.hpp> |
| 41 |
#include <stk_mesh/base/ModificationObserver.hpp> |
| 42 |
#include <stk_mesh/base/Types.hpp> // for EntityRank |
| 43 |
#include <stk_topology/topology.hpp> // for topology, etc |
| 44 |
#include <stk_util/util/ReportHandler.hpp> // for ThrowRequireMsg |
| 45 |
#include <stk_util/parallel/Parallel.hpp> // for ParallelMachine, etc |
| 46 |
#include <stk_util/parallel/ParallelReduce.hpp> // for all_reduce_max |
| 47 |
#include <vector> // for vector |
| 48 |
#include "mpi.h" // for ompi_communicator_t, etc |
| 49 |
|
| 50 |
namespace { |
| 51 |
|
| 52 |
void reduce_max(std::vector<size_t> &localVector, stk::ParallelMachine comm) |
| 53 |
{ |
| 54 |
std::vector<size_t> globalVector(localVector.size()); |
| 55 |
stk::all_reduce_max(comm, localVector.data(), globalVector.data(), globalVector.size()); |
| 56 |
localVector.swap(globalVector); |
| 57 |
} |
| 58 |
|
| 59 |
class TestListener : public stk::mesh::ModificationObserver { |
| 60 |
public: |
| 61 |
TestListener(stk::ParallelMachine comm) |
| 62 |
: stk::mesh::ModificationObserver(stk::mesh::ModificationObserverPriority::APPLICATION), |
| 63 |
m_comm(comm), |
| 64 |
m_local_entities_created_or_deleted(5, 0), |
| 65 |
m_entity_comm_info_changed(5, 0), |
| 66 |
m_buckets_changed(5, 0) |
| 67 |
{} |
| 68 |
|
| 69 |
virtual void local_entities_created_or_deleted_notification(stk::mesh::EntityRank rank) |
| 70 |
{ |
| 71 |
STK_ThrowRequireMsg(m_local_entities_created_or_deleted.size() > static_cast<unsigned>(rank), "TestListener::local_entities_created_or_deleted ERROR, rank ("<<rank<<") out of range."); |
| 72 |
m_local_entities_created_or_deleted[rank]++; |
| 73 |
} |
| 74 |
|
| 75 |
virtual void local_entity_comm_info_changed_notification(stk::mesh::EntityRank rank) |
| 76 |
{ |
| 77 |
STK_ThrowRequireMsg(m_entity_comm_info_changed.size() > static_cast<unsigned>(rank), "TestListener::global_entity_comm_info_changed ERROR, rank ("<<rank<<") out of range."); |
| 78 |
m_entity_comm_info_changed[rank]++; |
| 79 |
} |
| 80 |
|
| 81 |
virtual void local_buckets_changed_notification(stk::mesh::EntityRank rank) |
| 82 |
{ |
| 83 |
STK_ThrowRequireMsg(m_buckets_changed.size() > static_cast<unsigned>(rank), "TestListener::global_buckets_changed ERROR, rank ("<<rank<<") out of range."); |
| 84 |
m_buckets_changed[rank]++; |
| 85 |
} |
| 86 |
|
| 87 |
virtual void finished_modification_end_notification() |
| 88 |
{ |
| 89 |
reduce_max(m_local_entities_created_or_deleted, m_comm); |
| 90 |
reduce_max(m_entity_comm_info_changed, m_comm); |
| 91 |
reduce_max(m_buckets_changed, m_comm); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
size_t get_local_entities_created_or_deleted(stk::mesh::EntityRank rank) const |
| 96 |
{ |
| 97 |
return m_local_entities_created_or_deleted[rank]; |
| 98 |
} |
| 99 |
|
| 100 |
size_t get_global_entity_comm_info_changed(stk::mesh::EntityRank rank) const |
| 101 |
{ |
| 102 |
return m_entity_comm_info_changed[rank]; |
| 103 |
} |
| 104 |
|
| 105 |
size_t get_global_buckets_changed(stk::mesh::EntityRank rank) const |
| 106 |
{ |
| 107 |
return m_buckets_changed[rank]; |
| 108 |
} |
| 109 |
|
| 110 |
private: |
| 111 |
stk::ParallelMachine m_comm; |
| 112 |
std::vector<size_t> m_local_entities_created_or_deleted; |
| 113 |
std::vector<size_t> m_entity_comm_info_changed; |
| 114 |
std::vector<size_t> m_buckets_changed; |
| 115 |
}; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
TEST(MeshModNotifier, testLocalEvents) |
| 120 |
{ |
| 121 |
std::shared_ptr<TestListener> listener = std::make_shared<TestListener>(MPI_COMM_WORLD); |
| 122 |
stk::mesh::ModificationNotifier notifier; |
| 123 |
notifier.register_observer(listener); |
| 124 |
|
| 125 |
EXPECT_EQ(0u, listener->get_local_entities_created_or_deleted(stk::topology::NODE_RANK)); |
| 126 |
|
| 127 |
notifier.notify_local_entities_created_or_deleted(stk::topology::NODE_RANK); |
| 128 |
|
| 129 |
EXPECT_EQ(1u, listener->get_local_entities_created_or_deleted(stk::topology::NODE_RANK)); |
| 130 |
} |
| 131 |
|
| 132 |
TEST(MeshModNotifier, testGlobalEvents) |
| 133 |
{ |
| 134 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 135 |
if (stk::parallel_machine_size(comm) == 2) |
| 136 |
{ |
| 137 |
int procId = stk::parallel_machine_rank(comm); |
| 138 |
|
| 139 |
std::shared_ptr<TestListener> listener = std::make_shared<TestListener>(comm); |
| 140 |
stk::mesh::ModificationNotifier notifier; |
| 141 |
notifier.register_observer(listener); |
| 142 |
|
| 143 |
EXPECT_EQ(0u, listener->get_global_entity_comm_info_changed(stk::topology::NODE_RANK)); |
| 144 |
EXPECT_EQ(0u, listener->get_global_buckets_changed(stk::topology::NODE_RANK)); |
| 145 |
|
| 146 |
if (procId == 0) { |
| 147 |
notifier.notify_local_entity_comm_info_changed(stk::topology::NODE_RANK); |
| 148 |
notifier.notify_local_buckets_changed(stk::topology::NODE_RANK); |
| 149 |
} |
| 150 |
notifier.notify_finished_modification_end(comm); |
| 151 |
|
| 152 |
EXPECT_EQ(1u, listener->get_global_entity_comm_info_changed(stk::topology::NODE_RANK)); |
| 153 |
EXPECT_EQ(1u, listener->get_global_buckets_changed(stk::topology::NODE_RANK)); |
| 154 |
} |
| 155 |
} |
| 156 |
|