forked from ROSS-org/ROSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tw-setup.c
502 lines (400 loc) · 13.9 KB
/
tw-setup.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
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
#include <ross.h>
#include "lz4.h"
/**
* @file tw-setup.c
* @brief tw_define_lps is defined here!
*/
#define VERIFY_MAPPING 0
static tw_pe *setup_pes(void);
unsigned int nkp_per_pe = 16;
static const tw_optdef kernel_options[] = {
TWOPT_GROUP("ROSS Kernel"),
TWOPT_UINT("synch", g_tw_synchronization_protocol, "Sychronization Protocol: SEQUENTIAL=1, CONSERVATIVE=2, OPTIMISTIC=3, OPTIMISTIC_DEBUG=4, OPTIMISTIC_REALTIME=5"),
TWOPT_UINT("nkp", nkp_per_pe, "number of kernel processes (KPs) per pe"),
TWOPT_STIME("end", g_tw_ts_end, "simulation end timestamp"),
TWOPT_UINT("batch", g_tw_mblock, "messages per scheduler block"),
TWOPT_UINT("extramem", g_tw_events_per_pe_extra, "Number of extra events allocated per PE."),
TWOPT_UINT("buddy-size", g_tw_buddy_alloc, "delta encoding buddy system allocation (2^X)"),
TWOPT_UINT("lz4-knob", g_tw_lz4_knob, "LZ4 acceleration factor (higher = faster)"),
TWOPT_ULONGLONG("max-opt-lookahead", g_tw_max_opt_lookahead, "Optimistic simulation: maximum lookahead allowed in virtual clock time"),
#ifdef AVL_TREE
TWOPT_UINT("avl-size", g_tw_avl_node_count, "AVL Tree contains 2^avl-size nodes"),
#endif
TWOPT_END()
};
void tw_init(int *argc, char ***argv) {
int i;
#if HAVE_CTIME
time_t raw_time;
#endif
tw_opt_add(tw_net_init(argc, argv));
// Print out the command-line so we know what we passed in
if (tw_ismaster()) {
for (i = 0; i < *argc; i++) {
printf("%s ", (*argv)[i]);
}
printf("\n\n");
}
// Print our revision if we have it
#ifdef ROSS_VERSION
if (tw_ismaster()) {
#if HAVE_CTIME
time(&raw_time);
printf("%s\n", ctime(&raw_time));
#endif
printf("ROSS Revision: %s\n\n", ROSS_VERSION);
}
#endif
tw_opt_add(kernel_options);
tw_opt_add(tw_gvt_setup());
tw_opt_add(tw_clock_setup());
tw_opt_add(tw_stats_setup());
#ifdef USE_RIO
tw_opt_add(io_opts);
#endif
// by now all options must be in
tw_opt_parse(argc, argv);
if(tw_ismaster() && NULL == (g_tw_csv = fopen("ross.csv", "a"))) {
tw_error(TW_LOC, "Unable to open: ross.csv\n");
}
tw_opt_print();
tw_net_start();
tw_gvt_start();
}
static void early_sanity_check(void) {
#if ROSS_MEMORY
if(0 == g_tw_memory_nqueues) {
tw_error(TW_LOC, "ROSS memory library enabled!");
}
#endif
if (!g_tw_npe) {
tw_error(TW_LOC, "need at least one PE");
}
if (!g_tw_nlp) {
tw_error(TW_LOC, "need at least one LP");
}
if (!nkp_per_pe) {
tw_printf(TW_LOC, "number of KPs (%u) must be >= PEs (%u), adjusting.", g_tw_nkp, g_tw_npe);
g_tw_nkp = g_tw_npe;
}
}
/*
* map: map LPs->KPs->PEs linearly
*/
void map_linear(void) {
tw_pe *pe;
int nlp_per_kp;
int lpid;
int kpid;
int i;
int j;
// may end up wasting last KP, but guaranteed each KP has == nLPs
nlp_per_kp = (int)ceil((double) g_tw_nlp / (double) g_tw_nkp);
if(!nlp_per_kp) {
tw_error(TW_LOC, "Not enough KPs defined: %d", g_tw_nkp);
}
g_tw_lp_offset = g_tw_mynode * g_tw_nlp;
#if VERIFY_MAPPING
printf("NODE %d: nlp %lld, offset %lld\n", g_tw_mynode, g_tw_nlp, g_tw_lp_offset);
#endif
for(kpid = 0, lpid = 0, pe = NULL; (pe = tw_pe_next(pe)); ) {
#if VERIFY_MAPPING
printf("\tPE %d\n", pe->id);
#endif
for(i = 0; i < nkp_per_pe; i++, kpid++) {
tw_kp_onpe(kpid, pe);
#if VERIFY_MAPPING
printf("\t\tKP %d", kpid);
#endif
for(j = 0; j < nlp_per_kp && lpid < g_tw_nlp; j++, lpid++) {
tw_lp_onpe(lpid, pe, g_tw_lp_offset+lpid);
tw_lp_onkp(g_tw_lp[lpid], g_tw_kp[kpid]);
#if VERIFY_MAPPING
if(0 == j % 20) {
printf("\n\t\t\t");
}
printf("%lld ", lpid+g_tw_lp_offset);
#endif
}
#if VERIFY_MAPPING
printf("\n");
#endif
}
}
if(!g_tw_lp[g_tw_nlp-1]) {
tw_error(TW_LOC, "Not all LPs defined! (g_tw_nlp=%d)", g_tw_nlp);
}
if(g_tw_lp[g_tw_nlp-1]->gid != g_tw_lp_offset + g_tw_nlp - 1) {
tw_error(TW_LOC, "LPs not sequentially enumerated!");
}
}
void map_round_robin(void) {
tw_pe * pe = g_tw_pe[0]; // ASSUMPTION: only 1 pe
int kpid, lpid;
int i;
// ASSUMPTION: g_tw_nlp is the same on each rank
int lp_stride = tw_nnodes();
int lp_offset = g_tw_mynode;
for(i = 0, lpid=lp_offset; i < g_tw_nlp; i++, lpid+=lp_stride) {
kpid = i % g_tw_nkp;
tw_lp_onpe(i, pe, lpid);
tw_kp_onpe(kpid, pe);
tw_lp_onkp(g_tw_lp[i], g_tw_kp[kpid]);
#if VERIFY_MAPPING
printf("LP %4d KP %4d PE %4d\n", lpid, kpid, pe->id);
#endif
}
}
/**
* IMPORTANT: This function sets the value for g_tw_nlp which is a rather
* important global variable. It is also set in (very few) other places,
* but mainly just here.
*/
void tw_define_lps(tw_lpid nlp, size_t msg_sz) {
int i;
g_tw_nlp = nlp;
#ifdef ROSS_MEMORY
g_tw_memory_sz = sizeof(tw_memory);
#endif
g_tw_msg_sz = msg_sz;
early_sanity_check();
/*
* Construct the KP array.
*/
if( g_tw_nkp == 1 && g_tw_synchronization_protocol != OPTIMISTIC_DEBUG ) { // if it is the default, then check with the overide param
g_tw_nkp = nkp_per_pe * g_tw_npe;
}
// else assume the application overloaded and has BRAINS to set its own g_tw_nkp
g_tw_kp = (tw_kp **) tw_calloc(TW_LOC, "KPs", sizeof(*g_tw_kp), g_tw_nkp);
/*
* Construct the LP array.
*/
g_tw_lp = (tw_lp **) tw_calloc(TW_LOC, "LPs", sizeof(*g_tw_lp), g_tw_nlp);
switch(g_tw_mapping) {
case LINEAR:
map_linear();
break;
case ROUND_ROBIN:
map_round_robin();
break;
case CUSTOM:
if( g_tw_custom_initial_mapping ) {
g_tw_custom_initial_mapping();
} else {
tw_error(TW_LOC, "CUSTOM mapping flag set but not custom mapping function! \n");
}
break;
default:
tw_error(TW_LOC, "Bad value for g_tw_mapping %d \n", g_tw_mapping);
}
// init LP RNG stream(s)
for(i = 0; i < g_tw_nlp; i++) {
if(g_tw_rng_default == 1) {
tw_rand_init_streams(g_tw_lp[i], g_tw_nRNG_per_lp);
}
}
}
static void late_sanity_check(void) {
tw_kpid i;
tw_lptype null_type;
tw_kp *kp;
memset(&null_type, 0, sizeof(null_type));
/* KPs must be mapped . */
// should we worry if model doesn't use all defined KPs? probably not.
for (i = 0; i < g_tw_nkp; i++) {
kp = tw_getkp(i);
if( kp == NULL ) {
tw_error(TW_LOC, "Local KP %u is NULL \n", i);
}
if (kp->pe == NULL) {
tw_error(TW_LOC, "Local KP %u has no PE assigned.", i);
}
}
/* LPs KP and PE must agree. */
for (i = 0; i < g_tw_nlp; i++) {
tw_lp *lp = g_tw_lp[i];
if (!lp || !lp->pe) {
tw_error(TW_LOC, "LP %u has no PE assigned.", i);
}
if (!lp->kp) {
tw_error(TW_LOC, "LP not mapped to a KP!");
}
if (lp->pe != lp->kp->pe) {
tw_error(TW_LOC, "LP %u has mismatched KP and PE.", lp->id);
}
if (!memcmp(lp->type, &null_type, sizeof(null_type))) {
tw_error(TW_LOC, "LP %u has no type.", lp->id);
}
}
}
#ifdef USE_BGPM
unsigned cacheList[] = {
PEVT_L2_HITS,
PEVT_L2_MISSES,
PEVT_L2_FETCH_LINE,
PEVT_L2_STORE_LINE,
};
unsigned instList[] = {
PEVT_LSU_COMMIT_CACHEABLE_LDS,
PEVT_L1P_BAS_MISS,
PEVT_INST_XU_ALL,
PEVT_INST_QFPU_ALL,
PEVT_INST_QFPU_FPGRP1,
PEVT_INST_ALL,
};
#endif
void tw_run(void) {
tw_pe *me;
if (g_st_stats_enabled || g_st_real_time_samp || g_st_ev_trace)
st_stats_init();
late_sanity_check();
me = setup_pes();
#ifdef USE_BGPM
Bgpm_Init(BGPM_MODE_SWDISTRIB);
// Create event set and add events needed to calculate CPI,
int hEvtSet = Bgpm_CreateEventSet();
// Change the type here to get the other performance monitor sets -- defaults to instList set
// Sets need to be different because there are conflicts when mixing certain events.
Bgpm_AddEventList(hEvtSet, instList, sizeof(instList)/sizeof(unsigned) );
if( me->id == 0) {
printf("***************************************************************************************** \n");
printf("* NOTICE: Build configured with Blue Gene/Q specific, BGPM performance monitoring code!!* \n");
printf("***************************************************************************************** \n");
}
Bgpm_Apply(hEvtSet);
Bgpm_Start(hEvtSet);
#endif
tw_sched_init(me);
switch(g_tw_synchronization_protocol) {
case SEQUENTIAL: // case 1
tw_scheduler_sequential(me);
break;
case CONSERVATIVE: // case 2
tw_scheduler_conservative(me);
break;
case OPTIMISTIC: // case 3
tw_scheduler_optimistic(me);
break;
case OPTIMISTIC_DEBUG: // case 4
tw_scheduler_optimistic_debug(me);
break;
case OPTIMISTIC_REALTIME: // case 5
tw_scheduler_optimistic_realtime(me);
break;
default:
tw_error(TW_LOC, "No Synchronization Protocol Specified! \n");
}
#ifdef USE_BGPM
Bgpm_Stop(hEvtSet);
if( me->id == 0 ) { // Have only RANK 0 output stats
int i;
uint64_t cnt;
int numEvts = Bgpm_NumEvents(hEvtSet);
printf("\n \n ================================= \n");
printf( "Performance Counter Results:\n");
printf("--------------------------------- \n");
for (i=0; i<numEvts; i++) {
Bgpm_ReadEvent(hEvtSet, i, &cnt);
printf(" %40s = %20llu\n", Bgpm_GetEventLabel(hEvtSet, i), cnt);
}
printf("================================= \n");
}
#endif
}
void tw_end(void) {
if(tw_ismaster()) {
fprintf(g_tw_csv, "\n");
fclose(g_tw_csv);
}
tw_net_stop();
}
/**
* By the time this function gets called, g_tw_delta_sz should be as large
* as it will ever get.
*/
static void tw_delta_alloc(tw_pe *pe) {
g_tw_delta_sz = LZ4_compressBound(g_tw_delta_sz);
pe->delta_buffer[0] = (unsigned char *)tw_calloc(TW_LOC, "Delta buffers", g_tw_delta_sz, 1);
pe->delta_buffer[1] = (unsigned char *)tw_calloc(TW_LOC, "Delta buffers", g_tw_delta_sz, 1);
pe->delta_buffer[2] = (unsigned char *)tw_calloc(TW_LOC, "Delta buffers", g_tw_delta_sz, 1);
}
static tw_pe * setup_pes(void) {
tw_pe *pe;
tw_pe *master;
int i;
unsigned int num_events_per_pe;
num_events_per_pe = 1 + g_tw_events_per_pe + g_tw_events_per_pe_extra;
master = g_tw_pe[0];
if (!master) {
tw_error(TW_LOC, "No PE configured on this node.");
}
if (g_tw_mynode == g_tw_masternode) {
master->master = 1;
}
master->local_master = 1;
for(i = 0; i < g_tw_npe; i++) {
pe = g_tw_pe[i];
if (g_tw_buddy_alloc) {
g_tw_buddy_master = create_buddy_table(g_tw_buddy_alloc);
if (g_tw_buddy_master == NULL) {
tw_error(TW_LOC, "create_buddy_table() failed.");
}
tw_delta_alloc(pe);
}
pe->pq = tw_pq_create();
tw_eventq_alloc(&pe->free_q, num_events_per_pe);
pe->abort_event = tw_eventq_shift(&pe->free_q);
#ifdef USE_RIO
tw_clock start = tw_clock_read();
for (i = 0; i < g_io_events_buffered_per_rank; i++) {
tw_eventq_push(&g_io_free_events, tw_eventq_pop(&g_tw_pe[0]->free_q));
}
pe->stats.s_rio_load = (tw_clock_read() - start);
#endif
}
if (g_tw_mynode == g_tw_masternode) {
printf("\nROSS Core Configuration: \n");
printf("\t%-50s %11u\n", "Total Nodes", tw_nnodes());
fprintf(g_tw_csv, "%u,", tw_nnodes());
printf("\t%-50s [Nodes (%u) x PE_per_Node (%lu)] %lu\n", "Total Processors", tw_nnodes(), g_tw_npe, (tw_nnodes() * g_tw_npe));
fprintf(g_tw_csv, "%lu,", (tw_nnodes() * g_tw_npe));
printf("\t%-50s [Nodes (%u) x KPs (%lu)] %lu\n", "Total KPs", tw_nnodes(), g_tw_nkp, (tw_nnodes() * g_tw_nkp));
fprintf(g_tw_csv, "%lu,", (tw_nnodes() * g_tw_nkp));
printf("\t%-50s %11llu\n", "Total LPs",
((unsigned long long)tw_nnodes() * (unsigned long long)g_tw_npe * g_tw_nlp));
fprintf(g_tw_csv, "%llu,", ((unsigned long long)tw_nnodes() * (unsigned long long)g_tw_npe * g_tw_nlp));
printf("\t%-50s %11.2lf\n", "Simulation End Time", g_tw_ts_end);
fprintf(g_tw_csv, "%11.2lf\n", g_tw_ts_end);
switch(g_tw_mapping) {
case LINEAR:
printf("\t%-50s %11s\n", "LP-to-PE Mapping", "linear");
fprintf(g_tw_csv, "%s,", "linear");
break;
case ROUND_ROBIN:
printf("\t%-50s %11s\n", "LP-to-PE Mapping", "round robin");
fprintf(g_tw_csv, "%s,", "round robin");
break;
case CUSTOM:
printf("\t%-50s %11s\n", "LP-to-PE Mapping", "model defined");
fprintf(g_tw_csv, "%s,", "model defined");
break;
}
printf("\n");
#ifndef ROSS_DO_NOT_PRINT
printf("\nROSS Event Memory Allocation:\n");
printf("\t%-50s %11d\n", "Model events", num_events_per_pe);
fprintf(g_tw_csv, "%d,", num_events_per_pe);
printf("\t%-50s %11d\n", "Network events", g_tw_gvt_threshold);
fprintf(g_tw_csv, "%d,", g_tw_gvt_threshold);
printf("\t%-50s %11d\n", "Total events", g_tw_events_per_pe);
fprintf(g_tw_csv, "%d,", g_tw_events_per_pe);
printf("\n");
#endif
}
return master;
}
// This is the default lp type mapping function
// valid ONLY if there is one lp type
tw_lpid map_onetype (tw_lpid gid) {
return 0;
}