Skip to content

@zonca /

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
hdf5 read compound with boolean
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)
#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,
} 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 */
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);
/* 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);
main(int argc, char **argv)
pointing_t data[10];
read_pointing(0, 2, 5, data, "testout.h5");
mpicc readbool.c -lhdf5 && mpirun -n 1 ./a.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.