Skip to content

Instantly share code, notes, and snippets.

@grimbough
Created February 17, 2021 22:46
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/effec0aa0b071bdb8817b9ad96c42a53 to your computer and use it in GitHub Desktop.
Save grimbough/effec0aa0b071bdb8817b9ad96c42a53 to your computer and use it in GitHub Desktop.
Confirming hyperslab issue remains when combining two selections using H5Scombine_select
/*
* Confirming hyperslab issue remains when combining two selections
* using H5Scombine_select
*/
#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_id1, dataspace_id2, dataspace_id3, dataspace_id4;
hid_t 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_id1 = H5Screate_simple (RANK, dims, NULL);
dataset_id = H5Dcreate2 (file_id, DATASETNAME, H5T_STD_I32BE, dataspace_id1,
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_id1);
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_id1 = H5Dget_space (dataset_id);
dataspace_id2 = H5Dget_space (dataset_id);
/* First select rows 2 & 5, columns 3 & 4 */
offset[0] = 1;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id1, H5S_SELECT_SET, offset,
stride, count, block);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id1,
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_id2, H5S_SELECT_SET, offset,
stride, count, block);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id2,
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);
dataspace_id3 = H5Scombine_select (dataspace_id1, H5S_SELECT_OR, dataspace_id2);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id3,
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 *
********************************************/
dataspace_id4 = H5Scombine_select (dataspace_id2, H5S_SELECT_OR, dataspace_id1);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id4,
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_id1);
status = H5Sclose (dataspace_id2);
status = H5Sclose (dataspace_id3);
status = H5Sclose (dataspace_id4);
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