Skip to content

Instantly share code, notes, and snippets.

@grimbough
Last active February 16, 2021 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grimbough/1cc71e75c4df5918e2299fb933df62fd to your computer and use it in GitHub Desktop.
Save grimbough/1cc71e75c4df5918e2299fb933df62fd to your computer and use it in GitHub Desktop.
Demonstrating a possible bug when combining hyperslab selections in HDF5 1.10.7
/*
* This example illustrates issues when trying to read using a
* hyperslab generated with the H5S_SELECT_OR operation
*/
#include "hdf5.h"
#define FILE "subset.h5"
#define DATASETNAME "IntArray"
#define RANK 2
#define DIM0_SUB 2 /* subset dimensions */
#define DIM1_SUB 2
#define DIM0 10 /* size of dataset */
#define DIM1 10
int
main (void)
{
hsize_t dims[2], dimsm[2];
int data[DIM0][DIM1]; /* data to write */
int sdata1[DIM0_SUB][DIM1_SUB]; /* subset to read */
int sdata2[DIM0_SUB][DIM1_SUB * 2]; /* combined subset to read */
hid_t file_id, dataset_id; /* handles */
hid_t dataspace_id, memspace_id;
herr_t status;
hsize_t count[2]; /* size of subset in the file */
hsize_t offset[2]; /* subset offset in the file */
hsize_t stride[2];
hsize_t block[2];
int i, j;
unsigned ver_info[3];
/* Print the HDF5 library version for diagnostics */
H5get_libversion(&ver_info[0], &ver_info[1], &ver_info[2]);
printf("HDF5 Version: %u.%u.%u\n", ver_info[0], ver_info[1], ver_info[2]);
/*****************************************************************
* Create a new file with default creation and access properties.*
* Then create a dataset and write data to it and close the file *
* and dataset. *
*****************************************************************/
file_id = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
dims[0] = DIM0;
dims[1] = DIM1;
dataspace_id = H5Screate_simple (RANK, dims, NULL);
dataset_id = H5Dcreate2 (file_id, DATASETNAME, H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
for (j = 0; j < DIM0; j++) {
for (i = 0; i < DIM1; i++)
data[j][i] = (j+1) + (i*10);
}
status = H5Dwrite (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, data);
printf ("\nData Written to File:\n");
for (i = 0; i<DIM0; i++){
for (j = 0; j<DIM1; j++)
printf ("\t%i", data[i][j]);
printf ("\n");
}
status = H5Sclose (dataspace_id);
status = H5Dclose (dataset_id);
status = H5Fclose (file_id);
/*******************************************************
* Reopen the file and dataset and read varius subsets *
******************************************************/
file_id = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
dataset_id = H5Dopen2 (file_id, DATASETNAME, H5P_DEFAULT);
/* Create memory space with size of subset. */
dimsm[0] = DIM0_SUB;
dimsm[1] = DIM1_SUB;
memspace_id = H5Screate_simple (RANK, dimsm, NULL);
/* Specify hyperslab paramets that are constant across runs */
count[0] = 2;
count[1] = 2;
stride[0] = 3;
stride[1] = 1;
block[0] = 1;
block[1] = 1;
/* Open file dataspace */
dataspace_id = H5Dget_space (dataset_id);
/* First select rows 2 & 5, columns 3 & 4 */
offset[0] = 1;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id,
H5P_DEFAULT, sdata1);
printf ("\nHyperslab 1:\n");
for (i = 0; i<DIM0_SUB; i++){
for (j = 0; j<(DIM1_SUB); j++)
printf ("\t%i", sdata1[i][j]);
printf ("\n");
}
/* Clear selection.
Now select rows 3 & 6, columns 3 & 4 */
offset[0] = 2;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id,
H5P_DEFAULT, sdata1);
printf ("\nHyperslab 2:\n");
for (i = 0; i<DIM0_SUB; i++){
for (j = 0; j<(DIM1_SUB); j++)
printf ("\t%i", sdata1[i][j]);
printf ("\n");
};
status = H5Sclose (memspace_id);
/*******************************************************
* Now we'll combine our two hyperslabs in both orders *
******************************************************/
/* Create a larger memspace for the combined hyperslabs */
dimsm[0] = DIM0_SUB;
dimsm[1] = DIM1_SUB * 2;
memspace_id = H5Screate_simple (RANK, dimsm, NULL);
/* First select rows 2 & 5, columns 3 & 4 */
offset[0] = 1;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
/* Combine with rows 3 & 6, columns 3 & 4 */
offset[0] = 2;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_OR, offset,
stride, count, block);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id,
H5P_DEFAULT, sdata2);
if(status < 0) { return(1); }
printf ("\nHyperslab 1 OR Hyperslab 2:\n");
for (i = 0; i<DIM0_SUB; i++){
for (j = 0; j<(DIM1_SUB * 2); j++)
printf ("\t%i", sdata2[i][j]);
printf ("\n");
};
/********************************************
* Switch the order hyperslabs are combined *
********************************************/
offset[0] = 2;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
offset[0] = 1;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_OR, offset,
stride, count, block);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id,
H5P_DEFAULT, sdata2);
if(status < 0) { return(1); }
printf ("\nHyperslab 2 OR Hyperslab 1:\n");
for (i = 0; i<DIM0_SUB; i++){
for (j = 0; j<(DIM1_SUB * 2); j++)
printf ("\t%i", sdata2[i][j]);
printf ("\n");
};
/* Close open handles */
status = H5Sclose (memspace_id);
status = H5Sclose (dataspace_id);
status = H5Dclose (dataset_id);
status = H5Fclose (file_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment