Skip to content

Instantly share code, notes, and snippets.

@grimbough
Created March 5, 2021 14:05
Show Gist options
  • Save grimbough/c9a59b6aba450204f6c7ad9702391821 to your computer and use it in GitHub Desktop.
Save grimbough/c9a59b6aba450204f6c7ad9702391821 to your computer and use it in GitHub Desktop.
Comparing H5Sselect_hyperslab and H5Scombine_hyperslab
/*
* Confirming hyperslab issue remains when combining two selections
* using H5Scombine_select
*/
#include <hdf5.h>
#include <stdlib.h>
#define FILE "subset.h5"
#define DATASETNAME "IntArray"
#define RANK 2
#define DIM0_SUB 2 /* subset dimensions */
#define DIM1_SUB 1
#define DIM0 10 /* size of dataset */
#define DIM1 10
void print_hyperslab_details (hid_t dataspace_id) {
herr_t status;
hsize_t *buf;
hsize_t nblocks;
int l,k, inc;
hsize_t start_out[2],
stride_out[2],
count_out[2],
block_out[2];
printf("Number of points: %llu\n", H5Sget_select_npoints(dataspace_id));
printf("Number of hyperslab blocks: %llu\n", H5Sget_select_hyper_nblocks(dataspace_id));
if (H5Sget_select_type(dataspace_id) == H5S_SEL_HYPERSLABS) {
nblocks = H5Sget_select_hyper_nblocks (dataspace_id);
buf = (hsize_t *)malloc(sizeof(hsize_t)*2*RANK*nblocks);
status = H5Sget_select_hyper_blocklist (dataspace_id, (hsize_t)0, nblocks, buf);
for (l=0; l<nblocks; l++) {
inc = 2*l*RANK;
printf("(");
for (k=0; k<RANK-1; k++)
printf("%d,", (int)buf[k+inc]);
printf("%d) - (", (int)buf[k+inc]);
for (k=0; k<RANK-1; k++)
printf("%d,", (int)buf[RANK+k+inc]);
printf("%d)\n", (int)buf[RANK+k+inc]);
}
printf("Is regular hyperslab: %d\n", (int) H5Sis_regular_hyperslab(dataspace_id));
if (H5Sis_regular_hyperslab(dataspace_id)) {
status = H5Sget_regular_hyperslab (dataspace_id, start_out, stride_out, count_out, block_out);
printf(" start = [%llu, %llu] \n", (unsigned long long)start_out[0], (unsigned long long)start_out[1]);
printf(" stride = [%llu, %llu] \n", (unsigned long long)stride_out[0], (unsigned long long)stride_out[1]);
printf(" count = [%llu, %llu] \n", (unsigned long long)count_out[0], (unsigned long long)count_out[1]);
printf(" block = [%llu, %llu] \n", (unsigned long long)block_out[0], (unsigned long long)block_out[1]);
}
free(buf);
}
}
int
main (void)
{
hsize_t dims[2], dimsm[2], finaldim[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, dataspace_id2, 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];
/*****************************************************************
* 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] = 1;
count[1] = 2;
stride[0] = 1;
stride[1] = 2;
block[0] = 1;
block[1] = 1;
/* Open file dataspace */
dataspace_id = H5Dget_space (dataset_id);
offset[0] = 0;
offset[1] = 0;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
printf ("\nHyperslab 1:\n");
print_hyperslab_details(dataspace_id);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id,
H5P_DEFAULT, sdata1);
printf("Selection values:\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);
/* Clear selection.
Now select rows 3 & 6, columns 3 & 4 */
/* Create memory space with size of subset. */
dimsm[0] = DIM0_SUB;
dimsm[1] = DIM1_SUB;
memspace_id = H5Screate_simple (RANK, dimsm, NULL);
offset[0] = 2;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
printf ("\nHyperslab 2:\n");
print_hyperslab_details(dataspace_id);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id,
H5P_DEFAULT, sdata1);
printf("Selection values:\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 using two functions *
***********************************************************/
printf("\nCombining with H5Sselect_hyperslab\n");
/* Create a larger memspace for the combined hyperslabs */
finaldim[0] = DIM0_SUB;
finaldim[1] = DIM1_SUB*2;
memspace_id = H5Screate_simple (RANK, finaldim, NULL);
offset[0] = 0;
offset[1] = 0;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
offset[0] = 2;
offset[1] = 2;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_OR, offset,
stride, count, block);
print_hyperslab_details(dataspace_id);
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id,
H5P_DEFAULT, sdata2);
printf("Selection values:\n");
for (i = 0; i<DIM0_SUB; i++){
for (j = 0; j<DIM1_SUB*2; j++)
printf ("\t%i", sdata2[i][j]);
printf ("\n");
};
printf("\nCombining with H5Scombine_hyperslab\n");
offset[0] = 0;
offset[1] = 0;
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
offset[0] = 2;
offset[1] = 2;
dataspace_id2 = H5Scombine_hyperslab (dataspace_id, H5S_SELECT_OR, offset,
stride, count, block);
print_hyperslab_details(dataspace_id2);
/* This will throw an error */
status = H5Dread (dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id2,
H5P_DEFAULT, sdata2);
/* Close open handles */
status = H5Sclose (memspace_id);
status = H5Sclose (dataspace_id);
status = H5Sclose (dataspace_id2);
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