forked from parmes/solfec-1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpbf.c
More file actions
1429 lines (1193 loc) · 31.5 KB
/
pbf.c
File metadata and controls
1429 lines (1193 loc) · 31.5 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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* pbf.c
* Copyright (C) 2006, Tomasz Koziara (t.koziara AT gmail.com)
* --------------------------------------------------------------
* portable binary format (PBF)
*/
/* This file is part of Solfec.
* Solfec is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Solfec is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Solfec. If not, see <http://www.gnu.org/licenses/>. */
#if MPI
#include <mpi.h>
#endif
#if HDF5
#include <stdlib.h>
#include <string.h>
#include "pbf.h"
#include "pck.h"
#include "err.h"
/* create or open group */
static hid_t gmake (hid_t loc_id, const char *name)
{
if (H5Lexists (loc_id, name, H5P_DEFAULT))
{
return H5Gopen (loc_id, name, H5P_DEFAULT);
}
else return H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
}
/* push new frame group */
static void new_frame (PBF *bf, int frame, double *time)
{
char name [128];
while (bf->top > 0) PBF_Pop (bf);
snprintf (name, 128, "/%d", frame);
PBF_Push (bf, name);
PBF_Double2 (bf, "time", time, 1);
bf->frame = frame;
}
/* read new frame */
static void read_frame (PBF *bf, int frame, double *time)
{
if (frame >= 0 && frame < bf->count) /* could be a frameless file */
{
new_frame (bf, frame, time);
}
free (bf->i);
bf->ipos = 0;
PBF_Int2 (bf, "ints", &bf->ints, 1);
ERRMEM (bf->i = malloc (sizeof (int [bf->ints])));
PBF_Int2 (bf, "i", bf->i, bf->ints);
free (bf->d);
bf->dpos = 0;
PBF_Int2 (bf, "doubles", &bf->doubles, 1);
ERRMEM (bf->d = malloc (sizeof (double [bf->doubles])));
PBF_Double2 (bf, "d", bf->d, bf->doubles);
}
/* write last frame data */
static void write_frame (PBF *bf)
{
PBF_Int2 (bf, "ints", &bf->ipos, 1);
PBF_Int2 (bf, "i", bf->i, bf->ipos);
PBF_Int2 (bf, "doubles", &bf->dpos, 1);
PBF_Double2 (bf, "d", bf->d, bf->dpos);
H5Fflush (bf->stack[0], H5F_SCOPE_GLOBAL); /* fixes Issue 55 ? */
}
/* count existing time frames */
static int count_time_frames (PBF *bf)
{
char name [128];
int n;
for (n = 0;; n ++) /* count frames */
{
snprintf (name, 128, "/%d", n);
if (!H5Lexists (bf->stack[0], name, H5P_DEFAULT)) break;
}
return n;
}
/* initialize time frames */
static void initialize_time_frames (PBF *bf)
{
int n;
bf->count = count_time_frames (bf); /* count frames */
ERRMEM (bf->times = malloc (sizeof (double [bf->count]))); /* allocate times */
for (n = 0; n < bf->count; n ++)
{
new_frame (bf, n, &bf->times [n]); /* read nth time */
}
}
/* =================== INTERFACE ==================== */
PBF* PBF_Write (const char *path, PBF_FLG append, PBF_FLG parallel)
{
FILE *dat;
char *txt;
PBF *bf;
ERRMEM (txt = malloc (strlen (path) + 64));
ERRMEM (bf = malloc (sizeof (PBF)));
bf->compression = PBF_OFF;
bf->mode = PBF_WRITE;
bf->times = NULL;
bf->time = 0.0;
bf->i = NULL;
bf->ipos = bf->ints = 0;
bf->d = NULL;
bf->dpos = bf->doubles = 0;
#if MPI
if (parallel == PBF_ON)
{
int rank;
bf->parallel = PBF_ON;
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
sprintf (txt, "%s.h5.%d", path, rank);
}
else
#endif
{
bf->parallel = PBF_OFF;
sprintf (txt, "%s.h5", path);
}
bf->top = 0; /* set to zero before frames are initialized (while loop in new_frame) */
if (append == PBF_ON && (dat = fopen (txt, "r")) != NULL) /* HDF5 is noisy if file does not exist */
{
fclose (dat);
if ((bf->stack[0] = H5Fopen(txt, H5F_ACC_RDWR, H5P_DEFAULT)) < 0)
{
free (bf);
free (txt);
return NULL;
}
bf->count = count_time_frames (bf); /* count frames that have been written already */
bf->frame = bf->count; /* set new frame counter */
}
else /* write from scratch */
{
if ((bf->stack[0] = H5Fcreate(txt, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
{
free (bf);
free (txt);
return NULL;
}
bf->count = 0;
bf->frame = 0;
}
bf->next = NULL;
bf->path = txt;
return bf;
}
PBF* PBF_Read (const char *path)
{
PBF *bf, *out;
FILE *dat;
char *txt;
int n, m;
/* count input files */
m = 0;
do
{
ERRMEM (txt = malloc (strlen (path) + 64));
sprintf (txt, "%s.h5.%d", path, m);
dat = fopen (txt, "r");
free (txt);
} while (dat && fclose (dat) == 0 && ++ m); /* m incremented as last */
/* open input files */
out = NULL;
n = m-1;
do
{
ERRMEM (txt = malloc (strlen (path) + 64));
ERRMEM (bf = malloc (sizeof (PBF)));
bf->mode = PBF_READ;
bf->compression = PBF_OFF;
if (m) bf->parallel = PBF_ON;
else bf->parallel = PBF_OFF;
if (m) sprintf (txt, "%s.h5.%d", path, n);
else sprintf (txt, "%s.h5", path);
dat = fopen (txt, "r"); /* H5Fopen is noisy if file does not exist */
if (!dat)
{
free (bf);
free (txt);
return NULL;
}
else fclose (dat);
if ((bf->stack[0] = H5Fopen(txt, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0)
{
free (bf);
free (txt);
return NULL;
}
bf->i = NULL;
bf->ipos = bf->ints = 0;
bf->d = NULL;
bf->dpos = bf->doubles = 0;
bf->top = 0; /* set to zero before frames are initialized (while loop in new_frame) */
initialize_time_frames (bf); /* initialize frames */
bf->next = out;
bf->path = txt;
out = bf;
} while (-- n >= 0); /* the first item in the returned list corresponds to rank 0 */
for (bf = out; bf; bf = bf->next) /* for all input files */
{
read_frame (bf, 0, &bf->time); /* start with first frame */
}
return out;
}
void PBF_Close (PBF *bf)
{
PBF *next;
for (; bf; bf = next)
{
if (bf->mode == PBF_WRITE) write_frame (bf); /* write last frame */
if (bf->times) free (bf->times); /* may exist in both modes (appended wrie) */
while (bf->top > 0) PBF_Pop (bf);
H5Fclose (bf->stack[0]);
if (bf->count == 0 && bf->ipos == 0 && bf->dpos == 0) /* empty file */
{
remove (bf->path);
}
next = bf->next;
free (bf->path);
free (bf);
}
}
void PBF_Time (PBF *bf, double *time)
{
if (bf->mode == PBF_WRITE)
{
ASSERT ((*time) >= bf->time, ERR_PBF_OUTPUT_TIME_DECREASED);
if (bf->frame > bf->count) write_frame (bf); /* write last frame (count > 0 in append mode) */
new_frame (bf, bf->frame, time); /* create new frame */
bf->ipos = bf->dpos = 0; /* zero buffer pointers */
bf->time = *time;
bf->frame ++; /* next frame counter */
}
else
{
*time = bf->time; /* current time */
}
}
int PBF_Label (PBF *bf, const char *label)
{
ASSERT_DEBUG (bf->top >= 1, "PBF ERROR: PBF_Time must be called before PBF_Label!\n");
if (bf->mode == PBF_WRITE)
{
hid_t g = gmake (bf->stack[1], "LABELS");
ASSERT (g >= 0, ERR_PBF_WRITE);
int data [2] = {bf->ipos, bf->dpos};
ASSERT (H5LTset_attribute_int (g, ".", label, data, 2) >= 0, ERR_PBF_WRITE);
H5Gclose (g);
return 1;
}
else
{
if (!H5Lexists (bf->stack[1], "LABELS", H5P_DEFAULT)) return 0;
hid_t g = H5Gopen (bf->stack[1], "LABELS", H5P_DEFAULT);
ASSERT (g >= 0, ERR_PBF_READ);
int data [2];
if (!H5LTfind_attribute (g, label))
{
H5Gclose (g);
return 0;
}
else ASSERT (H5LTget_attribute_int (g, ".", label, data) >= 0, ERR_PBF_READ);
bf->ipos = data [0];
bf->dpos = data [1];
H5Gclose (g);
return 1;
}
}
void PBF_Short (PBF *bf, short *value, int length)
{
int *copy, i;
ERRMEM (copy = malloc (sizeof (int [length])));
if (bf->mode == PBF_WRITE)
{
for (i = 0; i < length; i ++) copy [i] = value [i];
}
PBF_Int (bf, copy, length);
if (bf->mode == PBF_READ)
{
for (i = 0; i < length; i ++) value [i] = copy [i];
}
free (copy);
}
void PBF_Int (PBF *bf, int *value, int length)
{
if (bf->mode == PBF_WRITE)
{
pack_ints (&bf->ints, &bf->i, &bf->ipos, value, length);
}
else
{
unpack_ints (&bf->ipos, bf->i, bf->ints, value, length);
}
}
void PBF_Uint (PBF *bf, unsigned int *value, int length)
{
int *copy, i;
ERRMEM (copy = malloc (sizeof (int [length])));
if (bf->mode == PBF_WRITE)
{
for (i = 0; i < length; i ++) copy [i] = value [i];
}
PBF_Int (bf, copy, length);
if (bf->mode == PBF_READ)
{
for (i = 0; i < length; i ++) value [i] = copy [i];
}
free (copy);
}
void PBF_Double (PBF *bf, double *value, int length)
{
if (bf->mode == PBF_WRITE)
{
pack_doubles (&bf->doubles, &bf->d, &bf->dpos, value, length);
}
else
{
unpack_doubles (&bf->dpos, bf->d, bf->doubles, value, length);
}
}
void PBF_String (PBF *bf, char **value)
{
if (bf->mode == PBF_WRITE) /* pack into ints */
{
int len = strlen (*value);
PBF_Int (bf, &len, 1);
int cpi = sizeof (int)/sizeof (char);
int ints = (len/cpi) + (len%cpi ? 1 : 0);
int *i = malloc (sizeof (int [ints]));
memcpy (i, *value, len*sizeof(char));
PBF_Int (bf, i, ints);
free (i);
}
else /* unpack from ints */
{
int len;
PBF_Int (bf, &len, 1);
int cpi = sizeof (int)/sizeof (char);
int ints = (len/cpi) + (len%cpi ? 1 : 0);
(*value) = malloc (sizeof (int [ints]));
PBF_Int (bf, (int*)(*value), ints);
(*value) [len] = '\0';
}
}
int PBF_Has_Group (PBF *bf, const char *name)
{
return H5Lexists (bf->stack[bf->top], name, H5P_DEFAULT);
}
void PBF_Push (PBF *bf, const char *name)
{
bf->top ++;
if (bf->mode == PBF_WRITE)
{
ASSERT (bf->top < PBF_MAXSTACK, ERR_PBF_WRITE);
ASSERT ((bf->stack[bf->top] = H5Gcreate (bf->stack[bf->top-1], name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) >= 0, ERR_PBF_WRITE);
}
else
{
ASSERT (bf->top < PBF_MAXSTACK, ERR_PBF_READ);
ASSERT ((bf->stack [bf->top] = H5Gopen (bf->stack[bf->top-1], name, H5P_DEFAULT)) >= 0, ERR_PBF_READ);
}
}
void PBF_Pop (PBF *bf)
{
ASSERT_DEBUG (bf->top > 0, "PBF ERROR: too many pops!\n");
H5Gclose (bf->stack [bf->top]);
bf->top --;
}
void PBF_Int2 (PBF *bf, const char *name, int *value, hsize_t length)
{
if (bf->mode == PBF_WRITE)
{
if (length == 1)
{
ASSERT (H5LTset_attribute_int (bf->stack [bf->top], ".", name, value, length) >= 0, ERR_PBF_WRITE);
}
else
{
ASSERT (H5LTmake_dataset_int (bf->stack [bf->top], name, 1, &length, value) >= 0, ERR_PBF_WRITE);
}
}
else
{
if (length == 1)
{
ASSERT (H5LTget_attribute_int (bf->stack [bf->top], ".", name, value) >= 0, ERR_PBF_READ);
}
else
{
ASSERT (H5LTread_dataset_int (bf->stack [bf->top], name, value) >= 0, ERR_PBF_READ);
}
}
}
void PBF_Double2 (PBF *bf, const char *name, double *value, hsize_t length)
{
if (bf->mode == PBF_WRITE)
{
if (length == 1)
{
ASSERT (H5LTset_attribute_double (bf->stack [bf->top], ".", name, value, length) >= 0, ERR_PBF_WRITE);
}
else
{
ASSERT (H5LTmake_dataset_double (bf->stack [bf->top], name, 1, &length, value) >= 0, ERR_PBF_WRITE);
}
}
else
{
if (length == 1)
{
ASSERT (H5LTget_attribute_double (bf->stack [bf->top], ".", name, value) >= 0, ERR_PBF_READ);
}
else
{
ASSERT (H5LTread_dataset_double (bf->stack [bf->top], name, value) >= 0, ERR_PBF_READ);
}
}
}
void PBF_String2 (PBF *bf, const char *name, char **value)
{
if (bf->mode == PBF_WRITE)
{
ASSERT (H5LTset_attribute_string (bf->stack [bf->top], ".", name, *value) >= 0, ERR_PBF_WRITE);
}
else
{
H5T_class_t c;
hsize_t d;
size_t s;
ASSERT (H5LTget_attribute_info (bf->stack [bf->top], ".", name, &d, &c, &s) >= 0, ERR_PBF_READ);
ERRMEM (*value = malloc (s));
ASSERT (H5LTget_attribute_string (bf->stack [bf->top], ".", name, *value) >= 0, ERR_PBF_READ);
}
}
void PBF_Limits (PBF *bf, double *start, double *end)
{
if (bf->mode == PBF_READ)
{
*start = bf->times [0];
*end = bf->times [bf->count-1];
}
}
void PBF_Seek (PBF *bf, double time)
{
if (bf->mode == PBF_READ)
{
for (; bf; bf = bf->next)
{
double *l, *h, *m;
/* binary search of time frame */
l = bf->times;
h = l + bf->count - 1;
while (l <= h)
{
m = l + (h - l) / 2;
if (time == *m) break;
else if (time < *m) h = m - 1;
else l = m + 1;
}
/* handle limit cases */
if (h < bf->times) m = l;
else if (l > (bf->times + bf->count - 1)) m = h;
/* read data from found frame */
read_frame (bf, m - bf->times, &bf->time);
}
}
}
int PBF_Backward (PBF *bf, int steps)
{
if (bf->mode == PBF_READ)
{
int pos, ret;
for (; bf; bf = bf->next)
{
if (bf->frame < steps) pos = 0, ret = 0;
else pos = bf->frame - steps, ret = 1;
read_frame (bf, pos, &bf->time);
}
return ret;
}
return 0;
}
int PBF_Forward (PBF *bf, int steps)
{
if (bf->mode == PBF_READ)
{
int pos, ret;
for (; bf; bf = bf->next)
{
if (bf->frame + steps >= bf->count) pos = bf->count - 1, ret = 0;
else pos = bf->frame + steps, ret = 1;
read_frame (bf, pos, &bf->time);
}
return ret;
}
return 0;
}
unsigned int PBF_Span (PBF *bf, double t0, double t1)
{
if (bf->mode == PBF_READ)
{
double *l, *h, *m0, *m1;
ASSERT_DEBUG (t0 <= t1, "t0 > t1");
/* binary search for t0 time frame */
l = bf->times;
h = l + bf->count - 1;
while (l <= h)
{
m0 = l + (h - l) / 2;
if (t0 == *m0) break;
else if (t0 < *m0) h = m0 - 1;
else l = m0 + 1;
}
/* handle limit cases for t0 */
if (h < bf->times) m0 = l;
else if (l > (bf->times + bf->count - 1)) m0 = h;
/* binary search for t1 time frame */
l = bf->times;
h = l + bf->count - 1;
while (l <= h)
{
m1 = l + (h - l) / 2;
if (t1 == *m1) break;
else if (t1 < *m1) h = m1 - 1;
else l = m1 + 1;
}
/* handle limit cases for t1 */
if (h < bf->times) m1 = l;
else if (l > (bf->times + bf->count - 1)) m1 = h;
return m1 - m0; /* return span = difference of between frames */
}
return 0;
}
#else /* old XDR based implementation */
#include <string.h>
#include <limits.h>
#include <float.h>
#include "ext/fastlz.h"
#include "pbf.h"
#include "err.h"
#include "alg.h"
/* memory increment */
#define CHUNK 1024
/* memory margin */
#define MARGIN 64
/* DAT file format:
* ----------------
* [FRAME_0]
* [FRAME_1]
* ...
* [FRAME_N]
* ----------
* FRAME_i:
* -------------------
* [UNLABELED_DATA]
* [LABELED_0]
* [LABELED_1]
* ...
* [LABELED_K]
* --------------
*/
/* IDX file format:
* ----------------
* [FRAME_0]
* [FRAME_1]
* ...
* [FRAME_N]
* [FRAME_INF]
* ----------
* FRAME_i:
* -------------------------------
* [TIME] (double) {current time}
* [DOFF] (uint64_t) {offest of FRAME_i in DAT file}
* [IDX_0] (int) {index of first label}
* [POS_0] (u_int) {relative position of labeled data in XDR FRAME_i stream}
* [IDX_1]
* [POS_1]
* ...
* [IDX_K]
* [POS_K]
* [-1] (int) {end of labels marker}
* ----------------------------------
* FRAME_INF:
* -------------------------------
* [TIME] (double) {DBL_MAX time}
* [DOFF] (uint64_t) {offest to last data in DAT file}
* [-2] (int) {INF frame marker}
* ----------------------------------
*/
/* LAB file format:
* ----------------
* [LABEL_0] (string) {first label name}
* [LABEL_1]
* ...
* [LABEL_M]
* ----------
*/
/* write to data file */
static u_int fileread (char **mem, u_int size, FILE *f)
{
char cmp;
fread (&cmp, 1, 1, f); /* read 1 byte */
size --; /* subtract 1 byte */
if (cmp)
{
int maxout, outsize;
char *inp;
ERRMEM (inp = malloc (size));
ASSERT (fread (inp, 1, size, f) == size, ERR_PBF_READ);
maxout = 2 * size;
do
{
maxout *= 2;
free (*mem);
ERRMEM (*mem = malloc (maxout));
outsize = fastlz_decompress (inp, size, *mem, maxout);
} while (outsize == 0);
size = outsize;
free (inp);
}
else
{
free (*mem);
ERRMEM (*mem = malloc (size));
ASSERT (fread (*mem, 1, size, f) == size, ERR_PBF_READ);
}
return size;
}
/* read from data file */
static void filewrite (char *mem, u_int size, FILE *f, char cmp)
{
if (cmp && size < 16) cmp = 0; /* see ext/fastlz.h */
fwrite (&cmp, 1, 1, f); /* write compresion flag (adds 1 byte per frame) */
if (cmp)
{
int nbuf, num;
char *out;
nbuf = 2 * MAX (size, 66); /* see ext/fastlz.h */
ERRMEM (out = malloc (nbuf));
num = fastlz_compress (mem, size, out);
WARNING (num < (int)size, "Compression increased the buffer size => Consider disabling it.");
ASSERT_TEXT (num < nbuf, "Compression increased the buffer size by more than 100%%.");
ASSERT (fwrite (out, 1, num, f) == (unsigned)num, ERR_PBF_WRITE);
free (out);
}
else ASSERT (fwrite (mem, 1, size, f) == size, ERR_PBF_WRITE);
}
/* grow memory buffer in WRITE mode */
static void growmem (PBF *bf, u_int nb)
{
/* increment memory base */
bf->membase += xdr_getpos (&bf->x_dat);
/* resize buffer */
bf->memsize += 2 * bf->memsize + nb;
ERRMEM (bf->mem = realloc (bf->mem, bf->memsize));
/* create new bigger stream starting at base */
xdr_destroy (&bf->x_dat);
xdrmem_create (&bf->x_dat, bf->mem + bf->membase, bf->memsize - bf->membase, XDR_ENCODE);
}
/* initialize a frame to be red */
static void initialise_frame (PBF *bf, int frm)
{
PBF_LABEL *l;
int index;
bf->cur = frm; /* set current frame */
bf->time = bf->mtab [frm].time; /* and time */
/* create new memory XDR stream for DATA chunk */
bf->memsize = bf->mtab [frm+1].doff - bf->mtab [frm].doff;
FSEEK (bf->dat, (OFF_T) bf->mtab [frm].doff, SEEK_SET);
bf->memsize = fileread (&bf->mem, bf->memsize, bf->dat);
xdr_destroy (&bf->x_dat);
xdrmem_create (&bf->x_dat, bf->mem, bf->memsize, XDR_DECODE);
/* empty current labels set */
MAP_Free (&bf->mappool, &bf->labels);
/* seek to the frame in IDX file */
ASSERT (xdr_setpos (&bf->x_idx, bf->mtab [frm].ipos), ERR_PBF_INDEX_FILE_CORRUPTED);
/* read labels */
ASSERT (xdr_int (&bf->x_idx, &index), ERR_PBF_INDEX_FILE_CORRUPTED);
while (index >= 0)
{
ASSERT (index < bf->lsize, ERR_PBF_INDEX_FILE_CORRUPTED);
l = &bf->ltab [index];
/* map label as current one */
MAP_Insert (&bf->mappool, &bf->labels, l->name, l, (MAP_Compare) strcmp);
/* read position */
ASSERT (xdr_u_int (&bf->x_idx, &l->dpos), ERR_PBF_INDEX_FILE_CORRUPTED);
/* get next label */
ASSERT (xdr_int (&bf->x_idx, &index), ERR_PBF_INDEX_FILE_CORRUPTED);
}
}
/* initialise labels and time index */
static void initialise_reading (PBF *bf)
{
int index, num, siz;
u_int dpos;
/* create labels table */
num = 0; siz = CHUNK;
ERRMEM (bf->ltab = malloc (sizeof (PBF_LABEL) * siz));
while (! feof (bf->lab))
{
bf->ltab [num].name = NULL;
if (xdr_string (&bf->x_lab, &bf->ltab [num].name, PBF_MAXSTRING))
{
bf->ltab [num].index = num;
if (++ num >= siz) { siz += CHUNK; ERRMEM (bf->ltab = realloc (bf->ltab, sizeof (PBF_LABEL) * siz)); }
}
else break;
}
bf->ltab = realloc (bf->ltab, sizeof (PBF_LABEL) * num); /* shrink */
bf->lsize = num;
/* create markers table */
num = 0; siz = CHUNK;
ERRMEM (bf->mtab = malloc (sizeof (PBF_MARKER) * siz));
while (! feof (bf->idx))
{
/* time and unlabeled data position */
ASSERT (xdr_double (&bf->x_idx, &bf->mtab [num].time), ERR_PBF_INDEX_FILE_CORRUPTED);
ASSERT (xdr_uint64_t (&bf->x_idx, &bf->mtab [num].doff), ERR_PBF_INDEX_FILE_CORRUPTED);
bf->mtab [num].ipos = xdr_getpos (&bf->x_idx);
/* skip labels */
ASSERT (xdr_int (&bf->x_idx, &index), ERR_PBF_INDEX_FILE_CORRUPTED);
while (index >= 0)
{
ASSERT (index < bf->lsize, ERR_PBF_INDEX_FILE_CORRUPTED);
ASSERT (xdr_u_int (&bf->x_idx, &dpos), ERR_PBF_INDEX_FILE_CORRUPTED);
ASSERT (xdr_int (&bf->x_idx, &index), ERR_PBF_INDEX_FILE_CORRUPTED);
}
if (index == -2) break; /* infinite frame */
if (++ num >= siz) { siz += CHUNK; ERRMEM (bf->mtab = realloc (bf->mtab, sizeof (PBF_MARKER) * siz)); }
}
ASSERT (index == -2, ERR_PBF_INDEX_FILE_CORRUPTED);
bf->mtab = realloc (bf->mtab, sizeof (PBF_MARKER) * (num + 1)); /* shrink (add INF frame) */
bf->msize = num;
/* read first frame */
initialise_frame (bf, 0);
}
/* write last frame data */
static void write_frame (PBF *bf)
{
if (xdr_getpos (&bf->x_idx) > 0)
{
/* mark end of frame labels */
int index = -1;
ASSERT (xdr_int (&bf->x_idx, &index), ERR_PBF_WRITE);
/* write from XDR stream to file */
filewrite (bf->mem, bf->membase + xdr_getpos (&bf->x_dat),
bf->dat, bf->compression == PBF_ON);
/* rewind XDR */
bf->membase = 0;
xdr_destroy (&bf->x_dat);
xdrmem_create (&bf->x_dat, bf->mem, bf->memsize, XDR_ENCODE);
}
}
/* finalize frames after last write */
static void finalize_frames (PBF *bf)
{
if (bf->mode == PBF_WRITE)
{
uint64_t doff;
double time = DBL_MAX;
int index = -2;
/* write last frame */
write_frame (bf);
/* write infinite frame marker */
ASSERT (xdr_double (&bf->x_idx, &time), ERR_PBF_WRITE);
doff = (uint64_t) FTELL (bf->dat);
ASSERT (xdr_uint64_t (&bf->x_idx, &doff), ERR_PBF_WRITE);
ASSERT (xdr_int (&bf->x_idx, &index), ERR_PBF_WRITE);
}
}
/* test before closing */
static int is_empty (FILE *f)
{
fseek (f, 0, SEEK_END);
return ftell (f) == 0;
}
/* copy path */
static char* copypath (const char *path)
{
char *out = NULL;
int l;
if ((l = path ? strlen (path) : 0))
{
ERRMEM (out = malloc (l + 1));
strcpy (out, path);
}
return out;
}
PBF* PBF_Write (const char *path, PBF_FLG append, PBF_FLG parallel)
{
char *txt;
PBF *bf;
#if MPI
int rank;
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
#endif
ERRMEM (bf = malloc (sizeof (PBF)));
ERRMEM (txt = malloc (strlen (path) + 16));
bf->compression = PBF_OFF;
bf->membase = 0;
bf->memsize = CHUNK;
ERRMEM (bf->mem = malloc (bf->memsize));
/* openin files */
#if MPI
sprintf (txt, "%s.dat.%d", path, rank);
#else
sprintf (txt, "%s.dat", path);
#endif
if (! (bf->dat = fopen (txt, "w"))) goto failure;
xdrmem_create (&bf->x_dat, bf->mem, bf->memsize, XDR_ENCODE);
bf->dph = copypath (txt);
#if MPI
sprintf (txt, "%s.idx.%d", path, rank);
#else
sprintf (txt, "%s.idx", path);
#endif
if (! (bf->idx = fopen (txt, "w"))) goto failure;
xdrstdio_create (&bf->x_idx, bf->idx, XDR_ENCODE);
bf->iph = copypath (txt);
#if MPI
sprintf (txt, "%s.lab.%d", path, rank);
#else
sprintf (txt, "%s.lab", path);