-
Notifications
You must be signed in to change notification settings - Fork 2
/
inUtils_HDF5.c
804 lines (658 loc) · 24 KB
/
inUtils_HDF5.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
/******************************************************************************
*** An example of how to use the INHDF5 API
***
*** Copyright (c) 2020-2024 Ioannis Nompelis
*** Ioannis Nompelis <nompelis@nobelware.com>
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
#include <hdf5.h>
#include "inUtils_HDF5.h"
int inUtils_HDF_CreateFile( const char* filename, MPI_Comm comm,
hid_t* id_alist,
hid_t* id_file )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
hid_t id_alist_tmp, id_clist_tmp, id_file_tmp;
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// create creation list and file access property list with parallel I/O
id_clist_tmp = H5Pcreate( H5P_FILE_CREATE );
id_alist_tmp = H5Pcreate( H5P_FILE_ACCESS );
H5Pset_fapl_mpio( id_alist_tmp, comm, MPI_INFO_NULL );
// H5Pset_fapl_mpiposix( id_alist_tmp, comm ); // not avail. in my library
id_file_tmp = H5Fcreate( filename, H5F_ACC_TRUNC,
id_clist_tmp, id_alist_tmp );
if( id_file_tmp < 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
if( irank == 0 ) printf(" e Could not create file: \"%s\"\n",filename);
if( id_file_tmp >= 0 ) H5Fclose( id_file_tmp );
H5Pclose( id_alist_tmp );
H5Pclose( id_clist_tmp );
ierr = 1;
goto return_point;
}
// copy structures to returned pointers
memcpy( id_alist, &id_alist_tmp, sizeof(hid_t) );
memcpy( id_file, &id_file_tmp, sizeof(hid_t) );
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//
// function to open a file and return handle and access list
// (closes the access property list as well)
//
int inUtils_HDF_OpenFile( const char* filename, MPI_Comm comm,
hid_t* id_alist,
hid_t* id_file )
{
return inUtils_HDF_OpenFileOpt( filename, comm, id_alist, id_file, 1 );
}
int inUtils_HDF_OpenFileOpt( const char* filename, MPI_Comm comm,
hid_t* id_alist,
hid_t* id_file,
int iop )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
hid_t id_alist_tmp, id_file_tmp;
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// create file access property list with parallel I/O
id_alist_tmp = H5Pcreate( H5P_FILE_ACCESS );
H5Pset_fapl_mpio( id_alist_tmp, comm, MPI_INFO_NULL );
// open the file
if( iop == 0 ) {
id_file_tmp = H5Fopen( filename, H5F_ACC_RDONLY, id_alist_tmp );
} else {
id_file_tmp = H5Fopen( filename, H5F_ACC_RDWR, id_alist_tmp );
}
if( id_file_tmp < 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
fprintf( stdout, " e Error opening file: \"%s\" \n",filename);
if( id_file_tmp >= 0 ) H5Fclose( id_file_tmp );
H5Pclose( id_alist_tmp );
ierr = 1;
goto return_point;
}
// copy the handles to return
memcpy( id_alist, &id_alist_tmp, sizeof(hid_t) );
memcpy( id_file, &id_file_tmp, sizeof(hid_t) );
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//
// function to close a file that is open in parallel
// (closes the access property list as well)
//
int inUtils_HDF_CloseFile( MPI_Comm comm,
hid_t* id_alist,
hid_t* id_file )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// close the file
ierr = H5Fclose( *id_file );
if( ierr != 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
fprintf( stdout, " e Error closeing file. (Access list stays open)\n");
ierr = 1;
goto return_point;
}
// close the access list
H5Pclose( *id_alist );
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//--- Groups ---
//
// function to create a group within a handle
//
int inUtils_HDF_CreateGroup( const char* grpname, MPI_Comm comm,
hid_t id_tree,
hid_t* id_grp )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
hid_t id_grp_tmp;
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// create a group in parallel
id_grp_tmp = H5Gcreate( id_tree, grpname,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
if( id_grp_tmp < 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
fprintf( stdout, " e Error creating group: \"%s\" \n",grpname);
if( id_grp_tmp >= 0 ) H5Gclose( id_grp_tmp );
ierr = 1;
goto return_point;
}
// copy the handle to return
memcpy( id_grp, &id_grp_tmp, sizeof(hid_t) );
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//
// function to close an open group that has been opened in parallel
//
int inUtils_HDF_CloseGroup( MPI_Comm comm, hid_t* id_grp )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// close the group in parallel
ierr = H5Gclose( *id_grp );
if( ierr != 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
fprintf( stdout, " e Error closing group \n");
ierr = 1;
goto return_point;
}
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//
// function to open a group in a handle and return handle
//
int inUtils_HDF_OpenGroup( const char* grpname, MPI_Comm comm,
hid_t id_tree,
hid_t* id_grp )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
hid_t id_grp_tmp;
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// open the group
id_grp_tmp = H5Gopen( id_tree, grpname, H5P_DEFAULT );
if( id_grp_tmp < 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
fprintf( stdout, " e Error opening group: \"%s\" \n",grpname);
if( id_grp_tmp >= 0 ) H5Gclose( id_grp_tmp );
ierr = 1;
goto return_point;
}
// copy the handle to return
memcpy( id_grp, &id_grp_tmp, sizeof(hid_t) );
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//--- Datasets ---
//
// function to create a dataset of the incoming data type
//
int inUtils_HDF_CreateDataset( const char* dataname, MPI_Comm comm,
hid_t id_tree,
hid_t id_type,
hid_t id_space,
hid_t* id_data )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
hid_t id_data_tmp;
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// create teh dataset
id_data_tmp = H5Dcreate( id_tree, dataname, id_type, id_space,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
if( id_data_tmp < 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
fprintf( stdout, " e Error creating dataset: \"%s\" \n",dataname);
if( id_data_tmp >= 0 ) H5Dclose( id_data_tmp );
ierr = 1;
goto return_point;
}
// copy the handle to return
memcpy( id_data, &id_data_tmp, sizeof(hid_t) );
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//
// function to close an open dataset that has been opened in parallel
//
int inUtils_HDF_CloseDataset( MPI_Comm comm, hid_t* id_data )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// close the dataset in parallel
ierr = H5Dclose( *id_data );
if( ierr != 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
fprintf( stdout, " e Error closing dataset \n");
ierr = 1;
goto return_point;
}
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//
// function to open a dataset in parallel
// (returns space and type of the data)
//
int inUtils_HDF_OpenDataset( const char* dataname, MPI_Comm comm, hid_t id_tree,
hid_t* id_data,
hid_t* id_type,
hid_t* id_space )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
hid_t id_data_tmp, id_type_tmp, id_space_tmp;
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// open the dataset in parallel
id_data_tmp = H5Dopen( id_tree, dataname, H5P_DEFAULT );
if( id_data_tmp < 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
fprintf( stdout, " e Error opening dataset: \"%s\" \n",dataname);
if( id_data_tmp >= 0 ) H5Dclose( id_data_tmp );
ierr = 1;
goto return_point;
}
/// retrieve the space of the dataset
id_space_tmp = H5Dget_space( id_data_tmp );
// retrieve the datatype in the dataset
id_type_tmp = H5Dget_type( id_data_tmp );
// Potentially provide info about the dataset using these handles...
// copy the handles to return
memcpy( id_data, &id_data_tmp, sizeof(hid_t) );
if( id_type != NULL ) {
memcpy( id_type, &id_type_tmp, sizeof(hid_t) );
} else {
H5Tclose( id_type_tmp );
}
if( id_space != NULL ) {
memcpy( id_space, &id_space_tmp, sizeof(hid_t) );
} else {
H5Sclose( id_space_tmp );
}
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
//
// functions to write to a dataset that has been opened in parallel
//
int inUtils_HDF_DatasetWriteCall( int iop,
MPI_Comm comm, hid_t id_data,
hid_t id_type, hid_t id_space,
int nd,
hsize_t* ioff,
hsize_t* icnt,
void* data )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
hid_t id_type_tmp, id_space_tmp, id_mem, id_plist;
htri_t ht;
int nds,ndd, n;
hsize_t *idims,*idim1,*idimd,*idim2,*istd,*iblk;
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// retrieve the datatype in the dataset
id_type_tmp = H5Dget_type( id_data );
// check for concistency of data types
ht = H5Tequal( id_type, id_type_tmp );
// immediatelly release the datatype
H5Tclose( id_type_tmp );
if( ht <= 0 ) {
if( irank == 0 ) fprintf( stdout, " e Datatype is inconcistent \n");
ierr = 1;
goto return_point;
}
// retrieve the space of the dataset
id_space_tmp = H5Dget_space( id_data );
// check for concistency of data space rank
nds = H5Sget_simple_extent_ndims( id_space );
ndd = H5Sget_simple_extent_ndims( id_space_tmp );
if( nds != ndd || nds != nd || ndd != nd ) {
if( irank == 0 ) fprintf( stdout, " e Sizes are inconcistent \n");
H5Sclose( id_space_tmp );
ierr = 2;
goto return_point;
}
idims = (hsize_t *) malloc(6*((size_t) nd) * sizeof(hsize_t) );
if( idims == NULL ) ierr = -1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
if( irank == 0 ) fprintf( stdout, " e Internal memory error\n");
if( idims != NULL ) free( idims );
H5Sclose( id_space_tmp );
ierr = -1;
goto return_point;
}
idim1 = &( idims[ nd*1 ] );
idimd = &( idims[ nd*2 ] );
idim2 = &( idims[ nd*3 ] );
istd = &( idims[ nd*4 ] );
iblk = &( idims[ nd*5 ] );
// check for concistency of data space dimensions
H5Sget_simple_extent_dims( id_space, idims, idim1 );
H5Sget_simple_extent_dims( id_space_tmp, idimd, idim2 );
// immediatelly drop the space handle
H5Sclose( id_space_tmp );
ierr = 0;
for(n=0;n<nds;++n) {
if( idims[n] != idimd[n] ) ierr = ierr + 1;
}
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_MAX, comm );
if( ierr != 0 ) {
if( irank == 0 ) fprintf( stdout, " e Dataspace dimension different \n");
free( idims );
ierr = 3;
goto return_point;
}
// create a transfer property list with or without parallel I/O
if( iop == 0 ) {
id_plist = H5Pcreate( H5P_DATASET_XFER );
H5Pset_dxpl_mpio( id_plist, H5FD_MPIO_COLLECTIVE );
} else if( iop == 1 ) {
id_plist = H5Pcreate( H5P_DATASET_XFER );
H5Pset_dxpl_mpio( id_plist, H5FD_MPIO_INDEPENDENT );
} else {
id_plist = H5P_DEFAULT;
}
// create a memory dataspace
id_mem = H5Screate_simple( nd, icnt, icnt );
// select the area where the data will be written
for(n=0;n<nds;++n) {
istd[n] = 1;
iblk[n] = 1;
}
H5Sselect_hyperslab( id_space, H5S_SELECT_SET, ioff, istd, icnt, iblk );
// perform the parallel write operations
ierr = H5Dwrite( id_data, id_type, id_mem, id_space, id_plist, data );
if( ierr != 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
if( irank == 0 ) fprintf( stdout, " e There was a problem writing \n");
H5Pclose( id_plist );
ierr = 100;
goto return_point;
}
// drop handles no longer needed
H5Sclose( id_mem );
if( iop == 0 || iop == 1 ) H5Pclose( id_plist );
//drop the sizes array
free( idims );
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
// the high level call
int inUtils_HDF_DatasetWrite( MPI_Comm comm, hid_t id_data,
hid_t id_type, hid_t id_space,
int nd,
hsize_t* ioff,
hsize_t* icnt,
void* data )
{
return inUtils_HDF_DatasetWriteCall( 0, comm, id_data, id_type, id_space,
nd, ioff, icnt, data );
}
//
// functions to read from a dataset that has been opened in parallel
//
int inUtils_HDF_DatasetReadCall( int iop,
MPI_Comm comm, hid_t id_data,
hid_t id_type, hid_t id_space,
int nd,
hsize_t* ioff,
hsize_t* icnt,
void* data )
{
int ierr=0;
int irank,nrank;
void *old_err_client_data; // to store the old error handler
H5E_auto_t old_err_func; // to store the old error handler
hid_t id_estack; // new error stack
hid_t id_type_tmp, id_space_tmp, id_mem, id_plist;
htri_t ht;
int nds,ndd, n;
hsize_t *idims,*idim1,*idimd,*idim2,*istd,*iblk;
MPI_Comm_rank( comm, &irank );
MPI_Comm_size( comm, &nrank );
// turn off HDF error-printing; store old handler to restore later
id_estack = H5Ecreate_stack();
H5Eget_auto( id_estack, &old_err_func, &old_err_client_data );
H5Eset_auto( id_estack, NULL, NULL );
// retrieve the datatype in the dataset
id_type_tmp = H5Dget_type( id_data );
// check for concistency of data types
ht = H5Tequal( id_type, id_type_tmp );
// immediatelly release the datatype
H5Tclose( id_type_tmp );
if( ht <= 0 ) {
if( irank == 0 ) fprintf( stdout, " e Datatype is inconcistent \n");
ierr = 1;
goto return_point;
}
// retrieve the space of the dataset
id_space_tmp = H5Dget_space( id_data );
// check for concistency of data space rank
nds = H5Sget_simple_extent_ndims( id_space );
ndd = H5Sget_simple_extent_ndims( id_space_tmp );
if( nds != ndd || nds != nd || ndd != nd ) {
if( irank == 0 ) fprintf( stdout, " e Sizes are inconcistent \n");
H5Sclose( id_space_tmp );
ierr = 2;
goto return_point;
}
idims = (hsize_t *) malloc(6*((size_t) nd) * sizeof(hsize_t) );
if( idims == NULL ) ierr = -1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
if( irank == 0 ) fprintf( stdout, " e Internal memory error\n");
if( idims != NULL ) free( idims );
H5Sclose( id_space_tmp );
ierr = -1;
goto return_point;
}
idim1 = &( idims[ nd*1 ] );
idimd = &( idims[ nd*2 ] );
idim2 = &( idims[ nd*3 ] );
istd = &( idims[ nd*4 ] );
iblk = &( idims[ nd*5 ] );
// check for concistency of data space dimensions
H5Sget_simple_extent_dims( id_space, idims, idim1 );
H5Sget_simple_extent_dims( id_space_tmp, idimd, idim2 );
// immediatelly drop the space handle
H5Sclose( id_space_tmp );
ierr = 0;
for(n=0;n<nds;++n) {
if( idims[n] != idimd[n] ) ierr = ierr + 1;
}
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_MAX, comm );
if( ierr != 0 ) {
if( irank == 0 ) fprintf( stdout, " e Dataspace dimension different \n");
free( idims );
ierr = 3;
goto return_point;
}
// create a transfer property list with parallel I/O
// create a transfer property list with or without parallel I/O
if( iop == 0 ) {
id_plist = H5Pcreate( H5P_DATASET_XFER );
H5Pset_dxpl_mpio( id_plist, H5FD_MPIO_COLLECTIVE );
} else if( iop == 1 ) {
id_plist = H5Pcreate( H5P_DATASET_XFER );
H5Pset_dxpl_mpio( id_plist, H5FD_MPIO_INDEPENDENT );
} else {
id_plist = H5P_DEFAULT;
}
// create a memory dataspace
id_mem = H5Screate_simple( nd, icnt, icnt );
// select the area where the data will be written
for(n=0;n<nds;++n) {
istd[n] = 1;
iblk[n] = 1;
}
H5Sselect_hyperslab( id_space, H5S_SELECT_SET, ioff, istd, icnt, iblk );
// perform the parallel write operations
ierr = H5Dread( id_data, id_type, id_mem, id_space, id_plist, data );
if( ierr != 0 ) ierr = 1;
MPI_Allreduce( MPI_IN_PLACE, &ierr, 1, MPI_INT, MPI_SUM, comm );
if( ierr != 0 ) {
if( irank == 0 ) fprintf( stdout, " e There was a problem writing \n");
H5Pclose( id_plist );
ierr = 100;
goto return_point;
}
// drop handles no longer needed
H5Sclose( id_mem );
if( iop == 0 || iop == 1 ) H5Pclose( id_plist );
//drop the sizes array
free( idims );
return_point:
// restore previous error handler
H5Eset_auto( id_estack, old_err_func, old_err_client_data );
// drop error stack no longer needed
H5Eclear( id_estack );
H5Eclose_stack( id_estack );
return ierr;
}
// the high level call
int inUtils_HDF_DatasetRead( MPI_Comm comm, hid_t id_data,
hid_t id_type, hid_t id_space,
int nd,
hsize_t* ioff,
hsize_t* icnt,
void* data )
{
return inUtils_HDF_DatasetReadCall( 0, comm, id_data, id_type, id_space,
nd, ioff, icnt, data );
}