public
Created

hdf5 read compound with boolean

  • Download Gist
create_testfile.py
Python
1 2 3 4 5 6 7
import numpy as np
import h5py
 
data = np.ones(10, dtype=[("THETA",np.double),("PHI",np.double),("PSI",np.double),("FLAG",np.bool)])
 
with h5py.File("testout.h5", mode='w') as f:
f.create_dataset("data", data=data)
readbool.c
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
#include <assert.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <omp.h>
#include "hdf5.h"
#define FAIL -1
#define CPTR(VAR,CONST) ((VAR)=(CONST),&(VAR))
 
typedef enum {
FALSE = 0,
TRUE
} bool;
 
typedef struct {
double THETA, PHI, PSI;
bool FLAG;
} pointing_t;
 
int read_pointing(int MyPID, long firstelem, int numelements, pointing_t *data, const char *filename)
{
hsize_t start[1]; hsize_t count[1]; hsize_t stride[1]; hsize_t dims[1];
int i, n;
bool val;
 
stride[0] = 1; count[0] = numelements;
start[0] = firstelem;
 
printf("%d read_h5_data ", MyPID);
printf(" %ld -" , (long)start[0]);
printf(" %ld \n", (long)count[0]);
 
int status;
hid_t boolenumtype = H5Tcreate(H5T_ENUM, sizeof(bool));
status = H5Tenum_insert(boolenumtype, "FALSE", CPTR(val, FALSE ));
printf ("H5Tenum_insert (FALSE): %i\n", status);
status = H5Tenum_insert(boolenumtype, "TRUE", CPTR(val, TRUE ));
printf ("H5Tenum_insert (TRUE): %i\n", status);
 
///* setup file access template */
hid_t acc_tpl1 = H5Pcreate (H5P_FILE_ACCESS); assert(acc_tpl1 != FAIL);
///* set Parallel access with communicator */
int ret = H5Pset_fapl_mpio(acc_tpl1, MPI_COMM_WORLD, MPI_INFO_NULL); assert(ret != FAIL);
 
/* open the file collectively */
//printf("Open\n");
hid_t fid1=H5Fopen(filename,H5F_ACC_RDONLY,acc_tpl1);
 
/* Release file-access template */
ret=H5Pclose(acc_tpl1); assert(ret != FAIL);
/* open the dataset1 collectively */
hid_t dataset = H5Dopen2(fid1, "data", H5P_DEFAULT); assert(dataset != FAIL);
 
hid_t file_dataspace = H5Dget_space (dataset); assert(file_dataspace != FAIL);
 
 
hid_t memtype = H5Tcreate (H5T_COMPOUND, sizeof(pointing_t));
H5Tinsert (memtype, "THETA", HOFFSET (pointing_t, THETA), H5T_NATIVE_DOUBLE);
H5Tinsert (memtype, "PHI", HOFFSET (pointing_t, PHI), H5T_NATIVE_DOUBLE);
H5Tinsert (memtype, "PSI", HOFFSET (pointing_t, PSI), H5T_NATIVE_DOUBLE);
H5Tinsert (memtype, "FLAG", HOFFSET (pointing_t, FLAG), boolenumtype);
 
/* create a memory dataspace independently */
hid_t mem_dataspace = H5Screate_simple (1, count, NULL); assert (mem_dataspace != FAIL);
 
/* set up the collective transfer properties list */
hid_t xfer_plist = H5Pcreate (H5P_DATASET_XFER); assert(xfer_plist != FAIL);
//ret=H5Pset_dxpl_mpio(xfer_plist, H5FD_MPIO_COLLECTIVE);
ret=H5Pset_dxpl_mpio(xfer_plist, H5FD_MPIO_INDEPENDENT); assert(ret != FAIL);
 
//printf("Hyperslab\n");
/* select hyperslab */
ret=H5Sselect_hyperslab(file_dataspace, H5S_SELECT_SET, start, stride, count, NULL); assert(ret != FAIL);
start[0] = 0;
ret=H5Sselect_hyperslab(mem_dataspace, H5S_SELECT_SET, start, stride, count, NULL); assert(ret != FAIL);
 
/* read data collectively */
ret = H5Dread(dataset, memtype, mem_dataspace, file_dataspace, xfer_plist, data);
assert(ret != FAIL);
 
for (i=0; i<2; i++)
printf("%f, %f, %f, %d\n", data[i].THETA, data[i].PHI, data[i].PSI, data[i].FLAG);
 
H5Sclose(file_dataspace); H5Sclose(mem_dataspace); H5Pclose(xfer_plist); ret=H5Dclose(dataset); assert(ret != FAIL);
H5Fclose(fid1);
}
 
int
main(int argc, char **argv)
{
MPI_Init(&argc,&argv);
pointing_t data[10];
read_pointing(0, 2, 5, data, "testout.h5");
}
run.sh
Shell
1
mpicc readbool.c -lhdf5 && mpirun -n 1 ./a.out

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.