Skip to content

Commit

Permalink
count argument in H5Sselect_hyperslab
Browse files Browse the repository at this point in the history
Argument 'count' in NetCDF is not exactly the same as the 'count' in
H5Sselect_hyperslabs(space_id, op, start, stride, count, block).
When the argument 'stride' is NULL, NetCDF's 'count' should be used in
argument 'block', for example,
   H5Sselect_hyperslabs(space_id, op, start, NULL, ones, count);
where 'one' is an array of all 1s. Although using NULL 'block' below
   H5Sselect_hyperslabs(space_id, op, start, NULL, count, NULL);
has the same effect, HDF5 internally stores the space of a subarray as a
list of single elements, instead of a "block", which can affect the
performance.
  • Loading branch information
wkliao committed Apr 20, 2022
1 parent dd4edd0 commit 4518ae8
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 32 deletions.
5 changes: 3 additions & 2 deletions h5_test/tst_h_dimscales2.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ main()
hsize_t h5dimlen[DIMS2], h5dimlenmax[DIMS2], xtend_size[DIMS2] = {1, NUM_VALS};
hsize_t start[DIMS2] = {0, 0};
hsize_t count[DIMS2] = {1, NUM_VALS};
hsize_t ones[DIMS2] = {1, 1};
double value[NUM_VALS];
int dataset_ndims;
int i;
Expand Down Expand Up @@ -661,7 +662,7 @@ main()

/* Set up the file and memory spaces. */
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, ones, count) < 0) ERR;
if ((mem_spaceid = H5Screate_simple(DIMS2, count, NULL)) < 0) ERR;

/* Write a slice of data. */
Expand All @@ -683,7 +684,7 @@ main()
/* Set up the file and memory spaces for a second slice. */
start[0]++;
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, ones, count) < 0) ERR;
if ((mem_spaceid = H5Screate_simple(DIMS2, count, NULL)) < 0) ERR;

/* Write a second slice of data. */
Expand Down
4 changes: 2 additions & 2 deletions h5_test/tst_h_enums.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ main()
char lang[NUM_LANG][STR_LEN + 1] = {"C", "Fortran", "C++", "MISSING"};
enum langs {CLANG=0, Fortran=1, CPP=2, MISSING=255};
short the_value, fill_value = MISSING, data_point = CLANG;
hsize_t start[1] = {1}, count[1] = {1};
hsize_t start[1] = {1}, count[1] = {1}, one[1] = {1};
int num_members;
size_t size;
hid_t base_hdf_typeid;
Expand Down Expand Up @@ -197,7 +197,7 @@ main()
if ((mem_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
if ((file_spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR;
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, one, count) < 0) ERR;
if (H5Dwrite(datasetid, typeid, mem_spaceid, file_spaceid,
H5P_DEFAULT, &data_point) < 0) ERR;

Expand Down
10 changes: 6 additions & 4 deletions h5_test/tst_h_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ main()
#define MILLION 1000000

hid_t fileid, write_spaceid, datasetid, mem_spaceid;
hsize_t start[NDIMS], count[NDIMS];
hsize_t start[NDIMS], count[NDIMS], ones[NDIMS];
hsize_t dims[1];
int *data;
int num_steps;
Expand Down Expand Up @@ -210,8 +210,9 @@ main()
{
/* Select hyperslab for write of one slice. */
start[0] = s * SC;
ones[0] = 1;
if (H5Sselect_hyperslab(write_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, ones, count) < 0) ERR;

if (H5Dwrite(datasetid, H5T_NATIVE_INT, mem_spaceid, write_spaceid,
H5P_DEFAULT, data) < 0) ERR;
Expand Down Expand Up @@ -242,7 +243,7 @@ main()
hid_t mem_spaceid, xfer_plistid, native_typeid;
hsize_t *chunksize, dims[1], maxdims[1], *dimsize, *maxdimsize;
hsize_t fdims[MAX_DIMS], fmaxdims[MAX_DIMS];
hsize_t start[MAX_DIMS], count[MAX_DIMS];
hsize_t start[MAX_DIMS], count[MAX_DIMS], ones[MAX_DIMS];
char file_name[STR_LEN + 1];
char dimscale_wo_var[STR_LEN];
void *bufr;
Expand Down Expand Up @@ -342,7 +343,8 @@ main()
start[1] = 0;
count[0] = 1;
count[1] = 2097153;
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, count, NULL) < 0) ERR;
ones[0] = ones[1] = 1;
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, ones, count) < 0) ERR;
if ((mem_spaceid = H5Screate_simple(NDIMS2, count, NULL)) < 0) ERR;
if ((xfer_plistid = H5Pcreate(H5P_DATASET_XFER)) < 0) ERR;
if ((native_typeid = H5Tget_native_type(H5T_NATIVE_SCHAR, H5T_DIR_DEFAULT)) < 0) ERR;
Expand Down
8 changes: 5 additions & 3 deletions h5_test/tst_h_par.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ main(int argc, char **argv)
printf("*** Creating file for parallel I/O read, and rereading it...");
{
hid_t fapl_id, fileid, whole_spaceid, dsid, slice_spaceid, whole_spaceid1, xferid;
hsize_t start[NDIMS], count[NDIMS];
hsize_t start[NDIMS], count[NDIMS], ones[NDIMS];
hsize_t dims[1];
int data[SC1], data_in[SC1];
int num_steps;
Expand Down Expand Up @@ -126,8 +126,9 @@ main(int argc, char **argv)
/* Select hyperslab for write of one slice. */
start[0] = s * SC1 * p + my_rank * SC1;
count[0] = SC1;
ones[0] = 1;
if (H5Sselect_hyperslab(whole_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, ones, count) < 0) ERR;

if (H5Dwrite(dsid, H5T_NATIVE_INT, slice_spaceid, whole_spaceid,
xferid, data) < 0) ERR;
Expand Down Expand Up @@ -185,8 +186,9 @@ main(int argc, char **argv)
/* Select hyperslab for read of one slice. */
start[0] = s * SC1 * p + my_rank * SC1;
count[0] = SC1;
ones[0] = 1;
if (H5Sselect_hyperslab(whole_spaceid1, H5S_SELECT_SET,
start, NULL, count, NULL) < 0)
start, NULL, ones, count) < 0)
{
ERR;
return 2;
Expand Down
8 changes: 5 additions & 3 deletions h5_test/tst_h_par_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ main(int argc, char **argv)
{
hid_t fapl_id, fileid, whole_spaceid, dsid, slice_spaceid, whole_spaceid1, xferid;
hid_t plistid;
hsize_t start[NDIMS], count[NDIMS];
hsize_t start[NDIMS], count[NDIMS], ones[NDIMS];
hsize_t dims[1], chunksize = SC1;
int data[SC1], data_in[SC1];
int num_steps;
Expand Down Expand Up @@ -120,8 +120,9 @@ main(int argc, char **argv)
/* Select hyperslab for write of one slice. */
start[0] = s * SC1 * p + my_rank * SC1;
count[0] = SC1;
ones[0] = 1;
if (H5Sselect_hyperslab(whole_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, ones, count) < 0) ERR;

if (H5Dwrite(dsid, H5T_NATIVE_INT, slice_spaceid, whole_spaceid,
xferid, data) < 0) ERR;
Expand Down Expand Up @@ -160,8 +161,9 @@ main(int argc, char **argv)
/* Select hyperslab for read of one slice. */
start[0] = s * SC1 * p + my_rank * SC1;
count[0] = SC1;
ones[0] = 1;
if (H5Sselect_hyperslab(whole_spaceid1, H5S_SELECT_SET,
start, NULL, count, NULL) < 0)
start, NULL, ones, count) < 0)
{
ERR;
return 2;
Expand Down
7 changes: 4 additions & 3 deletions h5_test/tst_h_strings2.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ main()
hid_t file_spaceid, mem_spaceid;
hsize_t dims[1] = {0}, max_dims[1] = {H5S_UNLIMITED}, chunk_dims[1] = {1};
hsize_t xtend_size[NDIMS] = {2}, start[NDIMS] = {1}, count[NDIMS] = {1};
hsize_t ones[NDIMS] = {1};
/* void *fillp;*/
char *data = "A man who carries a cat by the tail learns "
"something he can learn in no other way.";
Expand Down Expand Up @@ -91,7 +92,7 @@ main()
/* Select space in file to write a record. */
if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR;
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, ones, count) < 0) ERR;

/* Select space in memory to read from. */
if ((mem_spaceid = H5Screate_simple(NDIMS, count, NULL)) < 0) ERR;
Expand Down Expand Up @@ -126,7 +127,7 @@ main()
hid_t typeid, datasetid, plistid;
hid_t file_spaceid, mem_spaceid;
hsize_t dims[1] = {2}, chunk_dims[1] = {1};
hsize_t start[NDIMS] = {1}, count[NDIMS] = {1};
hsize_t start[NDIMS] = {1}, count[NDIMS] = {1}, ones[NDIMS] = {1};
/* void *fillp;*/
char *data = "A man who carries a cat by the tail learns "
"something he can learn in no other way.";
Expand Down Expand Up @@ -179,7 +180,7 @@ To be good is noble; but to show others how to be good is nobler and no trouble.
/* Select space in file to write a record. */
if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR;
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, ones, count) < 0) ERR;

/* Select space in memory to read from. */
if ((mem_spaceid = H5Screate_simple(NDIMS, count, NULL)) < 0)
Expand Down
5 changes: 3 additions & 2 deletions h5_test/tst_h_vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ main()
float float_data_out[LAT_LEN][LON_LEN];
hsize_t dims[NDIMS], max_dims[NDIMS];
hsize_t dims_in[NDIMS], max_dims_in[NDIMS];
hsize_t start[MAX_DIMS], count[MAX_DIMS];
hsize_t start[MAX_DIMS], count[MAX_DIMS], ones[MAX_DIMS];
int lat, lon;

/* Set up some phoney data, 1 record's worth. In C, first
Expand Down Expand Up @@ -153,8 +153,9 @@ main()
start[0] = 1;
start[1] = 0;
start[2] = 0;
ones[0] = ones[1] = ones[2] = 1;
if (H5Sselect_hyperslab(write_spaceid, H5S_SELECT_SET,
start, NULL, count, NULL) < 0) ERR;
start, NULL, ones, count) < 0) ERR;

/* Write second record of data to each dataset. */
if (H5Dwrite(pres_dsid, H5T_IEEE_F32LE, mem_spaceid, write_spaceid,
Expand Down
37 changes: 29 additions & 8 deletions libhdf5/hdf5var.c
Original file line number Diff line number Diff line change
Expand Up @@ -1541,11 +1541,12 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
NC_VAR_INFO_T *var;
NC_DIM_INFO_T *dim;
NC_HDF5_VAR_INFO_T *hdf5_var;
herr_t herr;
hid_t file_spaceid = 0, mem_spaceid = 0, xfer_plistid = 0;
long long unsigned xtend_size[NC_MAX_VAR_DIMS];
hsize_t fdims[NC_MAX_VAR_DIMS], fmaxdims[NC_MAX_VAR_DIMS];
hsize_t start[NC_MAX_VAR_DIMS], count[NC_MAX_VAR_DIMS];
hsize_t stride[NC_MAX_VAR_DIMS];
hsize_t stride[NC_MAX_VAR_DIMS], ones[NC_MAX_VAR_DIMS];
int need_to_extend = 0;
#ifdef USE_PARALLEL4
int extend_possible = 0;
Expand Down Expand Up @@ -1596,6 +1597,7 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
start[i] = startp[i];
count[i] = countp ? countp[i] : var->dim[i]->len;
stride[i] = stridep ? stridep[i] : 1;
ones[i] = 1;
LOG((4, "start[%d] %ld count[%d] %ld stride[%d] %ld", i, start[i], i, count[i], i, stride[i]));

/* Check to see if any counts are zero. */
Expand Down Expand Up @@ -1646,8 +1648,13 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
}
else
{
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, stride,
count, NULL) < 0)
if (stridep == NULL)
herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start,
NULL, ones, count);
else
herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start,
stride, count, NULL);
if (herr < 0)
BAIL(NC_EHDFERR);

/* Create a space for the memory, just big enough to hold the slab
Expand Down Expand Up @@ -1777,8 +1784,14 @@ NC4_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
BAIL2(NC_EHDFERR);
if ((file_spaceid = H5Dget_space(hdf5_var->hdf_datasetid)) < 0)
BAIL(NC_EHDFERR);
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, stride, count, NULL) < 0)

if (stridep == NULL)
herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, NULL, ones, count);
else
herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, stride, count, NULL);
if (herr < 0)
BAIL(NC_EHDFERR);
}
}
Expand Down Expand Up @@ -1877,7 +1890,7 @@ NC4_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
hsize_t count[NC_MAX_VAR_DIMS];
hsize_t fdims[NC_MAX_VAR_DIMS], fmaxdims[NC_MAX_VAR_DIMS];
hsize_t start[NC_MAX_VAR_DIMS];
hsize_t stride[NC_MAX_VAR_DIMS];
hsize_t stride[NC_MAX_VAR_DIMS], ones[NC_MAX_VAR_DIMS];
void *fillvalue = NULL;
int no_read = 0, provide_fill = 0;
hssize_t fill_value_size[NC_MAX_VAR_DIMS];
Expand Down Expand Up @@ -1925,6 +1938,7 @@ NC4_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
start[i] = startp[i];
count[i] = countp[i];
stride[i] = stridep ? stridep[i] : 1;
ones[i] = 1;

/* if any of the count values are zero don't actually read. */
if (count[i] == 0)
Expand Down Expand Up @@ -2054,9 +2068,16 @@ NC4_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
}
else
{
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, stride, count, NULL) < 0)
herr_t herr;
if (stridep == NULL)
herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, NULL, ones, count);
else
herr = H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, stride, count, NULL);
if (herr < 0)
BAIL(NC_EHDFERR);

/* Create a space for the memory, just big enough to hold the slab
we want. */
if ((mem_spaceid = H5Screate_simple(var->ndims, count, NULL)) < 0)
Expand Down
13 changes: 9 additions & 4 deletions libnczarr/zvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
#endif
size64_t fdims[NC_MAX_VAR_DIMS];
size64_t start[NC_MAX_VAR_DIMS], count[NC_MAX_VAR_DIMS];
size64_t stride[NC_MAX_VAR_DIMS];
size64_t stride[NC_MAX_VAR_DIMS], ones[NC_MAX_VAR_DIMS];
int retval, range_error = 0, i, d2;
void *bufr = NULL;
int bufrd = 0; /* 1 => we allocated bufr */
Expand Down Expand Up @@ -1584,6 +1584,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
start[0] = 0;
count[0] = 1;
stride[0] = 1;
ones[0] = 1;
} else {
for (i = 0; i < var->ndims; i++)
{
Expand All @@ -1594,6 +1595,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
start[i] = startp[i];
count[i] = countp ? countp[i] : var->dim[i]->len;
stride[i] = stridep ? stridep[i] : 1;
ones[i] = 1;

/* Check to see if any counts are zero. */
if (!count[i])
Expand Down Expand Up @@ -1649,7 +1651,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
else
{
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, stride,
count, NULL) < 0)
ones, count) < 0)
BAIL(NC_EHDFERR);

/* Create a space for the memory, just big enough to hold the slab
Expand Down Expand Up @@ -1757,7 +1759,7 @@ NCZ_put_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
if ((file_spaceid = H5Dget_space(ncz_var->hdf_datasetid)) < 0)
BAIL(NC_EHDFERR);
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, stride, count, NULL) < 0)
start, stride, ones, count) < 0)
BAIL(NC_EHDFERR);
}
#endif
Expand Down Expand Up @@ -1864,6 +1866,7 @@ NCZ_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
size64_t fmaxdims[NC_MAX_VAR_DIMS];
size64_t start[NC_MAX_VAR_DIMS];
size64_t stride[NC_MAX_VAR_DIMS];
size64_t ones[NC_MAX_VAR_DIMS];
int no_read = 0, provide_fill = 0;
int fill_value_size[NC_MAX_VAR_DIMS];
int retval, range_error = 0, i, d2;
Expand Down Expand Up @@ -1898,6 +1901,7 @@ NCZ_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
start[0] = 0;
count[0] = 1;
stride[0] = 1;
ones[0] = 1;
} else {
for (i = 0; i < var->ndims; i++)
{
Expand All @@ -1907,6 +1911,7 @@ NCZ_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
start[i] = startp[i];
count[i] = countp[i];
stride[i] = stridep ? stridep[i] : 1;
ones[i] = 1;
/* if any of the count values are zero don't actually read. */
if (count[i] == 0)
no_read++;
Expand Down Expand Up @@ -2040,7 +2045,7 @@ NCZ_get_vars(int ncid, int varid, const size_t *startp, const size_t *countp,
else
{
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
start, stride, count, NULL) < 0)
start, stride, ones, count) < 0)
BAIL(NC_EHDFERR);
/* Create a space for the memory, just big enough to hold the slab
we want. */
Expand Down
3 changes: 2 additions & 1 deletion nc_perf/tst_files3.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ int dump_hdf_file(const float *data, int docompression)
hsize_t dims[NDIMS] = {X_LEN, Y_LEN, Z_LEN};
hsize_t start[NDIMS] = {0, 0, 0};
hsize_t count[NDIMS] = {1, 1, Z_LEN};
hsize_t ones[NDIMS] = {1, 1, 1};

/* create file */
file_id = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC,
Expand Down Expand Up @@ -138,7 +139,7 @@ int dump_hdf_file(const float *data, int docompression)
for (start[1] = 0; start[1] < Y_LEN; start[1]++)
{
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL,
count, NULL) < 0) ERR_RET;
ones, count) < 0) ERR_RET;
if (H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, mem_spaceid, file_spaceid,
xfer_plistid, data) < 0) ERR_RET;
}
Expand Down

0 comments on commit 4518ae8

Please sign in to comment.