-
Notifications
You must be signed in to change notification settings - Fork 113
/
nrnmpi.cpp
321 lines (296 loc) · 8.77 KB
/
nrnmpi.cpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <../../nrnconf.h>
#include <assert.h>
#include "nrnassrt.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "nrnpthread.h"
/* do not want the redef in the dynamic load case */
#include <nrnmpiuse.h>
#if NRNMPI_DYNAMICLOAD
#include <nrnmpi_dynam.h> /* define all the nrnmpi functions name to f_name */
#endif
#include <nrnmpi.h>
#include <mpispike.h>
#if NRNMPI_DYNAMICLOAD
#else
#include "nrnmpi_def_cinc"
#endif
extern double nrn_timeus();
#if NRNMPI
#include <mpi.h>
#define asrt(arg) nrn_assert(arg == MPI_SUCCESS)
#define USE_HPM 0
#if USE_HPM
#include <libhpm.h>
#endif
int nrnmusic;
#if NRN_MUSIC
MPI_Comm nrnmusic_comm;
extern void nrnmusic_init(int* parg, char*** pargv);
extern void nrnmusic_terminate();
#endif
MPI_Comm nrnmpi_world_comm;
MPI_Comm nrnmpi_comm;
MPI_Comm nrn_bbs_comm;
static MPI_Group grp_bbs;
static MPI_Group grp_net;
extern void nrnmpi_spike_initialize();
#define nrnmpidebugleak 0
#if nrnmpidebugleak
extern void nrnmpi_checkbufleak();
#endif
static int nrnmpi_under_nrncontrol_;
static int nrnmpi_is_setup_;
#endif
extern "C" void nrnmpi_init(int nrnmpi_under_nrncontrol, int* pargc, char*** pargv) {
#if NRNMPI
int i, b, flag;
if (nrnmpi_use) {
return;
}
nrnmpi_under_nrncontrol_ = nrnmpi_under_nrncontrol;
if (nrnmpi_under_nrncontrol_) {
#if 0
{int i;
printf("nrnmpi_init: argc=%d\n", *pargc);
for (i=0; i < *pargc; ++i) {
printf("%d |%s|\n", i, (*pargv)[i]);
}
}
#endif
#if NRN_MUSIC
nrnmusic_init(pargc, pargv); /* see src/nrniv/nrnmusic.cpp */
#endif
#if !ALWAYS_CALL_MPI_INIT
/* this is not good. depends on mpirun adding at least one
arg that starts with -p4 but that probably is dependent
on mpich and the use of the ch_p4 device. We are trying to
work around the problem that MPI_Init may change the working
directory and so when not invoked under mpirun we would like to
NOT call MPI_Init.
*/
b = 0;
for (i = 0; i < *pargc; ++i) {
if (strncmp("-p4", (*pargv)[i], 3) == 0) {
b = 1;
break;
}
if (strcmp("-mpi", (*pargv)[i]) == 0) {
b = 1;
break;
}
}
if (nrnmpi_under_nrncontrol_ == 2) {
b = 1;
nrnmpi_under_nrncontrol_ = 1;
}
if (nrnmusic) {
b = 1;
}
if (!b) {
nrnmpi_under_nrncontrol_ = 0;
return;
}
#endif
MPI_Initialized(&flag);
/* only call MPI_Init if not already initialized */
if (!flag) {
#if (USE_PTHREAD)
int required = MPI_THREAD_SERIALIZED;
int provided;
asrt(MPI_Init_thread(pargc, pargv, required, &provided));
if (required > provided) {
nrn_cannot_use_threads_and_mpi = 1;
}
#else
asrt(MPI_Init(pargc, pargv));
#endif
nrnmpi_under_nrncontrol_ = 1;
} else {
nrnmpi_under_nrncontrol_ = 0;
}
#if NRN_MUSIC
if (nrnmusic) {
asrt(MPI_Comm_dup(nrnmusic_comm, &nrnmpi_world_comm));
} else {
#else
{
#endif
asrt(MPI_Comm_dup(MPI_COMM_WORLD, &nrnmpi_world_comm));
}
}
grp_bbs = MPI_GROUP_NULL;
grp_net = MPI_GROUP_NULL;
asrt(MPI_Comm_dup(nrnmpi_world_comm, &nrnmpi_comm));
asrt(MPI_Comm_dup(nrnmpi_world_comm, &nrn_bbs_comm));
asrt(MPI_Comm_rank(nrnmpi_world_comm, &nrnmpi_myid_world));
asrt(MPI_Comm_size(nrnmpi_world_comm, &nrnmpi_numprocs_world));
nrnmpi_numprocs = nrnmpi_numprocs_bbs = nrnmpi_numprocs_world;
nrnmpi_myid = nrnmpi_myid_bbs = nrnmpi_myid_world;
nrnmpi_spike_initialize();
nrnmpi_use = 1;
/*begin instrumentation*/
#if USE_HPM
hpmInit(nrnmpi_myid_world, "mpineuron");
#endif
#if 0
{int i;
printf("nrnmpi_init: argc=%d\n", *pargc);
for (i=0; i < *pargc; ++i) {
printf("%d |%s|\n", i, (*pargv)[i]);
}
}
#endif
#if 1
if (nrnmpi_myid == 0) {
printf("numprocs=%d\n", nrnmpi_numprocs_world);
}
#endif
#endif /* NRNMPI */
}
double nrnmpi_wtime() {
#if NRNMPI
if (nrnmpi_use) {
return MPI_Wtime();
}
#endif
return nrn_timeus();
}
void nrnmpi_terminate() {
#if NRNMPI
if (nrnmpi_use) {
#if 0
printf("%d nrnmpi_terminate\n", nrnmpi_myid_world);
#endif
#if USE_HPM
hpmTerminate(nrnmpi_myid_world);
#endif
if (nrnmpi_under_nrncontrol_) {
#if NRN_MUSIC
if (nrnmusic) {
nrnmusic_terminate();
} else
#endif
MPI_Finalize();
}
nrnmpi_use = 0;
#if nrnmpidebugleak
nrnmpi_checkbufleak();
#endif
}
#endif /*NRNMPI*/
}
void nrnmpi_abort(int errcode) {
#if NRNMPI
int flag;
MPI_Initialized(&flag);
if (flag) {
MPI_Abort(MPI_COMM_WORLD, errcode);
} else {
abort();
}
#else
abort();
#endif
}
#if NRNMPI
void nrnmpi_subworld_size(int n) {
/* n is the (desired) size of a subworld (pc.nhost) */
/* A subworld (net) is contiguous */
/* In case pc.nhost_world/n is not an integer, there are
pc.nhost_world/n + 1 subworlds and the last subworld
has pc.nhost_world%n ranks. All the other subworlds have n ranks.
*/
if (nrnmpi_use != 1) {
return;
}
if (nrnmpi_comm != MPI_COMM_NULL) {
asrt(MPI_Comm_free(&nrnmpi_comm));
nrnmpi_comm = MPI_COMM_NULL;
}
if (nrn_bbs_comm != MPI_COMM_NULL) {
asrt(MPI_Comm_free(&nrn_bbs_comm));
nrn_bbs_comm = MPI_COMM_NULL;
}
if (grp_bbs != MPI_GROUP_NULL) {
asrt(MPI_Group_free(&grp_bbs));
grp_bbs = MPI_GROUP_NULL;
}
if (grp_net != MPI_GROUP_NULL) {
asrt(MPI_Group_free(&grp_net));
grp_net = MPI_GROUP_NULL;
}
MPI_Group wg;
asrt(MPI_Comm_group(nrnmpi_world_comm, &wg));
int r = nrnmpi_myid_world;
/* special cases */
if (n == 1) {
asrt(MPI_Group_incl(wg, 1, &r, &grp_net));
asrt(MPI_Comm_dup(nrnmpi_world_comm, &nrn_bbs_comm));
asrt(MPI_Comm_create(nrnmpi_world_comm, grp_net, &nrnmpi_comm));
asrt(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid));
asrt(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs));
asrt(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs));
asrt(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs));
} else if (n == nrnmpi_numprocs_world) {
asrt(MPI_Group_incl(wg, 1, &r, &grp_bbs));
asrt(MPI_Comm_dup(nrnmpi_world_comm, &nrnmpi_comm));
asrt(MPI_Comm_create(nrnmpi_world_comm, grp_bbs, &nrn_bbs_comm));
asrt(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid));
asrt(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs));
if (r == 0) {
asrt(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs));
asrt(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs));
} else {
nrnmpi_myid_bbs = -1;
nrnmpi_numprocs_bbs = -1;
}
} else {
int nw = nrnmpi_numprocs_world;
int nb = nw / n; /* nrnmpi_numprocs_bbs */
int ib;
int range[3];
if (nw%n) {
nb += 1; /* and the last will have pc.nhost = nw%n */
}
/* A subworld (net) has contiguous ranks. */
/* Every rank is in a specific nrnmpi_comm communicator */
ib = r / n;
range[0] = ib*n; /* first rank in group */
range[1] = range[0] + n - 1; /* last rank in group */
if (range[1] >= nw) {
range[1] = nw - 1;
}
range[2] = 1; /* stride */
asrt(MPI_Group_range_incl(wg, 1, &range, &grp_net));
asrt(MPI_Comm_create(nrnmpi_world_comm, grp_net, &nrnmpi_comm));
asrt(MPI_Comm_rank(nrnmpi_comm, &nrnmpi_myid));
asrt(MPI_Comm_size(nrnmpi_comm, &nrnmpi_numprocs));
/* nrn_bbs_com ranks stride is nrnmpi_numprocs */
/* only rank 0 of each subworld participates in nrn_bbs_comm */
range[0] = 0; /* first world rank in nrn_bbs_comm */
range[1] = (nb - 1)*n; /* last world rank in nrn_bbs_comm */
range[2] = n; /* stride */
asrt(MPI_Group_range_incl(wg, 1, &range, &grp_bbs));
asrt(MPI_Comm_create(nrnmpi_world_comm, grp_bbs, &nrn_bbs_comm));
if (r % n == 0) { /* only rank 0 participates in nrn_bbs_comm */
asrt(MPI_Comm_rank(nrn_bbs_comm, &nrnmpi_myid_bbs));
asrt(MPI_Comm_size(nrn_bbs_comm, &nrnmpi_numprocs_bbs));
} else {
#if 1
nrnmpi_myid_bbs = -1;
nrnmpi_numprocs_bbs = -1;
#else
nrnmpi_myid_bbs = r/n;
nrnmpi_numprocs_bbs = nb;
#endif
}
}
asrt(MPI_Group_free(&wg));
}
/* so src/nrnpython/inithoc.cpp does not have to include a c++ mpi.h */
int nrnmpi_wrap_mpi_init(int* flag) {
return MPI_Initialized(flag);
}
#endif