-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathping_pong.c
391 lines (337 loc) · 9.83 KB
/
ping_pong.c
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
/*
* Copyright (C) Bull S.A.S - 2024
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if
* not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* BXI Low Level Team
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdatomic.h>
#include <stdbool.h>
#include "portals4.h"
#include "portals4_ext.h"
/*
* Example how to use logical interface to communicate within two interfaces
*
* Usage: ./ping_pong [number of ping pong]
*
* By default, the ping pong continue forever.
*/
int ping(atomic_int *ni_initialized, atomic_int *set_map_done, ptl_process_t *mapping,
int max_tours)
{
int ret;
char msg[PTL_EV_STR_SIZE];
ptl_process_t id;
ptl_handle_ni_t nih1;
ptl_handle_eq_t eqh1;
ptl_index_t pti1;
ptl_handle_md_t mdh1;
ptl_handle_le_t leh1;
ptl_event_t ev;
ptl_process_t rank1;
ptl_le_t le1;
ptl_md_t md1;
char init_data[50];
char *data1 = "Hello pong !";
rank1.rank = 1;
ret = PtlNIInit(PTL_IFACE_DEFAULT, PTL_NI_NO_MATCHING | PTL_NI_LOGICAL, PTL_PID_ANY, NULL,
NULL, &nih1);
if (ret != PTL_OK) {
fprintf(stderr, "PtlNIInit failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
ret = PtlGetPhysId(nih1, &id);
if (ret != PTL_OK) {
fprintf(stderr, "PtlGetId failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
mapping[0] = id;
*ni_initialized += 1;
}
while (atomic_load(ni_initialized) < 2) {
}
/* Set mapping from logical to physical id */
ret = PtlSetMap(nih1, 2, mapping);
if (ret != PTL_OK) {
fprintf(stderr, "PtlSetMap failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
*set_map_done += 1;
}
ret = PtlEQAlloc(nih1, 2, &eqh1);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQAlloc 1 failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
ret = PtlPTAlloc(nih1, 0, eqh1, PTL_PT_ANY, &pti1);
if (ret != PTL_OK) {
fprintf(stderr, "PtlPTAlloc failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
while (atomic_load(set_map_done) < 2) {
}
le1 = (ptl_le_t){ .start = init_data,
.length = strlen(data1) + 1,
.ct_handle = PTL_CT_NONE,
.uid = PTL_UID_ANY,
.options = PTL_LE_OP_PUT };
while ((max_tours > 0) || (max_tours <= -1)) {
ret = PtlLEAppend(nih1, pti1, &le1, PTL_PRIORITY_LIST, NULL, &leh1);
if (ret != PTL_OK) {
fprintf(stderr, "PtlLEAppend failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
ret = PtlEQWait(eqh1, &ev);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQWait failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
PtlEvToStr(0, &ev, msg);
printf("Event interface 1 : %s \n", msg);
}
md1 = (ptl_md_t){ .start = data1,
.length = strlen(data1) + 1,
.options = 0,
.eq_handle = eqh1,
.ct_handle = PTL_CT_NONE };
ret = PtlMDBind(nih1, &md1, &mdh1);
if (ret != PTL_OK) {
fprintf(stderr, "PtlMDBind failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
/* send the message to interface 2 */
ret = PtlPut(mdh1, 0, strlen(data1) + 1, PTL_ACK_REQ, rank1, 0, 0, 0, NULL, 0);
if (ret != PTL_OK) {
fprintf(stderr, "PtlPut failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
/* wait to receive the send events message */
ret = PtlEQWait(eqh1, &ev);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQPoll 1 failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
} else {
PtlEvToStr(0, &ev, msg);
printf("Event interface 1 : %s \n", msg);
}
/* wait to receive the ack from our recipent */
ret = PtlEQWait(eqh1, &ev);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQWait failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
PtlEvToStr(0, &ev, msg);
printf("Event interface 1 : %s \n", msg);
}
PtlEQPoll(&eqh1, 0, 1, &ev, 0);
/* interface 1 wait to receive the response message */
ret = PtlEQWait(eqh1, &ev);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQPoll failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
PtlEvToStr(0, &ev, msg);
printf("Event interface 1 : %s \n", msg);
}
if (ev.type == PTL_EVENT_PUT) {
printf("ping received message from pong: \n");
printf(" header : %ld \n", ev.hdr_data);
printf(" message : %s \n", (char *)ev.start);
}
sleep(1.5);
max_tours--;
}
return 0;
}
int pong(atomic_int *ni_initialized, atomic_int *set_map_done, ptl_process_t *mapping,
int max_tours)
{
int ret;
char msg[PTL_EV_STR_SIZE];
ptl_process_t id;
ptl_handle_ni_t nih2;
ptl_handle_eq_t eqh2;
ptl_index_t pti2;
ptl_handle_md_t mdh2;
ptl_handle_le_t leh2;
ptl_event_t ev;
ptl_process_t rank0;
ptl_le_t le2;
ptl_md_t md2;
char init_data[50];
char *data2 = "Hello ping !";
rank0.rank = 0;
ret = PtlNIInit(PTL_IFACE_DEFAULT, PTL_NI_NO_MATCHING | PTL_NI_LOGICAL, PTL_PID_ANY, NULL,
NULL, &nih2);
if (ret != PTL_OK) {
fprintf(stderr, "PtlNIInit failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
ret = PtlGetPhysId(nih2, &id);
if (ret != PTL_OK) {
fprintf(stderr, "PtlGetPhysId failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
mapping[1] = id;
*ni_initialized += 1;
}
while (atomic_load(ni_initialized) < 2) {
}
/* Set mapping from logical to physical id */
ret = PtlSetMap(nih2, 2, mapping);
if (ret != PTL_OK) {
fprintf(stderr, "PtlSetMap failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
*set_map_done += 1;
}
while (atomic_load(set_map_done) < 2) {
}
/* Interface 2 prepare to receive a message */
ret = PtlEQAlloc(nih2, 2, &eqh2);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQAlloc 2 failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
ret = PtlPTAlloc(nih2, 0, eqh2, PTL_PT_ANY, &pti2);
if (ret != PTL_OK) {
fprintf(stderr, "PtlPTAlloc failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
le2 = (ptl_le_t){ .start = init_data,
.length = strlen(data2) + 1,
.ct_handle = PTL_CT_NONE,
.uid = PTL_UID_ANY,
.options = PTL_LE_OP_PUT };
while ((max_tours > 0) || (max_tours <= -1)) {
ret = PtlLEAppend(nih2, pti2, &le2, PTL_PRIORITY_LIST, NULL, &leh2);
if (ret != PTL_OK) {
fprintf(stderr, "PtlLEAppend failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
ret = PtlEQWait(eqh2, &ev);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQWait failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
PtlEvToStr(0, &ev, msg);
printf("Event interface 2 : %s \n", msg);
}
/* Interface 2 receive the message and reply */
ret = PtlEQWait(eqh2, &ev);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQWait 2 failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
PtlEvToStr(0, &ev, msg);
printf("Event interface 2 : %s \n", msg);
}
if (ev.type == PTL_EVENT_PUT) {
printf(" pong received message from ping : \n");
printf(" header : %ld \n", ev.hdr_data);
printf(" message : %s \n", (char *)ev.start);
}
sleep(1.5);
md2 = (ptl_md_t){ .start = data2,
.length = strlen(data2) + 1,
.options = 0,
.eq_handle = eqh2,
.ct_handle = PTL_CT_NONE };
ret = PtlMDBind(nih2, &md2, &mdh2);
if (ret != PTL_OK) {
fprintf(stderr, "PtlMDBind failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
ret = PtlPut(mdh2, 0, strlen(data2) + 1, PTL_ACK_REQ, rank0, 0, 0, 0, NULL, 0);
if (ret != PTL_OK) {
fprintf(stderr, "PtlPut failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
ret = PtlEQWait(eqh2, &ev);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQWait failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
PtlEvToStr(0, &ev, msg);
printf("Event interface 2 : %s \n", msg);
}
ret = PtlEQWait(eqh2, &ev);
if (ret != PTL_OK) {
fprintf(stderr, "PtlEQWait failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
} else {
PtlEvToStr(0, &ev, msg);
printf("Event interface 2 : %s \n", msg);
}
PtlEQPoll(&eqh2, 0, 1, &ev, 0);
max_tours--;
}
return 0;
}
int main(int argc, char **argv)
{
int ret;
ptl_process_t *mapping;
char *p;
int max_tours;
if (argc < 2) {
max_tours = -1;
} else {
max_tours = strtol(argv[1], &p, 10);
if (*p != '\0') {
fprintf(stderr,
"An invalid character was found before the end of the string\n");
return 1;
}
}
mapping = (ptl_process_t *)mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, 0, 0);
atomic_int *ni_initialized =
&((atomic_int *)(mapping))[2 * (sizeof(ptl_process_t) / sizeof(int)) + 1];
*ni_initialized = ATOMIC_VAR_INIT(0);
atomic_int *set_map_done =
&((atomic_int *)(mapping))[2 * (sizeof(ptl_process_t) / sizeof(int)) +
sizeof(ni_initialized) + 1];
*set_map_done = ATOMIC_VAR_INIT(0);
/*
* Fork in two process for our two interfaces.
* Note that this must be called before PtlInit, as required by the Portals4 specification
*/
int res = fork();
ret = PtlInit();
if (ret != PTL_OK) {
fprintf(stderr, "PtlInit failed : %s \n", PtlToStr(ret, PTL_STR_ERROR));
return 1;
}
/* interface 1 */
if (res != 0) {
ret = ping(ni_initialized, set_map_done, mapping, max_tours);
if (ret != 0) {
fprintf(stderr, "ping failed\n");
return 1;
}
}
/* interface 2 */
else {
ret = pong(ni_initialized, set_map_done, mapping, max_tours);
if (ret != 0) {
fprintf(stderr, "pong failed\n");
return 1;
}
}
}