netcdf-c/nczarr_test/tst_chunks.c
Dennis Heimbigner 59e04ae071 This PR adds EXPERIMENTAL support for accessing data in the
cloud using a variant of the Zarr protocol and storage
format. This enhancement is generically referred to as "NCZarr".

The data model supported by NCZarr is netcdf-4 minus the user-defined
types and the String type. In this sense it is similar to the CDF-5
data model.

More detailed information about enabling and using NCZarr is
described in the document NUG/nczarr.md and in a
[Unidata Developer's blog entry](https://www.unidata.ucar.edu/blogs/developer/en/entry/overview-of-zarr-support-in).

WARNING: this code has had limited testing, so do use this version
for production work. Also, performance improvements are ongoing.
Note especially the following platform matrix of successful tests:

Platform | Build System | S3 support
------------------------------------
Linux+gcc      | Automake     | yes
Linux+gcc      | CMake        | yes
Visual Studio  | CMake        | no

Additionally, and as a consequence of the addition of NCZarr,
major changes have been made to the Filter API. NOTE: NCZarr
does not yet support filters, but these changes are enablers for
that support in the future.  Note that it is possible
(probable?) that there will be some accidental reversions if the
changes here did not correctly mimic the existing filter testing.

In any case, previously filter ids and parameters were of type
unsigned int. In order to support the more general zarr filter
model, this was all converted to char*.  The old HDF5-specific,
unsigned int operations are still supported but they are
wrappers around the new, char* based nc_filterx_XXX functions.
This entailed at least the following changes:
1. Added the files libdispatch/dfilterx.c and include/ncfilter.h
2. Some filterx utilities have been moved to libdispatch/daux.c
3. A new entry, "filter_actions" was added to the NCDispatch table
   and the version bumped.
4. An overly complex set of structs was created to support funnelling
   all of the filterx operations thru a single dispatch
   "filter_actions" entry.
5. Move common code to from libhdf5 to libsrc4 so that it is accessible
   to nczarr.

Changes directly related to Zarr:
1. Modified CMakeList.txt and configure.ac to support both C and C++
   -- this is in support of S3 support via the awd-sdk libraries.
2. Define a size64_t type to support nczarr.
3. More reworking of libdispatch/dinfermodel.c to
   support zarr and to regularize the structure of the fragments
   section of a URL.

Changes not directly related to Zarr:
1. Make client-side filter registration be conditional, with default off.
2. Hack include/nc4internal.h to make some flags added by Ed be unique:
   e.g. NC_CREAT, NC_INDEF, etc.
3. cleanup include/nchttp.h and libdispatch/dhttp.c.
4. Misc. changes to support compiling under Visual Studio including:
   * Better testing under windows for dirent.h and opendir and closedir.
5. Misc. changes to the oc2 code to support various libcurl CURLOPT flags
   and to centralize error reporting.
6. By default, suppress the vlen tests that have unfixed memory leaks; add option to enable them.
7. Make part of the nc_test/test_byterange.sh test be contingent on remotetest.unidata.ucar.edu being accessible.

Changes Left TO-DO:
1. fix provenance code, it is too HDF5 specific.
2020-06-28 18:02:47 -06:00

389 lines
14 KiB
C

/* This is part of the netCDF package.
Copyright 2018 University Corporation for Atmospheric Research/Unidata
See COPYRIGHT file for conditions of use.
Test netcdf-4 variables.
Ed Hartnett, Dennis Heimbigner
modified version of nc_test4/tst_chunks.c
*/
#include "test_nczarr_utils.h"
#define DEBUGNOFILL
#undef PRINT_DEFAULT_CHUNKSIZE_TABLE
#undef FILTERS
#define FILE_NAME "tst_chunks"
#define NDIMS1 1
#define D_SMALL "small_dim"
#define D_SMALL_LEN 16
#define D_MEDIUM "medium_dim"
#define D_MEDIUM_LEN 65546
#define D_LARGE "large_dim"
#define D_LARGE_LEN 1048586
#define V_SMALL "small_var"
#define V_MEDIUM "medium_var"
#define V_LARGE "large_var"
int
main(int argc, char **argv)
{
processoptions(argc,argv,FILE_NAME);
printf("\n*** Testing netcdf-4 variable chunking.\n");
printf("**** testing that fixed vars with forced chunking end up being chunked, with good sizes...");
{
int ncid;
int nvars, ndims, ngatts, unlimdimid;
int storage;
int ndims_in, natts_in, dimids_in;
int small_dimid, medium_dimid, large_dimid;
int small_varid, medium_varid, large_varid;
char var_name_in[NC_MAX_NAME + 1];
size_t chunksize_in[NDIMS1];
nc_type xtype_in;
/* Create a netcdf-4 file with three dimensions. */
if (nc_create(options.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN, &small_dimid)) ERR;
if (nc_def_dim(ncid, D_MEDIUM, D_MEDIUM_LEN, &medium_dimid)) ERR;
if (nc_def_dim(ncid, D_LARGE, D_LARGE_LEN, &large_dimid)) ERR;
/* Add three vars, with forced chunking. */
if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
if (nc_def_var(ncid, V_MEDIUM, NC_INT64, NDIMS1, &medium_dimid, &medium_varid)) ERR;
if (nc_def_var(ncid, V_LARGE, NC_INT64, NDIMS1, &large_dimid, &large_varid)) ERR;
#ifdef FILTERS
if (nc_def_var_deflate(ncid, small_varid, 0, 1, 4)) ERR;
if (nc_def_var_deflate(ncid, medium_varid, 1, 0, 0)) ERR;
if (nc_def_var_fletcher32(ncid, large_varid, 1)) ERR;
#else
if(nc_def_var_chunking(ncid,small_varid,NC_CHUNKED,NULL)) ERR;
if(nc_def_var_chunking(ncid,medium_varid,NC_CHUNKED,NULL)) ERR;
if(nc_def_var_chunking(ncid,large_varid,NC_CHUNKED,NULL)) ERR;
#endif
if (nc_close(ncid)) ERR;
/* Open the file and check. */
if (nc_open(options.path, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
if (nvars != 3 || ndims != 3 || ngatts != 0 || unlimdimid != -1) ERR;
if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, &dimids_in, &natts_in)) ERR;
if (strcmp(var_name_in, V_SMALL) || xtype_in != NC_INT64 || ndims_in != 1 ||
natts_in != 0) ERR;
/* Make sure chunking sizes are what we expect. */
if (nc_inq_var_chunking(ncid, small_varid, &storage, chunksize_in)) ERR;
if (storage || chunksize_in[0] != D_SMALL_LEN) ERR;
if (nc_inq_var_chunking(ncid, medium_varid, &storage, chunksize_in)) ERR;
if (storage || chunksize_in[0] * sizeof(long long) > DEFAULT_CHUNK_SIZE) ERR;
if (nc_inq_var_chunking(ncid, large_varid, &storage, chunksize_in)) ERR;
if (storage || chunksize_in[0] * sizeof(long long) > DEFAULT_CHUNK_SIZE) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing default chunksizes...");
{
int nvars, ndims, ngatts, unlimdimid;
int storage;
#define NUM_DIM 4
#define NUM_TYPE 2
int ncid;
int dim_len[NUM_DIM] = {1, 100, 1000, 2000};
// int dim_len[NUM_DIM] = {1, 50, 100, 200};
size_t chunksize_in[NUM_DIM];
int type_id[NUM_TYPE] = {NC_BYTE, NC_INT};
int dimid[NUM_DIM], varid[NUM_TYPE];
char dim_name[NC_MAX_NAME + 1], var_name[NC_MAX_NAME + 1];
int d, t;
/* Create a netcdf-4 file with NUM_DIM dimensions. */
if (nc_create(options.path, NC_NETCDF4, &ncid)) ERR;
#ifdef DEBUGNOFILL
if(nc_set_fill(ncid,NC_NOFILL,&d)) ERR;
#endif
for (d = 0; d < NUM_DIM; d++)
{
sprintf(dim_name, "dim_%d", dim_len[d]);
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
printf("creating dim[%d] %s = %d\n", d, dim_name, dim_len[d]);
#endif
if (nc_def_dim(ncid, dim_name, dim_len[d], &dimid[d])) ERR;
}
for (t = 0; t < NUM_TYPE; t++)
{
sprintf(var_name, "var_%d", type_id[t]);
if (nc_def_var(ncid, var_name, type_id[t], NUM_DIM, dimid, &varid[t])) ERR;
if (nc_inq_var_chunking(ncid, varid[t], &storage, chunksize_in)) ERR;
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
printf("chunksizes for %d x %d x %d x %d var %s: %d x %d x %d x %d (=%d)\n",
dim_len[0], dim_len[1], dim_len[2], dim_len[3],
var_name,
(int)chunksize_in[0], (int)chunksize_in[1], (int)chunksize_in[2],
(int)chunksize_in[3],
(int)(chunksize_in[0] * chunksize_in[1] * chunksize_in[2] * chunksize_in[3]));
#endif
}
if (nc_close(ncid)) ERR;
/* Open the file and check. */
if (nc_open(options.path, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
if (nvars != NUM_TYPE || ndims != NUM_DIM || ngatts != 0 || unlimdimid == 0) ERR;
for (t = 0; t < NUM_TYPE; t++)
{
sprintf(var_name, "var_%d", type_id[t]);
if (nc_inq_var_chunking(ncid, varid[t], &storage, chunksize_in)) ERR;
if (storage) ERR;
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
printf("chunksizes for %d x %d x %d x %d var: %d x %d x %d x %d (=%d)\n",
dim_len[0], dim_len[1], dim_len[2], dim_len[3],
(int)chunksize_in[0], (int)chunksize_in[1], (int)chunksize_in[2],
(int)chunksize_in[3],
(int)(chunksize_in[0] * chunksize_in[1] * chunksize_in[2] * chunksize_in[3]));
#endif
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing that chunking works on classic mode files...");
{
#define D_SMALL_LEN2 66
int ncid;
int nvars, ndims, ngatts, unlimdimid;
int storage;
int ndims_in, natts_in, dimids_in;
int small_dimid, medium_dimid, large_dimid;
int small_varid, medium_varid, large_varid;
char var_name_in[NC_MAX_NAME + 1];
size_t chunks[1], chunksize_in;
nc_type xtype_in;
/* Create a netcdf-4 file with three dimensions. */
if (nc_create(options.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN2, &small_dimid)) ERR;
if (nc_def_dim(ncid, D_MEDIUM, D_MEDIUM_LEN, &medium_dimid)) ERR;
if (nc_def_dim(ncid, D_LARGE, D_LARGE_LEN, &large_dimid)) ERR;
/* Add three vars. */
if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
if (nc_def_var_chunking(ncid, small_varid, 1, NULL)) ERR;
if (nc_def_var(ncid, V_MEDIUM, NC_INT64, NDIMS1, &medium_dimid, &medium_varid)) ERR;
chunks[0] = D_MEDIUM_LEN / 100;
if (nc_def_var_chunking(ncid, medium_varid, 0, chunks)) ERR;
#ifdef FILTERS
if (nc_def_var_deflate(ncid, medium_varid, 1, 0, 0)) ERR;
#endif
if (nc_def_var(ncid, V_LARGE, NC_INT64, NDIMS1, &large_dimid, &large_varid)) ERR;
chunks[0] = D_LARGE_LEN / 1000;
if (nc_def_var_chunking(ncid, large_varid, 0, chunks)) ERR;
#ifdef FILTERS
if (nc_def_var_fletcher32(ncid, large_varid, 1)) ERR;
#endif
if (nc_close(ncid)) ERR;
/* Open the file and check. */
if (nc_open(options.path, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
if (nvars != 3 || ndims != 3 || ngatts != 0 || unlimdimid != -1) ERR;
if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, &dimids_in, &natts_in)) ERR;
if (strcmp(var_name_in, V_SMALL) || xtype_in != NC_INT64 || ndims_in != 1 ||
natts_in != 0) ERR;
/* Make sure chunking settings are what we expect. */
if (nc_inq_var_chunking(ncid, small_varid, &storage, &chunksize_in)) ERR;
if (storage != NC_CHUNKED) ERR;
if (nc_inq_var_chunking(ncid, medium_varid, &storage, &chunksize_in)) ERR;
if (storage || chunksize_in != D_MEDIUM_LEN / 100) ERR;
if (nc_inq_var_chunking(ncid, large_varid, &storage, &chunksize_in)) ERR;
if (storage || chunksize_in != D_LARGE_LEN / 1000) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing many chunking variables...");
{
#define NDIMS_3 3
#define NUM_PLANS 30
#define D_SNEAKINESS "sneakiness"
#define D_SNEAKINESS_LEN 5
#define D_CLEVERNESS "clevernesss"
#define D_CLEVERNESS_LEN 3
#define D_EFFECTIVENESS "effectiveness"
#define D_EFFECTIVENESS_LEN 2
int ncid, dimids[NDIMS_3], varid[NUM_PLANS];
size_t chunksize[NDIMS_3] = {D_SNEAKINESS_LEN, D_CLEVERNESS_LEN,
D_EFFECTIVENESS_LEN};
char plan_name[NC_MAX_NAME + 1];
int storage;
size_t chunksize_in[NDIMS_3];
int i, j;
/* Create a netcdf-4 file with three dimensions. */
if (nc_create(options.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, D_SNEAKINESS, D_SNEAKINESS_LEN, &dimids[0])) ERR;
if (nc_def_dim(ncid, D_CLEVERNESS, D_CLEVERNESS_LEN, &dimids[1])) ERR;
if (nc_def_dim(ncid, D_EFFECTIVENESS, D_EFFECTIVENESS_LEN, &dimids[2])) ERR;
/* Oh that tricky Cardinal Richelieu, he had many plans! */
for (i = 0; i < NUM_PLANS; i++)
{
sprintf(plan_name, "Richelieu_sneaky_plan_%d", i);
if (nc_def_var(ncid, plan_name, i % (NC_STRING - 1) + 1, NDIMS_3,
dimids, &varid[i])) ERR;
if (nc_def_var_chunking(ncid, varid[i], 0, chunksize)) ERR;
}
/* Check the chunking. */
for (i = 0; i < NUM_PLANS; i++)
{
if (nc_inq_var_chunking(ncid, varid[i], &storage, chunksize_in)) ERR;
{
for (j = 0; j < NDIMS_3; j++)
if (chunksize_in[j] != chunksize[j]) ERR;
}
}
if (nc_close(ncid)) ERR;
/* Open the file and check. */
if (nc_open(options.path, NC_WRITE, &ncid)) ERR;
/* Check the chunking. */
for (i = 0; i < NUM_PLANS; i++)
{
if (nc_inq_var_chunking(ncid, varid[i], &storage, chunksize_in)) ERR;
{
for (j = 0; j < NDIMS_3; j++)
if (chunksize_in[j] != chunksize[j]) ERR;
}
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing that too large chunksizes fail...");
{
#define D_SMALL_LEN2 66
int stat = NC_NOERR;
int ncid;
int small_dimid;
int small_varid;
size_t chunks[1];
/* Create a netcdf-4 file with three dimensions. */
if (nc_create(options.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN2, &small_dimid)) ERR;
/* Add one var. */
if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
/* Attempt to set too large chunksizes */
chunks[0] = D_SMALL_LEN2 + 1;
stat = nc_def_var_chunking(ncid, small_varid, NC_CHUNKED, chunks);
if(stat != NC_EBADCHUNK) {
printf("Return code is '%s', expected NC_BADCHUNK",nc_strerror(stat));
ERR;
}
/* try again with proper chunksize */
chunks[0] = D_SMALL_LEN2;
stat = nc_def_var_chunking(ncid, small_varid, NC_CHUNKED, chunks);
if(stat != NC_NOERR) {
printf("Return code is '%s', expected NC_NOERR",nc_strerror(stat));
ERR;
}
if (nc_abort(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing cache size smaller than chunk size...");
{
#define NDIM2 2
#define DIM_X_LEN 10000
#define DIM_Y_LEN 10000
#define DIM_NAME_X_CACHE_CHUNK "Height"
#define DIM_NAME_Y_CACHE_CHUNK "Width"
#define VAR_NAME_CACHE_CHUNK "House_Size"
#define VAR_NAME_CACHE_CHUNK_2 "Boat_Size"
#define VAR_NAME_CACHE_CHUNK_3 "Deck_Size"
int ncid;
int dimid[NDIM2];
int varid, varid2, varid3;
size_t chunks[NDIM2] = {100, 100};
size_t chunks_big[NDIM2] = {DIM_X_LEN, DIM_Y_LEN};
size_t chunks_in[NDIM2];
int storage;
size_t cache_size = 16;
size_t cache_nelems = 1;
float cache_preemption = 0.5;
size_t cache_size_in;
size_t cache_nelems_in;
float cache_preemption_in;
/* Create a netcdf-4 file with two dimensions. */
if (nc_create(options.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, DIM_NAME_X_CACHE_CHUNK, DIM_X_LEN, &dimid[0])) ERR;
if (nc_def_dim(ncid, DIM_NAME_Y_CACHE_CHUNK, DIM_Y_LEN, &dimid[1])) ERR;
/* Add vars. */
if (nc_def_var(ncid, VAR_NAME_CACHE_CHUNK, NC_INT64, NDIM2, dimid, &varid)) ERR;
if (nc_def_var(ncid, VAR_NAME_CACHE_CHUNK_2, NC_INT64, NDIM2, dimid, &varid2)) ERR;
if (nc_def_var(ncid, VAR_NAME_CACHE_CHUNK_3, NC_INT64, NDIM2, dimid, &varid3)) ERR;
/* Set the var cache. */
if (nc_set_var_chunk_cache(ncid, varid, cache_size, cache_nelems,
cache_preemption)) ERR;
/* Set the chunking. */
if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunks)) ERR;
if (nc_inq_var_chunking(ncid, varid, &storage, chunks_in)) ERR;
if (storage || chunks_in[0] != chunks[0] || chunks_in[1] != chunks[1]) ERR;
if (nc_def_var_chunking(ncid, varid2, NC_CHUNKED, chunks)) ERR;
if (nc_inq_var_chunking(ncid, varid2, &storage, chunks_in)) ERR;
if (storage || chunks_in[0] != chunks[0] || chunks_in[1] != chunks[1]) ERR;
if (nc_def_var_chunking(ncid, varid3, NC_CHUNKED, chunks_big)) ERR;
if (nc_inq_var_chunking(ncid, varid3, &storage, chunks_in)) ERR;
if (storage || chunks_in[0] != chunks_big[0] || chunks_in[1] != chunks_big[1]) ERR;
/* Get the var cache values. */
if (nc_get_var_chunk_cache(ncid, varid, &cache_size_in, &cache_nelems_in,
&cache_preemption_in)) ERR;
if (cache_size_in != cache_size || cache_nelems_in != cache_nelems ||
cache_preemption_in != cache_preemption) ERR;
if (nc_get_var_chunk_cache(ncid, varid2, &cache_size_in, &cache_nelems_in,
&cache_preemption_in)) ERR;
if (cache_size_in != CHUNK_CACHE_SIZE || cache_nelems_in != CHUNK_CACHE_NELEMS ||
cache_preemption_in != CHUNK_CACHE_PREEMPTION) ERR;
/* The cache_size has been increased due to larger chunksizes
* for varid3. */
if (nc_get_var_chunk_cache(ncid, varid3, &cache_size_in, &cache_nelems_in,
&cache_preemption_in)) ERR;
if (cache_nelems_in != CHUNK_CACHE_NELEMS ||
cache_preemption_in != CHUNK_CACHE_PREEMPTION) ERR;
/* printf("cache_size_in %ld\n", cache_size_in); */
/* Close the file. */
if (nc_close(ncid)) ERR;
/* Reopen the file. */
if (nc_open(options.path, NC_NOWRITE, &ncid)) ERR;
/* Close the file. */
if (nc_close(ncid)) ERR;
}
clearoptions();
SUMMARIZE_ERR;
FINAL_RESULTS;
}