pythonchdf5

Unable to open .h5 files during runtime of C program


I am writing a simulation in C, and want to output the data during runtime to .h5 files using the HDF5 library. The following code is used to output the files:

void output_data(int t, int n_output) {
    char filename[32];
    sprintf(filename, "data_%d.h5", n_output);
    hid_t hdf5_fp = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    hid_t group_id = H5Gcreate2(hdf5_fp, "/data", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    hsize_t dim_1d[1] = {1};
    hid_t dataspace_id_1d = H5Screate_simple(1, dim_1d, NULL);
    hsize_t dims[2] = {NX, NY};
    hid_t dataspace_id = H5Screate_simple(2, dims, NULL);

    hid_t dataset_id;
    dataset_id = H5Dcreate2(group_id, "time", H5T_NATIVE_INT32, dataspace_id_1d, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    H5Dwrite(dataset_id, H5T_NATIVE_INT32, H5S_ALL, H5S_ALL, H5P_DEFAULT, &t);
    
    // Output of other data

    H5Sclose(dataspace_id_1d);
    H5Sclose(dataspace_id);
    H5Dclose(dataset_id);
    H5Gclose(group_id);
    H5Fclose(hdf5_fp);
}

I link statically with the hdf5 library using:

gcc main.c -o main -std=c17 -O3 -Wall -Wextra -I/usr/local/hdf5/include -L/usr/local/hdf5/lib -l:libhdf5.a -lm -lz

And I could also link with the shared library using:

gcc main.c -o main -std=c17 -O3 -Wall -Wextra -I/usr/local/hdf5/include -L/usr/local/hdf5/lib -Wl,-rpath /usr/local/hdf5/lib -l:libhdf5.so -lm -lz

If I want to read out the .h5 files during runtime using the h5py library in python, I get the following error:

BlockingIOError: [Errno 11] Unable to synchronously open file (unable to lock file, errno = 11, error message = 'Resource temporarily unavailable')

And if I abort the simulation during runtime, I get the following error when reading out the .h5 files:

OSError: Unable to synchronously open file (bad object header version number)

As far as I know, I am closing the .h5 files correctly in the C code. What could be causing these problems?


Solution

  • Both issues of being unable to open files during runtime and corrupted files when aborting the program can be fixed by adding H5Fflush(hdf5_fp, H5F_SCOPE_GLOBAL); before the close functions. Found this with the help of ChatGPT, thanks Stack Overflow!