-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathpio_darray.c
More file actions
1034 lines (917 loc) · 39.8 KB
/
pio_darray.c
File metadata and controls
1034 lines (917 loc) · 39.8 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
/** @file
*
* Public functions that read and write distributed arrays in PIO.
*
* When arrays are distributed, each processor holds some of the
* array. Only by combining the distributed arrays from all processor
* can the full array be obtained.
*
* @author Jim Edwards
*/
#include <config.h>
#include <pio.h>
#include <pio_internal.h>
#include <uthash.h>
/**
* @defgroup PIO_read_darray_c Reading Distributes Arrays
* Read data from a netCDF file to a distributed array in C.
*
* @defgroup PIO_write_darray_c Writing Distributes Arrays
* Write data from a distributed array to a netCDF file in C.
*/
/** 10MB default limit. */
PIO_Offset pio_pnetcdf_buffer_size_limit = PIO_BUFFER_SIZE;
/** Global buffer pool pointer. */
void *CN_bpool = NULL;
/** Maximum buffer usage. */
PIO_Offset maxusage = 0;
/** For write_darray_multi_serial() and write_darray_multi_par() to
* indicate that fill is being written. */
#define DARRAY_FILL 1
/** For write_darray_multi_serial() and write_darray_multi_par() to
* indicate that data are being written. */
#define DARRAY_DATA 0
#ifdef USE_MPE
/* The event numbers for MPE logging. */
extern int event_num[2][NUM_EVENTS];
#endif /* USE_MPE */
/**
* Set the PIO IO node data buffer size limit.
*
* The pio_pnetcdf_buffer_size_limit will only apply to files opened after
* the setting is changed.
*
* @param limit the size of the buffer on the IO nodes
* @return The previous limit setting.
* @author Jim Edwards
*/
PIO_Offset
PIOc_set_buffer_size_limit(PIO_Offset limit)
{
PIO_Offset oldsize = pio_pnetcdf_buffer_size_limit;
/* If the user passed a valid size, use it. */
if (limit > 0)
pio_pnetcdf_buffer_size_limit = limit;
return oldsize;
}
/**
* Write one or more arrays with the same IO decomposition to the
* file.
*
* This funciton is similar to PIOc_write_darray(), but allows the
* caller to use their own data buffering (instead of using the
* buffering implemented in PIOc_write_darray()).
*
* When the user calls PIOc_write_darray() one or more times, then
* PIO_write_darray_multi() will be called when the buffer is flushed.
*
* Internally, this function will:
* <ul>
* <li>Find info about file, decomposition, and variable.
* <li>Do a special flush for pnetcdf if needed.
* <li>Allocates a buffer big enough to hold all the data in the
* multi-buffer, for all tasks.
* <li>Calls rearrange_comp2io() to move data from compute to IO
* tasks.
* <li>For parallel iotypes (pnetcdf and netCDF-4 parallel) call
* pio_write_darray_multi_nc().
* <li>For serial iotypes (netcdf classic and netCDF-4 serial) call
* write_darray_multi_serial().
* <li>For subset rearranger, create holegrid to write missing
* data. Then call pio_write_darray_multi_nc() or
* write_darray_multi_serial() to write the holegrid.
* <li>Special buffer flush for pnetcdf.
* </ul>
*
* @param ncid identifies the netCDF file.
* @param varids an array of length nvars containing the variable ids to
* be written.
* @param ioid the I/O description ID as passed back by
* PIOc_InitDecomp().
* @param nvars the number of variables to be written with this
* call.
* @param arraylen the length of the array to be written. This is the
* length of the distrubited array. That is, the length of the portion
* of the data that is on the processor. The same arraylen is used for
* all variables in the call.
* @param array pointer to the data to be written. This is a pointer
* to an array of arrays with the distributed portion of the array
* that is on this processor. There are nvars arrays of data, and each
* array of data contains one record worth of data for that variable.
* @param frame an array of length nvars with the frame or record
* dimension for each of the nvars variables in IOBUF. NULL if this
* iodesc contains non-record vars.
* @param fillvalue pointer an array (of length nvars) of pointers to
* the fill value to be used for missing data.
* @param flushtodisk non-zero to cause buffers to be flushed to disk.
* @return 0 for success, error code otherwise.
* @ingroup PIO_write_darray_c
* @author Jim Edwards, Ed Hartnett
*/
int
PIOc_write_darray_multi(int ncid, const int *varids, int ioid, int nvars,
PIO_Offset arraylen, void *array, const int *frame,
void **fillvalue, bool flushtodisk)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
io_desc_t *iodesc; /* Pointer to IO description information. */
int rlen; /* Total data buffer size. */
var_desc_t *vdesc0; /* First entry in array of var_desc structure for each var. */
int fndims, fndims2; /* Number of dims in the var in the file. */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function calls. */
int ierr; /* Return code. */
void *tmparray;
/* #ifdef USE_MPE */
/* pio_start_mpe_log(DARRAY_WRITE); */
/* #endif /\* USE_MPE *\/ */
/* Get the file info. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__);
ios = file->iosystem;
/* Check inputs. */
if (nvars <= 0 || !varids)
return pio_err(ios, file, PIO_EINVAL, __FILE__, __LINE__);
PLOG((1, "PIOc_write_darray_multi ncid = %d ioid = %d nvars = %d arraylen = %ld "
"flushtodisk = %d",
ncid, ioid, nvars, arraylen, flushtodisk));
/* Check that we can write to this file. */
if (!file->writable)
return pio_err(ios, file, PIO_EPERM, __FILE__, __LINE__);
/* Get iodesc. */
if (!(iodesc = pio_get_iodesc_from_id(ioid)))
return pio_err(ios, file, PIO_EBADID, __FILE__, __LINE__);
pioassert(iodesc->rearranger == PIO_REARR_BOX || iodesc->rearranger == PIO_REARR_SUBSET,
"unknown rearranger", __FILE__, __LINE__);
pioassert(iodesc->readonly == 0,"Multiple sources in map for a single destination",__FILE__,__LINE__);
/* Check the types of all the vars. They must match the type of
* the decomposition. */
for (int v = 0; v < nvars; v++)
{
var_desc_t *vdesc;
if ((ierr = get_var_desc(varids[v], &file->varlist, &vdesc)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
/* if (vdesc->pio_type != iodesc->piotype)
return pio_err(ios, file, PIO_EINVAL, __FILE__, __LINE__);*/
}
/* Get a pointer to the variable info for the first variable. */
if ((ierr = get_var_desc(varids[0], &file->varlist, &vdesc0)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
/* Run these on all tasks if async is not in use, but only on
* non-IO tasks if async is in use. */
if ((!ios->async || !ios->ioproc) && (file->iotype != PIO_IOTYPE_GDAL))
{
/* Get the number of dims for this var. */
PLOG((3, "about to call PIOc_inq_varndims varids[0] = %d", varids[0]));
if ((ierr = PIOc_inq_varndims(file->pio_ncid, varids[0], &fndims)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
PLOG((3, "called PIOc_inq_varndims varids[0] = %d fndims = %d", varids[0], fndims));
for (int v=1; v < nvars; v++){
if ((ierr = PIOc_inq_varndims(file->pio_ncid, varids[v], &fndims2)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
if(fndims != fndims2)
return pio_err(ios, file, PIO_EVARDIMMISMATCH, __FILE__, __LINE__);
}
}
if ((!ios->async || !ios->ioproc) && (file->iotype = PIO_IOTYPE_GDAL))
{
// Do we even need to sort out fndims here? (MSL<<>>)
fndims = 1;
}
/* If async is in use, and this is not an IO task, bcast the
* parameters. */
if (ios->async)
{
if (!ios->ioproc)
{
int msg = PIO_MSG_WRITEDARRAYMULTI;
char frame_present = frame ? true : false; /* Is frame non-NULL? */
char fillvalue_present = fillvalue ? true : false; /* Is fillvalue non-NULL? */
int flushtodisk_int = flushtodisk; /* Need this to be int not boolean. */
if (ios->compmain == MPI_ROOT)
mpierr = MPI_Send(&msg, 1, MPI_INT, ios->ioroot, 1, ios->union_comm);
/* Send the function parameters and associated informaiton
* to the msg handler. */
if (!mpierr)
mpierr = MPI_Bcast(&ncid, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&nvars, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast((void *)varids, nvars, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&ioid, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&arraylen, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(array, arraylen * iodesc->piotype_size, MPI_CHAR, ios->compmain,
ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&frame_present, 1, MPI_CHAR, ios->compmain, ios->intercomm);
if (!mpierr && frame_present)
mpierr = MPI_Bcast((void *)frame, nvars, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&fillvalue_present, 1, MPI_CHAR, ios->compmain, ios->intercomm);
if (!mpierr && fillvalue_present)
mpierr = MPI_Bcast((void *)fillvalue, nvars * iodesc->piotype_size, MPI_CHAR,
ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&flushtodisk_int, 1, MPI_INT, ios->compmain, ios->intercomm);
PLOG((2, "PIOc_write_darray_multi file->pio_ncid = %d nvars = %d ioid = %d arraylen = %d "
"frame_present = %d fillvalue_present = %d flushtodisk = %d", file->pio_ncid, nvars,
ioid, arraylen, frame_present, fillvalue_present, flushtodisk));
}
/* Handle MPI errors. */
if ((mpierr2 = MPI_Bcast(&mpierr, 1, MPI_INT, ios->comproot, ios->my_comm)))
return check_mpi(NULL, file, mpierr2, __FILE__, __LINE__);
if (mpierr)
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
/* Share results known only on computation tasks with IO tasks. */
if ((mpierr = MPI_Bcast(&fndims, 1, MPI_INT, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
PLOG((3, "shared fndims = %d", fndims));
}
/* if the buffer is already in use in pnetcdf we need to flush first */
if (file->iotype == PIO_IOTYPE_PNETCDF && file->iobuf)
if ((ierr = flush_output_buffer(file, 1, 0)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
pioassert(!file->iobuf, "buffer overwrite",__FILE__, __LINE__);
/* Determine total size of aggregated data (all vars/records).
* For netcdf serial writes we collect the data on io nodes and
* then move that data one node at a time to the io main node
* and write (or read). The buffer size on io task 0 must be as
* large as the largest used to accommodate this serial io
* method. */
rlen = 0;
if (iodesc->llen > 0 ||
((file->iotype == PIO_IOTYPE_NETCDF ||
file->iotype == PIO_IOTYPE_NETCDF4C) && ios->iomain))
rlen = iodesc->maxiobuflen * nvars;
/* Allocate iobuf. */
if (rlen > 0)
{
/* Allocate memory for the buffer for all vars/records. */
if (!(file->iobuf = malloc(iodesc->mpitype_size * (size_t)rlen)))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
PLOG((3, "allocated %lld bytes for variable buffer", (size_t)rlen * iodesc->mpitype_size));
/* If fill values are desired, and we're using the BOX
* rearranger, insert fill values. */
if (iodesc->needsfill && iodesc->rearranger == PIO_REARR_BOX && fillvalue)
{
PLOG((3, "inerting fill values iodesc->maxiobuflen = %d", iodesc->maxiobuflen));
for (int nv = 0; nv < nvars; nv++)
for (int i = 0; i < iodesc->maxiobuflen; i++)
memcpy(&((char *)file->iobuf)[iodesc->mpitype_size * (i + nv * iodesc->maxiobuflen)],
&((char *)fillvalue)[nv * iodesc->mpitype_size], iodesc->mpitype_size);
}
}
else if (file->iotype == PIO_IOTYPE_PNETCDF && ios->ioproc)
{
/* this assures that iobuf is allocated on all iotasks thus
assuring that the flush_output_buffer call above is called
collectively (from all iotasks) */
if (!(file->iobuf = malloc(1)))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
PLOG((3, "allocated token for variable buffer"));
}
if (iodesc->needssort)
{
if (!(tmparray = calloc(arraylen*nvars, iodesc->piotype_size)))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
pio_sorted_copy(array, tmparray, iodesc, nvars, 0);
}
else
{
tmparray = array;
}
/* Move data from compute to IO tasks. */
if ((ierr = rearrange_comp2io(ios, iodesc, tmparray, file->iobuf, nvars)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
/* Write the darray based on the iotype. */
PLOG((2, "about to write darray for iotype = %d", file->iotype));
switch (file->iotype)
{
case PIO_IOTYPE_NETCDF4P:
case PIO_IOTYPE_PNETCDF:
if ((ierr = write_darray_multi_par(file, nvars, fndims, varids, iodesc,
DARRAY_DATA, frame)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
break;
case PIO_IOTYPE_NETCDF4C:
case PIO_IOTYPE_NETCDF:
if ((ierr = write_darray_multi_serial(file, nvars, fndims, varids, iodesc,
DARRAY_DATA, frame)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
break;
case PIO_IOTYPE_GDAL:
if ((ierr = write_darray_multi_serial(file, nvars, fndims, varids, iodesc,
DARRAY_DATA, frame)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
break;
default:
return pio_err(NULL, NULL, PIO_EBADIOTYPE, __FILE__, __LINE__);
}
/* For PNETCDF the iobuf is freed in flush_output_buffer() */
if (file->iotype != PIO_IOTYPE_PNETCDF)
{
/* Release resources. */
if (file->iobuf)
{
PLOG((3,"freeing variable buffer in pio_darray"));
free(file->iobuf);
file->iobuf = NULL;
}
}
/* The box rearranger will always have data (it could be fill
* data) to fill the entire array - that is the aggregate start
* and count values will completely describe one unlimited
* dimension unit of the array. For the subset method this is not
* necessarily the case, areas of missing data may never be
* written. In order to make sure that these areas are given the
* missing value a 'holegrid' is used to describe the missing
* points. This is generally faster than the netcdf method of
* filling the entire array with missing values before overwriting
* those values later. */
if (iodesc->rearranger == PIO_REARR_SUBSET && iodesc->needsfill)
{
PLOG((2, "nvars = %d holegridsize = %ld iodesc->needsfill = %d\n", nvars,
iodesc->holegridsize, iodesc->needsfill));
pioassert(!vdesc0->fillbuf, "buffer overwrite",__FILE__, __LINE__);
/* Get a buffer. */
if (ios->io_rank == 0)
vdesc0->fillbuf = malloc(iodesc->maxholegridsize * iodesc->mpitype_size * nvars);
else if (iodesc->holegridsize > 0)
vdesc0->fillbuf = malloc(iodesc->holegridsize * iodesc->mpitype_size * nvars);
/* copying the fill value into the data buffer for the box
* rearranger. This will be overwritten with data where
* provided. */
if(fillvalue)
for (int nv = 0; nv < nvars; nv++)
for (int i = 0; i < iodesc->holegridsize; i++)
memcpy(&((char *)vdesc0->fillbuf)[iodesc->mpitype_size * (i + nv * iodesc->holegridsize)],
&((char *)fillvalue)[iodesc->mpitype_size * nv], iodesc->mpitype_size);
/* Write the darray based on the iotype. */
switch (file->iotype)
{
case PIO_IOTYPE_PNETCDF:
case PIO_IOTYPE_NETCDF4P:
if ((ierr = write_darray_multi_par(file, nvars, fndims, varids, iodesc,
DARRAY_FILL, frame)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
break;
case PIO_IOTYPE_NETCDF4C:
case PIO_IOTYPE_NETCDF:
if ((ierr = write_darray_multi_serial(file, nvars, fndims, varids, iodesc,
DARRAY_FILL, frame)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
break;
default:
return pio_err(ios, file, PIO_EBADIOTYPE, __FILE__, __LINE__);
}
/* For PNETCDF fillbuf is freed in flush_output_buffer() */
if (file->iotype != PIO_IOTYPE_PNETCDF)
{
/* Free resources. */
if (vdesc0->fillbuf)
{
free(vdesc0->fillbuf);
vdesc0->fillbuf = NULL;
}
}
}
if(iodesc->needssort && tmparray != NULL)
free(tmparray);
/* Flush data to disk for pnetcdf. */
if (ios->ioproc && file->iotype == PIO_IOTYPE_PNETCDF)
if ((ierr = flush_output_buffer(file, flushtodisk, 0)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
/* #ifdef USE_MPE */
/* pio_stop_mpe_log(DARRAY_WRITE, __func__); */
/* #endif /\* USE_MPE *\/ */
return PIO_NOERR;
}
/**
* Find the fill value that would be used for a variable, if fill mode
* was turned on.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param pio_type Type of the variable.
* @param type_size Size of one element of this type.
* @param fillvalue Pointer that will get the fill value.
*
* @return 0 for success, error code otherwise.
* @ingroup PIO_write_darray_c
* @author Ed Hartnett
*/
static int
pio_inq_var_fill_expected(int ncid, int varid, int pio_type, PIO_Offset type_size,
void *fillvalue)
{
signed char byte_fill_value = NC_FILL_BYTE;
char char_fill_value = NC_FILL_CHAR;
short short_fill_value = NC_FILL_SHORT;
int int_fill_value = NC_FILL_INT;
float float_fill_value = NC_FILL_FLOAT;
double double_fill_value = NC_FILL_DOUBLE;
unsigned char ubyte_fill_value = NC_FILL_UBYTE;
unsigned short ushort_fill_value = NC_FILL_USHORT;
unsigned int uint_fill_value = NC_FILL_UINT;
long long int64_fill_value = NC_FILL_INT64;
unsigned long long uint64_fill_value = NC_FILL_UINT64;
char *string_fill_value = "";
int ret;
/* Check inputs. */
assert(fillvalue);
PLOG((2, "pio_inq_var_fill_expected ncid %d varid %d pio_type %d type_size %d",
ncid, varid, pio_type, type_size));
/* Is there a _FillValue attribute? */
ret = PIOc_inq_att_eh(ncid, varid, "_FillValue", 0, NULL, NULL);
PLOG((3, "pio_inq_var_fill_expected ret %d", ret));
/* If there is a fill value, get it. */
if (!ret)
{
if ((ret = PIOc_get_att(ncid, varid, "_FillValue", fillvalue)))
return ret;
}
else /* If no _FillValue at was found we still have work to do. */
{
/* Did we get some other error? */
if (ret != PIO_ENOTATT)
return ret;
/* What is the default fill value for this type? */
switch (pio_type)
{
case PIO_BYTE:
memcpy(fillvalue, &byte_fill_value, type_size);
break;
case PIO_CHAR:
memcpy(fillvalue, &char_fill_value, type_size);
break;
case PIO_SHORT:
memcpy(fillvalue, &short_fill_value, type_size);
break;
case PIO_INT:
memcpy(fillvalue, &int_fill_value, type_size);
break;
case PIO_FLOAT:
memcpy(fillvalue, &float_fill_value, type_size);
break;
case PIO_DOUBLE:
memcpy(fillvalue, &double_fill_value, type_size);
break;
#if defined(_NETCDF4) || defined(_PNETCDF)
case PIO_UBYTE:
memcpy(fillvalue, &ubyte_fill_value, type_size);
break;
case PIO_USHORT:
memcpy(fillvalue, &ushort_fill_value, type_size);
break;
case PIO_UINT:
memcpy(fillvalue, &uint_fill_value, type_size);
break;
case PIO_INT64:
memcpy(fillvalue, &int64_fill_value, type_size);
break;
case PIO_UINT64:
memcpy(fillvalue, &uint64_fill_value, type_size);
break;
#ifdef _NETCDF4
case PIO_STRING:
memcpy(fillvalue, string_fill_value, type_size);
break;
#endif /* _NETCDF4 */
#endif/* _NETCDF4 || _PNETCDF */
default:
return PIO_EBADTYPE;
}
}
return PIO_NOERR;
}
/**
* Find the fillvalue that should be used for a variable.
*
* @param file Info about file we are writing to.
* @param varid the variable ID.
* @param vdesc pointer to var_desc_t info for this var.
* @returns 0 for success, non-zero error code for failure.
* @ingroup PIO_write_darray_c
* @author Ed Hartnett
*/
int
find_var_fillvalue(file_desc_t *file, int varid, var_desc_t *vdesc)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
int pio_type;
PIO_Offset type_size;
int no_fill;
int ierr;
/* Check inputs. */
pioassert(file && file->iosystem && vdesc, "invalid input", __FILE__, __LINE__);
ios = file->iosystem;
PLOG((3, "find_var_fillvalue file->pio_ncid = %d varid = %d", file->pio_ncid, varid));
/* Find out PIO data type of var. */
if ((ierr = PIOc_inq_vartype(file->pio_ncid, varid, &pio_type)))
return pio_err(ios, NULL, ierr, __FILE__, __LINE__);
/* Find out length of type. */
if ((ierr = PIOc_inq_type(file->pio_ncid, pio_type, NULL, &type_size)))
return pio_err(ios, NULL, ierr, __FILE__, __LINE__);
PLOG((3, "getting fill value for varid = %d pio_type = %d type_size = %d",
varid, pio_type, type_size));
/* Allocate storage for the fill value. */
if (!(vdesc->fillvalue = malloc(type_size)))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
/* Get the fill mode and value, if fill mode is on (which is will
* not be, because it is turned off at open/create). */
if ((ierr = PIOc_inq_var_fill(file->pio_ncid, varid, &no_fill, vdesc->fillvalue)))
return pio_err(ios, NULL, ierr, __FILE__, __LINE__);
vdesc->use_fill = no_fill ? 0 : 1;
PLOG((3, "vdesc->use_fill = %d", vdesc->use_fill));
/* Get the fill value one would expect, if NOFILL were not turned
* on. */
if (!vdesc->use_fill)
if ((ierr = pio_inq_var_fill_expected(file->pio_ncid, varid, pio_type, type_size,
vdesc->fillvalue)))
return pio_err(ios, NULL, ierr, __FILE__, __LINE__);
return PIO_NOERR;
}
/**
* Write a distributed array to the output file.
*
* This routine aggregates output on the compute nodes and only sends
* it to the IO nodes when the compute buffer is full or when a flush
* is triggered.
*
* Internally, this function will:
* <ul>
* <li>Locate info about this file, decomposition, and variable.
* <li>If we don't have a fillvalue for this variable, determine one
* and remember it for future calls.
* <li>Initialize or find the multi_buffer for this record/var.
* <li>Find out how much free space is available in the multi buffer
* and flush if needed.
* <li>Store the new user data in the mutli buffer.
* <li>If needed (only for subset rearranger), fill in gaps in data
* with fillvalue.
* <li>Remember the frame value (i.e. record number) of this data if
* there is one.
* </ul>
*
* NOTE: The write multi buffer wmulti_buffer is the cache on compute
* nodes that will collect and store multiple variables before sending
* them to the io nodes. Aggregating variables in this way leads to a
* considerable savings in communication expense. Variables in the wmb
* array must have the same decomposition and base data size and we
* also need to keep track of whether each is a recordvar (has an
* unlimited dimension) or not.
*
* @param ncid the ncid of the open netCDF file.
* @param varid the ID of the variable that these data will be written
* to.
* @param ioid the I/O description ID as passed back by
* PIOc_InitDecomp().
* @param arraylen the length of the array to be written. This should
* be at least the length of the local component of the distrubited
* array. (Any values beyond length of the local component will be
* ignored.)
* @param array pointer to an array of length arraylen with the data
* to be written. This is a pointer to the distributed portion of the
* array that is on this task.
* @param fillvalue pointer to the fill value to be used for missing
* data.
* @returns 0 for success, non-zero error code for failure.
* @ingroup PIO_write_darray_c
* @author Jim Edwards, Ed Hartnett
*/
int
PIOc_write_darray(int ncid, int varid, int ioid, PIO_Offset arraylen, void *array,
void *fillvalue)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Info about file we are writing to. */
io_desc_t *iodesc; /* The IO description. */
var_desc_t *vdesc; /* Info about the var being written. */
void *bufptr; /* A data buffer. */
wmulti_buffer *wmb; /* The write multi buffer for one or more vars. */
int needsflush = 0; /* True if we need to flush buffer. */
void *realloc_data = NULL;
int hashid;
int mpierr = MPI_SUCCESS; /* Return code from MPI functions. */
int ierr = PIO_NOERR; /* Return code. */
size_t io_data_size; /* potential size of data on io task */
PLOG((1, "PIOc_write_darray ncid = %d varid = %d ioid = %d arraylen = %d",
ncid, varid, ioid, arraylen));
#ifdef USE_MPE
pio_start_mpe_log(DARRAY_WRITE);
#endif /* USE_MPE */
/* Get the file info. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__);
ios = file->iosystem;
/* Can we write to this file? */
if (!file->writable)
return pio_err(ios, file, PIO_EPERM, __FILE__, __LINE__);
/* Get decomposition information. */
if (!(iodesc = pio_get_iodesc_from_id(ioid)))
return pio_err(ios, file, PIO_EBADID, __FILE__, __LINE__);
pioassert(iodesc->readonly == 0,"Multiple sources in map for a single destination",__FILE__,__LINE__);
/* Check that the local size of the variable passed in matches the
* size expected by the io descriptor. Fail if arraylen is too
* small, just put a warning in the log if it is too big (the
* excess values will be ignored.) */
if (arraylen < iodesc->ndof)
return pio_err(ios, file, PIO_EINVAL, __FILE__, __LINE__);
PLOG((2, "%s arraylen = %d iodesc->ndof = %d",
(iodesc->ndof != arraylen) ? "WARNING: iodesc->ndof != arraylen" : "",
arraylen, iodesc->ndof));
/* Get var description. */
if ((ierr = get_var_desc(varid, &file->varlist, &vdesc)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
/* If the type of the var doesn't match the type of the
* decomposition, return an error. */
/* if (iodesc->piotype != vdesc->pio_type) */
/* return pio_err(ios, file, PIO_EINVAL, __FILE__, __LINE__); */
/* pioassert(iodesc->mpitype_size == vdesc->mpi_type_size, "wrong mpi info", */
/* __FILE__, __LINE__); */
/* If we don't know the fill value for this var, get it. */
if (!vdesc->fillvalue && file->iotype != PIO_IOTYPE_GDAL)
if ((ierr = find_var_fillvalue(file, varid, vdesc)))
return pio_err(ios, file, PIO_EBADID, __FILE__, __LINE__);
/* Check that if the user passed a fill value, it is correct. If
* use_fill is false, then find_var_fillvalue will not end up
* getting a fill value. */
if (fillvalue && vdesc->use_fill)
if (memcmp(fillvalue, vdesc->fillvalue, vdesc->pio_type_size))
return pio_err(ios, file, PIO_EINVAL, __FILE__, __LINE__);
/* Move to end of list or the entry that matches this ioid. */
hashid = ioid * 10 + vdesc->rec_var;
HASH_FIND_INT( file->buffer, &hashid, wmb);
if (wmb)
PLOG((3, "wmb->ioid = %d wmb->recordvar = %d", wmb->ioid, wmb->recordvar));
/* If we did not find an existing wmb entry, create a new wmb. */
if (!wmb)
{
/* Allocate a buffer. */
if (!(wmb = malloc(sizeof(wmulti_buffer))))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
/* Set pointer to newly allocated buffer and initialize.*/
wmb->recordvar = vdesc->rec_var;
wmb->ioid = ioid;
wmb->num_arrays = 0;
wmb->arraylen = arraylen;
wmb->vid = NULL;
wmb->data = NULL;
wmb->frame = NULL;
wmb->fillvalue = NULL;
wmb->htid = hashid;
HASH_ADD_INT( file->buffer, htid, wmb );
}
PLOG((2, "wmb->num_arrays = %d arraylen = %d iodesc->mpitype_size = %d\n",
wmb->num_arrays, arraylen, iodesc->mpitype_size));
/* Try realloc first and call flush if realloc fails. */
if (arraylen > 0)
{
size_t data_size = (1 + wmb->num_arrays) * arraylen * iodesc->mpitype_size;
if ((realloc_data = realloc(wmb->data, data_size)))
{
needsflush = 0;
wmb->data = realloc_data;
}
else /* Failed to realloc, but wmb->data is still valid for a flush. */
{
needsflush = 1;
}
PLOG((2, "realloc attempted to get %ld bytes for data, needsflush %d", data_size,
needsflush));
}
/* the limit of data_size < INT_MAX is due to a bug in ROMIO which limits
the size of contiguous data to INT_MAX, a fix has been proposed in
https://github.com/pmodels/mpich/pull/2888 */
io_data_size = (1 + wmb->num_arrays) * iodesc->maxiobuflen * iodesc->mpitype_size;
if(io_data_size > INT_MAX)
needsflush = 2;
/* Tell all tasks on the computation communicator whether we need
* to flush data. */
if ((mpierr = MPI_Allreduce(MPI_IN_PLACE, &needsflush, 1, MPI_INT, MPI_MAX,
ios->comp_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
PLOG((2, "needsflush = %d", needsflush));
/* Flush data if needed. */
if (needsflush > 0)
{
/* If needsflush == 2 flush to disk otherwise just flush to io
* node. This will cause PIOc_write_darray_multi() to be
* called. */
if ((ierr = flush_buffer(ncid, wmb, needsflush == 2)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
}
/* Try realloc again if there is a flush. */
if (arraylen > 0 && needsflush > 0)
{
if (!(wmb->data = realloc(wmb->data, (1 + wmb->num_arrays) * arraylen * iodesc->mpitype_size)))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
PLOG((2, "after a flush, realloc got %ld bytes for data", (1 + wmb->num_arrays) * arraylen * iodesc->mpitype_size));
}
/* vid is an array of variable ids in the wmb list, grow the list
* and add the new entry. */
if (!(wmb->vid = realloc(wmb->vid, sizeof(int) * (1 + wmb->num_arrays))))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
/* wmb->frame is the record number, we assume that the variables
* in the wmb list may not all have the same unlimited dimension
* value although they usually do. */
if (vdesc->record >= 0)
if (!(wmb->frame = realloc(wmb->frame, sizeof(int) * (1 + wmb->num_arrays))))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
/* If we need a fill value, get it. If we are using the subset
* rearranger and not using the netcdf fill mode then we need to
* do an extra write to fill in the holes with the fill value. */
if (iodesc->needsfill)
{
/* Get memory to hold fill value. */
if (!(wmb->fillvalue = realloc(wmb->fillvalue, iodesc->mpitype_size * (1 + wmb->num_arrays))))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
memcpy((char *)wmb->fillvalue + iodesc->mpitype_size * wmb->num_arrays,
vdesc->fillvalue, iodesc->mpitype_size);
}
/* Tell the buffer about the data it is getting. */
wmb->arraylen = arraylen;
wmb->vid[wmb->num_arrays] = varid;
PLOG((3, "wmb->num_arrays = %d wmb->vid[wmb->num_arrays] = %d", wmb->num_arrays,
wmb->vid[wmb->num_arrays]));
/* Copy the user-provided data to the buffer. */
bufptr = (void *)((char *)wmb->data + arraylen * iodesc->mpitype_size * wmb->num_arrays);
if (arraylen > 0)
{
PLOG((3, "copying %ld bytes of user data %d", arraylen * iodesc->mpitype_size, iodesc->mpitype_size));
memcpy(bufptr, array, arraylen * iodesc->mpitype_size);
}
/* Add the unlimited dimension value of this variable to the frame
* array in wmb. */
if (wmb->frame)
wmb->frame[wmb->num_arrays] = vdesc->record;
wmb->num_arrays++;
#ifdef USE_MPE
pio_stop_mpe_log(DARRAY_WRITE, __func__);
#endif /* USE_MPE */
PLOG((2, "wmb->num_arrays = %d iodesc->maxbytes / iodesc->mpitype_size = %d "
"iodesc->ndof = %d iodesc->llen = %d", wmb->num_arrays,
iodesc->maxbytes / iodesc->mpitype_size, iodesc->ndof, iodesc->llen));
return PIO_NOERR;
}
/**
* Read a field from a file to the IO library using distributed
* arrays.
*
* @param ncid identifies the netCDF file.
* @param varid the variable ID to be read.
* @param ioid the I/O description ID as passed back by
* PIOc_InitDecomp().
* @param arraylen this parameter is ignored. Nominally it is the
* length of the array to be read. This is the length of the
* distrubited array. That is, the length of the portion of the data
* that is on the processor. This is already known because it is in
* the decomposition.
* @param array pointer to the data to be read. This is a
* pointer to the distributed portion of the array that is on this
* processor.
* @return 0 for success, error code otherwise.
* @ingroup PIO_read_darray_c
* @author Jim Edwards, Ed Hartnett
*/
int
PIOc_read_darray(int ncid, int varid, int ioid, PIO_Offset arraylen,
void *array)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
io_desc_t *iodesc; /* Pointer to IO description information. */
void *iobuf = NULL; /* holds the data as read on the io node. */
size_t rlen = 0; /* the length of data in iobuf. */
void *tmparray; /* unsorted copy of array buf if required */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function calls. */
int ierr; /* Return code. */
#ifdef USE_MPE
pio_start_mpe_log(DARRAY_READ);
#endif /* USE_MPE */
PLOG((1, "PIOc_read_darray ncid %d varid %d ioid %d arraylen %ld ",
ncid, varid, ioid, arraylen));
/* Get the file info. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__);
ios = file->iosystem;
/* If async is in use, and this is not an IO task, bcast the
* parameters. */
if (ios->async)
{
if (!ios->ioproc)
{
int msg = PIO_MSG_READDARRAY;
if (ios->compmain == MPI_ROOT)
mpierr = MPI_Send(&msg, 1, MPI_INT, ios->ioroot, 1, ios->union_comm);
/* Send the function parameters and associated informaiton
* to the msg handler. */
if (!mpierr)
mpierr = MPI_Bcast(&ncid, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&varid, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&ioid, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&arraylen, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
PLOG((2, "PIOc_read_darray ncid %d varid %d ioid %d arraylen %d",
ncid, varid, ioid, arraylen));
}
/* Handle MPI errors. */
if ((mpierr2 = MPI_Bcast(&mpierr, 1, MPI_INT, ios->comproot, ios->my_comm)))
return check_mpi(NULL, file, mpierr2, __FILE__, __LINE__);
if (mpierr)
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
}
/* Get the iodesc. */
if (!(iodesc = pio_get_iodesc_from_id(ioid)))
return pio_err(ios, file, PIO_EBADID, __FILE__, __LINE__);
pioassert(iodesc->rearranger == PIO_REARR_BOX || iodesc->rearranger == PIO_REARR_SUBSET,
"unknown rearranger", __FILE__, __LINE__);
/* iomain needs max of buflen, others need local len */
if (ios->iomain == MPI_ROOT)
rlen = iodesc->maxiobuflen;
else
rlen = iodesc->llen;
/* Allocate a buffer for one record. */
if (ios->ioproc && rlen > 0)
if (!(iobuf = malloc(iodesc->mpitype_size * rlen)))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
/* Call the correct darray read function based on iotype. */
switch (file->iotype)
{
case PIO_IOTYPE_NETCDF:
case PIO_IOTYPE_NETCDF4C:
if ((ierr = pio_read_darray_nc_serial(file, iodesc, varid, iobuf)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
break;
case PIO_IOTYPE_PNETCDF:
case PIO_IOTYPE_NETCDF4P:
if ((ierr = pio_read_darray_nc(file, iodesc, varid, iobuf)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
break;
#ifdef USE_GDAL
case PIO_IOTYPE_GDAL:
if ((ierr = pio_read_darray_shp_par(file, iodesc, varid, iobuf)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
// if ((ierr = pio_gdal_read_features_par(file->pio_ncid, varid, iodesc, iobuf)))
// return pio_err(ios, file, ierr, __FILE__, __LINE__);
break;
#endif
default:
return pio_err(NULL, NULL, PIO_EBADIOTYPE, __FILE__, __LINE__);
}
/* If the map is not monotonically increasing we will need to sort
* it. */
PLOG((2, "iodesc->needssort %d", iodesc->needssort));
if (iodesc->needssort)
{
if (!(tmparray = calloc(iodesc->maplen, iodesc->piotype_size)))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
}
else
tmparray = array;
/* prefill the output array with 0 then overwrite from iobuf */
/* switch(iodesc->piotype)
{
case PIO_SHORT:
for(int i=0; i<iodesc->maplen; i++)
((short *) array)[i] = (short) 0;
break;
case PIO_INT:
for(int i=0; i<iodesc->maplen; i++)
((int *) array)[i] = (int) 0;
break;
case PIO_FLOAT:
for(int i=0; i<iodesc->maplen; i++)