-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtracking.h
576 lines (532 loc) · 16.2 KB
/
tracking.h
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#ifndef TRACKING_H
#define TRACKING_H
#include "criticalPath.h"
#include "errorhandler.h"
#include <assert.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "containers.h"
#include <mutex>
#include <shared_mutex>
// critical path tool only needs comm and request tracking
#define HANDLE_COMM 1
#define HANDLE_REQUEST 1
#define HANDLE_MESSAGE 1
// use memory management for tool data
#define REAL_DATAPOOL 1
#ifdef __cplusplus
#define _EXTERN_C_ extern "C"
#endif
#ifdef HANDLE_WIN
#define preWin(w) w = wf.getHandle(w)
#define postWin(w) *w = wf.newHandle(*w)
#else
#define preWin(w) (void)w
#define postWin(w) (void)w
#endif
#ifdef HANDLE_FILE
#define preFile(f) f = ff.getHandle(f)
#define postFile(f) *f = ff.newHandle(*f)
#else
#define preFile(f) (void)f
#define postFile(f) (void)f
#endif
#ifdef HANDLE_COMM
#define preComm(c) c = cf.getHandle(c)
#define postComm(c) *c = cf.newHandle(*c)
#else
#define preComm(c) (void)c
#define postComm(c) (void)c
#endif
#if defined(HAVE_SESSION) && defined(HANDLE_SESSION)
#define preSession(s) s = sf.getHandle(s)
#define postSession(s) *s = sf.newHandle(*s)
#else
#define preSession(s) (void)s
#define postSession(s) (void)s
#endif
#ifdef HANDLE_MESSAGE
#define preMessage(m) m = mf.getHandle(m)
#define postMessage(m) *m = mf.newHandle(*m)
#else
#define preMessage(m) (void)m
#define postMessage(m) (void)m
#endif
#ifdef HANDLE_REQUEST
#define postRequest(r) *r = rf.newRequest(*r)
#define postRequestInit(r) *r = rf.newRequest(*r, true)
#else
#define postRequest(r) (void)r
#define postRequestInit(r) (void)r
#endif
enum toolDataEnum {
toolWinData,
toolFileData,
toolCommData,
toolSessionData,
toolMessageData,
toolRequestData
};
#include "handle-data.h"
using namespace __otfcpt;
// Abstract interface of the HandleFactor
template <typename M, typename T, auto E> class AbstractHandleFactory {
protected:
CompactHashMap<M, T> predefHandles{};
virtual bool isPredefined(M handle) { return handle == T::nullHandle; }
virtual T *findPredefinedData(M handle) {
auto iter = predefHandles.Find(handle);
if (iter == predefHandles.end())
return nullptr;
return &(iter->second);
}
public:
virtual M newHandle(M &handle) = 0;
virtual M newHandle(M &handle, T *data) = 0;
virtual M freeHandle(M handle) = 0;
virtual T *detachHandle(M handle) = 0;
virtual T *findData(M handle) = 0;
virtual T *newData() = 0;
virtual M &getHandle(M &handle) = 0;
virtual M &getHandleLocked(M &handle) = 0;
virtual std::shared_lock<std::shared_mutex> getSharedLock() = 0;
virtual ~AbstractHandleFactory(){};
virtual void initPredefined() {
predefHandles[T::nullHandle].init(T::nullHandle);
}
};
// Abstract interface of the RequestFactor
class AbstractRequestFactory
: public virtual AbstractHandleFactory<MPI_Request, RequestData,
toolRequestData> {
public:
virtual MPI_Request newRequest(MPI_Request req, bool persistent = false) = 0;
virtual MPI_Request newRequest(MPI_Request req, RequestData *data,
bool persistent = false) = 0;
virtual MPI_Request completeRequest(MPI_Request req, MPI_Status *status) = 0;
virtual MPI_Request completeRequest(MPI_Request req, MPI_Request mpi_req,
MPI_Status *status) = 0;
virtual MPI_Request startRequest(MPI_Request req) = 0;
virtual MPI_Request cancelRequest(MPI_Request req) = 0;
virtual ~AbstractRequestFactory(){};
};
#ifdef REAL_DATAPOOL
// Memory management for ToolData
template <typename M, typename T> class DataPool {
private:
mutable std::shared_mutex DPMutex{};
Vector<T *> dataPointer{};
Vector<void *> memory;
virtual void newDatas() {
assert(sizeof(T) % 64 == 0);
int ndatas = 4096 / sizeof(T);
char *datas = (char *)malloc(ndatas * sizeof(T));
memory.PushBack(datas);
for (int i = 0; i < ndatas; i++) {
dataPointer.PushBack(new (datas + i * sizeof(T)) T());
}
}
public:
virtual T *getData() {
std::unique_lock<std::shared_mutex> lock(DPMutex);
if (dataPointer.Empty())
newDatas();
T *ret = dataPointer.Back();
dataPointer.PopBack();
return ret;
}
virtual void returnData(T *data) {
std::unique_lock<std::shared_mutex> lock(DPMutex);
dataPointer.PushBack(data);
}
virtual ~DataPool() {
for (auto i : this->dataPointer)
if (i)
i->~T();
for (auto i : this->memory)
if (i)
free(i);
}
};
#else
// No memory management for ToolData, just use new/delete
template <typename M, typename T> class DataPool {
public:
virtual T *getData() { return new T(); }
virtual void returnData(T *data) { delete data; }
};
#endif
// General template for HandleFactory / RequestFactory
// should not be instanciated
template <typename M, typename T, typename D, auto E>
class HandleFactory : public virtual AbstractHandleFactory<M, T, E> {
// Instanciating the primary class should fail because of the private default
// constructor
HandleFactory() { /*static_assert(false, "primary template of HandleFactory
instanciated unexpectedly");*/
}
};
template <typename D> class RequestFactoryInst {
// Instanciating the primary class should fail because of the private default
// constructor
RequestFactoryInst() { /*static_assert(false, "primary template of
RequestFactory instanciated unexpectedly");*/
}
};
// Specialized template of HandleFactory for pointer-type handles
template <typename M, typename T, typename MI, auto E>
class HandleFactory<M, T, MI *, E>
: public virtual AbstractHandleFactory<M, T, E>, public DataPool<M, T> {
protected:
mutable std::shared_mutex DummyMutex{};
public:
T *newData() { return this->getData(); }
M newHandle(M &handle) {
if (this->isPredefined(handle))
return handle;
T *ret = this->getData();
return newHandle(handle, ret);
}
M newHandle(M &handle, T *ret) {
ret->init(handle);
return (M)(uintptr_t)ret;
}
M freeHandle(M handle) {
if (this->isPredefined(handle))
return (M)T::nullHandle;
((T *)(uintptr_t)(handle))->fini();
this->returnData((T *)(uintptr_t)(handle));
return (M)T::nullHandle;
}
T *detachHandle(M handle) {
assert (!this->isPredefined(handle));
return (T *)(uintptr_t)(handle);
}
T *findData(M handle) {
T *ret = this->findPredefinedData(handle);
if (ret != nullptr)
return ret;
return (T *)(uintptr_t)(handle);
}
M &getHandle(M &handle) {
if (this->isPredefined(handle))
return handle;
return (M &)(((T *)(uintptr_t)(handle))->handle);
}
M &getHandleLocked(M &handle) {
if (this->isPredefined(handle))
return handle;
return (M &)(((T *)(uintptr_t)(handle))->handle);
}
std::shared_lock<std::shared_mutex> getSharedLock() {
return std::shared_lock<std::shared_mutex>{DummyMutex, std::defer_lock};
}
virtual ~HandleFactory() {}
};
// Specialized template of RequestFactory for pointer-type handles
template <typename MI>
class RequestFactoryInst<MI *>
: public HandleFactory<MPI_Request, RequestData, MI *, toolRequestData>,
public AbstractRequestFactory {
MPI_Request _newRequest(MPI_Request req, bool persistent) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
RequestData *ret = this->getData();
ret->init(req, persistent);
return (MPI_Request)(uintptr_t)ret;
}
public:
// get data from the pool
MPI_Request newRequest(MPI_Request req, bool persistent) {
return _newRequest(req, persistent);
}
MPI_Request newRequest(MPI_Request req, RequestData *data, bool persistent) {
data->init(req, persistent);
return (MPI_Request)(uintptr_t)data;
}
MPI_Request completeRequest(MPI_Request req, MPI_Request mpi_req,
MPI_Status *status) {
((RequestData *)(uintptr_t)(req))->handle = mpi_req;
return completeRequest(req, status);
}
MPI_Request completeRequest(MPI_Request req, MPI_Status *status) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
if (((RequestData *)(uintptr_t)(req))->isPersistent()) {
((RequestData *)(uintptr_t)(req))->complete(status);
return req;
}
((RequestData *)(uintptr_t)(req))->fini(status);
this->returnData((RequestData *)(uintptr_t)(req));
return MPI_REQUEST_NULL;
}
MPI_Request startRequest(MPI_Request req) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
RequestData *ret = ((RequestData *)(uintptr_t)(req));
assert(ret->isPersistent());
ret->start();
return ret->handle;
}
MPI_Request cancelRequest(MPI_Request req) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
RequestData *ret = ((RequestData *)(uintptr_t)(req));
ret->cancel();
return req;
}
};
// Specialized template of HandleFactory for int-type handles
template <typename M, typename T, auto E>
class HandleFactory<M, T, int, E>
: public virtual AbstractHandleFactory<M, T, E>, public DataPool<M, T> {
protected:
mutable std::shared_mutex DTMutex{};
mutable std::shared_mutex AHMutex{};
Vector<T *> dataTable{};
Vector<M> availableHandles{};
void newAH() {
int ndatas = 4096 / 64;
size_t oldSize = dataTable.Size(), newSize = oldSize + ndatas;
{
// Only the resize actually modifies the vector and
// can lead to a reallocation of the elements
std::unique_lock<std::shared_mutex> lock(DTMutex);
dataTable.Resize(newSize);
}
// Getting the lock here is not necessary:
// unique_lock can only be taken while having unique AHMutex
// std::shared_lock<std::shared_mutex> lock(DTMutex);
for (size_t i = oldSize, j = 0; i < newSize; i++, j++) {
dataTable[i] = nullptr;
availableHandles.PushBack((M)(i));
}
}
public:
T *newData() { return this->getData(); }
M newHandle(M &handle) {
if (this->isPredefined(handle))
return handle;
T *ret = this->getData();
return newHandle(handle, ret);
}
M newHandle(M &handle, T *dp) {
M ret;
{
std::unique_lock<std::shared_mutex> lock(AHMutex);
if (availableHandles.Empty())
newAH();
ret = availableHandles.Back();
availableHandles.PopBack();
// Getting the lock here is not necessary:
// unique_lock can only be taken while having unique AHMutex
// std::shared_lock<std::shared_mutex> lock(DTMutex);
dataTable[(size_t)(ret)] = dp;
}
dp->init(handle);
return ret;
}
M freeHandle(M handle) {
if (this->isPredefined(handle))
return T::nullHandle;
T *ret;
{
std::unique_lock<std::shared_mutex> lock(AHMutex);
ret = dataTable[(size_t)(handle)];
availableHandles.PushBack(handle);
}
ret->fini();
this->returnData(ret);
return T::nullHandle;
}
T *detachHandle(M handle) {
assert (!this->isPredefined(handle));
T *ret;
{
std::unique_lock<std::shared_mutex> lock(AHMutex);
ret = dataTable[(size_t)(handle)];
availableHandles.PushBack(handle);
}
return ret;
}
T *findData(M handle) {
T *ret = this->findPredefinedData(handle);
if (ret != nullptr)
return ret;
std::shared_lock<std::shared_mutex> lock(DTMutex);
return dataTable[(size_t)(handle)];
}
M &getHandle(M &handle) {
if (this->isPredefined(handle))
return handle;
std::shared_lock<std::shared_mutex> lock(DTMutex);
return dataTable[(size_t)(handle)]->handle;
}
M &getHandleLocked(M &handle) {
if (this->isPredefined(handle))
return handle;
return dataTable[(size_t)(handle)]->handle;
}
std::shared_lock<std::shared_mutex> getSharedLock() {
return std::shared_lock<std::shared_mutex>{DTMutex};
}
virtual ~HandleFactory() {}
};
// Specialized template of RequestFactory for int-type handles
template <>
class RequestFactoryInst<int>
: public HandleFactory<MPI_Request, RequestData, int, toolRequestData>,
public AbstractRequestFactory {
MPI_Request _newRequest(MPI_Request req, bool persistent) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
MPI_Request ret;
RequestData *dp = this->getData();
{
std::unique_lock<std::shared_mutex> lock(AHMutex);
if (availableHandles.Empty())
newAH();
ret = availableHandles.Back();
availableHandles.PopBack();
dataTable[(size_t)(ret)] = dp;
}
dp->init(req, persistent);
return ret;
}
public:
// get data from the pool
MPI_Request newRequest(MPI_Request req, RequestData *data, bool persistent) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
data->init(req, persistent);
MPI_Request ret;
std::unique_lock<std::shared_mutex> lock(AHMutex);
if (availableHandles.Empty())
newAH();
ret = availableHandles.Back();
availableHandles.PopBack();
dataTable[(size_t)(ret)] = data;
return ret;
}
MPI_Request newRequest(MPI_Request req, bool persistent) {
return _newRequest(req, persistent);
}
// returning to the datapool using lock
MPI_Request completeRequest(MPI_Request req, MPI_Status *status) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
RequestData *ret;
bool persistent{false};
{
std::unique_lock<std::shared_mutex> lock(AHMutex);
ret = dataTable[(size_t)(req)];
persistent = ret->isPersistent();
if (!persistent) {
availableHandles.PushBack(req);
}
}
if (persistent) {
ret->complete(status);
return req;
}
ret->fini(status);
this->returnData(ret);
return MPI_REQUEST_NULL;
}
MPI_Request completeRequest(MPI_Request req, MPI_Request mpi_req,
MPI_Status *status) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
RequestData *ret;
bool persistent{false};
{
std::unique_lock<std::shared_mutex> lock(AHMutex);
ret = dataTable[(size_t)(req)];
persistent = ret->isPersistent();
if (!persistent) {
availableHandles.PushBack(req);
}
}
ret->handle = mpi_req;
if (persistent) {
ret->complete(status);
return req;
}
ret->fini(status);
this->returnData(ret);
return MPI_REQUEST_NULL;
}
MPI_Request startRequest(MPI_Request req) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
RequestData *ret;
{
std::shared_lock<std::shared_mutex> lock(DTMutex);
ret = dataTable[(size_t)(req)];
}
assert(ret->isPersistent());
ret->start();
return ret->handle;
}
MPI_Request cancelRequest(MPI_Request req) {
if (req == MPI_REQUEST_NULL)
return MPI_REQUEST_NULL;
RequestData *ret;
{
std::shared_lock<std::shared_mutex> lock(DTMutex);
ret = dataTable[(size_t)(req)];
}
ret->cancel();
return req;
}
};
using WinData = HandleData<MPI_Win, toolWinData>;
using FileData = HandleData<MPI_File, toolFileData>;
using MessageData = RequestData;
#ifdef HAVE_SESSION
using SessionData = HandleData<MPI_Session, toolSessionData>;
#endif
using WinFactory = HandleFactory<MPI_Win, WinData, MPI_Win, toolWinData>;
using FileFactory = HandleFactory<MPI_File, FileData, MPI_File, toolFileData>;
using CommFactory = HandleFactory<MPI_Comm, CommData, MPI_Comm, toolCommData>;
#ifdef HAVE_SESSION
using SessionFactory =
HandleFactory<MPI_Session, SessionData, MPI_Session, toolSessionData>;
#endif
using MessageFactory =
HandleFactory<MPI_Message, MessageData, MPI_Message, toolMessageData>;
using RequestFactory = RequestFactoryInst<MPI_Request>;
#ifdef HANDLE_WIN
extern WinFactory wf;
#endif
#ifdef HANDLE_FILE
extern FileFactory ff;
#endif
#ifdef HANDLE_COMM
template <>
bool AbstractHandleFactory<MPI_Comm, CommData, toolCommData>::isPredefined(
MPI_Comm handle);
template <>
void AbstractHandleFactory<MPI_Comm, CommData, toolCommData>::initPredefined();
extern CommFactory cf;
#endif
#ifdef HANDLE_MESSAGE
template <>
bool AbstractHandleFactory<MPI_Message, MessageData,
toolMessageData>::isPredefined(MPI_Message handle);
template <>
void AbstractHandleFactory<MPI_Message, MessageData,
toolMessageData>::initPredefined();
extern MessageFactory mf;
#endif
#ifdef HANDLE_REQUEST
extern RequestFactory rf;
#endif
#if defined(HAVE_SESSION) && defined(HANDLE_SESSION)
extern SessionFactory sf;
#elif !defined(HAVE_SESSION)
typedef int MPI_Session;
#endif
#endif