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