-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
432 lines (376 loc) · 11 KB
/
util.c
File metadata and controls
432 lines (376 loc) · 11 KB
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
#include "util.h"
#include "common.h"
#include <limits.h>
#define REQUESTED_INDEX 99
int
init_p4_ctx(p4_ctx_t* const ctx, const ni_mode_t mode)
{
int eret = -1;
unsigned int ni_matching =
mode == MATCHING ? PTL_NI_MATCHING : PTL_NI_NO_MATCHING;
ptl_ni_limits_t ni_limits;
ptl_ni_limits_t ni_requested_limits = {
.max_entries = INT_MAX,
.max_unexpected_headers = INT_MAX,
.max_mds = INT_MAX,
.max_eqs = INT_MAX,
.max_cts = INT_MAX,
.max_pt_index = INT_MAX,
.max_iovecs = INT_MAX,
.max_list_size = INT_MAX,
.max_triggered_ops = INT_MAX,
.max_msg_size = LONG_MAX,
.max_atomic_size = LONG_MAX,
.max_fetch_atomic_size = LONG_MAX,
.max_waw_ordered_size = LONG_MAX,
.max_war_ordered_size = LONG_MAX,
.max_volatile_size = LONG_MAX,
.features = PTL_TARGET_BIND_INACCESSIBLE,
};
ctx->eq_h = PTL_INVALID_HANDLE;
ctx->ct_h = PTL_INVALID_HANDLE;
ctx->ni_h = PTL_INVALID_HANDLE;
eret = PtlNIInit(PTL_IFACE_DEFAULT, ni_matching | PTL_NI_PHYSICAL,
PTL_PID_ANY, &ni_requested_limits, &ni_limits, &ctx->ni_h);
if(PTL_OK != eret)
return eret;
eret = PtlGetPhysId(ctx->ni_h, &ctx->my_addr);
if(PTL_OK != eret)
return eret;
eret = PtlEQAlloc(ctx->ni_h, 4096, &ctx->eq_h);
if(PTL_OK != eret)
return eret;
eret = PtlCTAlloc(ctx->ni_h, &ctx->ct_h);
if(PTL_OK != eret)
return eret;
return PTL_OK;
}
void
destroy_p4_ctx(p4_ctx_t* const ctx)
{
if(!PtlHandleIsEqual(ctx->eq_h, PTL_INVALID_HANDLE))
PtlEQFree(ctx->eq_h);
if(!PtlHandleIsEqual(ctx->ct_h, PTL_INVALID_HANDLE))
PtlCTFree(ctx->ct_h);
if(!PtlHandleIsEqual(ctx->ni_h, PTL_INVALID_HANDLE))
PtlNIFini(ctx->ni_h);
}
int
exchange_ni_address(p4_ctx_t* const ctx, const int my_rank)
{
MPI_Request req[4];
int peer = (my_rank + 1) % 2;
MPI_Irecv(&ctx->peer_addr.phys.nid, 1, MPI_UNSIGNED, peer, 1, MPI_COMM_WORLD,
req);
MPI_Irecv(&ctx->peer_addr.phys.pid, 1, MPI_UNSIGNED, peer, 2, MPI_COMM_WORLD,
req + 1);
MPI_Isend(&ctx->my_addr.phys.nid, 1, MPI_UNSIGNED, peer, 1, MPI_COMM_WORLD,
req + 2);
MPI_Isend(&ctx->my_addr.phys.pid, 1, MPI_UNSIGNED, peer, 2, MPI_COMM_WORLD,
req + 3);
MPI_Waitall(4, req, MPI_STATUS_IGNORE);
return 0;
}
int
p4_pt_alloc(p4_ctx_t* const ctx, ptl_index_t* const index)
{
return PtlPTAlloc(ctx->ni_h, 0, ctx->eq_h, REQUESTED_INDEX, index);
}
void
p4_pt_free(p4_ctx_t* const ctx, ptl_index_t index)
{
PtlPTFree(ctx->ni_h, index);
}
int
p4_md_alloc_ct(p4_ctx_t* const ctx, ptl_handle_md_t* const md_h,
void* const start, const ptl_size_t length)
{
ptl_md_t md = {.start = start,
.length = length,
.options = PTL_MD_VOLATILE | PTL_MD_EVENT_CT_ACK |
PTL_MD_EVENT_CT_REPLY |
PTL_MD_EVENT_SUCCESS_DISABLE,
.ct_handle = ctx->ct_h,
.eq_handle = PTL_EQ_NONE};
return PtlMDBind(ctx->ni_h, &md, md_h);
}
int
p4_md_alloc_eq(p4_ctx_t* const ctx, ptl_handle_md_t* const md_h,
void* const start, const ptl_size_t length)
{
ptl_md_t md = {.start = start,
.length = length,
.options = PTL_MD_VOLATILE | PTL_MD_EVENT_SEND_DISABLE,
.ct_handle = PTL_CT_NONE,
.eq_handle = ctx->eq_h};
return PtlMDBind(ctx->ni_h, &md, md_h);
}
int
p4_md_alloc_eq_empty(p4_ctx_t* const ctx, ptl_handle_md_t* const md_h)
{
ptl_md_t md = {.start = NULL,
.length = PTL_SIZE_MAX,
.options = PTL_MD_EVENT_SEND_DISABLE,
.ct_handle = PTL_CT_NONE,
.eq_handle = ctx->eq_h};
return PtlMDBind(ctx->ni_h, &md, md_h);
}
void
p4_md_free(ptl_handle_md_t md_h)
{
PtlMDRelease(md_h);
}
int
p4_le_insert(p4_ctx_t* const ctx, ptl_handle_le_t* const le_h,
void* const start, const ptl_size_t length,
const ptl_index_t index)
{
int eret = -1;
ptl_event_t event;
ptl_le_t le = {.start = start,
.length = length,
.options = PTL_LE_OP_GET | PTL_LE_OP_PUT |
PTL_LE_EVENT_COMM_DISABLE |
PTL_LE_EVENT_UNLINK_DISABLE,
.uid = PTL_UID_ANY,
.ct_handle = PTL_CT_NONE};
eret = PtlLEAppend(ctx->ni_h, index, &le, PTL_PRIORITY_LIST, NULL, le_h);
if(PTL_OK != eret)
return eret;
PtlEQWait(ctx->eq_h, &event);
if(PTL_EVENT_LINK != event.type)
{
fprintf(stderr, "Failed to link LE\n");
return -1;
}
if(PTL_NI_OK != event.ni_fail_type)
{
fprintf(stderr, "ni_fail_type != PTL_NI_OK");
return -1;
}
return eret;
}
int
p4_le_insert_ct_comm(p4_ctx_t* const ctx, ptl_handle_le_t* const le_h,
void* const start, const ptl_size_t length,
const ptl_index_t index)
{
int eret = -1;
ptl_event_t event;
ptl_le_t le = {.start = start,
.length = length,
.options = PTL_LE_OP_GET | PTL_LE_OP_PUT |
PTL_LE_EVENT_UNLINK_DISABLE |
PTL_LE_EVENT_COMM_DISABLE | PTL_LE_EVENT_CT_COMM,
.uid = PTL_UID_ANY,
.ct_handle = ctx->ct_h};
eret = PtlLEAppend(ctx->ni_h, index, &le, PTL_PRIORITY_LIST, NULL, le_h);
if(PTL_OK != eret)
return eret;
PtlEQWait(ctx->eq_h, &event);
if(PTL_EVENT_LINK != event.type)
{
fprintf(stderr, "Failed to link LE\n");
return -1;
}
if(PTL_NI_OK != event.ni_fail_type)
{
fprintf(stderr, "ni_fail_type != PTL_NI_OK");
return -1;
}
return eret;
}
int
p4_le_insert_full_comm(p4_ctx_t* const ctx, ptl_handle_le_t* const le_h,
void* const start, const ptl_size_t length,
const ptl_index_t index)
{
int eret = -1;
ptl_event_t event;
ptl_le_t le = {.start = start,
.length = length,
.options = PTL_LE_OP_GET | PTL_LE_OP_PUT |
PTL_LE_EVENT_UNLINK_DISABLE,
.uid = PTL_UID_ANY,
.ct_handle = PTL_CT_NONE};
eret = PtlLEAppend(ctx->ni_h, index, &le, PTL_PRIORITY_LIST, NULL, le_h);
if(PTL_OK != eret)
return eret;
PtlEQWait(ctx->eq_h, &event);
if(PTL_EVENT_LINK != event.type)
{
fprintf(stderr, "Failed to link LE\n");
return -1;
}
if(PTL_NI_OK != event.ni_fail_type)
{
fprintf(stderr, "ni_fail_type != PTL_NI_OK");
return -1;
}
return eret;
}
int
p4_le_insert_empty(p4_ctx_t* const ctx, ptl_handle_le_t* const le_h,
const ptl_index_t index)
{
int eret = -1;
ptl_event_t event;
ptl_le_t le = {.start = NULL,
.length = PTL_SIZE_MAX,
.options = PTL_LE_OP_GET | PTL_LE_OP_PUT |
PTL_LE_EVENT_COMM_DISABLE |
PTL_LE_EVENT_UNLINK_DISABLE,
.uid = PTL_UID_ANY,
.ct_handle = PTL_CT_NONE};
eret = PtlLEAppend(ctx->ni_h, index, &le, PTL_PRIORITY_LIST, NULL, le_h);
if(PTL_OK != eret)
return eret;
PtlEQWait(ctx->eq_h, &event);
if(PTL_EVENT_LINK != event.type)
{
fprintf(stderr, "Failed to link LE\n");
return -1;
}
if(PTL_NI_OK != event.ni_fail_type)
{
fprintf(stderr, "ni_fail_type != PTL_NI_OK");
return -1;
}
return eret;
}
void
p4_le_remove(ptl_handle_le_t le_h)
{
PtlLEUnlink(le_h);
}
int
p4_me_insert_persistent(p4_ctx_t* const ctx, ptl_handle_me_t* const me_h,
void* const start, const ptl_size_t length,
const ptl_index_t index)
{
int eret = -1;
ptl_event_t event;
ptl_process_t src;
src.phys.nid = PTL_NID_ANY;
src.phys.pid = PTL_PID_ANY;
ptl_me_t me = {.start = start,
.length = length,
.options = PTL_ME_OP_GET | PTL_ME_OP_PUT |
PTL_ME_EVENT_COMM_DISABLE |
PTL_ME_EVENT_UNLINK_DISABLE | PTL_ME_IS_ACCESSIBLE,
.ct_handle = PTL_CT_NONE,
.uid = PTL_UID_ANY,
.match_id = src,
.match_bits = 0xDEADBEEF,
.ignore_bits = 0,
.min_free = 0};
eret = PtlMEAppend(ctx->ni_h, index, &me, PTL_PRIORITY_LIST, NULL, me_h);
if(PTL_OK != eret)
return eret;
PtlEQWait(ctx->eq_h, &event);
if(PTL_EVENT_LINK != event.type)
{
fprintf(stderr, "Failed to link LE\n");
return -1;
}
if(PTL_NI_OK != event.ni_fail_type)
{
fprintf(stderr, "ni_fail_type != PTL_NI_OK");
return -1;
}
return eret;
}
int
__p4_me_insert(p4_ctx_t* const ctx, ptl_handle_me_t* const me_h, void* start,
const ptl_size_t length, const ptl_index_t index,
const ptl_handle_ct_t ct_handle,
const ptl_match_bits_t match_bits)
{
int eret = -1;
ptl_event_t event;
ptl_process_t src;
src.phys.nid = PTL_NID_ANY;
src.phys.pid = PTL_PID_ANY;
ptl_me_t me = {.start = start,
.length = length,
.options = PTL_ME_OP_GET | PTL_ME_OP_PUT |
PTL_ME_EVENT_UNLINK_DISABLE | PTL_ME_IS_ACCESSIBLE |
PTL_ME_USE_ONCE,
.ct_handle = ct_handle,
.uid = PTL_UID_ANY,
.match_id = src,
.match_bits = match_bits,
.ignore_bits = ~match_bits,
.min_free = 0};
eret = PtlMEAppend(ctx->ni_h, index, &me, PTL_PRIORITY_LIST, NULL, me_h);
if(PTL_OK != eret)
return eret;
PtlEQWait(ctx->eq_h, &event);
if(PTL_EVENT_LINK != event.type)
{
fprintf(stderr, "Failed to link LE\n");
return -1;
}
if(PTL_NI_OK != event.ni_fail_type)
{
fprintf(stderr, "ni_fail_type != PTL_NI_OK");
return -1;
}
return eret;
}
int
p4_me_insert_list_use_once(p4_ctx_t* const ctx, ptl_handle_me_t* const me_h,
void* const* start, ptl_size_t* const length,
const int num_entries, const ptl_index_t index,
const ptl_handle_ct_t ct_handle)
{
int eret = -1;
ptl_match_bits_t match_bits = 1;
for(int i = 0; i < num_entries; ++i)
{
eret = __p4_me_insert(ctx, &me_h[i], start[i], length[i], index, ct_handle,
match_bits++);
if(eret < 0)
return -1;
}
return 0;
}
void
p4_me_remove(ptl_handle_me_t me_h)
{
PtlMEUnlink(me_h);
}
int
alloc_buffer_init(void** ptr, size_t bytes)
{
size_t page_size = sysconf(_SC_PAGESIZE);
posix_memalign(ptr, page_size, bytes);
if(NULL == *ptr)
return -1;
memset(*ptr, 'c', bytes);
return 0;
}
void
invalidate_cache(int* const cache_buffer, const size_t elements)
{
cache_buffer[0] = 1;
for(size_t i = 1; i < elements; ++i)
{
cache_buffer[i] = cache_buffer[i - 1];
}
}
int
set_cache_regions(const int pids)
{
int eret = -1;
FILE* fptr;
fptr = fopen("/sys/class/bxi/bxi0/v2p/cache_pids", "w");
if(fptr == NULL)
{
fprintf(stderr, "The file is not opened. The program will "
"now exit.");
return -1;
}
fprintf(fptr, "%i", pids);
return fclose(fptr);
}