-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstacs.C
More file actions
570 lines (512 loc) · 18 KB
/
stacs.C
File metadata and controls
570 lines (512 loc) · 18 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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/**
* Copyright (C) 2017 Felix Wang
*
* Simulation Tool for Asynchronous Cortical Streams (stacs)
*/
#include "stacs.h"
/**************************************************************************
* Charm++ Read-Only Variables
**************************************************************************/
/*readonly*/ CProxy_Main mainProxy;
/*readonly*/ CkGroupID mCastGrpId;
/*readonly*/ unsigned int randseed;
/*readonly*/ std::string netwkdir;
/*readonly*/ int netparts;
/*readonly*/ int netfiles;
/*readonly*/ std::string filebase;
/*readonly*/ std::string fileload;
/*readonly*/ std::string filesave;
/*readonly*/ std::string recordir;
/*readonly*/ std::string groupdir;
/*readonly*/ tick_t tstep;
/*readonly*/ idx_t nevtday;
/*readonly*/ idx_t intdisp;
/*readonly*/ idx_t intrec;
/*readonly*/ idx_t intbal;
/*readonly*/ idx_t intsave;
/*readonly*/ tick_t tmax;
/*readonly*/ tick_t tepisode;
/*readonly*/ idx_t episodes;
/*readonly*/ int grpminlen;
/*readonly*/ tick_t grpmaxdur;
/*readonly*/ idx_t grpvtxmin;
/*readonly*/ idx_t grpvtxmax;
/**************************************************************************
* Main
**************************************************************************/
// Main entry point
//
Main::Main(CkArgMsg *msg) {
// Display title
CkPrintf("\nSimulation Tool for Asynchronous Cortical Streams (stacs)\n");
// Command line arguments
std::string configfile;
if (msg->argc < 2) {
configfile = std::string(CONFIG_DEFAULT);
runmode = std::string(RUNMODE_EMPTY);
}
else if (msg->argc == 2) {
configfile = std::string(msg->argv[1]);
runmode = std::string(RUNMODE_EMPTY);
}
else if (msg->argc == 3) {
configfile = msg->argv[1];
runmode = msg->argv[2];
}
else {
CkPrintf("Usage: [config file] [run mode]\n");
CkExit();
}
delete msg;
// Read configuration
if (ReadConfig(configfile)) {
CkPrintf("Error reading configuration...\n");
CkExit();
}
// Charm information
mainProxy = thisProxy;
mCastGrpId = CProxy_CkMulticastMgr::ckNew();
real_t netpe = (real_t)netparts/CkNumPes();
if (netpe < 1) { netpe = 1; }
// Display configuration information
CkPrintf(" STACS Run Mode (runmode): %s\n"
" Network Data Files (netfiles): %d\n"
" Network Partitions (netparts): %d\n"
" Charm++ Processing Elements : %d\n"
" Network Partitions per PE : %.2g\n",
runmode.c_str(), netfiles, netparts, CkNumPes(), netpe);
// Read model information
if (ReadModel()) {
CkPrintf("Error reading model information...\n");
CkExit();
}
// Read graph information
if (ReadGraph()) {
CkPrintf("Error reading graph specification...\n");
CkExit();
}
//Read graph distribution files (for built networks)
if (runmode != std::string(RUNMODE_BUILD) &&
runmode != std::string(RUNMODE_BUILDSIM)) {
if (ReadDist()) {
CkPrintf("Error reading graph distribution...\n");
CkExit();
}
// Convert group percentage to index
grpvtxmin = (idx_t)(std::floor(grpvtxminreal * netdist[netparts].nvtx));
grpvtxmax = (idx_t)(std::floor(grpvtxmaxreal * netdist[netparts].nvtx));
}
// Netdata and network chare arrays
ninit = 0;
cinit = 0;
// Set Round Robin Mapping for data
// Ideally set this to one chare per compute node
CkArrayOptions netdataopts(netfiles);
CProxy_RRMap rrMap = CProxy_RRMap::ckNew();
netdataopts.setMap(rrMap);
// Create chare arrays with model information
++ninit;
//mModname *mmodname = BuildModname();
mModname *mmodname = BuildModname();
netdata = CProxy_Netdata::ckNew(mmodname, netdataopts);
// Initialize network chares as well
++ninit;
mModel *mmodel = BuildModel();
network = CProxy_Network::ckNew(mmodel, netparts);
// Set callback to return control to main
CkCallback cbinit(CkReductionTarget(Main, Init), mainProxy);
netdata.ckSetReductionClient(&cbinit);
network.ckSetReductionClient(&cbinit);
// bookkeeping for program control
readflag = true;
loadflag = true;
buildflag = false;
moveflag = false;
writeflag = false;
lbflag = true;
}
// Main migration
//
Main::Main(CkMigrateMessage *msg) {
delete msg;
}
/**************************************************************************
* STACS Startup
**************************************************************************/
// Coordination for file input, initialized chare arrays
//
void Main::Init() {
// Determine program control based on the run mode
if (++cinit == ninit) {
ninit = 0;
cinit = 0;
// Populate network (build or from disk)
if (readflag) {
readflag = false;
// Pass some metadata to network chares
// now that netdata has been initialized
++ninit;
network.InitProxy(netdata);
// Network needs to be built
if (runmode == std::string(RUNMODE_BUILD) ||
runmode == std::string(RUNMODE_BUILDSIM)) {
buildflag = true;
CkPrintf("Reading datafiles\n");
// Load data files (if any) from disk
++ninit;
netdata.LoadFile();
++ninit;
mGraph *mgraph = BuildGraph();
network.OrderGraph(mgraph);
}
// Network needs to be read from disk
else {
CkPrintf("Reading network\n");
// Load network from disk
++ninit;
mDist *mdist = BuildDist();
netdata.LoadData(mdist);
// Optional loading of information based on runmode
// Partitioning information
if (runmode == std::string(RUNMODE_MIGRATE) ||
runmode == std::string(RUNMODE_REPART) ||
runmode == std::string(RUNMODE_REORDER)) {
moveflag = true;
++ninit;
netdata.LoadRepart();
}
}
}
// Load network partitions
else if (loadflag) {
loadflag = false;
// Load any data files
if (buildflag) {
CkPrintf("Loading datafiles\n");
++ninit;
network.LoadFile();
}
// Regular loading of network
else {
CkPrintf("Loading network\n");
++ninit;
network.LoadData();
// Optional loading of information based on runmode
// Partitioning information
if (runmode == std::string(RUNMODE_MIGRATE) ||
runmode == std::string(RUNMODE_REPART) ||
runmode == std::string(RUNMODE_REORDER)) {
++ninit;
network.LoadRepart();
}
// Polychronous group information
else if (runmode == std::string(RUNMODE_FINDGROUP) ||
runmode == std::string(RUNMODE_ESTIMATE)) {
++ninit;
mGroup *mgroup = BuildGroup();
// TODO: Should this stay as network?
network.LoadGroup(mgroup);
}
}
}
// Build network (from graph and files)
else if (buildflag) {
buildflag = false;
writeflag = true;
CkPrintf("Building network\n");
// Start timer
wcstart = std::chrono::system_clock::now();
// Build Network
++ninit;
network.Build();
}
// Reorder an already built network
else if (moveflag) {
moveflag = false;
writeflag = true;
CkPrintf("Repartitioning network\n");
// Start timer
wcstart = std::chrono::system_clock::now();
++ninit;
network.Repart();
}
// Write out any generated network
else if (writeflag) {
writeflag = false;
CkPrintf("Writing network\n");
// Stop timer
wcstop = std::chrono::system_clock::now();
// Print timing
std::chrono::duration<real_t> wctime = std::chrono::duration_cast<std::chrono::milliseconds>(wcstop - wcstart);
CkPrintf(" Elapsed time (wall clock): %" PRIrealsec " seconds\n", wctime.count());
// Determine if exiting afterwards or not
if (runmode == std::string(RUNMODE_BUILD) ||
runmode == std::string(RUNMODE_MIGRATE) ||
runmode == std::string(RUNMODE_REPART) ||
runmode == std::string(RUNMODE_REORDER)) {
// Halting coordination
chalt = 0;
nhalt = 0;
// Set callback for halting (actually not needed)
CkCallback cbhalt(CkReductionTarget(Main, Halt), mainProxy);
network.ckSetReductionClient(&cbhalt);
// Write network to disk
++nhalt;
network.SaveCloseNetwork();
}
else {
if (runmode == std::string(RUNMODE_BUILDSIM)) {
// Write network to disk
++ninit;
network.SaveBuild();
// Convert runmode to simulate
runmode = std::string(RUNMODE_SIMULATE);
}
}
}
// Start running the network
else {
CkPrintf("Initializing network\n");
// Initialize coordination
CkCallback cbstart(CkReductionTarget(Main, Start), mainProxy);
CkCallback cbcheck(CkReductionTarget(Main, Check), mainProxy);
// Initialize network
++ninit;
network.InitNetwork(runmode, cbcheck);
network.ckSetReductionClient(&cbstart);
#ifdef STACS_WITH_YARP
// stream
++ninit;
mVtxs *mvtxs = BuildVtxs();
stream = CProxy_Stream::ckNew(mvtxs);
stream.ckSetReductionClient(&cbstart);
#endif
}
}
}
// Coordination for starting simulation, initialized network partitions
//
void Main::Start() {
if (++cinit == ninit) {
ninit = 0;
cinit = 0;
// Start simulation
CkCallback cbstop(CkReductionTarget(Main, Stop), mainProxy);
network.ckSetReductionClient(&cbstop);
if (runmode == RUNMODE_SIMULATE) {
if (episodic) {
CkPrintf(" Random Number Seed (randseed): %u\n"
" Network Plasticity (plastic): %s\n"
" Simulation Time Step (tstep): %" PRIrealms "ms\n"
" Event Queue Length (teventq): %" PRIrealms "ms\n"
" Time per Episode (tepisode): %" PRIrealms "ms\n"
" Number of Episodes (episodes): %" PRIidx "\n",
randseed, (plastic ? "yes" : "no"),
(((real_t) tstep)/TICKS_PER_MS), teventq,
(((real_t) tepisode)/TICKS_PER_MS), episodes);
}
else {
CkPrintf(" Random Number Seed (randseed): %u\n"
" Network Plasticity (plastic): %s\n"
" Simulation Time Step (tstep): %" PRIrealms "ms\n"
" Event Queue Length (teventq): %" PRIrealms "ms\n"
" Display Interval (tdisplay): %" PRIrealms "ms\n"
" Recording Interval (trecord): %" PRIrealms "ms\n"
" Rebalance Interval (tbalance): %" PRIrealms "ms\n"
" Save State Interval (tsave): %" PRIrealms "ms\n"
" Max Simulation Time (tmax): %" PRIrealms "ms\n",
randseed, (plastic ? "yes" : "no"),
(((real_t) tstep)/TICKS_PER_MS), teventq, tdisplay,
trecord, tbalance, tsave, (((real_t) tmax)/TICKS_PER_MS));
}
// Set compute cycle
netcycle = CkCallback(CkIndex_Network::CycleSim(), network);
}
else if (runmode == RUNMODE_SIMGPU) {
if (episodic) {
CkPrintf(" Random Number Seed (randseed): %u\n"
" Network Plasticity (plastic): %s\n"
" Simulation Time Step (tstep): %" PRIrealms "ms\n"
" Event Queue Length (teventq): %" PRIrealms "ms\n"
" Time per Episode (tepisode): %" PRIrealms "ms\n"
" Number of Episodes (episodes): %" PRIidx "\n",
randseed, (plastic ? "yes" : "no"),
(((real_t) tstep)/TICKS_PER_MS), teventq,
(((real_t) tepisode)/TICKS_PER_MS), episodes);
}
else {
CkPrintf(" Random Number Seed (randseed): %u\n"
" Network Plasticity (plastic): %s\n"
" Simulation Time Step (tstep): %" PRIrealms "ms\n"
" Event Queue Length (teventq): %" PRIrealms "ms\n"
" Display Interval (tdisplay): %" PRIrealms "ms\n"
" Recording Interval (trecord): %" PRIrealms "ms\n"
" Save State Interval (tsave): %" PRIrealms "ms\n"
" Max Simulation Time (tmax): %" PRIrealms "ms\n",
randseed, (plastic ? "yes" : "no"),
(((real_t) tstep)/TICKS_PER_MS), teventq, tdisplay,
trecord, tsave, (((real_t) tmax)/TICKS_PER_MS));
}
// Set compute cycle
netcycle = CkCallback(CkIndex_Network::CycleSimGPU(), network);
}
else if (runmode == RUNMODE_FINDGROUP) {
// collect active models
std::string grpactivestring;
for (std::size_t i = 0; i < grpactives.size(); ++i) {
std::ostringstream grpactive;
grpactive << " " << grpactives[i];
grpactivestring.append(grpactive.str());
}
// collect mother models
std::string grpmotherstring;
for (std::size_t i = 0; i < grpmothers.size(); ++i) {
std::ostringstream grpmother;
grpmother << " " << grpmothers[i];
grpmotherstring.append(grpmother.str());
}
// collect anchor models
std::string grpanchorstring;
for (std::size_t i = 0; i < grpanchors.size(); ++i) {
std::ostringstream grpanchor;
grpanchor << " " << grpanchors[i];
grpanchorstring.append(grpanchor.str());
}
// Compute vertices evaluated
CkPrintf(" Random Number Seed (randseed): %u\n"
" Group Directory (groupdir): %s\n"
" Simulation Time Step (tstep): %" PRIrealms "ms\n"
" Event Queue Length (teventq): %" PRIrealms "ms\n"
" Polychronization (grpactive):%s\n"
" (grpmother):%s\n"
" (grpanchor):%s\n"
" (grpminlen): %d\n"
" (grpmaxdur): %" PRIrealms "ms\n"
" Evaluated Vertices (grpvtxmin): %.6g (%" PRIidx ")\n"
" (grpvtxmax): %.6g (%" PRIidx ")\n",
randseed, groupdir.c_str(), (((real_t) tstep)/TICKS_PER_MS), teventq,
grpactivestring.c_str(), grpmotherstring.c_str(), grpanchorstring.c_str(),
grpminlen, (((real_t) grpmaxdur)/TICKS_PER_MS),
((real_t)grpvtxminreal), grpvtxmin, ((real_t)grpvtxmaxreal), grpvtxmax);
// Set compute cycle
netcycle = CkCallback(CkIndex_Network::CycleGroup(), network);
}
else if (runmode == RUNMODE_ESTIMATE) {
if (episodic) {
CkPrintf(" Random Number Seed (randseed): %u\n"
" Group Directory (groupdir): %s\n"
" Simulation Time Step (tstep): %" PRIrealms "ms\n"
" Event Queue Length (teventq): %" PRIrealms "ms\n"
" Time per Episode (tepisode): %" PRIrealms "ms\n"
" Number of Episodes (episodes): %" PRIidx "\n",
randseed, groupdir.c_str(), (((real_t) tstep)/TICKS_PER_MS), teventq,
(((real_t) tepisode)/TICKS_PER_MS), episodes);
}
else {
CkPrintf(" Random Number Seed (randseed): %u\n"
" Group Directory (groupdir): %s\n"
" Simulation Time Step (tstep): %" PRIrealms "ms\n"
" Event Queue Length (teventq): %" PRIrealms "ms\n"
" Display Interval (tdisplay): %" PRIrealms "ms\n"
" Recording Interval (trecord): %" PRIrealms "ms\n"
" Max Simulation Time (tmax): %" PRIrealms "ms\n",
randseed, groupdir.c_str(), (((real_t) tstep)/TICKS_PER_MS), teventq,
tdisplay, trecord, (((real_t) tmax)/TICKS_PER_MS));
}
// Set compute cycle
netcycle = CkCallback(CkIndex_Network::CycleEst(), network);
}
#ifdef STACS_WITH_YARP
// Open RPC port
stream.OpenRPC(network, netcycle, rpcpause);
// Start computing
if (rpcpause) {
// Start paused (wait for rpc)
CkPrintf("Starting (paused)\n");
}
else {
CkPrintf("Starting\n");
network.StartNetwork();
}
#else
CkPrintf("Starting\n");
network.StartNetwork();
#endif
// Start timer
wcstart = std::chrono::system_clock::now();
}
}
// Coordination of continuing simulation after repartitioning
//
void Main::Check() {
if (lbflag) {
lbflag = false;
CkPrintf("Repartitioning\n");
network.Repart();
}
else {
lbflag = true;
network.StartNetwork();
}
}
/**************************************************************************
* STACS Shutdown
**************************************************************************/
// Coordination for stopping simulation
//
void Main::Stop() {
CkPrintf("Stopping\n");
// Stop timer
wcstop = std::chrono::system_clock::now();
// Print timing
std::chrono::duration<real_t> wctime = std::chrono::duration_cast<std::chrono::milliseconds>(wcstop - wcstart);
CkPrintf(" Elapsed time (wall clock): %" PRIrealsec " seconds\n", wctime.count());
CkPrintf("Finalizing network\n");
#ifdef STACS_WITH_YARP
// Close RPC port
stream.CloseRPC();
#endif
// Halting coordination
chalt = 0;
nhalt = 0;
// Set callback for halting
CkCallback cbhalt(CkReductionTarget(Main, Halt), mainProxy);
netdata.ckSetReductionClient(&cbhalt);
// Save data from network parts to output files
if (runmode == RUNMODE_SIMULATE ||
runmode == RUNMODE_SIMGPU) {
network.SaveFinalRecord();
++nhalt;
if (plastic) {
network.SaveCloseNetwork();
++nhalt;
}
else {
network.CloseNetwork();
++nhalt;
}
}
else if (runmode == RUNMODE_ESTIMATE) {
network.SaveFinalEstimate();
++nhalt; ++nhalt;
network.CloseNetwork();
++nhalt;
}
else {
network.CloseNetwork();
++nhalt;
}
}
// Exit STACS
//
void Main::Halt() {
// Everything checks back to here
if (++chalt == nhalt) {
// Halt
CkExit();
}
}
/**************************************************************************
* Charm++ Definitions
**************************************************************************/
#include "stacs.def.h"