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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include "mpicomm.h"
#include "mpitypes.h"
#include "mesh.h"
#if MPI_VERSION < 3
#pragma message("MPI_Version < 3 => Sparse collectives are not supported.")
#endif
// --------------------------------------------- Helper function declarations
// ==========================================================================
void mpi_broadcast_configuration_start(conf_t *configuration, MPI_Request *request)
{
MPI_Datatype conf_mpitype;
mpitype_conf_init(&conf_mpitype);
#if MPI_VERSION >= 3
MPI_Ibcast(configuration, 1, conf_mpitype, 0, MPI_COMM_WORLD, request);
#else
MPI_Bcast(configuration, 1, conf_mpitype, 0, MPI_COMM_WORLD);
*request = MPI_REQUEST_NULL;
#endif
mpitype_conf_free(&conf_mpitype);
}
void mpi_broadcast_configuration_finish(MPI_Request *request)
{
// Note for case MPI_VERSION < 3:
// Calling MPI_Wait with MPI_REQUEST_NULL is safe and will return immediately.
MPI_Wait(request, MPI_STATUS_IGNORE);
}
void mpi_broadcast_decomposition(box_t *decomposition)
{
int nprocs;
MPI_Datatype box_mpitype;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
mpitype_box_init(&box_mpitype);
MPI_Bcast(decomposition, nprocs, box_mpitype, 0, MPI_COMM_WORLD);
mpitype_box_free(&box_mpitype);
}
void mpi_create_graph_communicator(const mesh_t *mesh, const conf_t *configuration, MPI_Comm *graph_comm)
{
int n_neighbors = mesh->n_neighbors;
int *buffer = malloc(3 * n_neighbors * sizeof(*buffer));
int *neighbor_ranks = buffer + 0 * n_neighbors;
int *recv_weights = buffer + 1 * n_neighbors;
int *send_weights = buffer + 2 * n_neighbors;
const neighbor_info *nb;
int allow_reorder = 1;
int i;
for(i = 0; i < n_neighbors; i++) {
nb = &mesh->neighbors[i];
neighbor_ranks[i] = mesh->neighbors[i].mpi_rank;
recv_weights[i] = mesh->neighbors[i].halo_incoming.domain.volume;
send_weights[i] = mesh->neighbors[i].halo_outgoing.domain.volume;
}
MPI_Dist_graph_create_adjacent(
MPI_COMM_WORLD,
n_neighbors, neighbor_ranks, recv_weights,
n_neighbors, neighbor_ranks, send_weights,
MPI_INFO_NULL, allow_reorder,
graph_comm);
free(buffer);
}
void mpi_halo_exchange_int_sparse_collective(int_field_t *field)
{
#if MPI_VERSION >= 3
const mesh_t *mesh = field->mesh;
int *data = field->data;
int n_neighbors = mesh->n_neighbors;
int i;
int *counts = malloc(n_neighbors * sizeof(*counts));
MPI_Aint *displs = malloc(n_neighbors * sizeof(*displs));
MPI_Datatype *types = malloc(2 * n_neighbors * sizeof(*types));
MPI_Datatype *send_types = types + 0 * n_neighbors;
MPI_Datatype *recv_types = types + 1 * n_neighbors;
for(i = 0; i < n_neighbors; i++) {
counts[i] = 1;
displs[i] = 0;
send_types[i] = mesh->neighbors[i].halo_outgoing.transfer_type_int;
recv_types[i] = mesh->neighbors[i].halo_incoming.transfer_type_int;
}
MPI_Neighbor_alltoallw(
data, counts, displs, send_types,
data, counts, displs, recv_types,
mesh->communicator
);
free(counts);
free(displs);
free(types);
#else
fprintf(stderr, "Your MPI implementation does not support sparse collective operations.\n");
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
#endif
}
void mpi_halo_exchange_int_collective(int_field_t *field)
{
const mesh_t *mesh = field->mesh;
int *data = field->data;
int n_neighbors = mesh->n_neighbors;
int nprocs, i, nb_rank;
MPI_Comm_size(mesh->communicator, &nprocs);
int *counts = malloc(nprocs * sizeof(*counts));
int *displs = malloc(nprocs * sizeof(*displs));
MPI_Datatype *types = malloc(2 * nprocs * sizeof(*types));
MPI_Datatype *send_types = types + 0 * nprocs;
MPI_Datatype *recv_types = types + 1 * nprocs;
for(i = 0; i < nprocs; i++) {
counts[i] = 0;
displs[i] = 0;
send_types[i] = MPI_BYTE;
recv_types[i] = MPI_BYTE;
}
for(i = 0; i < n_neighbors; i++) {
nb_rank = mesh->neighbors[i].mpi_rank;
counts[nb_rank] = 1;
displs[nb_rank] = 0;
send_types[nb_rank] = mesh->neighbors[i].halo_outgoing.transfer_type_int;
recv_types[nb_rank] = mesh->neighbors[i].halo_incoming.transfer_type_int;
}
MPI_Alltoallw(
data, counts, displs, send_types,
data, counts, displs, recv_types,
mesh->communicator
);
free(counts);
free(displs);
free(types);
}
void mpi_halo_exchange_int_p2p_default(int_field_t *field)
{
const mesh_t *mesh = field->mesh;
int *data = field->data;
int n_neighbors = mesh->n_neighbors;
int i, tag = 0;
neighbor_info *nb;
MPI_Request *requests = malloc(2 * n_neighbors * sizeof(*requests));
MPI_Request *recv_requests = requests + 1 * n_neighbors;
MPI_Request *send_requests = requests + 0 * n_neighbors;
// Post non-blocking receives for all neighbors
for(i = 0; i < n_neighbors; i++) {
nb = &mesh->neighbors[i];
MPI_Irecv(
data, 1, nb->halo_incoming.transfer_type_int, nb->mpi_rank, tag,
mesh->communicator, &recv_requests[i]
);
}
// Post non-blocking sends for all neighbors
for(i = 0; i < n_neighbors; i++) {
nb = &mesh->neighbors[i];
MPI_Isend(
data, 1, nb->halo_outgoing.transfer_type_int, nb->mpi_rank, tag,
mesh->communicator, &send_requests[i]
);
}
// Finish operations
MPI_Waitall(2 * n_neighbors, requests, MPI_STATUSES_IGNORE);
free(requests);
}
void mpi_halo_exchange_int_p2p_synchronous(int_field_t *field)
{
const mesh_t *mesh = field->mesh;
int *data = field->data;
int n_neighbors = mesh->n_neighbors;
int i, tag = 0;
neighbor_info *nb;
MPI_Request *requests = malloc(2 * n_neighbors * sizeof(*requests));
MPI_Request *recv_requests = requests + 1 * n_neighbors;
MPI_Request *send_requests = requests + 0 * n_neighbors;
// Post non-blocking receives for all neighbors
for(i = 0; i < n_neighbors; i++) {
nb = &mesh->neighbors[i];
MPI_Irecv(
data, 1, nb->halo_incoming.transfer_type_int, nb->mpi_rank, tag,
mesh->communicator, &recv_requests[i]
);
}
// Post non-blocking synchronous sends for all neighbors
for(i = 0; i < n_neighbors; i++) {
nb = &mesh->neighbors[i];
MPI_Issend(
data, 1, nb->halo_outgoing.transfer_type_int, nb->mpi_rank, tag,
mesh->communicator, &send_requests[i]
);
}
// Finish operations
MPI_Waitall(2 * n_neighbors, requests, MPI_STATUSES_IGNORE);
free(requests);
}
void mpi_halo_exchange_int_p2p_ready(int_field_t *field)
{
const mesh_t *mesh = field->mesh;
int *data = field->data;
int n_neighbors = mesh->n_neighbors;
int i, tag = 0;
neighbor_info *nb;
MPI_Request *requests = malloc(2 * n_neighbors * sizeof(*requests));
MPI_Request *recv_requests = requests + 1 * n_neighbors;
MPI_Request *send_requests = requests + 0 * n_neighbors;
// Post non-blocking receives for all neighbors
for(i = 0; i < n_neighbors; i++) {
nb = &mesh->neighbors[i];
MPI_Irecv(
data, 1, nb->halo_incoming.transfer_type_int, nb->mpi_rank, tag,
mesh->communicator, &recv_requests[i]
);
}
// Be sure that all receives for current communication round have been posted
MPI_Barrier(mesh->communicator);
// Post non-blocking ready sends for all neighbors
for(i = 0; i < n_neighbors; i++) {
nb = &mesh->neighbors[i];
MPI_Irsend(
data, 1, nb->halo_outgoing.transfer_type_int, nb->mpi_rank, tag,
mesh->communicator, &send_requests[i]
);
}
// Finish operations
MPI_Waitall(2 * n_neighbors, requests, MPI_STATUSES_IGNORE);
free(requests);
}
// --------------------------------------------------------- Helper functions