forked from Unidata/netcdf-c
/
bm_file.c
1154 lines (1047 loc) · 33.1 KB
/
bm_file.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
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
/*
Copyright 2007, UCAR/Unidata
See COPYRIGHT file for copying and redistribution conditions.
This program benchmarks the write and read of some radar files with
different chunking and compression parameters set.
This program only works on classic model netCDF files. That is,
groups, user-defined types, and other new netCDF-4 features are not
handled by this program. (Input files may be in netCDF-4 format, but
they must conform to the classic model for this program to work.)
For the 3.7 and 4.0 netCDF releases, this program is not expected
for general use. It may be made safer and more general in future
releases, but for now, users should use this code with caution.
$Id: bm_file.c,v 1.64 2010/01/11 19:27:11 ed Exp $
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h> /* Extra high precision time info. */
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef USE_PARALLEL
#include <mpi.h>
#endif
#include <nc_tests.h> /* The ERR macro is here... */
#include <netcdf.h>
#define MILLION 1000000
#define BAD -99
#define NOMEM -98
#define MAX_VO 50 /* Max number of var options on command line. */
#define MAX_DIMS 7 /* Max dim for variables in input file. */
/* This struct holds data about what options we want to apply to
* variable in the created file. (Chunking, compression, etc.) */
typedef struct {
int varid;
int ndims;
int deflate_num;
int shuffle;
size_t chunksize[MAX_DIMS];
int endian;
size_t start[MAX_DIMS], count[MAX_DIMS], inc[MAX_DIMS];
} VAR_OPTS_T;
/* This macro prints an error message with line number and name of
* test program. */
#define ERR1(n) do { \
fflush(stdout); /* Make sure our stdout is synced with stderr. */ \
fprintf(stderr, "Sorry! Unexpected result, %s, line: %d - %s\n", \
__FILE__, __LINE__, nc_strerror(n)); \
return n; \
} while (0)
#ifdef USE_PARALLEL
/* Error handling code for MPI calls. */
#define MPIERR(e) do { \
MPI_Error_string(e, err_buffer, &resultlen); \
printf("MPI error, line %d, file %s: %s\n", __LINE__, __FILE__, err_buffer); \
MPI_Finalize(); \
return 2; \
} while (0)
#endif
/* This function will fill the start and count arrays for the reads
* and writes. */
static int
get_starts_counts(int ndims, size_t *dimlen, int p, int my_rank,
int slow_count, int use_scs, VAR_OPTS_T *vo,
int *num_steps, int *start_inc, int *slice_len,
size_t *last_count, size_t *start, size_t *count)
{
int extra_step = 0;
int total[NC_MAX_VAR_DIMS];
int total_len;
int s, d;
/* User has specified start/count/inc for this var. Parallel runs
* not allowed yet. */
if (use_scs)
{
/* Set the starts and counts for each dim, the len of the slice,
* the total len of the data, and the total extent of the
* dataset in each dimension. */
for (d = 0, *slice_len = 1, total_len = 1; d < vo->ndims; d++)
{
start[d] = vo->start[d];
count[d] = vo->count[d];
(*slice_len) *= count[d];
total_len *= dimlen[d];
}
/* The start increment is provided by the user. */
*start_inc = vo->inc[0];
/* How many steps to write/read these data? */
*num_steps = total_len / (*slice_len);
/* Init this for the total extent in each dim. */
for (d = 0; d < vo->ndims; d++)
total[d] = 0;
/* Check our numbers if we apply increments to start, and read
* count, for this many steps. */
for (s = 0; s < *num_steps; s++)
{
for (d = 0; d < vo->ndims; d++)
{
total[d] += count[d];
if (total[d] >= dimlen[d])
break;
}
if (d != vo->ndims)
break;
}
/* If the numbers didn't come out clean, then figure out the
* last set of counts needed to completely read the data. */
if (s == (*num_steps) - 1)
*last_count = count[0];
else
{
(*num_steps)++;
*last_count = dimlen[0] - total[0];
}
}
else
{
*start_inc = dimlen[0]/slow_count;
while (*start_inc * slow_count < dimlen[0])
(*start_inc)++;
*slice_len = *start_inc;
start[0] = *start_inc * my_rank;
if (start[0] > dimlen[0])
{
fprintf(stderr, "slow_count too large for this many processors, "
"start_inc=%d, slow_count=%d, p=%d, my_rank=%d start[0]=%ld\n",
*start_inc, slow_count, p, my_rank, start[0]);
return 2;
}
count[0] = *start_inc;
for (d = 1; d < ndims; d++)
{
start[d] = 0;
count[d] = dimlen[d];
*slice_len *= dimlen[d];
}
*num_steps = (float)dimlen[0] / (*start_inc * p);
if ((float)dimlen[0] / (*start_inc * p) != dimlen[0] / (*start_inc * p))
{
extra_step++;
(*num_steps)++;
}
if (p > 1)
{
if (!extra_step)
*last_count = 0;
else
{
int left;
left = dimlen[0] - (*num_steps - 1) * *start_inc * p;
if (left > (my_rank + 1) * *start_inc)
*last_count = *start_inc;
else
{
if (left - my_rank * *start_inc < 0)
*last_count = 0;
else
*last_count = left - my_rank * *start_inc;
}
}
}
else
*last_count = dimlen[0] - (*num_steps - 1) * *start_inc;
}
return 0;
}
/* This function finds the size of a file. */
static size_t
file_size(char* name)
{
struct stat stbuf;
stat(name, &stbuf);
return stbuf.st_size;
}
/* Check attribute number a of variable varid in copied file ncid2 to ensure
* it is the same as the corresponding attribute in original file ncid1. */
static int
check_att(int ncid1, int ncid2, int varid, int a)
{
int typeid, typeid2;
size_t len, len2, typelen;
char name[NC_MAX_NAME + 1];
void *d = NULL, *d2 = NULL;
int ret = 0;
/* Check the metadata about the metadata - name, type, length. */
if ((ret = nc_inq_attname(ncid1, varid, a, name)))
return ret;
if ((ret = nc_inq_att(ncid1, varid, name, &typeid, &len)))
return ret;
if ((ret = nc_inq_att(ncid2, varid, name, &typeid2, &len2)))
return ret;
if (len != len2 || typeid != typeid2)
return BAD;
if ((ret = nc_inq_type(ncid1, typeid, NULL, &typelen)))
return ret;
/* Get the two attributes, if they are non-zero. */
if (len)
{
if(!(d = malloc(typelen * len)))
return NOMEM;
if(!(d2 = malloc(typelen * len)))
{
ret = NOMEM;
goto exit;
}
if ((ret = nc_get_att(ncid1, varid, name, d)))
goto exit;
if ((ret = nc_get_att(ncid2, varid, name, d2)))
goto exit;
/* Are they the same? */
if (memcmp(d, d2, typelen * len))
ret = BAD;
}
exit:
/* Free up our resources. */
if (d)
free(d);
if (d2)
free(d2);
return ret;
}
/* Do two files contain the same data and metadata? */
static int
cmp_file(char *file1, char *file2, int *meta_read_us, int *data_read_us,
int use_par, int par_access, int do_cmp, int p, int my_rank,
int slow_count, int verbose, int num_vo, VAR_OPTS_T *vo, int use_scs)
{
int ncid1, ncid2;
int unlimdimid, unlimdimid2;
char name[NC_MAX_NAME + 1], name2[NC_MAX_NAME + 1];
size_t len, len2;
#ifdef USE_PARALLEL
double ftime;
#endif
struct timeval start_time, end_time, diff_time;
void *data = NULL, *data2 = NULL;
int a, v, d;
nc_type xtype, xtype2;
int nvars, ndims, dimids[NC_MAX_VAR_DIMS], natts, real_ndims;
int nvars2, ndims2, dimids2[NC_MAX_VAR_DIMS], natts2;
size_t *count = NULL, *start = NULL;
int slice_len = 1;
size_t *dimlen = NULL, type_size = 0;
size_t last_count;
int start_inc;
int num_steps, step;
int ret = NC_NOERR;
/* Note in the code below I only want to time stuff for file2. */
/* Read the metadata for both files. */
if (use_par)
{
#ifdef USE_PARALLEL
if ((ret = nc_open_par(file1, 0, MPI_COMM_WORLD, MPI_INFO_NULL, &ncid1)))
ERR1(ret);
MPI_Barrier(MPI_COMM_WORLD);
ftime = MPI_Wtime();
if ((ret = nc_open_par(file2, 0, MPI_COMM_WORLD, MPI_INFO_NULL, &ncid2)))
ERR1(ret);
*meta_read_us += (MPI_Wtime() - ftime) * MILLION;
#else
return NC_EPARINIT;
#endif
}
else
{
if ((ret = nc_open(file1, 0, &ncid1)))
ERR1(ret);
if (gettimeofday(&start_time, NULL)) ERR;
if ((ret = nc_open(file2, 0, &ncid2)))
ERR1(ret);
if (gettimeofday(&end_time, NULL)) ERR;
if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR;
*meta_read_us += (int)diff_time.tv_sec * MILLION + (int)diff_time.tv_usec;
}
if (verbose)
printf("%d: reading metadata took %d micro-seconds\n",
my_rank, *meta_read_us);
/* Check the counts of dims, vars, and atts. */
if ((ret = nc_inq(ncid1, &ndims, &nvars, &natts, &unlimdimid)))
ERR1(ret);
if ((ret = nc_inq(ncid1, &ndims2, &nvars2, &natts2, &unlimdimid2)))
ERR1(ret);
if (ndims != ndims2 || nvars != nvars2 || natts != natts2 ||
unlimdimid != unlimdimid2)
ERR1(BAD);
/* Check dims. */
for (d = 0; d < ndims; d++)
{
if ((ret = nc_inq_dim(ncid1, d, name, &len)))
ERR1(ret);
if ((ret = nc_inq_dim(ncid2, d, name2, &len2)))
ERR1(ret);
if (len != len2 || strcmp(name, name2))
ERR1(BAD);
}
/* Check global atts. */
for (a = 0; a < natts; a++)
if ((ret = check_att(ncid1, ncid2, NC_GLOBAL, a)))
ERR1(ret);
/* Check the variables. */
for (v = 0; v < nvars; v++)
{
/* Learn about this var in both files. */
if ((ret = nc_inq_var(ncid1, v, name, &xtype, &ndims, dimids, &natts)))
return ret;
if ((ret = nc_inq_var(ncid2, v, name2, &xtype2, &ndims2, dimids2, &natts2)))
return ret;
/* Check var metadata. */
if (strcmp(name, name2) || xtype != xtype2 || ndims != ndims2 || natts != natts2)
return BAD;
for (d = 0; d < ndims; d++)
if (dimids[d] != dimids2[d])
return BAD;
/* Check the attributes. */
for (a = 0; a < natts; a++)
if ((ret = check_att(ncid1, ncid2, v, a)))
ERR1(ret);
/* Check the data, one slice at a time. (slicing along slowest
* varying dimension.) */
/* Allocate memory for our start and count arrays. If ndims = 0
this is a scalar, which I will treat as a 1-D array with one
element. */
real_ndims = ndims ? ndims : 1;
if (!(start = malloc(real_ndims * sizeof(size_t))))
ERR1(NC_ENOMEM);
if (!(count = malloc(real_ndims * sizeof(size_t))))
ERR1(NC_ENOMEM);
/* The start array will be all zeros, except the first element,
which will be the slice number. Count will be the dimension
size, except for the first element, which will be one, because
we will copy one slice at a time. For this we need the var
shape. */
if (!(dimlen = malloc(real_ndims * sizeof(size_t))))
ERR1(NC_ENOMEM);
for (d=0; d<ndims; d++)
if ((ret = nc_inq_dimlen(ncid1, dimids[d], &dimlen[d])))
ERR1(ret);
/* If this is a scalar, then set the dimlen to 1. */
if (ndims == 0)
dimlen[0] = 1;
if ((ret = get_starts_counts(ndims, dimlen, p, my_rank, slow_count, use_scs,
&vo[v], &num_steps, &start_inc, &slice_len,
&last_count, start, count)))
return ret;
if (verbose)
printf("%d: num_steps=%d, start_inc=%d, slice_len=%d, last_count=%ld\n",
my_rank, num_steps, start_inc, slice_len, last_count);
/* If there are no records, we're done. */
if (!dimlen[0])
goto exit;
/* Find the size of this type. */
if ((ret = nc_inq_type(ncid1, xtype, NULL, &type_size)))
return ret;
/* I will read all this data the same way I eat a large pizze -
* one slice at a time. */
if (!(data = malloc(slice_len * type_size)))
ERR1(NC_ENOMEM);
if (!(data2 = malloc(slice_len * type_size)))
ERR1(NC_ENOMEM);
/* Check the var data for each slice. */
/* for (step = 0; !ret && step < num_steps; step++)*/
for (step = 0; !ret && step < num_steps; step++)
{
if (step == num_steps - 1 && last_count)
count[0] = last_count;
/* Read data from file1. */
if (nc_get_vara(ncid1, v, start, count, data)) ERR;
/* Read data from file2. */
#ifdef USE_PARALLEL
ftime = MPI_Wtime();
#else
if (gettimeofday(&start_time, NULL)) ERR;
#endif
if (nc_get_vara(ncid2, v, start, count, data2)) ERR;
#ifdef USE_PARALLEL
*data_read_us += (MPI_Wtime() - ftime) * MILLION;
#else
if (gettimeofday(&end_time, NULL)) ERR;
if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR;
*data_read_us += (int)diff_time.tv_sec * MILLION + (int)diff_time.tv_usec;
#endif
if (verbose)
printf("%d: reading copy step %d, var %d took %d micro-seconds\n",
my_rank, step, v, *data_read_us);
/* Check data. */
if (do_cmp)
if (memcmp(data, data2, slice_len * type_size))
ERR1(BAD);
/* Increment the start index for the slowest-varying
* dimension. */
start[0] += start_inc;
}
exit:
if (data) free(data);
if (data2) free(data2);
if (dimlen) free(dimlen);
if (start) free(start);
if (count) free(count);
}
if ((ret = nc_close(ncid1)))
ERR1(ret);
if ((ret = nc_close(ncid2)))
ERR1(ret);
return 0;
}
/* Copy a netCDF file, changing cmode if desired, applying chuncking,
* deflate, shuffle, and endianness parameters if desired. */
static
int copy_file(char *file_name_in, char *file_name_out, int cmode_out,
int num_vo, VAR_OPTS_T *vo, int *meta_read_us, int *meta_write_us,
int *data_read_us, int *data_write_us, int *in_format, int use_par,
int par_access, long long *num_bytes, int p, int my_rank,
int slow_count, int verbose, int use_scs, int endianness,
int convert_unlim)
{
int ncid_in, ncid_out;
int natts, nvars, ndims, unlimdimid;
char name[NC_MAX_NAME + 1];
size_t len;
size_t last_count;
int a, v, d;
int ret;
struct timeval start_time, end_time, diff_time;
#ifdef USE_PARALLEL
double ftime;
#endif
if (use_par)
{
#ifdef USE_PARALLEL
ftime = MPI_Wtime();
if ((ret = nc_open_par(file_name_in, 0, MPI_COMM_WORLD, MPI_INFO_NULL, &ncid_in)))
ERR1(ret);
*meta_read_us += (MPI_Wtime() - ftime) * MILLION;
#else
return NC_EPARINIT;
#endif
}
else
{
if (gettimeofday(&start_time, NULL)) ERR;
if ((ret = nc_open(file_name_in, 0, &ncid_in)))
ERR1(ret);
if (gettimeofday(&end_time, NULL)) ERR;
if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR;
*meta_read_us += (int)diff_time.tv_sec * MILLION + (int)diff_time.tv_usec;
}
if (verbose)
printf("%d: reading metadata took %d micro-seconds.\n", my_rank, *meta_read_us);
/* Only classic model files may be used as input. */
if ((ret = nc_inq_format(ncid_in, in_format)))
ERR1(ret);
if (*in_format == NC_FORMAT_NETCDF4)
ERR1(NC_ENOTNC3);
if (strlen(file_name_out))
{
if (use_par)
{
#ifdef USE_PARALLEL
if ((ret = nc_create_par(file_name_out, cmode_out, MPI_COMM_WORLD,
MPI_INFO_NULL, &ncid_out)))
ERR1(ret);
#else
return NC_EPARINIT;
#endif
}
else
{
#define SIXTEEN_MEG 16777216
#define PREEMPTION .75
#define NELEMS 7919
if ((ret = nc_set_chunk_cache(SIXTEEN_MEG, NELEMS, PREEMPTION)))
ERR1(ret);
if ((ret = nc_create(file_name_out, cmode_out, &ncid_out)))
ERR1(ret);
}
}
if ((ret = nc_inq(ncid_in, &ndims, &nvars, &natts, &unlimdimid)))
ERR1(ret);
if (strlen(file_name_out))
{
/* Copy dims. */
for (d = 0; d < ndims; d++)
{
if ((ret = nc_inq_dim(ncid_in, d, name, &len)))
ERR1(ret);
if (convert_unlim)
{
if ((ret = nc_def_dim(ncid_out, name, len, NULL)))
ERR1(ret);
}
else
{
if ((ret = nc_def_dim(ncid_out, name,
(d == unlimdimid) ? NC_UNLIMITED : len,
NULL)))
ERR1(ret);
}
}
/* Copy global atts. */
for (a = 0; a < natts; a++)
{
if (nc_inq_attname(ncid_in, NC_GLOBAL, a, name)) ERR;
if (nc_copy_att(ncid_in, NC_GLOBAL, name, ncid_out, NC_GLOBAL)) ERR;
}
/* Copy the variable metadata. */
for (v = 0; v < nvars; v++)
{
char name[NC_MAX_NAME + 1];
char att_name[NC_MAX_NAME + 1];
nc_type xtype;
int ndims, dimids[NC_MAX_VAR_DIMS], natts;
int varid_out;
int a, o1;
int ret = NC_NOERR;
/* Learn about this var. */
if ((ret = nc_inq_var(ncid_in, v, name, &xtype, &ndims, dimids, &natts)))
return ret;
/* Create the output var. */
if (nc_def_var(ncid_out, name, xtype, ndims, dimids, &varid_out)) ERR;
/* Set the output endianness. For simplicity in this program,
* all vars get the same endianness. But there's no reason why
* this couldn't be varied from var to var, though it is hard to
* see why one would do so. */
if (endianness)
if (nc_def_var_endian(ncid_out, varid_out, endianness)) ERR;
/* Sent chunking and compression if specified in the var options. */
for (o1 = 0; o1 < num_vo; o1++)
if (vo[o1].varid == v)
{
if (vo[o1].chunksize[0])
{
if (nc_def_var_chunking(ncid_out, v, 0, vo[o1].chunksize)) ERR;
}
else
{
if (nc_def_var_chunking(ncid_out, v, 1, NULL)) ERR;
}
if (vo[o1].deflate_num != -1)
if (nc_def_var_deflate(ncid_out, v, vo[o1].shuffle, 1, vo[o1].deflate_num)) ERR;
break;
}
/* Copy the attributes. */
for (a=0; a<natts; a++)
{
if (nc_inq_attname(ncid_in, v, a, att_name)) ERR;
if (nc_copy_att(ncid_in, v, att_name, ncid_out, varid_out)) ERR;
}
}
#ifdef USE_PARALLEL
ftime = MPI_Wtime();
#else
if (gettimeofday(&start_time, NULL)) ERR;
#endif
if ((ret = nc_enddef(ncid_out)))
ERR1(ret);
#ifdef USE_PARALLEL
*meta_write_us += (MPI_Wtime() - ftime) * MILLION;
#else
if (gettimeofday(&end_time, NULL)) ERR;
if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR;
*meta_write_us += (int)diff_time.tv_sec * MILLION + (int)diff_time.tv_usec;
#endif
if (verbose)
printf("%d: copying %d vars, %d global atts, and %d dims took %d micro-seconds\n",
my_rank, nvars, natts, ndims, *meta_write_us);
}
/* Copy the variable data. */
for (v = 0; v < nvars; v++)
{
char name[NC_MAX_NAME + 1];
nc_type xtype;
int ndims, dimids[NC_MAX_VAR_DIMS], natts, real_ndims;
int d;
void *data = NULL;
size_t *count = NULL, *start = NULL;
int slice_len = 1;
size_t *dimlen = NULL;
int ret = NC_NOERR;
size_t type_size;
char type_name[NC_MAX_NAME+1];
int start_inc;
int step, num_steps;
int var_num_bytes;
/* Learn about this var. */
if ((ret = nc_inq_var(ncid_in, v, name, &xtype, &ndims, dimids, &natts)))
return ret;
/* Later on, we will need to know the size of this type. */
if ((ret = nc_inq_type(ncid_in, xtype, type_name, &type_size)))
return ret;
/* Allocate memory for our start and count arrays. If ndims = 0
this is a scalar, which I will treat as a 1-D array with one
element. */
real_ndims = ndims ? ndims : 1;
/* Get the variable shape information. */
if (!(dimlen = malloc(real_ndims * sizeof(size_t))))
ERR1(NC_ENOMEM);
for (d = 0; d < ndims; d++)
if ((ret = nc_inq_dimlen(ncid_in, dimids[d], &dimlen[d])))
ERR1(ret);
if (!(start = malloc(real_ndims * sizeof(size_t))))
ERR1(NC_ENOMEM);
if (!(count = malloc(real_ndims * sizeof(size_t))))
ERR1(NC_ENOMEM);
/* If this is really a scalar, then set the dimlen to 1. */
if (ndims == 0)
dimlen[0] = 1;
/* Get the start and count arrays, and also the increment of the
* start array zeroth element, the number of read steps, the
* length of a slice in number of elements, and the count needed
* for the final read, in the cases where the length of the
* zeroth dimension is not evenly divisible by slow_count. The
* variable slow_count is the number of elements in the slowest
* varying (i.e. the zeroth) dimension to read at one time. For
* vars with an unlimited dimension, this is the number of
* records to read at once. */
if ((ret = get_starts_counts(ndims, dimlen, p, my_rank, slow_count, use_scs,
&vo[v], &num_steps, &start_inc, &slice_len,
&last_count, start, count)))
return ret;
if (verbose)
printf("%d: num_steps=%d, start_inc=%d, slice_len=%d, last_count=%ld\n",
my_rank, num_steps, start_inc, slice_len, last_count);
/* If there are no records, we're done. */
if (!dimlen[0])
goto exit;
/* Allocate memory for one slice. */
if (!(data = malloc(slice_len * type_size)))
return NC_ENOMEM;
/* Copy the var data one slice at a time. */
for (step = 0; !ret && step < num_steps; step++)
{
/* Make sure count is not too big. */
if (step == num_steps - 1 && last_count)
count[0] = last_count;
/* for (d=0; d<ndims; d++) */
/* printf("start[%d]=%d count[%d]=%d dimlen[%d]=%d, step=%d\n", */
/* d, start[d], d, count[d], d, dimlen[d], step); */
/* Read input data. */
#ifdef USE_PARALLEL
ftime = MPI_Wtime();
#else
if (gettimeofday(&start_time, NULL)) ERR;
#endif
if ((ret = nc_get_vara(ncid_in, v, start, count, data)))
ERR1(ret);
#ifdef USE_PARALLEL
*data_read_us += (MPI_Wtime() - ftime) * MILLION;
#else
if (gettimeofday(&end_time, NULL)) ERR;
if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR;
*data_read_us += (int)diff_time.tv_sec * MILLION + (int)diff_time.tv_usec;
#endif
if (verbose)
printf("%d: reading step %d, var %d took %d micro-seconds\n",
my_rank, step, v, *data_read_us);
/* Write the data to the output file. */
if (strlen(file_name_out))
{
#ifdef USE_PARALLEL
ftime = MPI_Wtime();
#else
if (gettimeofday(&start_time, NULL)) ERR;
#endif
if ((ret = nc_put_vara(ncid_out, v, start, count, data)))
ERR1(ret);
#ifdef USE_PARALLEL
*data_write_us += (MPI_Wtime() - ftime) * MILLION;
#else
if (gettimeofday(&end_time, NULL)) ERR;
if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR;
*data_write_us += (int)diff_time.tv_sec * MILLION + (int)diff_time.tv_usec;
#endif
if (verbose)
printf("%d: writing step %d, var %d took %d micro-seconds\n",
my_rank, step, v, *data_write_us);
}
/* Increment start index. */
start[0] += start_inc;
} /* next step */
/* Calculate the data read and write rates in MB/sec. */
for (d = 0, var_num_bytes = type_size; d < ndims; d++)
var_num_bytes *= dimlen[d];
(*num_bytes) += var_num_bytes;
exit:
if (data) free(data);
if (dimlen) free(dimlen);
if (start) free(start);
if (count) free(count);
} /* next var */
if (nc_close(ncid_in)) ERR;
if (strlen(file_name_out))
if (nc_close(ncid_out)) ERR;
return NC_NOERR;
}
#define NDIMS 3
#define MAX_DEFLATE 9
#define INPUT_FILE "/upc/share/testdata/nssl/mosaic3d_nc/tile1/20070803-2300.netcdf"
#define COLON ":"
#define COMMA ","
#define USAGE "\
[-v] Verbose\n\
[-o file] Output file name\n\
[-f N] Output format (1 - classic, 2 - 64-bit offset, 3 - netCDF-4, 4 - netCDF4/CLASSIC)\n\
[-h] Print output header\n\
[-c V:Z:S:C:C:C[,V:Z:S:C:C:C, etc.]] Deflate, shuffle, and chunking parameters for vars\n\
[-t V:S:S:S[,V:S:S:S, etc.]] Starts for reads/writes\n\
[-u V:C:C:C[,V:C:C:C, etc.]] Counts for reads/writes\n\
[-r V:I:I:I[,V:I:I:I, etc.]] Incs for reads/writes\n\
[-d] Doublecheck output by rereading each value\n\
[-m] Do compare of each data value during doublecheck (slow for large files!)\n\
[-p] Use parallel I/O\n\
[-s N] Denom of fraction of slowest varying dimension read.\n\
[-i] Use MPIIO (only relevant for parallel builds).\n\
[-l] Convert unlimited dimensions to fixed dimensions.\n\
[-e 1|2] Set the endianness of output (1=little 2=big).\n\
file Name of netCDF file\n"
static void
usage(void)
{
fprintf(stderr, "bm_file -v [-s N]|[-t V:S:S:S -u V:C:C:C -r V:I:I:I] -o file_out -f N -h"
" -c V:C:C,V:C:C:C -d -m -p -i -e 1|2 -l file\n%s", USAGE);
}
int
main(int argc, char **argv)
{
int num_vo = 0;
extern int optind;
extern int opterr;
extern char *optarg;
char file_in[NC_MAX_NAME + 1], file_out[NC_MAX_NAME + 1] = {""};
int c;
int out_format, in_format, header = 0, doublecheck = 0;
int convert_unlim = 0;
char *str1, *str2, *token, *subtoken;
char *saveptr1, *saveptr2;
int i, ndims, o1;
int cmode = 0;
int mpiio = 0;
int meta_read_us = 0, meta_write_us = 0, data_read_us = 0, data_write_us = 0;
int meta_read2_us = 0, data_read2_us = 0;
int tmeta_read_us = 0, tmeta_write_us = 0, tdata_read_us = 0, tdata_write_us = 0;
int tmeta_read2_us = 0, tdata_read2_us = 0;
VAR_OPTS_T vo[MAX_VO];
int use_par = 0, par_access = 0;
int do_cmp = 0, verbose = 0;
int ret;
float read_rate, write_rate, reread_rate;
int slow_count = 10, use_scs = 0;
int endianness = 0;
long long num_bytes = 0;
int p = 1, my_rank = 0;
int v, d;
#ifdef USE_PARALLEL
MPI_Init(&argc, &argv);
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
#endif
for (o1 = 0; o1 < MAX_VO; o1++)
for (i = 0; i < MAX_DIMS; i++)
vo[o1].chunksize[i] = 0;
while ((c = getopt(argc, argv, "vo:f:hc:dpms:it:u:r:e:l")) != EOF)
switch(c)
{
case 'v':
verbose++;
break;
case 'o':
strcpy(file_out, optarg);
break;
case 'f':
sscanf(optarg, "%d", &out_format);
switch (out_format)
{
case NC_FORMAT_CLASSIC:
break;
case NC_FORMAT_64BIT_OFFSET:
cmode = NC_64BIT_OFFSET;
break;
case NC_FORMAT_CDF5:
cmode = NC_CDF5;
break;
case NC_FORMAT_NETCDF4:
cmode = NC_NETCDF4;
break;
case NC_FORMAT_NETCDF4_CLASSIC:
cmode = NC_NETCDF4|NC_CLASSIC_MODEL;
break;
default:
usage();
return 1;
}
break;
case 'h':
header++;
break;
case 'c':
for (num_vo = 0, str1 = optarg; ; num_vo++, str1 = NULL)
{
int got_z = 0, got_s = 0;
if (num_vo > MAX_VO)
return 1;
if (!(token = strtok_r(str1, COMMA, &saveptr1)))
break;
for (ndims = 0, str2 = token; ; str2 = NULL)
{
int tmp_int;
if (!(subtoken = strtok_r(str2, COLON, &saveptr2)))
break;
if (str2)
sscanf(subtoken, "%d", &(vo[num_vo].varid));
else if (!got_z++)
sscanf(subtoken, "%d", &(vo[num_vo].deflate_num));
else if (!got_s++)
sscanf(subtoken, "%d", &(vo[num_vo].shuffle));
else
{
sscanf(subtoken, "%d", &tmp_int);
vo[num_vo].chunksize[ndims++] = tmp_int;
}
}
vo[num_vo].ndims = ndims;
}
break;
case 't':
for (num_vo = 0, str1 = optarg; ; num_vo++, str1 = NULL)
{
if (num_vo > MAX_VO)
return 1;
if (!(token = strtok_r(str1, COMMA, &saveptr1)))
break;
for (ndims = 0, str2 = token; ; str2 = NULL)
{
if (!(subtoken = strtok_r(str2, COLON, &saveptr2)))
break;
if (str2)
sscanf(subtoken, "%d", &(vo[num_vo].varid));
else
sscanf(subtoken, "%ld", &(vo[num_vo].start[ndims++]));
}
vo[num_vo].ndims = ndims;
}
use_scs++;
break;
case 'u':
for (num_vo = 0, str1 = optarg; ; num_vo++, str1 = NULL)
{
if (num_vo > MAX_VO)
return 1;
if (!(token = strtok_r(str1, COMMA, &saveptr1)))
break;
for (ndims = 0, str2 = token; ; str2 = NULL)
{
if (!(subtoken = strtok_r(str2, COLON, &saveptr2)))
break;
if (str2)
sscanf(subtoken, "%d", &(vo[num_vo].varid));
else
sscanf(subtoken, "%ld", &(vo[num_vo].count[ndims++]));
}
vo[num_vo].ndims = ndims;
}
break;
case 'r':
for (num_vo = 0, str1 = optarg; ; num_vo++, str1 = NULL)
{
if (num_vo > MAX_VO)
return 1;
if (!(token = strtok_r(str1, COMMA, &saveptr1)))
break;
for (ndims = 0, str2 = token; ; str2 = NULL)
{
if (!(subtoken = strtok_r(str2, COLON, &saveptr2)))
break;
if (str2)
sscanf(subtoken, "%d", &(vo[num_vo].varid));
else
sscanf(subtoken, "%ld", &(vo[num_vo].inc[ndims++]));
}
vo[num_vo].ndims = ndims;
}
break;
case 'd':
doublecheck++;
break;
case 'm':
do_cmp++;
doublecheck++;
break;
case 'p':
use_par++;
break;
case 'i':
mpiio++;
break;
case 's':
sscanf(optarg, "%d", &slow_count);
break;
case 'e':
sscanf(optarg, "%d", &endianness);
break;
case 'l':
convert_unlim++;
break;
case '?':
usage();