-
Notifications
You must be signed in to change notification settings - Fork 52
/
pio_getput_int.c
1484 lines (1346 loc) · 60.3 KB
/
pio_getput_int.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
/**
* @file
* Internal PIO functions to get and put attributes and data.
*
* @see https://github.com/NCAR/ParallelIO
*
* @author Ed Hartnett
* @date 2016
*/
#include <config.h>
#include <pio.h>
#include <pio_internal.h>
/**
* Write a netCDF attribute of any type, converting to any type.
*
* This routine is called collectively by all tasks in the
* communicator ios.union_comm.
*
* @param ncid the ncid of the open file, obtained from
* PIOc_openfile() or PIOc_createfile().
* @param varid the variable ID.
* @param name the name of the attribute.
* @param atttype the nc_type of the attribute in the file.
* @param len the length of the attribute array.
* @param memtype the nc_type of the attribute data in memory.
* @param op a pointer with the attribute data.
* @return PIO_NOERR for success, error code otherwise.
* @author Ed Hartnett
*/
int
PIOc_put_att_tc(int ncid, int varid, const char *name, nc_type atttype,
PIO_Offset len, nc_type memtype, const void *op)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
PIO_Offset atttype_len; /* Length (in bytes) of the att type in file. */
PIO_Offset memtype_len; /* Length of the att data type in memory. */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function codes. */
int ierr; /* Return code from function calls. */
/* Find the info about this file. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, ierr, __FILE__, __LINE__);
ios = file->iosystem;
/* User must provide some valid parameters. */
if (!name || !op || strlen(name) > NC_MAX_NAME || len < 0)
return pio_err(ios, file, PIO_EINVAL, __FILE__, __LINE__);
PLOG((1, "PIOc_put_att_tc ncid = %d varid = %d name = %s atttype = %d len = %d memtype = %d",
ncid, varid, name, atttype, len, memtype));
/* 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)
{
/* Get the length (in bytes) of the type in file. */
if ((ierr = PIOc_inq_type(ncid, atttype, NULL, &atttype_len)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
/* Get the length (in bytes) of the type in memory. */
if (memtype == PIO_LONG_INTERNAL)
memtype_len = sizeof(long int);
else
{
if ((ierr = PIOc_inq_type(ncid, memtype, NULL, &memtype_len)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
}
PLOG((2, "PIOc_put_att atttype_len = %d memtype_len = %d", ncid, atttype_len, memtype_len));
}
/* 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_PUT_ATT;
if (ios->compmain == MPI_ROOT)
mpierr = MPI_Send(&msg, 1, MPI_INT, ios->ioroot, 1, ios->union_comm);
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);
int namelen = strlen(name);
if (!mpierr)
mpierr = MPI_Bcast(&namelen, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast((void *)name, namelen + 1, MPI_CHAR, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&atttype, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&len, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&atttype_len, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&memtype, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&memtype_len, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast((void *)op, len * memtype_len, MPI_BYTE, ios->compmain,
ios->intercomm);
PLOG((2, "PIOc_put_att finished bcast ncid = %d varid = %d namelen = %d name = %s "
"len = %d atttype_len = %d memtype = %d memtype_len = %d", ncid, varid, namelen,
name, len, atttype_len, memtype, memtype_len));
}
/* Handle MPI errors. */
if ((mpierr2 = MPI_Bcast(&mpierr, 1, MPI_INT, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr2, __FILE__, __LINE__);
if (mpierr)
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
/* Broadcast values currently only known on computation tasks to IO tasks. */
if ((mpierr = MPI_Bcast(&atttype_len, 1, MPI_OFFSET, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if ((mpierr = MPI_Bcast(&memtype_len, 1, MPI_OFFSET, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
PLOG((2, "PIOc_put_att bcast from comproot = %d atttype_len = %d", ios->comproot,
atttype_len, memtype_len));
}
/* If this is an IO task, then call the netCDF function. */
if (ios->ioproc)
{
#ifdef _PNETCDF
if (file->iotype == PIO_IOTYPE_PNETCDF)
{
switch(memtype)
{
case NC_BYTE:
ierr = ncmpi_put_att_schar(file->fh, varid, name, atttype, len, op);
break;
case NC_CHAR:
ierr = ncmpi_put_att_text(file->fh, varid, name, len, op);
break;
case NC_SHORT:
ierr = ncmpi_put_att_short(file->fh, varid, name, atttype, len, op);
break;
case NC_INT:
ierr = ncmpi_put_att_int(file->fh, varid, name, atttype, len, op);
break;
case PIO_LONG_INTERNAL:
ierr = ncmpi_put_att_long(file->fh, varid, name, atttype, len, op);
break;
case NC_FLOAT:
ierr = ncmpi_put_att_float(file->fh, varid, name, atttype, len, op);
break;
case NC_DOUBLE:
ierr = ncmpi_put_att_double(file->fh, varid, name, atttype, len, op);
break;
default:
return pio_err(ios, file, PIO_EBADTYPE, __FILE__, __LINE__);
}
}
#endif /* _PNETCDF */
if (file->iotype != PIO_IOTYPE_PNETCDF && file->do_io)
{
switch(memtype)
{
case NC_CHAR:
ierr = nc_put_att_text(file->fh, varid, name, len, op);
break;
case NC_BYTE:
ierr = nc_put_att_schar(file->fh, varid, name, atttype, len, op);
break;
case NC_SHORT:
ierr = nc_put_att_short(file->fh, varid, name, atttype, len, op);
break;
case NC_INT:
ierr = nc_put_att_int(file->fh, varid, name, atttype, len, op);
break;
case PIO_LONG_INTERNAL:
ierr = nc_put_att_long(file->fh, varid, name, atttype, len, op);
break;
case NC_FLOAT:
ierr = nc_put_att_float(file->fh, varid, name, atttype, len, op);
break;
case NC_DOUBLE:
ierr = nc_put_att_double(file->fh, varid, name, atttype, len, op);
break;
#ifdef _NETCDF4
case NC_UBYTE:
ierr = nc_put_att_uchar(file->fh, varid, name, atttype, len, op);
break;
case NC_USHORT:
ierr = nc_put_att_ushort(file->fh, varid, name, atttype, len, op);
break;
case NC_UINT:
ierr = nc_put_att_uint(file->fh, varid, name, atttype, len, op);
break;
case NC_INT64:
PLOG((3, "about to call nc_put_att_longlong"));
ierr = nc_put_att_longlong(file->fh, varid, name, atttype, len, op);
break;
case NC_UINT64:
ierr = nc_put_att_ulonglong(file->fh, varid, name, atttype, len, op);
break;
/* case NC_STRING: */
/* ierr = nc_put_att_string(file->fh, varid, name, atttype, len, op); */
/* break; */
#endif /* _NETCDF4 */
default:
return pio_err(ios, file, PIO_EBADTYPE, __FILE__, __LINE__);
}
}
}
/* Broadcast and check the return code. */
if ((mpierr = MPI_Bcast(&ierr, 1, MPI_INT, ios->ioroot, ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if (ierr)
return check_netcdf(file, ierr, __FILE__, __LINE__);
return PIO_NOERR;
}
/**
* Get the value of an attribute of any type, converting to any type.
*
* This routine is called collectively by all tasks in the communicator
* ios.union_comm.
*
* @param ncid the ncid of the open file, obtained from
* PIOc_openfile() or PIOc_createfile().
* @param varid the variable ID.
* @param name the name of the attribute to get
* @param memtype the type of the data in memory (if different from
* the type of the attribute, the data will be converted to
* memtype). The ip pointer points to memory to hold att_len elements
* of type memtype.
* @param ip a pointer that will get the attribute value.
* @return PIO_NOERR for success, error code otherwise.
* @author Ed Hartnett
*/
int
PIOc_get_att_tc(int ncid, int varid, const char *name, nc_type memtype, void *ip)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
nc_type atttype; /* The type of the attribute. */
PIO_Offset attlen; /* Number of elements in the attribute array. */
PIO_Offset atttype_len; /* Length in bytes of one element of the attribute type. */
PIO_Offset memtype_len; /* Length in bytes of one element of the memory type. */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function calls. */
int ierr; /* Return code from function calls. */
/* Find the info about this file. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, ierr, __FILE__, __LINE__);
ios = file->iosystem;
/* User must provide a name and destination pointer. */
if (!name || !ip || strlen(name) > NC_MAX_NAME)
return pio_err(ios, file, PIO_EINVAL, __FILE__, __LINE__);
PLOG((1, "PIOc_get_att_tc ncid %d varid %d name %s memtype %d",
ncid, varid, name, memtype));
/* 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)
{
/* Get the type and length of the attribute. */
if ((ierr = PIOc_inq_att(ncid, varid, name, &atttype, &attlen)))
{
if (ios->async)
return ierr;
else
return check_netcdf(file, ierr, __FILE__, __LINE__);
}
PLOG((2, "atttype = %d attlen = %d", atttype, attlen));
/* Get the length (in bytes) of the type of the attribute. */
if ((ierr = PIOc_inq_type(ncid, atttype, NULL, &atttype_len)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
/* Get the length (in bytes) of the type that the user wants
* the data converted to. */
if (memtype == PIO_LONG_INTERNAL)
memtype_len = sizeof(long int);
else
{
if ((ierr = PIOc_inq_type(ncid, memtype, NULL, &memtype_len)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
}
}
PLOG((2, "atttype_len = %d memtype_len = %d", atttype_len, memtype_len));
/* If async is in use, and this is not an IO task, bcast the
* parameters and the attribute and type information we fetched. */
if (ios->async)
{
if (!ios->ioproc)
{
int msg = PIO_MSG_GET_ATT;
PLOG((2, "sending parameters"));
/* Send the message to IO main. */
if (ios->compmain == MPI_ROOT)
mpierr = MPI_Send(&msg, 1,MPI_INT, ios->ioroot, 1, ios->union_comm);
/* Send the function parameters. */
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);
int namelen = strlen(name);
if (!mpierr)
mpierr = MPI_Bcast(&namelen, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast((void *)name, namelen + 1, MPI_CHAR, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&file->iotype, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&atttype, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&attlen, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&atttype_len, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&memtype, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&memtype_len, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
PLOG((2, "Bcast complete ncid = %d varid = %d namelen = %d name = %s iotype = %d "
"atttype = %d attlen = %d atttype_len = %d", ncid, varid, namelen, name, file->iotype,
atttype, attlen, atttype_len));
}
/* Handle MPI errors. */
if ((mpierr2 = MPI_Bcast(&mpierr, 1, MPI_INT, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr2, __FILE__, __LINE__);
if (mpierr)
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
PLOG((2, "mpi errors handled"));
/* Broadcast values currently only known on computation tasks to IO tasks. */
PLOG((2, "PIOc_get_att_tc bcast from comproot = %d attlen = %d atttype_len = %d", ios->comproot, attlen, atttype_len));
if ((mpierr = MPI_Bcast(&attlen, 1, MPI_OFFSET, ios->comproot, ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if ((mpierr = MPI_Bcast(&atttype_len, 1, MPI_OFFSET, ios->comproot, ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if ((mpierr = MPI_Bcast(&memtype_len, 1, MPI_OFFSET, ios->comproot, ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
PLOG((2, "PIOc_get_att_tc bcast complete attlen = %d atttype_len = %d memtype_len = %d", attlen, atttype_len,
memtype_len));
}
/* If this is an IO task, then call the netCDF function. */
if (ios->ioproc)
{
PLOG((2, "calling pnetcdf/netcdf"));
#ifdef _PNETCDF
if (file->iotype == PIO_IOTYPE_PNETCDF)
{
switch(memtype)
{
case NC_BYTE:
ierr = ncmpi_get_att_schar(file->fh, varid, name, ip);
break;
case NC_CHAR:
ierr = ncmpi_get_att_text(file->fh, varid, name, ip);
break;
case NC_SHORT:
ierr = ncmpi_get_att_short(file->fh, varid, name, ip);
break;
case NC_INT:
ierr = ncmpi_get_att_int(file->fh, varid, name, ip);
break;
case PIO_LONG_INTERNAL:
ierr = ncmpi_get_att_long(file->fh, varid, name, ip);
break;
case NC_FLOAT:
ierr = ncmpi_get_att_float(file->fh, varid, name, ip);
break;
case NC_DOUBLE:
ierr = ncmpi_get_att_double(file->fh, varid, name, ip);
break;
default:
return pio_err(ios, file, PIO_EBADTYPE, __FILE__, __LINE__);
}
}
#endif /* _PNETCDF */
if (file->iotype != PIO_IOTYPE_PNETCDF && file->do_io)
{
switch(memtype)
{
case NC_CHAR:
ierr = nc_get_att_text(file->fh, varid, name, ip);
break;
case NC_BYTE:
ierr = nc_get_att_schar(file->fh, varid, name, ip);
break;
case NC_SHORT:
ierr = nc_get_att_short(file->fh, varid, name, ip);
break;
case NC_INT:
ierr = nc_get_att_int(file->fh, varid, name, ip);
break;
case PIO_LONG_INTERNAL:
ierr = nc_get_att_long(file->fh, varid, name, ip);
break;
case NC_FLOAT:
ierr = nc_get_att_float(file->fh, varid, name, ip);
break;
case NC_DOUBLE:
ierr = nc_get_att_double(file->fh, varid, name, ip);
break;
#ifdef _NETCDF4
case NC_UBYTE:
ierr = nc_get_att_uchar(file->fh, varid, name, ip);
break;
case NC_USHORT:
ierr = nc_get_att_ushort(file->fh, varid, name, ip);
break;
case NC_UINT:
ierr = nc_get_att_uint(file->fh, varid, name, ip);
break;
case NC_INT64:
PLOG((3, "about to call nc_get_att_longlong"));
ierr = nc_get_att_longlong(file->fh, varid, name, ip);
break;
case NC_UINT64:
ierr = nc_get_att_ulonglong(file->fh, varid, name, ip);
break;
/* case NC_STRING: */
/* ierr = nc_get_att_string(file->fh, varid, name, ip); */
/* break; */
#endif /* _NETCDF4 */
default:
return pio_err(ios, file, PIO_EBADTYPE, __FILE__, __LINE__);
}
}
}
/* Broadcast and check the return code. */
PLOG((2, "ierr = %d", ierr));
if ((mpierr = MPI_Bcast(&ierr, 1, MPI_INT, ios->ioroot, ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if (ierr)
return check_netcdf(file, ierr, __FILE__, __LINE__);
/* Broadcast results to all tasks. */
PLOG((2, "bcasting att values attlen = %d memtype_len = %d", attlen, memtype_len));
if ((mpierr = MPI_Bcast(ip, (int)attlen * memtype_len, MPI_BYTE, ios->ioroot,
ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
PLOG((2, "get_att_tc data bcast complete"));
return PIO_NOERR;
}
/**
* Internal PIO function which provides a type-neutral interface to
* nc_get_vars.
*
* Users should not call this function directly. Instead, call one of
* the derived functions, depending on the type of data you are
* reading: PIOc_get_vars_text(), PIOc_get_vars_uchar(),
* PIOc_get_vars_schar(), PIOc_get_vars_ushort(),
* PIOc_get_vars_short(), PIOc_get_vars_uint(), PIOc_get_vars_int(),
* PIOc_get_vars_long(), PIOc_get_vars_float(),
* PIOc_get_vars_double(), PIOc_get_vars_ulonglong(),
* PIOc_get_vars_longlong()
*
* This routine is called collectively by all tasks in the
* communicator ios.union_comm.
*
* @param ncid identifies the netCDF file
* @param varid the variable ID number
* @param start an array of start indicies (must have same number of
* entries as variable has dimensions). If NULL, indices of 0 will be
* used.
* @param count an array of counts (must have same number of entries
* as variable has dimensions). If NULL, counts matching the size of
* the variable will be used.
* @param stride an array of strides (must have same number of
* entries as variable has dimensions). If NULL, strides of 1 will be
* used.
* @param xtype the netCDF type of the data being passed in buf. Data
* will be automatically covnerted from the type of the variable being
* read from to this type. If NC_NAT then the variable's file type
* will be used. Use special PIO_LONG_INTERNAL for _long() functions.
* @param buf pointer to the data to be written.
* @return PIO_NOERR on success, error code otherwise.
* @author Ed Hartnett
*/
int
PIOc_get_vars_tc(int ncid, int varid, const PIO_Offset *start, const PIO_Offset *count,
const PIO_Offset *stride, nc_type xtype, void *buf)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
int ndims; /* The number of dimensions in the variable. */
PIO_Offset typelen; /* Size (in bytes) of the data type of data in buf. */
PIO_Offset num_elem = 1; /* Number of data elements in the buffer. */
nc_type vartype; /* The type of the var we are reading from. */
char start_present = start ? true : false;
char count_present = count ? true : false;
char stride_present = stride ? true : false;
PIO_Offset one = 1; /* For fake_stride. */
PIO_Offset *fake_stride = &one; /* Needed for NULL stride bug in netcdf-4.6.2. */
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function codes. */
int ierr; /* Return code. */
PLOG((1, "PIOc_get_vars_tc ncid = %d varid = %d xtype = %d start_present = %d "
"count_present = %d stride_present = %d", ncid, varid, xtype, start_present,
count_present, stride_present));
/* Find the info about this file. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, ierr, __FILE__, __LINE__);
ios = file->iosystem;
/* User must provide a place to put some data. */
if (!buf)
return pio_err(ios, file, PIO_EINVAL, __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)
{
/* Get the type of this var. */
if ((ierr = PIOc_inq_vartype(ncid, varid, &vartype)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
/* If no type was specified, use the var type. */
if (xtype == NC_NAT)
xtype = vartype;
/* Handle _long() calls with an special type. */
if (xtype == PIO_LONG_INTERNAL)
typelen = sizeof(long int);
else
{
if ((ierr = PIOc_inq_type(ncid, xtype, NULL, &typelen)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
}
/* Get the number of dims for this var. */
if ((ierr = PIOc_inq_varndims(ncid, varid, &ndims)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
PLOG((3, "ndims = %d", ndims));
/* Only scalar vars can pass NULL for start/count. */
pioassert(ndims == 0 || (start && count), "need start/count", __FILE__, __LINE__);
/* How many elements in buf? (For scalars, ndims is 0 and
* num_elem will remain 1). */
for (int vd = 0; vd < ndims; vd++)
num_elem *= count[vd];
PLOG((2, "PIOc_get_vars_tc num_elem = %d", num_elem));
}
/* 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_GET_VARS;
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(&ndims, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&start_present, 1, MPI_CHAR, ios->compmain, ios->intercomm);
if (!mpierr && start_present)
mpierr = MPI_Bcast((PIO_Offset *)start, ndims, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&count_present, 1, MPI_CHAR, ios->compmain, ios->intercomm);
if (!mpierr && count_present)
mpierr = MPI_Bcast((PIO_Offset *)count, ndims, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&stride_present, 1, MPI_CHAR, ios->compmain, ios->intercomm);
if (!mpierr && stride_present)
mpierr = MPI_Bcast((PIO_Offset *)stride, ndims, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&xtype, 1, MPI_INT, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&num_elem, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
if (!mpierr)
mpierr = MPI_Bcast(&typelen, 1, MPI_OFFSET, ios->compmain, ios->intercomm);
PLOG((2, "PIOc_get_vars_tc ncid = %d varid = %d ndims = %d start_present = %d "
"count_present = %d stride_present = %d xtype = %d num_elem = %d", ncid, varid,
ndims, start_present, count_present, stride_present, xtype, num_elem));
}
/* 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__);
/* Broadcast values currently only known on computation tasks to IO tasks. */
if ((mpierr = MPI_Bcast(&num_elem, 1, MPI_OFFSET, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if ((mpierr = MPI_Bcast(&typelen, 1, MPI_OFFSET, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if ((mpierr = MPI_Bcast(&xtype, 1, MPI_INT, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if ((mpierr = MPI_Bcast(&ndims, 1, MPI_INT, ios->comproot, ios->my_comm)))
check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
}
if (ndims)
{
if (!stride_present)
{
PLOG((2, "stride not present "));
if (!(fake_stride = malloc(ndims * sizeof(PIO_Offset))))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
for (int d = 0; d < ndims; d++)
fake_stride[d] = 1;
}
else
fake_stride = (PIO_Offset *)stride;
}
/* If this is an IO task, then call the netCDF function. */
if (ios->ioproc)
{
PLOG((2, "file->iotype = %d xtype = %d file->do_io = %d", file->iotype, xtype, file->do_io));
#ifdef _PNETCDF
if (file->iotype == PIO_IOTYPE_PNETCDF)
{
PLOG((2, "pnetcdf calling ncmpi_get_vars_*() file->fh = %d varid = %d", file->fh, varid));
/* Turn on independent access for pnetcdf file. */
if ((ierr = ncmpi_begin_indep_data(file->fh)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
/* Only the IO main does the IO, so we are not really
* getting parallel IO here. */
if (ios->iomain == MPI_ROOT)
{
switch(xtype)
{
case NC_BYTE:
ierr = ncmpi_get_vars_schar(file->fh, varid, start, count, stride, buf);
break;
case NC_CHAR:
ierr = ncmpi_get_vars_text(file->fh, varid, start, count, stride, buf);
break;
case NC_SHORT:
ierr = ncmpi_get_vars_short(file->fh, varid, start, count, stride, buf);
break;
case NC_INT:
ierr = ncmpi_get_vars_int(file->fh, varid, start, count, stride, buf);
break;
case PIO_LONG_INTERNAL:
ierr = ncmpi_get_vars_long(file->fh, varid, start, count, stride, buf);
break;
case NC_FLOAT:
ierr = ncmpi_get_vars_float(file->fh, varid, start, count, stride, buf);
break;
case NC_DOUBLE:
ierr = ncmpi_get_vars_double(file->fh, varid, start, count, stride, buf);
break;
default:
return pio_err(ios, file, PIO_EBADIOTYPE, __FILE__, __LINE__);
}
}
/* Turn off independent access for pnetcdf file. */
if ((ierr = ncmpi_end_indep_data(file->fh)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
}
#endif /* _PNETCDF */
PLOG((2, "duck ndims %d", ndims));
for (int d = 0; d < ndims; d++)
{
PLOG((2, "start[%d] %d", d, start[d]));
PLOG((2, "count[%d] %d", d, count[d]));
PLOG((2, "fake_stride[%d] %d", d, fake_stride[d]));
}
if (file->iotype != PIO_IOTYPE_PNETCDF && file->do_io)
{
switch(xtype)
{
case NC_BYTE:
ierr = nc_get_vars_schar(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_CHAR:
ierr = nc_get_vars_text(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_SHORT:
ierr = nc_get_vars_short(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_INT:
ierr = nc_get_vars_int(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case PIO_LONG_INTERNAL:
ierr = nc_get_vars_long(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_FLOAT:
ierr = nc_get_vars_float(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_DOUBLE:
ierr = nc_get_vars_double(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
#ifdef _NETCDF4
case NC_UBYTE:
ierr = nc_get_vars_uchar(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_USHORT:
ierr = nc_get_vars_ushort(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_UINT:
ierr = nc_get_vars_uint(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_INT64:
PLOG((3, "about to call nc_get_vars_longlong"));
ierr = nc_get_vars_longlong(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
case NC_UINT64:
ierr = nc_get_vars_ulonglong(file->fh, varid, (size_t *)start, (size_t *)count,
(ptrdiff_t *)fake_stride, buf);
break;
/* case NC_STRING: */
/* ierr = nc_get_vars_string(file->fh, varid, (size_t *)start, (size_t *)count, */
/* (ptrdiff_t *)fake_stride, (void *)buf); */
/* break; */
#endif /* _NETCDF4 */
default:
return pio_err(ios, file, PIO_EBADTYPE, __FILE__, __LINE__);
}
}
}
PLOG((2, "howdy ndims %d", ndims));
for (int d = 0; d < ndims; d++)
{
PLOG((2, "fake_stride[%d] %d", d, fake_stride[d]));
}
/* Free malloced resources. */
if (ndims && !stride_present)
free(fake_stride);
/* Broadcast and check the return code. */
if ((mpierr = MPI_Bcast(&ierr, 1, MPI_INT, ios->ioroot, ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
if (ierr)
return check_netcdf(file, ierr, __FILE__, __LINE__);
/* Send the data. */
PLOG((2, "PIOc_get_vars_tc bcasting data num_elem = %d typelen = %d ios->ioroot = %d", num_elem,
typelen, ios->ioroot));
if ((mpierr = MPI_Bcast(buf, num_elem * typelen, MPI_BYTE, ios->ioroot, ios->my_comm)))
return check_mpi(NULL, file, mpierr, __FILE__, __LINE__);
PLOG((2, "PIOc_get_vars_tc bcasting data complete"));
return PIO_NOERR;
}
/**
* Get one value of a variable of any type. This is an internal
* function.
*
* This routine is called collectively by all tasks in the
* communicator ios.union_comm.
*
* @param ncid identifies the netCDF file
* @param varid the variable ID number
* @param index an array of start indicies (must have same number of
* entries as variable has dimensions). If NULL, indices of 0 will be
* used.
* @param xtype the netcdf type of the variable.
* @param buf pointer that will get the data.
* @return PIO_NOERR on success, error code otherwise.
* @author Ed Hartnett
*/
int
PIOc_get_var1_tc(int ncid, int varid, const PIO_Offset *index, nc_type xtype,
void *buf)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
int ndims; /* The number of dimensions in the variable. */
int ierr; /* Return code from function calls. */
/* Find the info about this file. We need this for error handling. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, ierr, __FILE__, __LINE__);
ios = file->iosystem;
/* Find the number of dimensions. */
if ((ierr = PIOc_inq_varndims(ncid, varid, &ndims)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
/* Set up count array. */
PIO_Offset count[ndims];
for (int c = 0; c < ndims; c++)
count[c] = 1;
return PIOc_get_vars_tc(ncid, varid, index, count, NULL, xtype, buf);
}
/**
* Get a complete variable of any type. This is an internal function.
*
* This routine is called collectively by all tasks in the
* communicator ios.union_comm.
*
* @param ncid identifies the netCDF file
* @param varid the variable ID number
* @param xtype the netcdf type of the variable.
* @param buf pointer that will get the data.
* @return PIO_NOERR on success, error code otherwise.
* @author Ed Hartnett
*/
int
PIOc_get_var_tc(int ncid, int varid, nc_type xtype, void *buf)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
PIO_Offset *startp = NULL; /* Pointer to start array. */
PIO_Offset *countp = NULL; /* Pointer to count array. */
int ndims; /* The number of dimensions in the variable. */
int ierr; /* Return code from function calls. */
PLOG((1, "PIOc_get_var_tc ncid = %d varid = %d xtype = %d", ncid, varid,
xtype));
/* Find the info about this file. We need this for error handling. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, ierr, __FILE__, __LINE__);
ios = file->iosystem;
/* Find the number of dimensions. */
if ((ierr = PIOc_inq_varndims(ncid, varid, &ndims)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
/* Scalar vars (which have ndims == 0) should just pass NULLs for
* start/count. */
if (ndims > 0)
{
/* Find the dimension IDs. */
int dimids[ndims];
if ((ierr = PIOc_inq_vardimid(ncid, varid, dimids)))
return pio_err(ios, file, ierr, __FILE__, __LINE__);
if (!(startp = malloc(ndims * sizeof(PIO_Offset))))
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
if (!(countp = malloc(ndims * sizeof(PIO_Offset))))
{
free(startp);
return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__);
}
/* Find the dimension lengths. */
for (int d = 0; d < ndims; d++)
{
if ((ierr = PIOc_inq_dimlen(ncid, dimids[d], &countp[d])))
{
free(startp);
free(countp);
return pio_err(ios, file, ierr, __FILE__, __LINE__);
}
}
/* Set up start array. */
for (int d = 0; d < ndims; d++)
{
startp[d] = 0;
PLOG((3, "startp[%d] = %d countp[%d] = %d", d, startp[d], d,
countp[d]));
}
}
ierr = PIOc_get_vars_tc(ncid, varid, startp, countp, NULL, xtype, buf);
if (startp)
free(startp);
if (countp)
free(countp);
return ierr;
}
/**
* Internal PIO function which provides a type-neutral interface to
* nc_put_vars.
*
* Users should not call this function directly. Instead, call one of
* the derived functions, depending on the type of data you are
* writing: PIOc_put_vars_text(), PIOc_put_vars_uchar(),
* PIOc_put_vars_schar(), PIOc_put_vars_ushort(),
* PIOc_put_vars_short(), PIOc_put_vars_uint(), PIOc_put_vars_int(),
* PIOc_put_vars_long(), PIOc_put_vars_float(),
* PIOc_put_vars_longlong(), PIOc_put_vars_double(),
* PIOc_put_vars_ulonglong().
*
* This routine is called collectively by all tasks in the
* communicator ios.union_comm.
*
* @param ncid identifies the netCDF file
* @param varid the variable ID number
* @param start an array of start indicies (must have same number of
* entries as variable has dimensions). If NULL, indices of 0 will be
* used.
* @param count an array of counts (must have same number of entries
* as variable has dimensions). If NULL, counts matching the size of
* the variable will be used.
* @param stride an array of strides (must have same number of
* entries as variable has dimensions). If NULL, strides of 1 will be
* used.
* @param xtype the netCDF type of the data being passed in buf. Data
* will be automatically covnerted from this type to the type of the
* variable being written to. If NC_NAT then the variable's file type
* will be used. Use special PIO_LONG_INTERNAL for _long() functions.
* @param buf pointer to the data to be written.
*
* @return PIO_NOERR on success, error code otherwise.
* @author Ed Hartnett
*/
int
PIOc_put_vars_tc(int ncid, int varid, const PIO_Offset *start, const PIO_Offset *count,
const PIO_Offset *stride, nc_type xtype, const void *buf)
{
iosystem_desc_t *ios; /* Pointer to io system information. */
file_desc_t *file; /* Pointer to file information. */
int ndims; /* The number of dimensions in the variable. */
PIO_Offset typelen; /* Size (in bytes) of the data type of data in buf. */
PIO_Offset num_elem = 1; /* Number of data elements in the buffer. */
char start_present = start ? true : false; /* Is start non-NULL? */
char count_present = count ? true : false; /* Is count non-NULL? */
char stride_present = stride ? true : false; /* Is stride non-NULL? */
nc_type vartype; /* The type of the var we are reading from. */
PIO_Offset *fake_stride;
int mpierr = MPI_SUCCESS, mpierr2; /* Return code from MPI function codes. */
int ierr; /* Return code from function calls. */
PLOG((1, "PIOc_put_vars_tc ncid = %d varid = %d start_present = %d "
"count_present = %d stride_present = %d xtype = %d", ncid, varid,
start_present, count_present, stride_present, xtype));
/* Get file info. */
if ((ierr = pio_get_file(ncid, &file)))
return pio_err(NULL, NULL, ierr, __FILE__, __LINE__);
ios = file->iosystem;
/* User must provide a place to put some data. */
if (!buf)
return pio_err(ios, file, PIO_EINVAL, __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)
{
/* Get the type of this var. */
if ((ierr = PIOc_inq_vartype(ncid, varid, &vartype)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
/* If no type was specified, use the var type. */
if (xtype == NC_NAT)
xtype = vartype;
/* Get the number of dims for this var. */
if ((ierr = PIOc_inq_varndims(ncid, varid, &ndims)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
/* Get the length of the data type. */
if (xtype == PIO_LONG_INTERNAL)
typelen = sizeof(long int);
else
{
if ((ierr = PIOc_inq_type(ncid, xtype, NULL, &typelen)))
return check_netcdf(file, ierr, __FILE__, __LINE__);
}
PLOG((2, "ndims = %d typelen = %d", ndims, typelen));
/* How many elements of data? If no count array was passed,
* this is a scalar. */