-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_spmd_ocr.c
424 lines (372 loc) · 19.4 KB
/
test_spmd_ocr.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string.h>
#include <setjmp.h>
#include "ocr.h"
#define ENABLE_EXTENSION_RTITF
#include "extensions/ocr-runtime-itf.h"
#define NUM_SLAVES (4)
#define DB_STACK_SIZE (8*1024*1024) // 8 MB for now
#define RED_ZONE_SIZE (0)
#define NUM_CALLEE_SAVED_REGS (6)
#define HTA_OP_FINISHED (0)
#define HTA_OP_TO_BE_CONTINUED (1)
#define HTA_PARALLEL_OP(func_call) \
if((func_call) != HTA_OP_FINISHED) { \
printf("hta_main split\n"); \
return 1; \
}
#define MYRANK ((u64) ocrElsUserGet(0))
#ifdef __cplusplus
extern "C" {
ocrGuid_t mainEdt(u32 paramc, u64 *paramv, u32 depc, ocrEdtDep_t depv[]);
}
#endif
typedef struct {
char stack[DB_STACK_SIZE]; // the stack
unsigned int phase; // the new phase number
char* originalbp; // the original thread base pointer
char* originalsp; // the original thread stack pointer
ocrGuid_t args; // the command line arguments
ocrGuid_t self_context_guid; // the DB guid of the context
ocrGuid_t next_phase_edt_guid; // store the guid of the next phase for deferred activation
ocrGuid_t shutdown_edt_guid; // guid of the shutdown Edt
jmp_buf env; // the env data structure used in setjmp/longjmp
long int callee_saved[NUM_CALLEE_SAVED_REGS]; // separate storage location for callee saved register values
} Context;
ocrGuid_t procEdt(u32 paramc, u64 *paramv, u32 depc, ocrEdtDep_t depv[]);
ocrGuid_t slaveEdt(u32 paramc, u64 *paramv, u32 depc, ocrEdtDep_t depv[]);
ocrGuid_t slaveEdt(u32 paramc, u64 *paramv, u32 depc, ocrEdtDep_t depv[])
{
printf("map is executed\n");
return NULL_GUID; // satisfies control dependency of the continuation codelet
}
// This function fix pointers that points to thread stack addresses
void _fix_pointers(void* db_bp, void *thread_bp, void* max_thread_bp) {
while(thread_bp < max_thread_bp) {
size_t frame_size = *((char**)db_bp) - (char*)db_bp;
*((void**)thread_bp) = thread_bp + frame_size;
thread_bp += frame_size;
db_bp += frame_size;
}
}
int hta_map(int pid, Context* context)
{
register char * const basepointer __asm("rbp");
register char * const stackpointer __asm("rsp");
//======================================================================
// perform map
//======================================================================
// create slave EDTs
ocrGuid_t slaveEdt_template_guid;
ocrGuid_t slaveEdts[NUM_SLAVES];
ocrGuid_t slaveOutEvent[NUM_SLAVES];
ocrGuid_t slaveInDBs[NUM_SLAVES];
ocrEdtTemplateCreate(&slaveEdt_template_guid, slaveEdt, 0, 2);
printf("(%lu) slaveEDT template guid %lx\n", MYRANK, slaveEdt_template_guid);
for(int i = 0; i < NUM_SLAVES; i++) {
int *data;
slaveOutEvent[i] = NULL_GUID;
ocrDbCreate(&slaveInDBs[i], (void**) &data, sizeof(int), /*flags=*/DB_PROP_NO_ACQUIRE, /*affinity=*/NULL_GUID, NO_ALLOC);
ocrEdtCreate(&slaveEdts[i], slaveEdt_template_guid, /*paramc=*/0, /*paramv=*/(u64 *)NULL, /*depc=*/2, /*depv=*/NULL, /*properties=*/0 , /*affinity*/NULL_GUID, &slaveOutEvent[i]);
ocrAddDependence(slaveInDBs[i], slaveEdts[i], 1, DB_DEFAULT_MODE); // Immediately satisfy
printf("(%lu) slave %d EDT guid %lx\n", MYRANK, i, slaveEdts[i]);
printf("(%lu) slave %d out event guid %lx\n", MYRANK, i, slaveOutEvent[i]);
}
ocrEdtTemplateDestroy(slaveEdt_template_guid);
//======================================================================
// Create continuation to wait for slave EDTs to finish
//======================================================================
ocrGuid_t procEdt_template_guid;
ocrGuid_t procEdt_guid;
ocrEdtTemplateCreate(&procEdt_template_guid, procEdt, 1, 3+NUM_SLAVES);
u64 rank = MYRANK;
ocrGuid_t depv[3+NUM_SLAVES];
depv[0] = UNINITIALIZED_GUID;
depv[1] = context->args;
depv[2] = context->self_context_guid;
for(int i = 0; i < NUM_SLAVES; i++)
depv[3+i] = slaveOutEvent[i];
ocrEdtCreate(&procEdt_guid, procEdt_template_guid, /*paramc=*/1, /*paramv=*/(u64 *)&rank, /*depc=*/EDT_PARAM_DEF, /*depv=*/depv, /*properties=*/0 , /*affinity*/NULL_GUID, /*outputEvent*/NULL );
printf("(%lu) continuation procEDT template guid %lx\n", MYRANK, procEdt_template_guid);
printf("(%lu) continuation procEDT guid %lx\n", MYRANK, procEdt_guid);
// defer firing slave EDTs
for(int i = 0; i < NUM_SLAVES; i++) {
ocrAddDependence(NULL_GUID, slaveEdts[i], 0, DB_DEFAULT_MODE); //pure control dependence // Immediately satisfy
}
ocrEdtTemplateDestroy(procEdt_template_guid);
// setjmp call creates a continuation point
if(!setjmp(context->env))
{
// switch back to thread stack
// 1. compute the size that need to be copied (the growth of DB stack)
size_t size_to_copy = (context->stack + DB_STACK_SIZE) - stackpointer;
printf("(%lu) db stack (%p - %p) stack size growth = 0x%x\n", MYRANK, stackpointer, context->stack+DB_STACK_SIZE-1, size_to_copy);
// 2. compute the start address of the thread stack
char* originalbp = context->originalbp;
char* threadsp = originalbp - size_to_copy;
char* threadbp = threadsp + (basepointer-stackpointer);
// 3. copy DB stack to overwrite thread stack
printf("(%lu) Enabling continuation codelet\n", MYRANK);
printf("(%lu) switching back to thread stack at (%p - %p) original bp (%p)\n", MYRANK, threadsp, threadbp, originalbp);
memcpy(threadsp - RED_ZONE_SIZE, stackpointer - RED_ZONE_SIZE, size_to_copy + RED_ZONE_SIZE);
// 4. fix frame link addresses
_fix_pointers(basepointer, threadbp, originalbp);
// 5. store the next phase EDT guid for deferred activation
context->next_phase_edt_guid = procEdt_guid;
// 6. set rsp and rbp to point to thread stack. Stop writing to context->stack
__asm volatile(
"movq %0, %%rbp;"
"movq %1, %%rsp;"
:
:"r"(threadbp), "r"(threadsp)
);
printf("(%lu) ==hta_map splited==\n", MYRANK);
return HTA_OP_TO_BE_CONTINUED;
}
else
{
// Continuation should start from here
printf("(%lu) hta_map is continued\n", MYRANK);
// Stack pointer/base pointer are not changed here because it will
// keep using context->stack
return HTA_OP_FINISHED;
}
}
int hta_main(int argc, char** argv, int pid, Context* context)
{
int some_stack_variable = -111;
int other_stack_variable = 202;
// call a parallel operation
HTA_PARALLEL_OP(hta_map(pid, context)); // slave codelets are created in here
printf("(%lu) some stack variable = %d, other stack variable = %d\n", MYRANK, some_stack_variable, other_stack_variable);
// some computation
some_stack_variable = -222;
other_stack_variable = 404;
// call a second parallel operation
HTA_PARALLEL_OP(hta_map(pid, context)); // slave codelets are created in here
printf("(%lu) some stack variable = %d, other stack variable = %d\n", MYRANK, some_stack_variable, other_stack_variable);
// some computation
some_stack_variable = -333;
other_stack_variable = 808;
// call a second parallel operation
HTA_PARALLEL_OP(hta_map(pid, context)); // slave codelets are created in here
printf("(%lu) some stack variable = %d, other stack variable = %d\n", MYRANK, some_stack_variable, other_stack_variable);
printf("(%lu) hta_main() finishing\n", MYRANK);
// must restore thread stack before going back to normal execution
{
register char * const basepointer __asm("rbp");
register char * const stackpointer __asm("rsp");
// switch back to thread stack
// 1. compute the size that need to be copied (the growth of DB stack)
size_t size_to_copy = (context->stack + DB_STACK_SIZE) - stackpointer;
printf("(%lu) stack size growth = 0x%x\n", MYRANK, size_to_copy);
// 2. compute the start address of the thread stack
char* originalbp = context->originalbp;
char* threadsp = originalbp - size_to_copy;
char* threadbp = threadsp + (basepointer-stackpointer);
// 3. copy DB stack to overwrite thread stack
printf("(%lu) switching back to thread stack at (%p - %p) original bp (%p)\n", MYRANK, threadsp, threadbp, originalbp);
memcpy(threadsp - RED_ZONE_SIZE, stackpointer - RED_ZONE_SIZE, size_to_copy + RED_ZONE_SIZE);
// 4. fix frame link addresses
_fix_pointers(basepointer, threadbp, originalbp);
// 5. set rsp and rbp to point to thread stack. Stop writing to context->stack
__asm volatile(
"movq %0, %%rbp;"
"movq %1, %%rsp;"
:
:"r"(threadbp), "r"(threadsp)
);
}
return 0;
}
ocrGuid_t procEdt(u32 paramc, u64 *paramv, u32 depc, ocrEdtDep_t depv[])
{
register char * const basepointer __asm("rbp");
register char * const stackpointer __asm("rsp");
u64 rank = paramv[0];
ocrElsUserSet(0, (ocrGuid_t) rank);
if(depc > 1) { // it's a continuation
Context *context = (Context*) depv[2].ptr;
context->phase++;
int phase = context->phase;
printf("(%lu) ==========Phase %d starts===========\n", MYRANK, phase);
context->originalbp = basepointer;
context->originalsp = stackpointer;
// Store callee saved registers
__asm volatile(
"movq -24(%%rbp), %0;" /* rbx */
"movq -16(%%rbp), %1;" /* r12 */
"movq -8(%%rbp), %2;" /* r13 */
"movq %%r14, %3;"
"movq %%r15, %4;"
"movq %%rbp, %5;"
:"=r"(context->callee_saved[0]), "=r"(context->callee_saved[1]), "=r"(context->callee_saved[2]), "=r"(context->callee_saved[3]), "=r"(context->callee_saved[4]), "=r"(context->callee_saved[5])
:
);
printf("(%lu/%d) saving rbx = 0x%012lx\n", MYRANK, phase, context->callee_saved[0]);
printf("(%lu/%d) saving r12 = 0x%012lx\n", MYRANK, phase, context->callee_saved[1]);
printf("(%lu/%d) saving r13 = 0x%012lx\n", MYRANK, phase, context->callee_saved[2]);
printf("(%lu/%d) saving r14 = 0x%012lx\n", MYRANK, phase, context->callee_saved[3]);
printf("(%lu/%d) saving r15 = 0x%012lx\n", MYRANK, phase, context->callee_saved[4]);
printf("(%lu/%d) saving rbp = 0x%012lx\n", MYRANK, phase, context->callee_saved[5]);
longjmp(context->env, 0);
// will never reach here
assert(0 && "Program execution should never reach here");
} else {
char * newsp, * oldsp; // FIXME:use volatile to prevent compiler optimization?
char * newbp, * oldbp;
// create a data block to store program context
ocrGuid_t context_guid;
Context *context;
ocrDbCreate(&context_guid, (void**) &context, sizeof(Context), /*flags=*/DB_PROP_NONE, /*affinity=*/NULL_GUID, NO_ALLOC);
printf("(%lu) context DB guid %lx\n", MYRANK, context_guid);
context->phase = 0;
int phase = context->phase;
context->args = depv[0].guid;
context->self_context_guid = context_guid;
context->shutdown_edt_guid = paramv[1];
context->originalbp = basepointer;
context->originalsp = stackpointer;
int argc = getArgc(depv[0].ptr);
char **argv = (char **) malloc(argc * sizeof(char *));
for(int i = 0; i < argc; i++) {
char *arg = getArgv(depv[0].ptr, i);
argv[i] = arg;
}
printf("(%lu/%d) procEdt is executed\n", MYRANK, phase);
printf("(%lu/%d) procEdt frame address (0) %p\n", MYRANK, phase, __builtin_frame_address(0));
printf("(%lu/%d) procEdt rbp %p\n", MYRANK, phase, basepointer);
printf("(%lu/%d) procEdt rsp %p\n", MYRANK, phase, stackpointer);
printf("(%lu/%d) procEdt stack frame size = 0x%x\n", MYRANK, phase, basepointer - stackpointer);
printf("(%lu/%d) procEdt allocated DB stack %p - %p\n", MYRANK, phase, context->stack, context->stack + DB_STACK_SIZE - 1);
// =========================
// Setup stack frame
// =========================
oldbp = basepointer;
oldsp = stackpointer;
// Change stack pointer / base pointer value to use the heap memory as stack
newbp = (context->stack + DB_STACK_SIZE);
newsp = newbp - (oldbp - oldsp);
// Store callee saved registers
__asm volatile(
"movq -24(%%rbp), %0;" /* rbx */
"movq -16(%%rbp), %1;" /* r12 */
"movq -8(%%rbp), %2;" /* r13 */
"movq %%r14, %3;"
"movq %%r15, %4;"
"movq %%rbp, %5;"
:"=r"(context->callee_saved[0]), "=r"(context->callee_saved[1]), "=r"(context->callee_saved[2]), "=r"(context->callee_saved[3]), "=r"(context->callee_saved[4]), "=r"(context->callee_saved[5])
:
);
printf("(%lu/%d) saving rbx = 0x%012lx\n", MYRANK, phase, context->callee_saved[0]);
printf("(%lu/%d) saving r12 = 0x%012lx\n", MYRANK, phase, context->callee_saved[1]);
printf("(%lu/%d) saving r13 = 0x%012lx\n", MYRANK, phase, context->callee_saved[2]);
printf("(%lu/%d) saving r14 = 0x%012lx\n", MYRANK, phase, context->callee_saved[3]);
printf("(%lu/%d) saving r15 = 0x%012lx\n", MYRANK, phase, context->callee_saved[4]);
printf("(%lu/%d) saving rbp = 0x%012lx\n", MYRANK, phase, context->callee_saved[5]);
// Copy stack frame. This has to happen after the stack variable values are computed
memcpy(newsp - RED_ZONE_SIZE, stackpointer - RED_ZONE_SIZE, basepointer - stackpointer + RED_ZONE_SIZE);
printf("(%lu) Stack frame dumped. Switching frame pointer and stack pointer\n", MYRANK);
// After this line, stack variables should be read only to be safe
__asm volatile("movq %0, %%rbp;"
"movq %1, %%rsp;"
:
:"r"(newbp), "r"(newsp)
);
printf("(%lu/%d) Stack frame switched to data block space\n", MYRANK, phase);
printf("(%lu/%d) (db) procEdt frame address (0) %p\n", MYRANK, phase, __builtin_frame_address(0));
printf("(%lu/%d) (db) procEdt rbp %p\n", MYRANK, phase, basepointer);
printf("(%lu/%d) (db) procEdt rsp %p\n", MYRANK, phase, stackpointer);
printf("(%lu/%d) (db) procEdt stack frame size = 0x%x\n", MYRANK, phase, basepointer - stackpointer);
if(hta_main(argc, argv, 0, context) == 0) {
phase = context->phase;
printf("(%lu/%d) last procEdt. shutting down runtime\n", MYRANK, phase);
printf("(%lu/%d) Restore callee saved registers before returning to OCR runtime\n", MYRANK, phase);
printf("(%lu/%d) Restoring rbx = 0x%012lx\n", MYRANK, phase, context->callee_saved[0]);
printf("(%lu/%d) Restoring r12 = 0x%012lx\n", MYRANK, phase, context->callee_saved[1]);
printf("(%lu/%d) Restoring r13 = 0x%012lx\n", MYRANK, phase, context->callee_saved[2]);
printf("(%lu/%d) Restoring r14 = 0x%012lx\n", MYRANK, phase, context->callee_saved[3]);
printf("(%lu/%d) Restoring r15 = 0x%012lx\n", MYRANK, phase, context->callee_saved[4]);
printf("(%lu/%d) Restoring rbp = 0x%012lx\n", MYRANK, phase, context->callee_saved[5]);
__asm volatile(
"movq %0, -24(%%rbp);" /* rbx */
"movq %1, -16(%%rbp);" /* r12 */
"movq %2, -8(%%rbp);" /* r13 */
"movq %3, %%r14;"
"movq %4, %%r15;"
"movq %5, %%rbp;"
:
:"r"(context->callee_saved[0]), "r"(context->callee_saved[1]), "r"(context->callee_saved[2]), "r"(context->callee_saved[3]), "r"(context->callee_saved[4]), "r"(context->callee_saved[5])
);
printf("(%lu) shutdown guid %lx\n", MYRANK, context->shutdown_edt_guid);
ocrEventSatisfySlot(context->shutdown_edt_guid, NULL_GUID, MYRANK); // Satisfy shutdown slots
return NULL_GUID;
}
else {
phase = context->phase;
printf("(%lu/%d) procEdt single phase finished, will be continued\n", MYRANK, phase);
}
printf("(%lu/%d) Restore callee saved registers before returning to OCR runtime\n", MYRANK, phase);
printf("(%lu/%d) Restoring rbx = 0x%012lx\n", MYRANK, phase, context->callee_saved[0]);
printf("(%lu/%d) Restoring r12 = 0x%012lx\n", MYRANK, phase, context->callee_saved[1]);
printf("(%lu/%d) Restoring r13 = 0x%012lx\n", MYRANK, phase, context->callee_saved[2]);
printf("(%lu/%d) Restoring r14 = 0x%012lx\n", MYRANK, phase, context->callee_saved[3]);
printf("(%lu/%d) Restoring r15 = 0x%012lx\n", MYRANK, phase, context->callee_saved[4]);
printf("(%lu/%d) Restoring rbp = 0x%012lx\n", MYRANK, phase, context->callee_saved[5]);
__asm volatile(
"movq %0, -24(%%rbp);" /* rbx */
"movq %1, -16(%%rbp);" /* r12 */
"movq %2, -8(%%rbp);" /* r13 */
"movq %3, %%r14;"
"movq %4, %%r15;"
"movq %5, %%rbp;"
:
:"r"(context->callee_saved[0]), "r"(context->callee_saved[1]), "r"(context->callee_saved[2]), "r"(context->callee_saved[3]), "r"(context->callee_saved[4]), "r"(context->callee_saved[5])
);
ocrAddDependence(NULL_GUID, context->next_phase_edt_guid, 0, DB_DEFAULT_MODE); // the continuation is activated here
}
return NULL_GUID;
}
ocrGuid_t shutdownEdt(u32 paramc, u64 *paramv, u32 depc, ocrEdtDep_t depv[])
{
printf("Shutting down! Bye!\n");
ocrShutdown();
return NULL_GUID;
}
// Program entry (No hacks here all normal calling flow)
ocrGuid_t mainEdt(u32 paramc, u64 *paramv, u32 depc, ocrEdtDep_t depv[])
{
int NP = 4;
// Create shutdown EDT
ocrGuid_t shutdownEdt_template_guid;
ocrEdtTemplateCreate(&shutdownEdt_template_guid, shutdownEdt, 0, NP);
ocrGuid_t shutdownEdt_guid;
ocrEdtCreate(&shutdownEdt_guid, shutdownEdt_template_guid, /*paramc=*/0, /*paramv=*/NULL, /*depc=*/NP,
/*depv=*/NULL, /*properties=*/0 , /*affinity*/NULL_GUID, /*outputEvent*/NULL );
printf("shutdownEDT guid %lx\n", shutdownEdt_guid);
// Create proc EDT
ocrGuid_t procEdt_template_guid;
ocrEdtTemplateCreate(&procEdt_template_guid, procEdt, 2, 1);
int i = 0;
for(i = 0; i < NP; i++) {
ocrGuid_t procEdt_guid;
u64 rank[2];
rank[0] = i;
rank[1] = shutdownEdt_guid; // passed as argument
ocrEdtCreate(&procEdt_guid, procEdt_template_guid, /*paramc=*/2, /*paramv=*/rank, /*depc=*/1,
/*depv=*/NULL, /*properties=*/0 , /*affinity*/NULL_GUID, /*outputEvent*/NULL );
printf("procEDT template guid %lx\n", procEdt_template_guid);
printf("procEDT guid %lx\n", procEdt_guid);
// Add proc EDT dependences
ocrAddDependence(depv[0].guid, procEdt_guid, 0, DB_MODE_RW); // argc and argv // Immediately satisfy
}
// Satisfy proc EDT dependences for it to start
// Nothing for now
ocrEdtTemplateDestroy(procEdt_template_guid);
printf("mainEdt returned\n");
// mainEdt returns OCR runtime is not shutdown here
return NULL_GUID;
}