| 1 |
#include "gtest/gtest.h" |
| 2 |
|
| 3 |
#include "stk_middle_mesh/communication_api.hpp" |
| 4 |
#include "stk_middle_mesh/create_mesh.hpp" |
| 5 |
|
| 6 |
using namespace stk::middle_mesh; |
| 7 |
|
| 8 |
namespace { |
| 9 |
|
| 10 |
|
| 11 |
class CommunicationAPISPMDTest |
| 12 |
{ |
| 13 |
public: |
| 14 |
CommunicationAPISPMDTest(MPI_Comm comm) |
| 15 |
{ |
| 16 |
setup(comm); |
| 17 |
} |
| 18 |
|
| 19 |
void runtest() |
| 20 |
{ |
| 21 |
send_and_check_data_both_directions(); |
| 22 |
} |
| 23 |
|
| 24 |
private: |
| 25 |
|
| 26 |
void setup(MPI_Comm comm) |
| 27 |
{ |
| 28 |
m_comm = comm; |
| 29 |
|
| 30 |
int commSize = utils::impl::comm_size(comm); |
| 31 |
mesh::impl::MeshSpec spec; |
| 32 |
spec.xmin = 0; spec.ymin = 0; |
| 33 |
spec.xmax = 1; spec.ymax = 1; |
| 34 |
spec.numelX = commSize, spec.numelY = commSize; |
| 35 |
|
| 36 |
auto f = [](const utils::Point& pt) { return pt; }; |
| 37 |
m_mesh1 = create_mesh(spec, f, comm); |
| 38 |
m_mesh2 = create_mesh(spec, f, comm); |
| 39 |
} |
| 40 |
|
| 41 |
mesh::FieldPtr<mesh::RemoteSharedEntity> create_dest_field(std::shared_ptr<mesh::Mesh> mesh) |
| 42 |
{ |
| 43 |
auto fieldPtr = mesh::create_field<mesh::RemoteSharedEntity>(mesh, mesh::FieldShape(0, 0, 1), 1); |
| 44 |
|
| 45 |
int destRank = utils::impl::comm_rank(m_comm); |
| 46 |
int numEls = mesh->get_elements().size(); |
| 47 |
auto& field = *fieldPtr; |
| 48 |
for (auto el : mesh->get_elements()) |
| 49 |
{ |
| 50 |
int destLocalId = (el->get_id() + 1) % numEls; |
| 51 |
field(el, 0, 0) = mesh::RemoteSharedEntity{destRank, destLocalId}; |
| 52 |
} |
| 53 |
|
| 54 |
return fieldPtr; |
| 55 |
} |
| 56 |
|
| 57 |
mesh::FieldPtr<int> create_send_field(std::shared_ptr<mesh::Mesh> mesh, mesh::FieldPtr<mesh::RemoteSharedEntity> remoteInfoPtr) |
| 58 |
{ |
| 59 |
auto fieldPtr = mesh::create_field<int>(mesh, mesh::FieldShape(0, 0, m_numNodesPerElement), m_numComponentsPerNode); |
| 60 |
|
| 61 |
auto& remoteInfo = *remoteInfoPtr; |
| 62 |
auto& field = *fieldPtr; |
| 63 |
for (auto& el : mesh->get_elements()) |
| 64 |
if (el) |
| 65 |
{ |
| 66 |
mesh::RemoteSharedEntity remote = remoteInfo(el, 0, 0); |
| 67 |
for (int i=0; i < m_numNodesPerElement; ++i) |
| 68 |
for (int j=0; j < m_numComponentsPerNode; ++j) |
| 69 |
field(el, i, j) = get_unique_value(remote.remoteRank, remote.remoteId, i, j); |
| 70 |
} |
| 71 |
|
| 72 |
return fieldPtr; |
| 73 |
} |
| 74 |
|
| 75 |
void send_and_check_data_both_directions() |
| 76 |
{ |
| 77 |
auto remoteInfo1 = create_dest_field(m_mesh1); |
| 78 |
auto remoteInfo2 = create_dest_field(m_mesh2); |
| 79 |
stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(m_comm, m_mesh1, m_mesh2, remoteInfo1, remoteInfo2); |
| 80 |
|
| 81 |
send_and_check_data(exchanger, remoteInfo1, remoteInfo2, true); |
| 82 |
send_and_check_data(exchanger, remoteInfo1, remoteInfo2, false); |
| 83 |
} |
| 84 |
|
| 85 |
void send_and_check_data(stk::middle_mesh::MiddleMeshFieldCommunication<int>& exchanger, |
| 86 |
mesh::FieldPtr<mesh::RemoteSharedEntity> remoteInfo1, |
| 87 |
mesh::FieldPtr<mesh::RemoteSharedEntity> remoteInfo2, |
| 88 |
bool sendOneToTwo) |
| 89 |
{ |
| 90 |
auto sendMesh = sendOneToTwo ? m_mesh1 : m_mesh2; |
| 91 |
auto recvMesh = sendOneToTwo ? m_mesh2 : m_mesh1; |
| 92 |
auto remoteInfoSend = sendOneToTwo ? remoteInfo1 : remoteInfo2; |
| 93 |
auto remoteInfoRecv = sendOneToTwo ? remoteInfo2 : remoteInfo1; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
mesh::FieldPtr<int> fieldSend = create_send_field(sendMesh, remoteInfoSend); |
| 98 |
mesh::FieldPtr<int> fieldRecv = mesh::create_field<int>(recvMesh, mesh::FieldShape(0, 0, m_numNodesPerElement), |
| 99 |
m_numComponentsPerNode, -1); |
| 100 |
|
| 101 |
exchanger.start_exchange(fieldSend, fieldRecv); |
| 102 |
exchanger.finish_exchange(fieldRecv); |
| 103 |
check_recv_field(fieldRecv); |
| 104 |
} |
| 105 |
|
| 106 |
void check_recv_field(mesh::FieldPtr<int> fieldPtr) |
| 107 |
{ |
| 108 |
int myRank = utils::impl::comm_rank(m_comm); |
| 109 |
auto& field = *fieldPtr; |
| 110 |
for (auto el : fieldPtr->get_mesh()->get_elements()) |
| 111 |
for (int i=0; i < m_numNodesPerElement; ++i) |
| 112 |
for (int j=0; j < m_numComponentsPerNode; ++j) |
| 113 |
EXPECT_EQ(field(el, i, j), get_unique_value(myRank, el->get_id(), i, j)); |
| 114 |
} |
| 115 |
|
| 116 |
int get_unique_value(int rank, int elId, int node, int component) |
| 117 |
{ |
| 118 |
return rank + elId * m_numNodesPerElement * m_numComponentsPerNode + node * m_numComponentsPerNode + component; |
| 119 |
} |
| 120 |
|
| 121 |
private: |
| 122 |
MPI_Comm m_comm; |
| 123 |
std::shared_ptr<mesh::Mesh> m_mesh1; |
| 124 |
std::shared_ptr<mesh::Mesh> m_mesh2; |
| 125 |
|
| 126 |
const int m_numNodesPerElement = 2; |
| 127 |
const int m_numComponentsPerNode = 3; |
| 128 |
}; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
TEST(CommunicationAPISPMD, Values) |
| 133 |
{ |
| 134 |
CommunicationAPISPMDTest tester(MPI_COMM_WORLD); |
| 135 |
tester.runtest(); |
| 136 |
} |
| 137 |
|
| 138 |
TEST(CommunicationAPISPMD, RemoteInfoWrongShape) |
| 139 |
{ |
| 140 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 2) |
| 141 |
GTEST_SKIP(); |
| 142 |
|
| 143 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 144 |
int commSize = utils::impl::comm_size(comm); |
| 145 |
mesh::impl::MeshSpec spec; |
| 146 |
spec.xmin = 0; spec.ymin = 0; |
| 147 |
spec.xmax = 1; spec.ymax = 1; |
| 148 |
spec.numelX = commSize, spec.numelY = commSize; |
| 149 |
|
| 150 |
auto f = [](const utils::Point& pt) { return pt; }; |
| 151 |
auto mesh1 = create_mesh(spec, f, comm); |
| 152 |
auto mesh2 = create_mesh(spec, f, comm); |
| 153 |
|
| 154 |
auto remoteInfo1 = mesh::create_field<mesh::RemoteSharedEntity>(mesh1, mesh::FieldShape(1, 0, 0), 1); |
| 155 |
auto remoteInfo2 = mesh::create_field<mesh::RemoteSharedEntity>(mesh2, mesh::FieldShape(0, 0, 1), 1); |
| 156 |
|
| 157 |
EXPECT_ANY_THROW(stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(comm, mesh1, mesh2, remoteInfo1, remoteInfo2)); |
| 158 |
|
| 159 |
remoteInfo1 = mesh::create_field<mesh::RemoteSharedEntity>(mesh1, mesh::FieldShape(0, 1, 0), 1); |
| 160 |
EXPECT_ANY_THROW(stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(comm, mesh1, mesh2, remoteInfo1, remoteInfo2)); |
| 161 |
|
| 162 |
remoteInfo1 = mesh::create_field<mesh::RemoteSharedEntity>(mesh1, mesh::FieldShape(0, 0, 1), 1); |
| 163 |
remoteInfo2 = mesh::create_field<mesh::RemoteSharedEntity>(mesh2, mesh::FieldShape(1, 0, 0), 1); |
| 164 |
EXPECT_ANY_THROW(stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(comm, mesh1, mesh2, remoteInfo1, remoteInfo2)); |
| 165 |
|
| 166 |
remoteInfo2 = mesh::create_field<mesh::RemoteSharedEntity>(mesh2, mesh::FieldShape(0, 1, 0), 1); |
| 167 |
EXPECT_ANY_THROW(stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(comm, mesh1, mesh2, remoteInfo1, remoteInfo2)); |
| 168 |
|
| 169 |
remoteInfo1 = mesh::create_field<mesh::RemoteSharedEntity>(mesh1, mesh::FieldShape(0, 0, 1), 2); |
| 170 |
EXPECT_ANY_THROW(stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(comm, mesh1, mesh2, remoteInfo1, remoteInfo2)); |
| 171 |
|
| 172 |
remoteInfo1 = mesh::create_field<mesh::RemoteSharedEntity>(mesh1, mesh::FieldShape(0, 0, 1), 1); |
| 173 |
remoteInfo2 = mesh::create_field<mesh::RemoteSharedEntity>(mesh2, mesh::FieldShape(0, 0, 1), 2); |
| 174 |
EXPECT_ANY_THROW(stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(comm, mesh1, mesh2, remoteInfo1, remoteInfo2)); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
TEST(CommunicationAPISPMD, FieldsWrongShape) |
| 179 |
{ |
| 180 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 2) |
| 181 |
GTEST_SKIP(); |
| 182 |
|
| 183 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 184 |
int commSize = utils::impl::comm_size(comm); |
| 185 |
mesh::impl::MeshSpec spec; |
| 186 |
spec.xmin = 0; spec.ymin = 0; |
| 187 |
spec.xmax = 1; spec.ymax = 1; |
| 188 |
spec.numelX = commSize, spec.numelY = commSize; |
| 189 |
|
| 190 |
auto f = [](const utils::Point& pt) { return pt; }; |
| 191 |
auto mesh1 = create_mesh(spec, f, comm); |
| 192 |
auto mesh2 = create_mesh(spec, f, comm); |
| 193 |
|
| 194 |
auto remoteInfo1 = mesh::create_field<mesh::RemoteSharedEntity>(mesh1, mesh::FieldShape(0, 0, 1), 1); |
| 195 |
auto remoteInfo2 = mesh::create_field<mesh::RemoteSharedEntity>(mesh2, mesh::FieldShape(0, 0, 1), 1); |
| 196 |
|
| 197 |
stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(comm, mesh1, mesh2, remoteInfo1, remoteInfo2); |
| 198 |
|
| 199 |
auto field1 = mesh::create_field<int>(mesh1, mesh::FieldShape(1, 0, 0), 1); |
| 200 |
auto field2 = mesh::create_field<int>(mesh2, mesh::FieldShape(0, 0, 1), 1); |
| 201 |
|
| 202 |
EXPECT_ANY_THROW(exchanger.start_exchange(field1, field2)); |
| 203 |
|
| 204 |
field1 = mesh::create_field<int>(mesh1, mesh::FieldShape(0, 1, 0), 1); |
| 205 |
EXPECT_ANY_THROW(exchanger.start_exchange(field1, field2)); |
| 206 |
|
| 207 |
field1 = mesh::create_field<int>(mesh1, mesh::FieldShape(0, 0, 1), 1); |
| 208 |
field2 = mesh::create_field<int>(mesh2, mesh::FieldShape(1, 0, 0), 1); |
| 209 |
EXPECT_ANY_THROW(exchanger.start_exchange(field1, field2)); |
| 210 |
|
| 211 |
field2 = mesh::create_field<int>(mesh2, mesh::FieldShape(0, 1, 0), 1); |
| 212 |
EXPECT_ANY_THROW(exchanger.start_exchange(field1, field2)); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
TEST(CommunicationAPISPMD, FieldsDifferentShapes) |
| 217 |
{ |
| 218 |
if (utils::impl::comm_size(MPI_COMM_WORLD) != 2) |
| 219 |
GTEST_SKIP(); |
| 220 |
|
| 221 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 222 |
int commSize = utils::impl::comm_size(comm); |
| 223 |
mesh::impl::MeshSpec spec; |
| 224 |
spec.xmin = 0; spec.ymin = 0; |
| 225 |
spec.xmax = 1; spec.ymax = 1; |
| 226 |
spec.numelX = commSize, spec.numelY = commSize; |
| 227 |
|
| 228 |
auto f = [](const utils::Point& pt) { return pt; }; |
| 229 |
auto mesh1 = create_mesh(spec, f, comm); |
| 230 |
auto mesh2 = create_mesh(spec, f, comm); |
| 231 |
|
| 232 |
auto remoteInfo1 = mesh::create_field<mesh::RemoteSharedEntity>(mesh1, mesh::FieldShape(0, 0, 1), 1); |
| 233 |
auto remoteInfo2 = mesh::create_field<mesh::RemoteSharedEntity>(mesh2, mesh::FieldShape(0, 0, 1), 1); |
| 234 |
|
| 235 |
stk::middle_mesh::MiddleMeshFieldCommunication<int> exchanger(comm, mesh1, mesh2, remoteInfo1, remoteInfo2); |
| 236 |
|
| 237 |
auto field1 = mesh::create_field<int>(mesh1, mesh::FieldShape(0, 0, 1), 2); |
| 238 |
auto field2 = mesh::create_field<int>(mesh2, mesh::FieldShape(0, 0, 1), 1); |
| 239 |
EXPECT_ANY_THROW(exchanger.start_exchange(field1, field2)); |
| 240 |
|
| 241 |
field1 = mesh::create_field<int>(mesh1, mesh::FieldShape(0, 0, 2), 1); |
| 242 |
field2 = mesh::create_field<int>(mesh2, mesh::FieldShape(0, 0, 1), 1); |
| 243 |
EXPECT_ANY_THROW(exchanger.start_exchange(field1, field2)); |
| 244 |
} |