/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright by The HDF Group. * * Copyright by the Board of Trustees of the University of Illinois. * * All rights reserved. * * * * This file is part of HDF5. The full HDF5 copyright notice, including * * terms governing use, modification, and redistribution, is contained in * * the COPYING file, which can be found at the root of the source code * * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. * * If you do not have access to either file, you may request a copy from * * help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * Programmer: Pedro Vicente * April 7, 2008 * * Purpose: Tests the H5Dget_chunk_info API function * This program writes a 4x4 dataset by iterating on 2x2 chunks * at a time */ #include "h5test.h" #define PRINT_DATA #define H5FILE_NAME "chunk_info.h5" #define DATASETNAME "2d" #define RANK 2 #define NX 8 #define NY 8 #define CHUNK_NX 2 #define CHUNK_NY 2 int main( void ) { hid_t fid; /* file ID */ hid_t did; /* dataset ID */ hid_t f_sid; /* file space ID */ hid_t m_sid; /* memory space ID */ hid_t pid; /* property list ID */ hsize_t start[2]; /* chunk location to start writing */ hsize_t dims[2] = { NX, NY}; hsize_t chunk_dims[2] = { CHUNK_NX, CHUNK_NY }; int chunk_data[2][2] = { {1, 1}, {1, 1} }; int buf[4][4]; int fillvalue = 88; hsize_t addr = 0; hsize_t coord[2] = {0, 0}; hsize_t out_coord[2] = {99, 99}; hsize_t offset[2] = {0, 0}; int direct_buf[CHUNK_NX][CHUNK_NY]; int check_chunk[CHUNK_NX][CHUNK_NY]; int data[NX][NY]; unsigned filter_mask = 0; size_t buf_size = CHUNK_NX*CHUNK_NY*sizeof(int); hid_t mem_space = -1; hsize_t nchunks = 0; hsize_t size = 0; hsize_t index = 0; hsize_t stride[2]; /* Stride of hyperslab */ hsize_t count[2]; /* Block count */ hsize_t block[2]; /* Block sizes */ int i, j, n, ii, jj; /* create a new file using default properties. */ if ((fid = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; /* create the file space */ if ((f_sid = H5Screate_simple(RANK, dims, dims)) < 0) TEST_ERROR; /* create the memory space with chunk dimensions */ if ((m_sid = H5Screate_simple(RANK, chunk_dims, chunk_dims)) < 0) TEST_ERROR; start[0] = 0; start[1] = 0; if (H5Sselect_hyperslab(m_sid, H5S_SELECT_SET, start, NULL, chunk_dims, NULL) < 0) TEST_ERROR; TESTING("chunk info"); /*------------------------------------------------------------------------- * create a dataset *------------------------------------------------------------------------- */ /* modify dataset creation properties, i.e. enable chunking. */ if ((pid = H5Pcreate (H5P_DATASET_CREATE)) < 0) TEST_ERROR; /* if (H5Pset_chunk(pid, RANK, chunk_dims) < 0) TEST_ERROR; */ if (H5Pset_chunk(pid, RANK, chunk_dims) < 0) TEST_ERROR; if (H5Pset_fill_value(pid, H5T_NATIVE_INT, &fillvalue) < 0) TEST_ERROR; /* create a new dataset */ if((did = H5Dcreate2(fid , DATASETNAME, H5T_NATIVE_INT, f_sid, H5P_DEFAULT, pid, H5P_DEFAULT)) < 0) TEST_ERROR; /* Initialize data for one chunk */ for(i = n = 0; i < CHUNK_NX; i++) for(j = 0; j < CHUNK_NY; j++) { direct_buf[i][j] = n++; } offset[0] = 0; offset[1] = 6; fprintf(stderr, "test_skip_compress_write1: writing chunk\n"); filter_mask = 0x00000002; if(H5Dwrite_chunk(did, H5P_DEFAULT, filter_mask, offset, buf_size, direct_buf) < 0) TEST_ERROR; offset[0] = 0; offset[1] = 2; if(H5Dwrite_chunk(did, H5P_DEFAULT, filter_mask, offset, buf_size, direct_buf) < 0) TEST_ERROR; offset[0] = 2; offset[1] = 4; if(H5Dwrite_chunk(did, H5P_DEFAULT, filter_mask, offset, buf_size, direct_buf) < 0) TEST_ERROR; offset[0] = 4; offset[1] = 4; if(H5Dwrite_chunk(did, H5P_DEFAULT, filter_mask, offset, buf_size, direct_buf) < 0) TEST_ERROR; offset[0] = 2; offset[1] = 6; if(H5Dwrite_chunk(did, H5P_DEFAULT, filter_mask, offset, buf_size, direct_buf) < 0) TEST_ERROR; offset[0] = 6; offset[1] = 6; if(H5Dwrite_chunk(did, H5P_DEFAULT, filter_mask, offset, buf_size, direct_buf) < 0) TEST_ERROR; fprintf(stderr, "H5Dwrite_chunk passed\n"); if(H5Fflush(did, H5F_SCOPE_LOCAL) < 0) TEST_ERROR if(H5Dclose(did) < 0) TEST_ERROR if((did = H5Dopen2(fid, DATASETNAME, H5P_DEFAULT)) < 0) TEST_ERROR /* if((mem_space = H5Screate_simple(RANK, chunk_dims, NULL)) < 0) */ if((mem_space = H5Screate_simple(RANK, dims, dims)) < 0) TEST_ERROR /* * Select hyperslab for the chunk just written in the file */ start[0] = 0; start[1] = 0; stride[0] = 1; stride[1] = 1; count[0] = 1; count[1] = 1; block[0] = 1; block[1] = 1; /* start[0] = CHUNK_NX; start[1] = CHUNK_NY; stride[0] = 1; stride[1] = 1; count[0] = 1; count[1] = 1; block[0] = CHUNK_NX; block[1] = CHUNK_NY; */ if(H5Sselect_hyperslab(f_sid, H5S_SELECT_SET, start, stride, count, block) < 0) TEST_ERROR /* Read the chunk back */ /* if(H5Dread(did, H5T_NATIVE_INT, mem_space, f_sid, H5P_DEFAULT, check_chunk) < 0) TEST_ERROR */ fprintf(stderr, "calling H5Dread_chunk to see filter_mask\n"); { unsigned read_filter_mask = 0; int read_direct_buf[CHUNK_NX][CHUNK_NY]; offset[0] = 4; offset[1] = 4; /* Read the raw chunk back */ HDmemset(&read_direct_buf, 0, sizeof(read_direct_buf)); if(H5Dread_chunk(did, H5P_DEFAULT, offset, &read_filter_mask, read_direct_buf) < 0) TEST_ERROR fprintf(stderr, "read_filter_mask = %d\n", read_filter_mask); } /* Read entire dataset */ if(H5Dread(did, H5T_NATIVE_INT, mem_space, mem_space, H5P_DEFAULT, data) < 0) TEST_ERROR fprintf(stderr, "--------test_skip_compress_write1--------\n"); /* Check that the values read are the same as the values written */ for(i = 0; i < CHUNK_NX; i++) { fprintf(stderr, "\n"); for(j = 0; j < CHUNK_NY; j++) { fprintf(stderr, "%d ", direct_buf[i][j]); /* if(direct_buf[i][j] != check_chunk[i][j]) { HDprintf(" 1. Read different values than written."); HDprintf(" At index %d,%d\n", i, j); HDprintf(" direct_buf=%d, check_chunk=%d\n", direct_buf[i][j], check_chunk[i][j]); TEST_ERROR } */ } } fprintf(stderr, "\nEntire dataset [%d][%d]:\n", NX, NY); for(i = 0; i < NX; i++) { fprintf(stderr, "\n"); for(j = 0; j < NY; j++) { fprintf(stderr, "%d ", data[i][j]); } } fprintf(stderr, "\n"); if (H5Dget_num_chunks(did, mem_space, &nchunks) < 0) { fprintf(stderr, "H5Dget_num_chunks failed\n"); TEST_ERROR } fprintf(stderr, "nchunks = %d\n", nchunks); coord[0] = 0; coord[1] = 3; /* offset[0] = 0; offset[1] = 0; */ /* if (H5Dget_chunk_info_by_coord(did, coord, &filter_mask, &addr, &size) < 0) filter_mask = 0; fprintf(stderr, "\n---------- Getting chunk info with H5Dget_chunk_info_by_coord -------------\n"); */ /* if (H5Dread_chunk_info(did, coord, &filter_mask) < 0) fprintf(stderr, "\n....H5Dget_chunk_info_by_coord for coord: %d, %d\n", coord[0], coord[1]); if (H5Dget_chunk_info_by_coord(did, coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info_by_coord failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dread_chunk_info: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n\n", coord[0], coord[1], filter_mask, 0, 0); coord[0] = 0; coord[1] = 1; filter_mask = 0; fprintf(stderr, "\n....H5Dget_chunk_info_by_coord for coord: %d, %d\n", coord[0], coord[1]); if (H5Dget_chunk_info_by_coord(did, coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info_by_coord failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dget_chunk_info_by_coord: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n", coord[0], coord[1], filter_mask, addr, size); fprintf(stderr, "\n---------- Getting chunk info with H5Dget_chunk_info -------------\n"); */ /* coord[0] = 1; coord[1] = 1; */ index = 0; fprintf(stderr, "....H5Dget_chunk_info for index: %d\n", index); if (H5Dget_chunk_info(did, f_sid, index, out_coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dget_chunk_info: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n", coord[0], coord[1], filter_mask, addr, size); index = 3; fprintf(stderr, "....H5Dget_chunk_info for index: %d\n", index); if (H5Dget_chunk_info(did, f_sid, index, out_coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dget_chunk_info: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n", coord[0], coord[1], filter_mask, addr, size); /* index = 1; if (H5Dget_chunk_info(did, f_sid, index, out_coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info_by_coord failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dget_chunk_info_by_coord no data: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n", coord[0], coord[1], filter_mask, addr, size); index = 2; if (H5Dget_chunk_info(did, f_sid, index, out_coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info_by_coord failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dget_chunk_info_by_coord no data: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n", coord[0], coord[1], filter_mask, addr, size); index = 3; if (H5Dget_chunk_info(did, f_sid, index, out_coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info_by_coord failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dget_chunk_info_by_coord no data: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n", coord[0], coord[1], filter_mask, addr, size); coord[0] = 1; coord[1] = 2; filter_mask = 0; if (H5Dget_chunk_info_by_coord(did, coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info_by_coord failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dget_chunk_info_by_coord: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n", coord[0], coord[1], filter_mask, addr, size); coord[0] = 2; coord[1] = 2; if (H5Dget_chunk_info_by_coord(did, coord, &filter_mask, &addr, &size) < 0) { fprintf(stderr, "H5Dget_chunk_info_by_coord failed\n"); TEST_ERROR } fprintf(stderr, "by H5Dget_chunk_info_by_coord: coord %d, %d: filter_mask = %d, addr = %d, size = %d\n", coord[0], coord[1], filter_mask, addr, size); */ { unsigned read_filter_mask = 0; int read_direct_buf[CHUNK_NX][CHUNK_NY]; offset[0] = 4; offset[1] = 4; /* Read the raw chunk back */ HDmemset(&read_direct_buf, 0, sizeof(read_direct_buf)); if(H5Dread_chunk(did, H5P_DEFAULT, offset, &read_filter_mask, read_direct_buf) < 0) TEST_ERROR fprintf(stderr, "read_filter_mask = %d\n", read_filter_mask); } if (H5Dclose(did) < 0) TEST_ERROR if (H5Sclose(f_sid) < 0) TEST_ERROR if (H5Sclose(m_sid) < 0) TEST_ERROR if (H5Pclose(pid) < 0) TEST_ERROR if (H5Fclose(fid) < 0) TEST_ERROR PASSED(); puts("All chunk info tests passed."); return 0; error: H5Dclose( did ); H5Sclose( f_sid ); H5Sclose( m_sid ); H5Pclose( pid ); H5Fclose( fid ); H5_FAILED(); return 1; } #if 0 /*------------------------------------------------------------------------- * write the dataset in 2x2 chunks *------------------------------------------------------------------------- */ /* iterate in dim 0 */ for (j = 0; j < chunk_dims[0]; j++) { /* reset start in dim 1 */ start[1] = 0; /* iterate in dim 1 */ for (i = 0; i < chunk_dims[1]; i++) { /* select file hyperslab to save a 2x2 chunk */ if (H5Sselect_hyperslab(f_sid, H5S_SELECT_SET, start, NULL, chunk_dims, NULL) < 0) TEST_ERROR; /* write the data to the hyperslab. */ if (H5Dwrite(did, H5T_NATIVE_INT, m_sid, f_sid, H5P_DEFAULT, chunk_data) < 0) TEST_ERROR; /* read back and display complete dataset 4x4 */ if (H5Dread(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) TEST_ERROR; #if defined (PRINT_DATA) printf("\n"); printf("dataset: \n"); for (jj = 0; jj < dims[0]; jj++) { for (ii = 0; ii < dims[1]; ii++) printf("%d ", buf[jj][ii]); printf("\n"); } #endif /* increment start in dim 1 */ start[1] += 2; } /* increment start in dim 0 */ start[0] += 2; } #endif