forked from ROSS-org/ROSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tw-sched.c
442 lines (353 loc) · 9.75 KB
/
tw-sched.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
#include <ross.h>
/*
* Get all events out of my event queue and spin them out into
* the priority queue so they can be processed in time stamp
* order.
*/
static void
tw_sched_event_q(tw_pe * me)
{
tw_clock start;
tw_kp *dest_kp;
tw_event *cev;
tw_event *nev;
while (me->event_q.size)
{
cev = tw_eventq_pop_list(&me->event_q);
for (; cev; cev = nev)
{
nev = cev->next;
if(!cev->state.owner || cev->state.owner == TW_pe_free_q)
tw_error(TW_LOC, "no owner!");
if (cev->state.cancel_q)
{
cev->state.owner = TW_pe_anti_msg;
cev->next = cev->prev = NULL;
continue;
}
switch (cev->state.owner)
{
case TW_pe_event_q:
dest_kp = cev->dest_lp->kp;
if (dest_kp->last_time > cev->recv_ts)
{
/* cev is a straggler message which has arrived
* after we processed events occuring after it.
* We need to jump back to before cev's timestamp.
*/
start = tw_clock_read();
tw_kp_rollback_to(dest_kp, cev->recv_ts);
me->stats.s_rollback += tw_clock_read() - start;
}
start = tw_clock_read();
tw_pq_enqueue(me->pq, cev);
me->stats.s_pq += tw_clock_read() - start;
break;
default:
tw_error(
TW_LOC,
"Event in event_q, but owner %d not recognized",
cev->state.owner);
}
}
}
}
/*
* OPT: need to link events into canq in reverse order so
* that when we rollback the 1st event, we should not
* need to do any further rollbacks.
*/
static void
tw_sched_cancel_q(tw_pe * me)
{
tw_clock start;
tw_event *cev;
tw_event *nev;
start = tw_clock_read();
while (me->cancel_q) {
cev = me->cancel_q;
me->cancel_q = NULL;
for (; cev; cev = nev) {
nev = cev->cancel_next;
if (!cev->state.cancel_q)
tw_error(TW_LOC, "No cancel_q bit on event in cancel_q");
if(!cev->state.owner || cev->state.owner == TW_pe_free_q)
tw_error(TW_LOC, "Cancelled event, no owner!");
switch (cev->state.owner) {
case TW_pe_event_q:
/* This event hasn't been added to our pq yet and we
* have not officially received it yet either. We'll
* do the actual free of this event when we receive it
* as we spin out the event_q chain.
*/
tw_eventq_delete_any(&me->event_q, cev);
tw_event_free(me, cev);
break;
case TW_pe_anti_msg:
tw_event_free(me, cev);
break;
case TW_pe_pq:
/* Event was not cancelled directly from the event_q
* because the cancel message came after we popped it
* out of that queue but before we could process it.
*/
tw_pq_delete_any(me->pq, cev);
tw_event_free(me, cev);
break;
case TW_kp_pevent_q:
/* The event was already processed.
* SECONDARY ROLLBACK
*/
tw_kp_rollback_event(cev);
tw_event_free(me, cev);
break;
default:
tw_error(
TW_LOC,
"Event in cancel_q, but owner %d not recognized",
cev->state.owner);
}
}
}
me->stats.s_cancel_q += tw_clock_read() - start;
}
static void
tw_sched_batch(tw_pe * me)
{
tw_clock start;
unsigned int msg_i;
/* Process g_tw_mblock events, or until the PQ is empty
* (whichever comes first).
*/
for (msg_i = g_tw_mblock; msg_i; msg_i--) {
tw_event *cev;
tw_lp *clp;
tw_kp *ckp;
/* OUT OF FREE EVENT BUFFERS. BAD.
* Go do fossil collect immediately.
*/
if (me->free_q.size <= g_tw_gvt_threshold) {
tw_gvt_force_update(me);
break;
}
start = tw_clock_read();
if (!(cev = tw_pq_dequeue(me->pq)))
break;
me->stats.s_pq += tw_clock_read() - start;
clp = cev->dest_lp;
ckp = clp->kp;
me->cur_event = cev;
ckp->last_time = cev->recv_ts;
/* Save state if no reverse computation is available */
if (!clp->type.revent)
tw_error(TW_LOC, "Reverse Computation must be implemented!");
start = tw_clock_read();
(*clp->type.event)(
clp->cur_state,
&cev->cv,
tw_event_data(cev),
clp);
ckp->s_nevent_processed++;
me->stats.s_event_process += tw_clock_read() - start;
/* We ran out of events while processing this event. We
* cannot continue without doing GVT and fossil collect.
*/
if (me->cev_abort) {
start = tw_clock_read();
me->stats.s_nevent_abort++;
me->cev_abort = 0;
tw_event_rollback(cev);
tw_pq_enqueue(me->pq, cev);
cev = tw_eventq_peek(&ckp->pevent_q);
ckp->last_time = cev ? cev->recv_ts : me->GVT;
tw_gvt_force_update(me);
me->stats.s_event_abort += tw_clock_read() - start;
break;
}
/* Thread current event into processed queue of kp */
cev->state.owner = TW_kp_pevent_q;
tw_eventq_unshift(&ckp->pevent_q, cev);
}
}
static void
tw_sched_init(tw_pe * me)
{
(*me->type.pre_lp_init)(me);
tw_init_kps(me);
tw_init_lps(me);
(*me->type.post_lp_init)(me);
tw_net_barrier(me);
/*
* Recv all of the startup events out of the network before
* starting simulation.. at this point, all LPs are done with init.
*/
if (tw_nnodes() > 1)
{
tw_net_read(me);
tw_net_barrier(me);
}
if (tw_nnodes() > 1)
tw_clock_init(me);
/* This lets the signal handler know that we have started
* the scheduler loop, and to print out the stats before
* finishing if someone should type CTRL-c
*/
if((tw_nnodes() > 1 || g_tw_npe > 1) &&
tw_node_eq(&g_tw_mynode, &g_tw_masternode) &&
me->local_master)
{
if( g_tw_synchronization_protocol == CONSERVATIVE )
printf("*** START PARALLEL CONSERVATIVE SIMULATION ***\n\n");
if( g_tw_synchronization_protocol == OPTIMISTIC )
printf("*** START PARALLEL OPTIMISTIC SIMULATION ***\n\n");
}
else if(tw_nnodes() == 1 && g_tw_npe == 1)
{
// force the setting of SEQUENTIAL protocol
if( g_tw_synchronization_protocol!=SEQUENTIAL )
g_tw_synchronization_protocol=SEQUENTIAL;
printf("*** START SEQUENTIAL SIMULATION ***\n\n");
}
if (me->local_master)
g_tw_sim_started = 1;
}
/*************************************************************************/
/* Primary Schedulers -- In order: Sequential, Conservative, Optimistic */
/*************************************************************************/
void tw_scheduler_sequential(tw_pe * me) {
if(tw_nnodes() > 1)
tw_error(TW_LOC, "Sequential Scheduler used for world size greater than 1.");
tw_event *cev;
tw_sched_init(me);
tw_wall_now(&me->start_time);
while ((cev = tw_pq_dequeue(me->pq)))
{
tw_lp *clp = cev->dest_lp;
tw_kp *ckp = clp->kp;
me->cur_event = cev;
ckp->last_time = cev->recv_ts;
(*clp->type.event)(
clp->cur_state,
&cev->cv,
tw_event_data(cev),
clp);
if (me->cev_abort)
tw_error(TW_LOC, "insufficient event memory");
ckp->s_nevent_processed++;
tw_event_free(me, cev);
}
tw_wall_now(&me->end_time);
printf("*** END SIMULATION ***\n\n");
tw_stats(me);
(*me->type.final)(me);
}
void
tw_scheduler_conservative(tw_pe * me)
{
tw_clock start;
unsigned int msg_i;
unsigned int round = 0;
tw_sched_init(me);
tw_wall_now(&me->start_time);
me->stats.s_total = tw_clock_read();
for (;;)
{
if (tw_nnodes() > 1)
{
start = tw_clock_read();
tw_net_read(me);
me->stats.s_net_read += tw_clock_read() - start;
}
tw_gvt_step1(me);
tw_sched_event_q(me);
tw_gvt_step2(me);
if (me->GVT > g_tw_ts_end)
break;
// put "batch" loop directly here
/* Process g_tw_mblock events, or until the PQ is empty
* (whichever comes first).
*/
for (msg_i = g_tw_mblock; msg_i; msg_i--)
{
tw_event *cev;
tw_lp *clp;
tw_kp *ckp;
/* OUT OF FREE EVENT BUFFERS. BAD.
* Go do fossil collect immediately.
*/
if (me->free_q.size <= g_tw_gvt_threshold) {
tw_gvt_force_update(me);
break;
}
if(tw_pq_minimum(me->pq) >= me->GVT + g_tw_lookahead)
break;
start = tw_clock_read();
if (!(cev = tw_pq_dequeue(me->pq)))
break;
me->stats.s_pq += tw_clock_read() - start;
clp = cev->dest_lp;
ckp = clp->kp;
me->cur_event = cev;
ckp->last_time = cev->recv_ts;
start = tw_clock_read();
(*clp->type.event)(
clp->cur_state,
&cev->cv,
tw_event_data(cev),
clp);
ckp->s_nevent_processed++;
me->stats.s_event_process += tw_clock_read() - start;
if (me->cev_abort)
tw_error(TW_LOC, "insufficient event memory");
tw_event_free(me, cev);
}
if(me->type.periodic && (++round % g_tw_periodicity))
{
(*me->type.periodic)(me);
}
}
tw_wall_now(&me->end_time);
me->stats.s_total = tw_clock_read() - me->stats.s_total;
if((tw_nnodes() > 1 || g_tw_npe > 1) &&
tw_node_eq(&g_tw_mynode, &g_tw_masternode) &&
me->local_master)
printf("*** END SIMULATION ***\n\n");
tw_net_barrier(me);
// call the model PE finalize function
(*me->type.final)(me);
tw_stats(me);
}
void
tw_scheduler_optimistic(tw_pe * me)
{
tw_clock start;
tw_sched_init(me);
tw_wall_now(&me->start_time);
me->stats.s_total = tw_clock_read();
for (;;)
{
if (tw_nnodes() > 1)
{
start = tw_clock_read();
tw_net_read(me);
me->stats.s_net_read += tw_clock_read() - start;
}
tw_gvt_step1(me);
tw_sched_event_q(me);
tw_sched_cancel_q(me);
tw_gvt_step2(me);
if (me->GVT > g_tw_ts_end)
break;
tw_sched_batch(me);
}
tw_wall_now(&me->end_time);
me->stats.s_total = tw_clock_read() - me->stats.s_total;
if((tw_nnodes() > 1 || g_tw_npe > 1) &&
tw_node_eq(&g_tw_mynode, &g_tw_masternode) &&
me->local_master)
printf("*** END SIMULATION ***\n\n");
tw_net_barrier(me);
// call the model PE finalize function
(*me->type.final)(me);
tw_stats(me);
}