-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiocsr.C
More file actions
793 lines (713 loc) · 24.4 KB
/
iocsr.C
File metadata and controls
793 lines (713 loc) · 24.4 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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/**
* Copyright (C) 2015 Felix Wang
*
* Simulation Tool for Asynchronous Cortical Streams (stacs)
*
* iocsr.C
* Handles condensed sparse row format
*/
#include "stacs.h"
#include "network.h"
// Maximum size of input line (bytes)
//#define MAXLINE 2560000
#define MAXLINE 10240000
/**************************************************************************
* Charm++ Read-Only Variables
**************************************************************************/
extern /*readonly*/ std::string netwkdir;
extern /*readonly*/ int netparts;
extern /*readonly*/ int netfiles;
extern /*readonly*/ std::string filebase;
extern /*readonly*/ std::string fileload;
extern /*readonly*/ std::string filesave;
extern /*readonly*/ std::string recordir;
extern /*readonly*/ std::string groupdir;
/**************************************************************************
* Network Distribution
**************************************************************************/
// Read graph distribution
//
int Main::ReadDist() {
/* File operations */
FILE *pDist;
char csrfile[1024];
char *line;
char *oldstr, *newstr;
// Prepare buffer
line = new char[MAXLINE];
// Open file for reading
CkPrintf("Reading network distribution\n");//from %s/%s.dist\n", netwkdir.c_str(), filebase.c_str());
sprintf(csrfile, "%s/%s%s.dist", netwkdir.c_str(), filebase.c_str(), fileload.c_str());
pDist = fopen(csrfile,"r");
if (pDist == NULL || line == NULL) {
return 1;
}
netdist.resize(netparts+1);
// Get distribution info
for (int i = 0; i < netparts+1; ++i) {
while(fgets(line, MAXLINE, pDist) && line[0] == '%');
oldstr = line;
newstr = NULL;
// vtxdist
netdist[i].nvtx = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
// edgdist
netdist[i].nedg = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
// statedist
netdist[i].nstate = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
// stickdist
netdist[i].nstick = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
// eventdist
netdist[i].nevent = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
// prtidx
netdist[i].prtidx = 0;
}
// Cleanup
fclose(pDist);
delete[] line;
return 0;
}
// Write graph adjacency distribution
//
int Main::WriteDist(int checkflag) {
/* File operations */
FILE *pDist;
FILE *pMetis;
char csrfile[1024];
/* Bookkeeping */
idx_t nvtx;
idx_t nedg;
idx_t nstate;
idx_t nstick;
idx_t nevent;
std::string filecheck = (checkflag ? filesave : "");
// Open File
CkPrintf("Writing network distribution\n");
sprintf(csrfile, "%s/%s%s.dist", netwkdir.c_str(), filebase.c_str(), filecheck.c_str());
pDist = fopen(csrfile,"w");
sprintf(csrfile, "%s/%s%s.metis", netwkdir.c_str(), filebase.c_str(), filecheck.c_str());
pMetis = fopen(csrfile,"w");
if (pDist == NULL || pMetis == NULL) {
printf("Error opening file for writing\n");
return 1;
}
// Sort distribution
std::sort(netdist.begin(), netdist.end());
// Write to file
nvtx = nedg = nstate = nstick = nevent = 0;
fprintf(pDist, "%" PRIidx " %" PRIidx " %" PRIidx " %" PRIidx " %" PRIidx "\n", nvtx, nedg, nstate, nstick, nevent);
for (std::size_t i = 0; i < netdist.size(); ++i) {
nvtx += netdist[i].nvtx;
nedg += netdist[i].nedg;
nstate += netdist[i].nstate;
nstick += netdist[i].nstick;
nevent += netdist[i].nevent;
fprintf(pDist, "%" PRIidx " %" PRIidx " %" PRIidx " %" PRIidx " %" PRIidx "\n", nvtx, nedg, nstate, nstick, nevent);
}
// For Metis
nvtx = nedg = 0;
fprintf(pMetis, "%" PRIidx " %" PRIidx "\n", nvtx, nedg);
for (int datidx = 0; datidx < netfiles; ++datidx) {
idx_t ndiv = netparts/netfiles;
idx_t nrem = netparts%netfiles;
idx_t nprt = ndiv + (datidx < nrem);
idx_t xprt = datidx*ndiv + (datidx < nrem ? datidx : nrem);
for (idx_t jprt = 0; jprt < nprt; ++jprt) {
nvtx += netdist[xprt+jprt].nvtx;
nedg += netdist[xprt+jprt].nedg;
}
fprintf(pMetis, "%" PRIidx " %" PRIidx "\n", nvtx, nedg);
}
// Cleanup
fclose(pDist);
fclose(pMetis);
return 0;
}
/**************************************************************************
* Network data files
**************************************************************************/
// Read in network files
//
void Netdata::ReadNetwork() {
/* File operations */
FILE *pIndex;
FILE *pCoord;
FILE *pAdjcy;
FILE *pState;
FILE *pEvent;
char csrfile[1024];
char *line;
char *oldstr, *newstr;
/* Bookkeeping */
idx_t jadjcy; // edges
idx_t jedgmodidx; // edgmodidx
idx_t jstate; // state
idx_t jstick; // time state
idx_t jevent; // event
// Prepare buffer
line = new char[MAXLINE];
// Open files for reading
sprintf(csrfile, "%s/%s%s.index.%d", netwkdir.c_str(), filebase.c_str(), fileload.c_str(), datidx);
pIndex = fopen(csrfile,"r");
sprintf(csrfile, "%s/%s%s.coord.%d", netwkdir.c_str(), filebase.c_str(), fileload.c_str(), datidx);
pCoord = fopen(csrfile,"r");
sprintf(csrfile, "%s/%s%s.adjcy.%d", netwkdir.c_str(), filebase.c_str(), fileload.c_str(), datidx);
pAdjcy = fopen(csrfile,"r");
sprintf(csrfile, "%s/%s%s.state.%d", netwkdir.c_str(), filebase.c_str(), fileload.c_str(), datidx);
pState = fopen(csrfile,"r");
sprintf(csrfile, "%s/%s%s.event.%d", netwkdir.c_str(), filebase.c_str(), fileload.c_str(), datidx);
pEvent = fopen(csrfile,"r");
if (pIndex == NULL || pCoord == NULL || pAdjcy == NULL || pState == NULL ||
pEvent == NULL || line == NULL) {
CkPrintf("Error opening graph files on %d\n", datidx);
CkExit();
}
// Read in Parts
for (int k = 0; k < nprt; ++k) {
idx_t nvtx = vtxdist[xprt+k+1] - vtxdist[xprt+k];
idx_t nedg = edgdist[xprt+k+1] - edgdist[xprt+k];
idx_t nstate = statedist[xprt+k+1] - statedist[xprt+k];
idx_t nstick = stickdist[xprt+k+1] - stickdist[xprt+k];
idx_t nevent = eventdist[xprt+k+1] - eventdist[xprt+k];
// Initialize partition data message
int msgSize[MSG_Part];
msgSize[0] = netparts+1; // vtxdist
msgSize[1] = 0; // vtxidx (implicit)
msgSize[2] = nvtx; // vtxnameidx
msgSize[3] = nvtx; // vtxordidx
msgSize[4] = nvtx; // vtxmodidx
msgSize[5] = nvtx*3; // xyz
msgSize[6] = nvtx+1; // xadj
msgSize[7] = nedg; // adjcy
msgSize[8] = nedg; // edgmodidx
msgSize[9] = nstate; // state
msgSize[10] = nstick; // stick
msgSize[11] = nvtx+1; // xevent
msgSize[12] = nevent; // diffuse
msgSize[13] = nevent; // type
msgSize[14] = nevent; // source
msgSize[15] = nevent; // index
msgSize[16] = nevent; // data
parts[k] = new(msgSize, 0) mPart;
// Data sizes
parts[k]->nvtx = nvtx;
parts[k]->nedg = nedg;
parts[k]->nstate = nstate;
parts[k]->nstick = nstick;
parts[k]->nevent = nevent;
parts[k]->prtidx = xprt+k;
// vtxdist
for (int i = 0; i < netparts+1; ++i) {
parts[k]->vtxdist[i] = vtxdist[i];
}
// first prefix entry is zero
parts[k]->xadj[0] = 0;
parts[k]->xevent[0] = 0;
// initialize counters
jadjcy = 0;
jedgmodidx = 0;
jstate = 0;
jstick = 0;
jevent = 0;
// Extract Graph Information
for (idx_t i = 0; i < nvtx; ++i) {
// Read in line (model indexes)
while(fgets(line, MAXLINE, pIndex) && line[0] == '%');
oldstr = line;
newstr = NULL;
{
// vtxidx
idx_t index = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
parts[k]->vtxidx[i] = index;
// vtxnameidx
index = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
parts[k]->vtxnameidx[i] = index;
// vtxordidx
index = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
parts[k]->vtxordidx[i] = index;
}
// Read in line (coordinates)
while(fgets(line, MAXLINE, pCoord) && line[0] == '%');
oldstr = line;
newstr = NULL;
// xyz
for (idx_t j = 0; j < 3; ++j) {
real_t coord = strtoreal(oldstr, &newstr);
oldstr = newstr;
parts[k]->xyz[i*3+j] = coord;
}
// Read line (vertex adjacency)
while(fgets(line, MAXLINE, pAdjcy) && line[0] == '%');
// check for empty line (just the newline character)
if (line[0] == '\n') {
// warn the user
CkPrintf(" Warning: vertex %" PRIidx " has no edges\n", vtxdist[xprt+k] + i);
}
else {
oldstr = line;
newstr = NULL;
for(;;) {
idx_t edg = strtoidx(oldstr, &newstr, 10);
// check for end of line
if (edg == 0 && oldstr != line)
break;
oldstr = newstr;
// adjcy
parts[k]->adjcy[jadjcy++] = edg;
}
}
// xadj
parts[k]->xadj[i+1] = jadjcy;
// Read line (models and states)
while(fgets(line, MAXLINE, pState) && line[0] == '%');
oldstr = line;
newstr = NULL;
// extract model index from name
idx_t modidx = strtomodidx(oldstr, &newstr);
oldstr = newstr;
if (modidx == IDX_T_MAX) {
CkPrintf("vtx: %" PRIidx ", mod: %" PRIidx ", %s \n", i, modidx, line);
}
// first one is the vertex
CkAssert(modidx != IDX_T_MAX);
// modidx
parts[k]->vtxmodidx[i] = modidx;
// TODO: update models
for(idx_t s = 0; s < model[modidx]->getNState(); ++s) {
real_t state = strtoreal(oldstr, &newstr);
oldstr = newstr;
// state
parts[k]->state[jstate++] = state;
}
for(idx_t s = 0; s < model[modidx]->getNStick(); ++s) {
tick_t stick = strtotick(oldstr, &newstr, 16);
oldstr = newstr;
// state
parts[k]->stick[jstick++] = stick;
}
// then the edges
modidx = strtomodidx(oldstr, &newstr);
oldstr = newstr;
//CkPrintf(" Modidx: %s\n", oldstr);
while (modidx != IDX_T_MAX) {
// modidx
parts[k]->edgmodidx[jedgmodidx++] = modidx;
for(idx_t s = 0; s < model[modidx]->getNState(); ++s) {
real_t state = strtoreal(oldstr, &newstr);
oldstr = newstr;
// state
parts[k]->state[jstate++] = state;
}
for(idx_t s = 0; s < model[modidx]->getNStick(); ++s) {
tick_t stick = strtotick(oldstr, &newstr, 16);
oldstr = newstr;
// state
parts[k]->stick[jstick++] = stick;
}
modidx = strtomodidx(oldstr, &newstr);
oldstr = newstr;
}
// Read line (events)
while(fgets(line, MAXLINE, pEvent) && line[0] == '%');
oldstr = line;
newstr = NULL;
// xevent
jevent += strtoidx(oldstr, &newstr, 10);
parts[k]->xevent[i+1] = jevent;
oldstr = newstr;
for(idx_t e = parts[k]->xevent[i]; e < parts[k]->xevent[i+1]; ++e) {
// diffuse
parts[k]->diffuse[e] = strtotick(oldstr, &newstr, 16);
oldstr = newstr;
// type
idx_t type = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
parts[k]->type[e] = type;
// source
parts[k]->source[e] = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
// index
parts[k]->index[e] = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
// event types lacking data
if (type == EVENT_SPIKE) {
// data
parts[k]->data[e] = 0.0;
}
// event types with data
else {
// data
parts[k]->data[e] = strtoreal(oldstr, &newstr);
oldstr = newstr;
}
}
}
//CkPrintf("file: %d, part: %d, jadjcy %" PRIidx ", nedg %" PRIidx "\n", datidx, k, jadjcy, nedg);
// Sanity check
CkAssert(jadjcy == nedg);
CkAssert(jedgmodidx == nedg);
CkAssert(jstate == nstate);
CkAssert(jstick == nstick);
CkAssert(jevent == nevent);
}
// Cleanup
fclose(pIndex);
fclose(pCoord);
fclose(pAdjcy);
fclose(pState);
fclose(pEvent);
delete[] line;
}
// Read network parts (reordering)
//
void Netdata::ReadNetpart() {
/* File operations */
FILE *pPart;
char csrfile[1024];
char *line;
char *oldstr, *newstr;
// Prepare buffer
line = new char[MAXLINE];
// Open files for reading
sprintf(csrfile, "%s/%s%s.part.%d", netwkdir.c_str(), filebase.c_str(), fileload.c_str(), datidx);
pPart = fopen(csrfile,"r");
if (pPart == NULL) {
CkPrintf("Error opening files for reading\n");
CkExit();
}
if (line == NULL) {
CkPrintf("Could not allocate memory for lines\n");
CkExit();
}
// Initialize sizes
reprtidx.clear();
// Read in graph information
for (idx_t i = 0;; ++i) {
while(fgets(line, MAXLINE, pPart) && line[0] == '%');
if (feof(pPart)) { break; }
oldstr = line;
newstr = NULL;
// reprt
idx_t reprt = strtoidx(oldstr, &newstr, 10);
CkAssert(reprt < netparts);
reprtidx.push_back(reprt);
}
// Cleanup
fclose(pPart);
delete[] line;
}
// Write out network files
//
void Netdata::WriteNetwork(int checkflag) {
/* File operations */
FILE *pIndex;
FILE *pCoord;
FILE *pAdjcy;
FILE *pState;
FILE *pEvent;
char csrfile[1024];
std::string filecheck = (checkflag ? filesave : "");
// Open files for writing
CkPrintf("Writing network data files %d\n", datidx);
sprintf(csrfile, "%s/%s%s.index.%d", netwkdir.c_str(), filebase.c_str(), filecheck.c_str(), datidx);
pIndex = fopen(csrfile,"w");
sprintf(csrfile, "%s/%s%s.coord.%d", netwkdir.c_str(), filebase.c_str(), filecheck.c_str(), datidx);
pCoord = fopen(csrfile,"w");
sprintf(csrfile, "%s/%s%s.adjcy.%d", netwkdir.c_str(), filebase.c_str(), filecheck.c_str(), datidx);
pAdjcy = fopen(csrfile,"w");
sprintf(csrfile, "%s/%s%s.state.%d", netwkdir.c_str(), filebase.c_str(), filecheck.c_str(), datidx);
pState = fopen(csrfile,"w");
sprintf(csrfile, "%s/%s%s.event.%d", netwkdir.c_str(), filebase.c_str(), filecheck.c_str(), datidx);
pEvent = fopen(csrfile,"w");
if (pIndex == NULL || pCoord == NULL || pAdjcy == NULL || pState == NULL || pEvent == NULL) {
CkPrintf("Error opening files for writing %d\n", datidx);
CkExit();
}
// Initialize distributions
netdist.resize(nprt);
// Loop through parts
for (int k = 0; k < nprt; ++k) {
// Set up netdist
netdist[k].prtidx = xprt + k;
netdist[k].nvtx = parts[k]->nvtx;
netdist[k].nedg = parts[k]->nedg;
netdist[k].nstate = parts[k]->nstate;
netdist[k].nstick = parts[k]->nstick;
netdist[k].nevent = parts[k]->nevent;
// initialize counters
idx_t jstate = 0;
idx_t jstick = 0;
// Graph adjacency information
for (idx_t i = 0; i < parts[k]->nvtx; ++i) {
// vertex name (and index)
fprintf(pIndex, " %" PRIidx " %" PRIidx " %" PRIidx "\n",
parts[k]->vtxidx[i], parts[k]->vtxnameidx[i], parts[k]->vtxordidx[i]);
// xyz
// vertex coordinates
fprintf(pCoord, " %" PRIrealfull " %" PRIrealfull " %" PRIrealfull "\n",
parts[k]->xyz[i*3+0], parts[k]->xyz[i*3+1], parts[k]->xyz[i*3+2]);
// vertex state
CkAssert(parts[k]->vtxmodidx[i] > 0);
fprintf(pState, " %s", modname[parts[k]->vtxmodidx[i]].c_str());
for(idx_t s = 0; s < model[parts[k]->vtxmodidx[i]]->getNState(); ++s) {
fprintf(pState, " %" PRIrealfull "", parts[k]->state[jstate++]);
}
for(idx_t s = 0; s < model[parts[k]->vtxmodidx[i]]->getNStick(); ++s) {
fprintf(pState, " %" PRItickhex "", parts[k]->stick[jstick++]);
}
// Edges
for (idx_t j = parts[k]->xadj[i]; j < parts[k]->xadj[i+1]; ++j) {
// adjcy
fprintf(pAdjcy, " %" PRIidx "", parts[k]->adjcy[j]);
// edge state
fprintf(pState, " %s", modname[parts[k]->edgmodidx[j]].c_str());
for(idx_t s = 0; s < model[parts[k]->edgmodidx[j]]->getNState(); ++s) {
fprintf(pState, " %" PRIrealfull "", parts[k]->state[jstate++]);
}
for(idx_t s = 0; s < model[parts[k]->edgmodidx[j]]->getNStick(); ++s) {
fprintf(pState, " %" PRItickhex "", parts[k]->stick[jstick++]);
}
}
// Events
fprintf(pEvent, " %" PRIidx "", parts[k]->xevent[i+1] - parts[k]->xevent[i]);
for (idx_t e = parts[k]->xevent[i]; e < parts[k]->xevent[i+1]; ++e) {
// event
if (parts[k]->type[e] == EVENT_SPIKE) {
fprintf(pEvent, " %" PRItickhex " %" PRIidx " %" PRIidx " %" PRIidx "",
parts[k]->diffuse[e], parts[k]->type[e], parts[k]->source[e], parts[k]->index[e]);
}
else {
fprintf(pEvent, " %" PRItickhex " %" PRIidx " %" PRIidx " %" PRIidx " %" PRIrealfull "",
parts[k]->diffuse[e], parts[k]->type[e], parts[k]->source[e], parts[k]->index[e], parts[k]->data[e]);
}
}
// xadj
fprintf(pAdjcy, "\n");
fprintf(pState, "\n");
fprintf(pEvent, "\n");
}
// Sanity check
CkAssert(jstate == parts[k]->nstate);
CkAssert(jstick == parts[k]->nstick);
}
// Cleanup
fclose(pIndex);
fclose(pCoord);
fclose(pAdjcy);
fclose(pState);
fclose(pEvent);
}
/**************************************************************************
* Record Information
**************************************************************************/
// Writing Records to file
//
void Netdata::WriteRecord() {
/* File operations */
FILE *pEvtlog;
FILE *pRecord;
char recfile[1024];
// Only save when data exists
idx_t nevtlog = 0;
idx_t nrecord = 0;
for (int k = 0; k < nprt; ++k) {
nevtlog += records[k]->nevtlog;
nrecord += records[k]->nrecord;
}
if (nevtlog) {
// Open File
sprintf(recfile, "%s/%s/%s%s.evtlog.%" PRIidx ".%d", netwkdir.c_str(), recordir.c_str(), filebase.c_str(), fileload.c_str(), records[0]->iter, datidx);
pEvtlog = fopen(recfile,"w");
if (pEvtlog == NULL) {
CkPrintf("Error opening files for recording %d\n", datidx);
CkExit();
}
// TODO: Store records indexed by time and then by type
// Loop through parts
for (int k = 0; k < nprt; ++k) {
// Loop through events
for (idx_t e = 0; e < records[k]->nevtlog; ++e) {
// event types lacking data
if (records[k]->type[e] == EVENT_SPIKE) {
fprintf(pEvtlog, "%" PRIidx " %" PRItickhex " %" PRIidx "\n",
records[k]->type[e], records[k]->diffuse[e], records[k]->source[e]);
}
// event types with data
else {
fprintf(pEvtlog, "%" PRIidx " %" PRItickhex " %" PRIidx " %" PRIidx " %" PRIrealfull "\n",
records[k]->type[e], records[k]->diffuse[e], records[k]->source[e], records[k]->index[e], records[k]->data[e]);
}
}
}
// Cleanup
fclose(pEvtlog);
}
if (nrecord) {
// Open File
sprintf(recfile, "%s/%s/%s%s.record.%" PRIidx ".%d", netwkdir.c_str(), recordir.c_str(), filebase.c_str(), fileload.c_str(), records[0]->iter, datidx);
pRecord = fopen(recfile,"w");
if (pRecord == NULL) {
CkPrintf("Error opening files for recording %d\n", datidx);
CkExit();
}
// Loop through other records
// TODO: add record type and modify accordingly
for (int k = 0; k < nprt; ++k) {
for (idx_t r = 0; r < records[k]->nrecord; ++r) {
// timestamp followed by number of data entries
fprintf(pRecord, "%" PRIidx " %" PRItickhex " %" PRIidx " %" PRIidx " %" PRIidx "",
records[k]->type[records[k]->nevtlog+r], records[k]->drift[r],
(records[k]->xdata[r+1] - records[k]->xdata[r]),
(records[k]->xdiffuse[r+1] - records[k]->xdiffuse[r]),
(records[k]->xindex[r+1] - records[k]->xindex[r]));
// real data (states)
for (idx_t s = records[k]->xdata[r]; s < records[k]->xdata[r+1]; ++s) {
fprintf(pRecord, " %" PRIrealfull "", records[k]->data[s]);
}
// tick data (sticks)
for (idx_t s = records[k]->xdiffuse[r]; s < records[k]->xdiffuse[r+1]; ++s) {
fprintf(pRecord, " %" PRItickhex "", records[k]->diffuse[s]);
}
// idx data
for (idx_t s = records[k]->xindex[r]; s < records[k]->xindex[r+1]; ++s) {
fprintf(pRecord, " %" PRIidx "", records[k]->index[s]);
}
// one line per record
fprintf(pRecord, "\n");
}
}
// Cleanup
fclose(pRecord);
}
}
/**************************************************************************
* Group Information
**************************************************************************/
// Writing Groups to file
//
// TODO: Move this to Netdata?
void Network::WriteGroup(idx_t groupidx) {
/* File operations */
FILE *pStamp;
FILE *pRoute;
char grpfile[1024];
// Open File
sprintf(grpfile, "%s/%s/%s%s.stamp.%" PRIidx "", netwkdir.c_str(), groupdir.c_str(), filebase.c_str(), fileload.c_str(), vtxidx[groupidx]);
pStamp = fopen(grpfile,"w");
sprintf(grpfile, "%s/%s/%s%s.route.%" PRIidx "", netwkdir.c_str(), groupdir.c_str(), filebase.c_str(), fileload.c_str(), vtxidx[groupidx]);
pRoute = fopen(grpfile,"w");
if (pStamp == NULL || pRoute == NULL) {
CkPrintf("Error opening files for group output %" PRIidx "\n", vtxidx[groupidx]);
CkExit();
}
// Loop through groups
CkAssert(grproutes.size() == grpstamps[groupidx].size());
for (std::size_t p = 0; p < grproutes.size(); ++p) {
// Loop through maps
for (std::size_t s = 0; s < grproutes[p].size(); ++s) {
fprintf(pRoute, "%" PRItickhex " %" PRIidx " %" PRIidx " %" PRItickhex " %" PRItickhex "\n",
grproutes[p][s].diffuse, grproutes[p][s].source, grproutes[p][s].origin, grproutes[p][s].departure, grproutes[p][s].arrival);
}
// empty line between maps
fprintf(pRoute, "\n");
// Loop through stamps
for (std::size_t s = 0; s < grpstamps[groupidx][p].size(); ++s) {
fprintf(pStamp, " %" PRItickhex " %" PRIidx "",
grpstamps[groupidx][p][s].diffuse, grpstamps[groupidx][p][s].source);
}
// newline between stamps
fprintf(pStamp, "\n");
}
// Cleanup
fclose(pStamp);
fclose(pRoute);
}
// Reading Groups from file
//
void Network::ReadGroup(idx_t groupidx) {
/* File operations */
FILE *pStamp;
char grpfile[1024];
char *line;
char *oldstr, *newstr;
// Prepare buffer
line = new char[MAXLINE];
sprintf(grpfile, "%s/%s/%s%s.stamp.%" PRIidx "", netwkdir.c_str(), groupdir.c_str(), filebase.c_str(), fileload.c_str(), vtxidx[groupidx]);
pStamp = fopen(grpfile,"r");
if (line == NULL || pStamp == NULL) {
//CkPrintf("Warning: Group file does not exist %" PRIidx "\n", vtxidx[groupidx]);
return;
}
// Each line is a Group
for (idx_t p = 0;; ++p) {
// Read line (stamps)
while(fgets(line, MAXLINE, pStamp) && line[0] == '%');
if (feof(pStamp)) { break; }
oldstr = line;
newstr = NULL;
std::vector<stamp_t> stamps;
std::set<idx_t> grpsource;
for(;;) {
stamp_t stamp;
// diffuse
stamp.diffuse = strtotick(oldstr, &newstr, 16);
// check for end of line
if (stamp.diffuse == 0 && oldstr != line)
break;
oldstr = newstr;
// source
stamp.source = strtoidx(oldstr, &newstr, 10);
oldstr = newstr;
// Add to group
stamps.push_back(stamp);
// Add to source set
grpsource.insert(stamp.source);
}
// Add to groups
grpstamps[groupidx].push_back(stamps);
// Add to groups duration
grpdur[groupidx].push_back(stamps.back().diffuse + 10*TICKS_PER_MS);
// Add to map
for (std::set<idx_t>::iterator source = grpsource.begin(); source != grpsource.end(); ++source) {
grpmap[(*source)].push_back(std::array<idx_t, 2>{{groupidx, p}});
}
}
grpwindow[groupidx].resize(grpstamps[groupidx].size());
// Cleanup
fclose(pStamp);
delete[] line;
}
/**************************************************************************
* Estimation information
**************************************************************************/
// Writing Records to file
//
void Netdata::WriteEstimate() {
/* File operations */
FILE *pGrplog;
char recfile[1024];
// Only save when data exists
if (grplog.size() > 1) {
// Open File
sprintf(recfile, "%s/%s/%s%s.grplog.%" PRIidx "", netwkdir.c_str(), recordir.c_str(), filebase.c_str(), fileload.c_str(), grplog[0].index);
pGrplog = fopen(recfile,"w");
if (pGrplog == NULL) {
CkPrintf("Error opening files for recording\n");
CkExit();
}
// Loop through events
for (std::size_t e = 1; e < grplog.size(); ++e) {
fprintf(pGrplog, "%" PRIidx " %" PRItickhex " %" PRIidx " %" PRIidx " %" PRIrealsec "\n",
grplog[e].type, grplog[e].diffuse, grplog[e].source, grplog[e].index, grplog[e].data);
}
// Cleanup
fclose(pGrplog);
}
}