![]() |
NetCDF
4.9.1-rc1
|
This document attempts to record important information about the internal architecture and operation of the netcdf-c library.
The state of C compiler technology has reached the point where it is possible to include C++ code into the netcdf-c library code base. Two examples are:
However there are some consequences that must be handled for this to work. Specifically, the compiler must be told that the C++ runtime is needed in the following ways.
Suppose we have a flag ENABLE_XXX where that XXX feature entails using C++ code. Then the following must be added to lib_flags.am
The Makefile in which the C++ code is included and compiled (assumed here to be the libxxx directory) must have this set.
It is possible that other values (e.g. -std=c++14) may also work.
For a long time, there have been known problems with the management of complex types containing VLENs. This also involves the string type because it is stored as a VLEN of chars.
The term complex type refers to any type that directly or recursively references a VLEN type. So an array of VLENS, a compound with a VLEN field, and so on.
In order to properly handle instances of these complex types, it is necessary to have function that can recursively walk instances of such types to perform various actions on them. The term "deep" is also used to mean recursive.
Two deep walking operations are provided by the netcdf-c library to aid in managing instances of complex structures.
Previously The netcdf-c library only did shallow free and shallow copy of complex types. This meant that only the top level was properly free'd or copied, but deep internal blocks in the instance were not touched. This led to a host of memory leaks and failures when the deep data was effectively shared between the netcdf-c library internally and the user's data.
Note that the term "vector" is used to mean a contiguous (in memory) sequence of instances of some type. Given an array with, say, dimensions 2 X 3 X 4, this will be stored in memory as a vector of length 2*3*4=24 instances.
The use cases are primarily these.
Suppose one is reading a vector of instances using nc_get_vars (or nc_get_vara or nc_get_var, etc.). These functions will return the vector in the top-level memory provided. All interior blocks (form nested VLEN or strings) will have been dynamically allocated. Note that computing the size of the vector may be tricky because the strides must be taken into account.
After using this vector of instances, it is necessary to free (aka reclaim) the dynamically allocated memory, otherwise a memory leak occurs. So, the recursive reclaim function is used to walk the returned instance vector and do a deep reclaim of the data.
Currently functions are defined in netcdf.h that are supposed to handle this: nc_free_vlen(), nc_free_vlens(), and nc_free_string(). Unfortunately, these functions only do a shallow free, so deeply nested instances are not properly handled by them. They are marked in the description as deprecated in favor of the newer recursive function.
Suppose one is writing a vector of instances using nc_put_vars (or nc_put_vara or nc_put_var, etc.). These functions will write the contents of the vector to the specified variable. Note that internally, the data passed to the nc_put_xxx function is immediately written so there is no need to copy it internally. But the caller may need to reclaim the vector of data that was created and passed in to the nc_put_xxx function.
After writing this vector of instances, and assuming it was dynamically created, at some point it will be necessary to reclaim that data. So again, the recursive reclaim function can be used to walk the returned instance vector and do a deep reclaim of the data.
Suppose one is writing a vector of instances as the data of an attribute using, say, nc_put_att.
Internally, the incoming attribute data must be copied and stored so that changes/reclamation of the input data will not affect the attribute. Note that this copying behavior is different from writing to a variable, where the data is written immediately.
Again, the code inside the netcdf library used to use only shallow copying rather than deep copy. As a result, one saw effects such as described in Github Issue https://github.com/Unidata/netcdf-c/issues/2143.
Also, after defining the attribute, it may be necessary for the user to free the data that was provided as input to nc_put_att() as in the nc_put_xxx functions (previously described).
Suppose one is reading a vector of instances as the data of an attribute using, say, nc_get_att.
Internally, the existing attribute data must be copied and returned to the caller, and the caller is responsible for reclaiming the returned data.
Again, the code inside the netcdf library used to only do shallow copying rather than deep copy. So this could lead to memory leaks and errors because the deep data was shared between the library and the user.
Proper recursive functions were added to the netcdf-c library to provide reclaim and copy functions and use those as needed. These functions are defined in libdispatch/dinstance.c and their signatures are defined in include/netcdf.h. For back compatibility, corresponding "ncaux\_XXX" functions are defined in include/netcdf_aux.h.
There are two variants. The first two, nc_reclaim_data() and nc_copy_data(), assume the top-level vector is managed by the caller. For reclaim, this is so the user can use, for example, a statically allocated vector. For copy, it assumes the user provides the space into which the copy is stored.
The second two, nc_reclaim_data_all() and nc_copy_data_all(), allows the functions to manage the top-level. So for nc_reclaim_data_all, the top level is assumed to be dynamically allocated and will be free'd by nc_reclaim_data_all(). The nc_copy_data_all() function will allocate the top level and return a pointer to it to the user. The user can later pass that pointer to nc_reclaim_data_all() to reclaim the instance(s).
The netcdf-c library internals are changed to use the proper reclaim and copy functions. This also allows some simplification of the code since the stdata and vldata fields of NC_ATT_INFO are no longer needed. Currently this is commented out using the SEPDATA #define macro. When the bugs are found and fixed, all this code will be removed.
In order to make these functions as efficient as possible, it is desirable to classify all types as to whether or not they contain variable-size data. If a type is fixed sized (i.e. does not contain variable-size data) then it can be freed or copied as a single chunk. This significantly increases the performance for such types. For variable-size types, it is necessary to walk each instance of the type and recursively reclaim or copy it. As another optimization, if the type is a vector of strings, then the per-instance walk can be sped up by doing the reclaim or copy inline.
The rules for classifying types as fixed or variable size are as follows.
The classification of types can be made at the time the type is defined or is read in from some existing file. The reclaim and copy functions use this information to speed up the handling of fixed size types.
As described in the companion document – docs/dispatch.md – when nc_create() or nc_open() is called, it must figure out what kind of file is being created or opened. Once it has figured out the file kind, the appropriate "dispatch table" can be used to process that file.
Figuring out the kind of file is referred to as model inference and is, unfortunately, a complicated process. The complication is mostly a result of allowing a path argument to be a URL. Inferring the file kind from a URL requires deep processing of the URL structure: the protocol, the host, the path, and the fragment parts in particular. The query part is currently not used because it usually contains information to be processed by the server receiving the URL.
The "fragment" part of the URL may be unfamiliar. The last part of a URL may optionally contain a fragment, which is syntactically of this form in this pseudo URL specification.
The form of the fragment is similar to a query and takes this general form.
The key is a simple name, the value is any sequence of characters, although URL special characters such as '&' must be URL encoded in the 'XX' form where each X is a hexadecimal digit. An example might look like this non-sensical example:
It is important to note that the fragment part is not intended to be passed to the server, but rather is processed by the client program. It is this property that allows the netcdf-c library to use it to pass information deep into the dispatch table code that is processing the URL.
The inference algorithm is given the following information from which it must determine the kind of file being accessed.
The mode is a set of flags that are passed as the second argument to nc_create and nc_open. The set of flags is define in the netcdf.h header file. Generally it specifies the general format of the file: netcdf-3 (classic) or netcdf-4 (enhanced). Variants of these can also be specified, e.g. 64-bit netcdf-3 or classic netcdf-4. In the case where the path argument is a simple file path, using a mode flag is the most common mechanism for specifying the model.
The file path, the first argument to nc_create and nc_open, Can be either a simple file path or a URL. If it is a URL, then it will be deeply inspected to determine the model.
When the contents of a real file are available, the contents of the file can be used to determine the dispatch table. As a rule, this is likely to be useful only for nc_open. It also requires access to functions that can open and read at least the initial part of the file. As a rule, the initial small prefix of the file is read and examined to see if it matches any of the so-called "magic numbers" that indicate the kind of file being read.
Is the file being opened or is it being created?
Is parallel IO available?
The inferencing algorithm outputs two pieces of information.
The model output is actually a struct containing two fields:
The construction of the model is primarily carried out by the function NC_infermodel() (in libdispatch/dinfermodel.c). It is given the following parameters:
As a rule, these values are used in the this order of preference to infer the model.
The sequence of steps is as follows.
If the path appears to be a URL, then it is parsed and processed by the processuri function as follows.
If the fragment list produced by processuri() is non-empty, then it is processed for "macros". Notice that if the original path was not a URL, then the fragment list is empty and this processing will be bypassed. In any case, It is convenient to allow some singleton fragment keys to be expanded into larger fragment components. In effect, those singletons act as macros. They can help to simplify the user's URL. The term singleton means a fragment key with no associated value: "#bytes", for example.
The list of fragments is searched looking for keys whose value part is NULL or the empty string. Then the table of macros is searched for that key and if found, then a key and values is appended to the fragment list and the singleton is removed.
This function just processes the list of values associated with the "mode" key. It is similar to a macro in that certain mode values are added or removed based on tables of "inferences" and "negations". Again, the purpose is to allow users to provide simplified URL fragments.
The list of mode values is repeatedly searched and whenever a value is found that is in the "modeinferences" table, then the associated inference value is appended to the list of mode values. This process stops when no changes occur. This form of inference allows the user to specify "mode=zarr" and have it converted to "mode=nczarr,zarr". This avoids the need for the dispatch table code to do the same inference.
After the inferences are made, The list of mode values is again repeatedly searched and whenever a value is found that is in the "modenegations" table, then the associated negation value is removed from the list of mode values, assuming it is there. This process stops when no changes occur. This form of inference allows the user to make sure that "mode=bytes,nczarr" has the bytes mode take precedence by removing the "nczarr" value. Such illegal combinations can occur because of previous processing steps.
As the fragment list is processed, duplicates appear with the same key. A function – cleanfragments() – is applied to clean up the fragment list by coalesing the values of duplicate keys and removing duplicate key values.
If the URL is determined to be a reference to a resource on the Amazon S3 cloud, then the URL needs to be converted to what is called "path format". There are four S3 URL formats:
https://<bucket>.s3.<region>.amazonaws.com/<path>
https://s3.<region>.amazonaws.com/<bucket>/<path>
s3://<bucket>/<path>
https://<host>/<bucket>/<path>
The S3 processing converts all of these to the Path format. In the "S3" format case it is necessary to find or default the region from examining the ".aws" directory files.
If the URL protocol is "file" and its path is a relative file path, then it is made absolute by prepending the path of the current working directory.
In any case, after S3 or File rebuilds, the URL is completely rebuilt using any modified protocol, host, path, and fragments. The query is left unchanged in the current algorithm. The resulting rebuilt URL is passed back to the caller.
The set of values of the fragment's "mode" key are processed one by one to see if it is possible to determine the model. There is a table for format interpretations that maps a mode value to the model's implementation and format. So for example, if the mode value "dap2" is encountered, then the model implementation is set to NC_FORMATX_DAP2 and the format is set to NC_FORMAT_CLASSIC.
If processing the mode does not tell us the implementation, then all other fragment keys are processed to see if the implementaton (and format) can be deduced. Currently this does nothing.
If the model is still not determined and the path is a URL, then the implementation is defaulted to DAP2. This is for back compatibility when all URLS implied DAP2.
In the event that the path is not a URL, then it is necessary to use the mode flags and the isparallel arguments to choose a model. This is just a straight forward flag checking exercise.
If the path is being opened (as opposed to created), then it may be possible to actually read the first few bytes of the resource specified by the path and use that to determine the model. If this succeeds, then it takes precedence over all other model inferences.
Once the model is known, then the set of mode flags is modified to be consistent with that information. So for example, if DAP2 is the model, then all netcdf-4 mode flags and some netcdf-3 flags are removed from the set of mode flags because DAP2 provides only a standard netcdf-classic format.
The standard filter system extends the netcdf-c library API to support a fixed set of "standard" filters. This is similar to the way that deflate and szip are currently supported. For background, the file filter.md should be consulted.
In general, the API for a standard filter has the following prototypes. The case of zstandard (libzstd) is used as an example.
So generally the API has the ncid and the varid as fixed, and then a list of parameters specific to the filter – level in this case. For the inquire function, there is an additional argument – has_filterp – that is set to 1 if the filter is defined for the given variable and is 0 if not. The remainder of the inquiry parameters are pointers to memory into which the parameters are stored – levelp in this case.
It is important to note that including a standard filter still requires three supporting objects:
The implementation of a standard filter must be loaded from one of several locations.
However, the three objects listed above need to be stored in the HDF5_PLUGIN_PATH directory, so adding a standard filter still requires modification to the netcdf build system. This limitation may be lifted in the future.
In order to detect a standard library, the following changes must be made for Automake (configure.ac/Makefile.am) and CMake (CMakeLists.txt)
Configure.ac must have a block that similar to this that locates the implementing library.
Note the the entry point (ZSTD_compress) is library dependent and is used to see if the library is available.
It is assumed you have an HDF5 wrapper for zstd. If you want it to be built as part of the netcdf-c library then you need to add the following to netcdf-c/plugins/Makefile.am.
In an analog to configure.ac, a block like this needs to be in netcdf-c/CMakeLists.txt.
The FIND_PACKAGE requires a CMake module for the filter in the cmake/modules directory. The set_std_filter function is a macro.
An entry in the file config.h.cmake.in will also be needed.
As a template, here is the implementation for zstandard. It can be used as the template for adding other standard filters. It is currently located in netcdf-d/libdispatch/dfilter.c, but could be anywhere as indicated above.
Author: Dennis Heimbigner
Email: dmh at ucar dot edu
Initial Version: 12/22/2021
Last Revised: 01/25/2022