-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpi-critical.cpp
258 lines (232 loc) · 7.53 KB
/
mpi-critical.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
/*
* critPathAnalysis.cpp -- Critical path analysis runtime library, build for
* hybrid OpenMp and MPI applications
*/
#include "criticalPath.h"
#include <algorithm>
#include <execinfo.h>
#include <mpi.h>
#include "mpi-critical.h"
Vector<double> timeOffsets;
void MpiHappensAfter(ipcData &uc, int remote) { MpiHappensAfter(&uc, remote); }
void MpiHappensAfter(ipcData *uc, int remote) {
if (!analysis_flags->running)
return;
assert(thread_local_clock->stopped_mpi_clock == true);
assert(remote >= -1);
double offset = 0;
#ifdef DO_OFFSET
if (remote != -1)
offset = timeOffsets[remote];
#endif
update_maximum(thread_local_clock->useful_computation_critical,
uc->uc_double[0]);
update_maximum(thread_local_clock->outsidempi_critical, uc->uc_double[1]);
update_maximum(thread_local_clock->outsideomp_critical,
uc->uc_double[2] - offset);
update_maximum(thread_local_clock->outsideomp_critical_nooffset,
uc->uc_double[3]);
}
double *loadThreadTimers(ipcData &uc, int remote) {
return loadThreadTimers(&uc, remote);
}
double *loadThreadTimers(ipcData *uc, int remote) {
double offset = 0;
#ifdef DO_OFFSET
if (remote != -1)
offset = timeOffsets[remote];
#endif
uc->uc_double[0] = thread_local_clock->useful_computation_critical.load();
uc->uc_double[1] = thread_local_clock->outsidempi_critical.load();
uc->uc_double[2] = thread_local_clock->outsideomp_critical.load() + offset;
uc->uc_double[3] = thread_local_clock->outsideomp_critical_nooffset.load();
return uc->uc_double;
}
// completion callback function for wild-card recv
void completePBWC(RequestData *uc, MPI_Status *status) {
if (uc->isCancelled())
return;
assert(status->MPI_SOURCE >= 0);
assert(status->MPI_TAG == uc->tag || MPI_ANY_TAG == uc->tag);
assert(status->MPI_SOURCE == uc->remote || MPI_ANY_SOURCE == uc->remote);
assert(uc->comm->getDupComm() != MPI_COMM_NULL);
if (uc->remote == MPI_ANY_SOURCE)
uc->remote = status->MPI_SOURCE;
PMPI_Recv(uc->uc_double, 1, ipcData::ipcMpiType, status->MPI_SOURCE,
status->MPI_TAG, uc->comm->getDupComm(), MPI_STATUS_IGNORE);
MpiHappensAfter(uc, uc->remote);
}
// completion callback function with clock update
void completePBHB(RequestData *uc, MPI_Status *status) {
if (uc->pb_reqs[1] == MPI_REQUEST_NULL)
PMPI_Wait(uc->pb_reqs, MPI_STATUS_IGNORE);
else
PMPI_Waitall(2, uc->pb_reqs, MPI_STATUSES_IGNORE);
if (!uc->isCancelled())
MpiHappensAfter(uc, uc->remote);
}
// completion callback function without clock update
void completePBnoHB(RequestData *uc, MPI_Status *status) {
uc->isCancelled();
if (uc->pb_reqs[1] == MPI_REQUEST_NULL)
PMPI_Wait(uc->pb_reqs, MPI_STATUS_IGNORE);
else
PMPI_Waitall(2, uc->pb_reqs, MPI_STATUSES_IGNORE);
}
// cancel callback function
void cancelPB(RequestData *uc) {
if (uc->pb_reqs[0] != MPI_REQUEST_NULL)
PMPI_Cancel(uc->pb_reqs);
if (uc->pb_reqs[1] != MPI_REQUEST_NULL)
PMPI_Cancel(uc->pb_reqs + 1);
}
// completion callback function for persistent request with clock update
void completePersPBHB(RequestData *uc, MPI_Status *status) {
#if OnlyActivePB
if (!analysis_flags->running)
return;
#endif
if (uc->pb_reqs[1] == MPI_REQUEST_NULL)
PMPI_Wait(uc->pb_reqs, MPI_STATUS_IGNORE);
else
PMPI_Waitall(2, uc->pb_reqs, MPI_STATUSES_IGNORE);
if (!uc->isCancelled())
MpiHappensAfter(uc, uc->remote);
}
// completion callback function for persistent request without clock update
void completePersPBnoHB(RequestData *uc, MPI_Status *status) {
uc->isCancelled();
#if OnlyActivePB
if (!analysis_flags->running)
return;
#endif
if (uc->pb_reqs[1] == MPI_REQUEST_NULL)
PMPI_Wait(uc->pb_reqs, MPI_STATUS_IGNORE);
else
PMPI_Waitall(2, uc->pb_reqs, MPI_STATUSES_IGNORE);
}
// start callback function for persistent request with clock update
void startPersPBHB(RequestData *uc) {
#if OnlyActivePB
if (!analysis_flags->running)
return;
#endif
loadThreadTimers(uc);
if (uc->pb_reqs[1] == MPI_REQUEST_NULL)
PMPI_Start(uc->pb_reqs);
else
PMPI_Startall(2, uc->pb_reqs);
}
// start callback function for persistent request without clock update
void startPersPBnoHB(RequestData *uc) {
#if OnlyActivePB
if (!analysis_flags->running)
return;
#endif
if (uc->pb_reqs[1] == MPI_REQUEST_NULL)
PMPI_Start(uc->pb_reqs);
else
PMPI_Startall(2, uc->pb_reqs);
}
void init_timer_offsets() {
auto cw_dup_data = cf.findData(MPI_COMM_WORLD);
int size = cw_dup_data->getSize(), rank = cw_dup_data->getRank();
auto cw_dup = cw_dup_data->getDupComm();
// build offset table
#define NTIMES 19
Vector<double> localTimes, allTimes;
double localTime, endTime, remoteTime;
timeOffsets.Resize(size);
localTimes.Resize(NTIMES);
allTimes.Resize(size * NTIMES);
for (int i = 0; i < NTIMES; i++) {
if (rank == 0) {
for (int j = 1; j < size; j++) {
localTime = getTime();
PMPI_Send(&localTime, 1, MPI_DOUBLE, j, 42, cw_dup);
PMPI_Recv(&remoteTime, 1, MPI_DOUBLE, j, 42, cw_dup, MPI_STATUS_IGNORE);
endTime = getTime();
allTimes[j * NTIMES + i] = remoteTime - (localTime + endTime) / 2;
}
} else {
PMPI_Recv(&remoteTime, 1, MPI_DOUBLE, 0, 42, cw_dup, MPI_STATUS_IGNORE);
localTime = getTime();
PMPI_Send(&localTime, 1, MPI_DOUBLE, 0, 42, cw_dup);
}
}
if (rank == 0) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < NTIMES; j++)
allTimes[i * NTIMES + j] -= localTimes[j];
std::sort(allTimes.begin() + i * NTIMES,
allTimes.begin() + i * NTIMES + NTIMES);
timeOffsets[i] = allTimes[i * NTIMES + NTIMES / 2];
}
}
PMPI_Bcast(timeOffsets.begin(), size, MPI_DOUBLE, 0, cw_dup);
localTimeOffset = localTime = timeOffsets[rank];
if (rank == 0 && analysis_flags->verbose) {
printf("Timer Offsets:");
for (auto &v : timeOffsets) {
v -= localTime;
printf("%lf, ", v);
}
printf("\n");
}
}
void init_processes(mpiTimer &mt) {
PMPI_Comm_rank(MPI_COMM_WORLD, &myProcId);
if (analysis_flags->verbose) {
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
PMPI_Get_processor_name(processor_name, &name_len);
printf("Process %i at %s\n", myProcId, processor_name);
}
init_timer_offsets();
if (!analysis_flags->running) {
startTool(false, CLOCK_ALL);
resetMpiClock(thread_local_clock);
}
}
/************************
*
* MPI WRAPPER FUNCTIONS
*
************************/
extern "C" {
int MPI_Finalize(void) {
mpiTimer mt{false, __func__};
assert(thread_local_clock->stopped_clock == true);
assert(thread_local_clock->stopped_mpi_clock == true);
ipcData max_uc;
loadThreadTimers(max_uc, REF_RANK);
max_uc.Allreduce(cf.findData(MPI_COMM_WORLD));
thread_local_clock->useful_computation_critical = max_uc.uc_double[0];
thread_local_clock->outsidempi_critical = max_uc.uc_double[1];
double offset = 0;
#ifdef DO_OFFSET
if (REF_RANK != -1)
offset = timeOffsets[REF_RANK];
#endif
thread_local_clock->outsideomp_critical = max_uc.uc_double[2] - offset;
thread_local_clock->outsideomp_critical_nooffset = max_uc.uc_double[3];
finishMeasurement();
analysis_flags->running = false;
ipcData::finiIpcData();
return PMPI_Finalize();
}
/************************
* Pcontrol
************************/
int MPI_Pcontrol(const int level, ...) {
if (level == 1) {
if (analysis_flags->barrier)
PMPI_Barrier(cf.findData(MPI_COMM_WORLD)->getDupComm());
startTool();
} else if (level == 0) {
stopTool();
}
return MPI_SUCCESS;
}
} // extern "C"
//#include "fortran-wrapper.h"