Skip to content

Commit

Permalink
rest_vol_dataset: (fix) Write hyperslab (memory)
Browse files Browse the repository at this point in the history
- Improved the function RV_dataset_write() such it makes use
  of the function H5Dgather() to gather the data needed
  for writing a hyperslab (memory) to a file. This approach
  is similar to the one found in the function RV_dataset_read().
- Adapted the hyperslab and point selection test.
  • Loading branch information
jwsblokland committed Aug 25, 2023
1 parent a3d6d88 commit a88fd3a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/rest_vol_dataset.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,13 @@ RV_dataset_write(size_t count, void *dset[], hid_t mem_type_id[], hid_t mem_spac
FUNC_GOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "memory datatype is invalid");

write_body_len = (size_t)file_select_npoints * dtype_size;
if (NULL == (write_body = (char *)RV_malloc(write_body_len)))
FUNC_GOTO_ERROR(H5E_DATASPACE, H5E_CANTALLOC, FAIL,
"can't allocate space for the 'write_body' values");
if (H5Dgather(mem_space_id[0], buf[0], mem_type_id[0], write_body_len, write_body, NULL,
write_body) < 0)
FUNC_GOTO_ERROR(H5E_DATASET, H5E_WRITEERROR, FAIL, "can't gather data to write buffer");
buf[0] = write_body;
} /* end if */
else {
if (H5T_STD_REF_OBJ == mem_type_id[0]) {
Expand Down Expand Up @@ -897,6 +904,8 @@ RV_dataset_write(size_t count, void *dset[], hid_t mem_type_id[], hid_t mem_spac
printf("-> Base64-encoded data buffer: %s\n\n", base64_encoded_value);
#endif

if (write_body)
RV_free(write_body);
write_body_len = (strlen(fmt_string) - 4) + selection_body_len + value_body_len;
if (NULL == (write_body = RV_malloc(write_body_len + 1)))
FUNC_GOTO_ERROR(H5E_DATASET, H5E_CANTALLOC, FAIL, "can't allocate space for write buffer");
Expand Down
39 changes: 35 additions & 4 deletions test/test_rest_vol.c
Original file line number Diff line number Diff line change
Expand Up @@ -7464,11 +7464,13 @@ test_write_dataset_small_point_selection(void)
hsize_t points[DATASET_SMALL_WRITE_TEST_POINT_SELECTION_NUM_POINTS *
DATASET_SMALL_WRITE_TEST_POINT_SELECTION_DSET_SPACE_RANK];
hsize_t dims[DATASET_SMALL_WRITE_TEST_POINT_SELECTION_DSET_SPACE_RANK] = {10, 10, 10};
hsize_t mdims[1];
size_t i, data_size;
hid_t file_id = -1, fapl_id = -1;
hid_t container_group = -1;
hid_t dset_id = -1;
hid_t fspace_id = -1;
hid_t mspace_id = -1;
void *data = NULL;

TESTING("small write to dataset w/ point selection")
Expand Down Expand Up @@ -7511,6 +7513,9 @@ test_write_dataset_small_point_selection(void)
if (NULL == (data = malloc(data_size)))
TEST_ERROR

mdims[0] = DATASET_SMALL_WRITE_TEST_POINT_SELECTION_NUM_POINTS;
if ((mspace_id = H5Screate_simple(1, mdims, NULL)) < 0)
TEST_ERROR
for (i = 0; i < data_size / DATASET_SMALL_WRITE_TEST_POINT_SELECTION_DSET_DTYPESIZE; i++)
((int *)data)[i] = (int)i;

Expand All @@ -7532,7 +7537,7 @@ test_write_dataset_small_point_selection(void)
puts("Writing a small amount of data to dataset using a point selection\n");
#endif

if (H5Dwrite(dset_id, DATASET_SMALL_WRITE_TEST_POINT_SELECTION_DSET_DTYPE, H5S_ALL, fspace_id,
if (H5Dwrite(dset_id, DATASET_SMALL_WRITE_TEST_POINT_SELECTION_DSET_DTYPE, mspace_id, fspace_id,
H5P_DEFAULT, data) < 0) {
H5_FAILED();
printf(" couldn't write to dataset\n");
Expand All @@ -7544,6 +7549,8 @@ test_write_dataset_small_point_selection(void)
data = NULL;
}

if (H5Sclose(mspace_id) < 0)
TEST_ERROR
if (H5Sclose(fspace_id) < 0)
TEST_ERROR
if (H5Dclose(dset_id) < 0)
Expand Down Expand Up @@ -8539,6 +8546,7 @@ test_write_dataset_data_verification(void)
{
hssize_t space_npoints;
hsize_t dims[DATASET_DATA_VERIFY_WRITE_TEST_DSET_SPACE_RANK] = {10, 10, 10};
hsize_t mdims[1];
hsize_t start[DATASET_DATA_VERIFY_WRITE_TEST_DSET_SPACE_RANK];
hsize_t stride[DATASET_DATA_VERIFY_WRITE_TEST_DSET_SPACE_RANK];
hsize_t count[DATASET_DATA_VERIFY_WRITE_TEST_DSET_SPACE_RANK];
Expand All @@ -8550,6 +8558,7 @@ test_write_dataset_data_verification(void)
hid_t container_group = -1;
hid_t dset_id = -1;
hid_t fspace_id = -1;
hid_t mspace_id = -1;
void *data = NULL;
void *write_buf = NULL;
void *read_buf = NULL;
Expand Down Expand Up @@ -8697,23 +8706,34 @@ test_write_dataset_data_verification(void)
}

/* Write to first two rows of dataset */
mdims[0] = dims[1] * 2;
start[0] = 0;
stride[0] = 1;
count[0] = dims[1] * 2;
block[0] = 1;
if ((mspace_id = H5Screate_simple(1, mdims, NULL)) < 0)
TEST_ERROR
if (H5Sselect_hyperslab(mspace_id, H5S_SELECT_SET, start, stride, count, block) < 0)
TEST_ERROR

start[0] = start[1] = start[2] = 0;
stride[0] = stride[1] = stride[2] = 1;
count[0] = 2;
count[1] = dims[1];
count[2] = 1;
block[0] = block[1] = block[2] = 1;

if (H5Sselect_hyperslab(fspace_id, H5S_SELECT_SET, start, stride, count, block) < 0)
TEST_ERROR

if (H5Dwrite(dset_id, DATASET_DATA_VERIFY_WRITE_TEST_DSET_DTYPE, H5S_ALL, fspace_id, H5P_DEFAULT,
if (H5Dwrite(dset_id, DATASET_DATA_VERIFY_WRITE_TEST_DSET_DTYPE, mspace_id, fspace_id, H5P_DEFAULT,
write_buf) < 0) {
H5_FAILED();
printf(" couldn't write to dataset\n");
goto error;
}

if (H5Sclose(mspace_id) < 0)
TEST_ERROR
if (H5Sclose(fspace_id) < 0)
TEST_ERROR
if (H5Dclose(dset_id) < 0)
Expand Down Expand Up @@ -8814,6 +8834,15 @@ test_write_dataset_data_verification(void)
}

/* Select a series of 10 points in the dataset */
mdims[0] = DATASET_DATA_VERIFY_WRITE_TEST_NUM_POINTS;
if ((mspace_id = H5Screate_simple(1, mdims, NULL)) < 0)
TEST_ERROR
for (i = 0; i < DATASET_DATA_VERIFY_WRITE_TEST_NUM_POINTS; i++) {
points[i] = i;
}
if (H5Sselect_elements(mspace_id, H5S_SELECT_SET, DATASET_DATA_VERIFY_WRITE_TEST_NUM_POINTS, points) < 0)
TEST_ERROR

for (i = 0; i < DATASET_DATA_VERIFY_WRITE_TEST_NUM_POINTS; i++) {
size_t j;

Expand All @@ -8824,13 +8853,15 @@ test_write_dataset_data_verification(void)
if (H5Sselect_elements(fspace_id, H5S_SELECT_SET, DATASET_DATA_VERIFY_WRITE_TEST_NUM_POINTS, points) < 0)
TEST_ERROR

if (H5Dwrite(dset_id, DATASET_DATA_VERIFY_WRITE_TEST_DSET_DTYPE, H5S_ALL, fspace_id, H5P_DEFAULT,
if (H5Dwrite(dset_id, DATASET_DATA_VERIFY_WRITE_TEST_DSET_DTYPE, mspace_id, fspace_id, H5P_DEFAULT,
write_buf) < 0) {
H5_FAILED();
printf(" couldn't write to dataset\n");
goto error;
}

if (H5Sclose(mspace_id) < 0)
TEST_ERROR
if (H5Sclose(fspace_id) < 0)
TEST_ERROR
if (H5Dclose(dset_id) < 0)
Expand Down

0 comments on commit a88fd3a

Please sign in to comment.