Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <array>
#include <iostream>
#include "Communicator.hpp"
Communicator::Communicator(const MpiEnvironment& env, const Size& gridSize,
const Size& tileSize) {
// Begin definition of basic types
MPI_Type_contiguous(tileSize.Cols, MPI_CHAR, &_haloRowType);
MPI_Type_commit(&_haloRowType);
MPI_Type_vector(tileSize.Rows, 1, tileSize.Cols + 2, MPI_CHAR,
&_haloColumnType);
MPI_Type_commit(&_haloColumnType);
// End definition of basic types
// Begin definition of datastructures for a general cell
const std::array<MPI_Datatype, NoNeighbors> generalSendTypes{
_haloCornerType, _haloRowType, _haloCornerType, //
_haloColumnType, _haloColumnType, //
_haloCornerType, _haloRowType, _haloCornerType //
};
const auto tCols = tileSize.Cols;
const auto tRows = tileSize.Rows;
const auto dp =
[&](std::size_t x,
std::size_t y) { // character coordinates to displacement
return static_cast<MPI_Aint>(y * (tCols + 2) + x);
};
const std::array<MPI_Aint, NoNeighbors> generalSendDisplacements{
dp(1, 1), dp(1, 1), dp(tCols, 1), //
dp(1, 1), dp(tCols, 1), //
dp(1, tRows), dp(1, tRows), dp(tCols, tRows) //
};
const std::array<MPI_Aint, NoNeighbors> generalRecvDisplacements{
dp(0, 0), dp(1, 0), dp(tCols + 1, 0), //
dp(0, 1), dp(tCols + 1, 1), //
dp(0, tRows + 1), dp(1, tRows + 1), dp(tCols + 1, tRows + 1) //
};
const std::array<int, NoNeighbors> generalSizes{
1, 1, 1, //
1, 1, //
1, 1, 1 //
};
// End definition of datastructures for a general cell
// Begin definition of datastructures for this particular cell
const auto rank2coord = [&](std::size_t rank) {
return Coord{
rank % gridSize.Cols, //
rank / gridSize.Cols //
};
};
const auto coord2rank = [&](Coord c) { return gridSize.Cols * c.Y + c.X; };
const auto isInsideGrid = [&](Coord c) {
return c.X < gridSize.Cols && c.Y < gridSize.Rows;
};
const auto myCoord = rank2coord(env.worldRank());
const std::array<Coord, NoNeighbors> virtualNeighborCoords{{
{myCoord.X - 1, myCoord.Y - 1}, // intentional signed underflow
{myCoord.X + 0, myCoord.Y - 1}, //
{myCoord.X + 1, myCoord.Y - 1}, //
{myCoord.X - 1, myCoord.Y + 0}, //
{myCoord.X + 1, myCoord.Y + 0}, //
{myCoord.X - 1, myCoord.Y + 1}, //
{myCoord.X + 0, myCoord.Y + 1}, //
{myCoord.X + 1, myCoord.Y + 1} //
}};
for (std::size_t i{0}; i < NoNeighbors; ++i) {
const auto nbrCoord = virtualNeighborCoords[i];
if (isInsideGrid(nbrCoord)) {
_neighbors.push_back(coord2rank(nbrCoord));
_sendTypes.push_back(generalSendTypes[i]);
_sendDisplacements.push_back(generalSendDisplacements[i]);
_recvDisplacements.push_back(generalRecvDisplacements[i]);
_sizes.push_back(generalSizes[i]);
}
}
MPI_Dist_graph_create_adjacent(
MPI_COMM_WORLD, // comm_old
_neighbors.size(), // indegree
_neighbors.data(), // sources
reinterpret_cast<int*>(MPI_UNWEIGHTED), // sourceweights
_neighbors.size(), // outdegree
_neighbors.data(), // destinations
reinterpret_cast<int*>(MPI_UNWEIGHTED), // destweights
MPI_INFO_NULL, // info
0, // reorder
&_commDistGraph // comm_dist_graph
);
// End definition of datastructures for this particular cell
}
Communicator::~Communicator() {
if (_commDistGraph != MPI_COMM_NULL) {
MPI_Comm_free(&_commDistGraph);
MPI_Type_free(&_haloColumnType);
MPI_Type_free(&_haloRowType);
}
}
void Communicator::swap(Communicator& first, Communicator& second) {
using std::swap;
swap(first._neighbors, second._neighbors);
swap(first._sizes, second._sizes);
swap(first._sendTypes, second._sendTypes);
swap(first._sendDisplacements, second._sendDisplacements);
swap(first._recvDisplacements, second._recvDisplacements);
swap(first._commDistGraph, second._commDistGraph);
swap(first._haloRowType, second._haloRowType);
swap(first._haloColumnType, second._haloColumnType);
swap(first._haloCornerType, second._haloCornerType);
}
Communicator::Communicator(Communicator&& other) noexcept {
swap(*this, other);
}
Communicator& Communicator::operator=(Communicator&& other) noexcept {
swap(*this, other);
return *this;
}
void Communicator::Communicate(gsl::multi_span<State, -1, -1>& model) {
if (_commDistGraph == MPI_COMM_NULL)
throw std::logic_error("Communicator not initialized");
MPI_Neighbor_alltoallw(model.data(), // sendbuf
_sizes.data(), // sendcounts
_sendDisplacements.data(), // sdispl
_sendTypes.data(), // sendtypes
model.data(), // recvbuf
_sizes.data(), // recvcounts
_recvDisplacements.data(), // rdispls
_recvTypes.data(), // recvtypes
_commDistGraph // comm
);
}
Communicator::MpiRequest::MpiRequest(MPI_Request req) : _req(req) {}
void Communicator::MpiRequest::Wait() {
MPI_Wait(&_req, MPI_STATUS_IGNORE);
finished = true;
}
Communicator::MpiRequest::~MpiRequest() {
if (!finished) {
std::cerr << "Forgot to Wait for MPI_Request\n";
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
}
Communicator::MpiRequest
Communicator::AsyncCommunicate(gsl::multi_span<State, -1, -1>& model) {
if (_commDistGraph == MPI_COMM_NULL)
throw std::logic_error("Communicator not initialized");
MPI_Request req;
MPI_Ineighbor_alltoallw(model.data(), // sendbuf
_sizes.data(), // sendcounts
_sendDisplacements.data(), // sdispl
_sendTypes.data(), // sendtypes
model.data(), // recvbuf
_sizes.data(), // recvcounts
_recvDisplacements.data(), // rdispls
_recvTypes.data(), // recvtypes
_commDistGraph, // comm
&req // request
);
return MpiRequest{req};
}