Next: , Previous: , Up: Variables   [Index]


4.13 Write a Mapped Array of Values: ncmpi_put_varm_<type>

The ncmpi_put_varm_<type> family of functions writes a mapped array section of values into a netCDF variable of an opened netCDF file. The mapped array section is specified by giving a corner, a vector of counts, a stride vector, and an index mapping vector. The index mapping vector is a vector of integers that specifies the mapping between the dimensions of a netCDF variable and the in-memory structure of the internal data array. No assumptions are made about the ordering or length of the dimensions of the data array.

The most common use case for this API is to write a matrix-transposed array. See an example below.

Data types

<type > for API names <C type> for API arguments
text char
schar signed char
short short
int int
float float
double double
uchar unsigned char
ushort unsigned short
uint unsigned int
longlong long long
ulonglong unsigned longlong

Operational Mode

These API must be called while the file is in data mode.

Collective I/O

The corresponding collective APIs have the suffix name "_all" and must be called in collective data mode.

Usage

int ncmpi_put_varm_<type>     (int                  ncid,
                                int                 varid,
                                const MPI_Offset    start[],
                                const MPI_Offset    count[],
                                const MPI_Offset    stride[],
                                const MPI_Offset    imap[],
                                const <C type>     *buf);

int ncmpi_put_varm             (int                 ncid,
                                int                 varid,
                                const MPI_Offset    start[],
                                const MPI_Offset    count[],
                                const MPI_Offset    stride[],
                                const MPI_Offset    imap[],
                                const void         *buf,
                                MPI_Offset          bufcount,
                                MPI_Datatype        buftype);

int ncmpi_put_varm_<type>_all (int                  ncid,
                                int                 varid,
                                const MPI_Offset    start[],
                                const MPI_Offset    count[],
                                const MPI_Offset    stride[],
                                const MPI_Offset    imap[],
                                const <C type>     *buf);

int ncmpi_put_varm_all         (int                 ncid,
                                int                 varid,
                                const MPI_Offset    start[],
                                const MPI_Offset    count[],
                                const MPI_Offset    stride[],
                                const MPI_Offset    imap[],
                                const void         *buf,
                                MPI_Offset          bufcount,
                                MPI_Datatype        buftype);
ncid

NetCDF ID, from a previous call to ncmpi_open or ncmpi_create.

varid

Variable ID. Different MPI processes may use different variable IDs.

start

A vector of MPI_Offset integers specifying the index in the variable where the first of the data values will be written. The indices are relative to 0, so for example, the first data value of a variable would have index (0, 0, ... , 0). The elements of start correspond, in order, to the variable’s dimensions. Hence, if the variable is a record variable, the first index corresponds to the starting record number for writing the data values.

count

A vector of MPI_Offset integers specifying the number of indices selected along each dimension. To write a single value, for example, specify count as (1, 1, ... , 1). The elements of count correspond, in order, to the variable’s dimensions. Hence, if the variable is a record variable, the first element of count corresponds to a count of the number of records to write.

stride

A vector of MPI_Offset integers that specifies the sampling interval along each dimension of the netCDF variable. The elements of the stride vector correspond, in order, to the netCDF variable’s dimensions (stride[0] gives the sampling interval along the most slowly varying dimension of the netCDF variable). Sampling intervals are specified in type-independent units of elements (a value of 1 selects consecutive elements of the netCDF variable along the corresponding dimension, a value of 2 selects every other element, etc.). A NULL stride argument is treated as (1, 1, ... , 1).

imap

A vector of MPI_Offset integers that specifies the mapping between the dimensions of a netCDF variable and the in-memory structure of the internal data array. The elements of the index mapping vector correspond, in order, to the netCDF variable’s dimensions (imap[0] gives the distance between elements of the internal array corresponding to the most slowly varying dimension of the netCDF variable). Distances between elements are specified in type-independent units of elements (the distance between internal elements that occupy adjacent memory locations is 1 and not the element’s byte-length as in netCDF 2). A NULL argument means the memory-resident values have the same structure as the associated netCDF variable.

buf

Pointer to the location used for computing where the data values will be found; the data should be of the type appropriate for the function called. If the type of data values differs from the netCDF variable type, type conversion will occur.

bufcount

An integer indicates the number of MPI derived data type elements in the buf to be written to the file.

buftype

An MPI derived data type that describes the memory layout of buf. Starting from PnetCDF version 1.6.0, buftype can be MPI_DATATYPE_NULL. In this case, bufcount is ignored and the buf’s data type must match the type of the variable defined in the file - no data conversion will be done.

Layout Illustration for Write Buffer in Memory and Data Written in File

put_varm mapping

Return Error Codes

ncmpi_put_varm_<type> returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

The following imap vector maps in the trivial way a 4x3x2 netCDF variable and an internal array of the same shape:

float a[4][3][2];       /* same shape as netCDF variable */
int   imap[3] = {6, 2, 1};
                        /* netCDF dimension       inter-element distance */
                        /* ----------------       ---------------------- */
                        /* most rapidly varying       1                  */
                        /* intermediate               2 (=imap[2]*2)     */
                        /* most slowly varying        6 (=imap[1]*3)     */

Using the imap vector above with ncmpi_put_varm_float_all obtains the same result as simply using ncmpi_put_var_float.

Here is an example of using ncmpi_put_varm_float_all to write – from a transposed, internal array – a netCDF variable named rh which is described by the C declaration float rh[6][4] (note the size and order of the dimensions):

#include <pnetcdf.h>
   ... 
#define NDIM 2                    /* rank of netCDF variable */
int ncid;                         /* netCDF ID */
int status;                       /* error status */
int rhid;                         /* variable ID */
MPI_Offset start[NDIM] = {0, 0};  /* netCDF variable start point: first element */
MPI_Offset count[NDIM] = {6, 4};  /* size of internal array: entire netCDF variable; order corresponds to netCDF variable -- not internal array */
MPI_Offset stride[NDIM] = {1, 1}; /* variable subsampling intervals: sample every netCDF element */
MPI_Offset imap[NDIM] = {1, 6};   /* internal array inter-element distances; would be {4, 1} if not transposing */
float buf[4][6];                  /* note transposition of netCDF variable dimensions */
   ... 
status = ncmpi_open(MPI_COMM_WORLD, "foo.nc", NC_WRITE, MPI_INFO_NULL,  &ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = ncmpi_inq_varid(ncid, "rh", &rhid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = ncmpi_put_varm_float_all(ncid, rhid, start, count, stride, imap, buf);
if (status != NC_NOERR) handle_error(status);

   In memory:                   In file:
     buf is a memory buffer       rh is a netCDF variable

     buf[4][6] = a b c d e f      rh[6][4] =  a g m s
                 g h i j k l                  b h n t
                 m n o p q r                  c i o u
                 s t u v w x                  d j p v
                                              e k q w
                                              f l r x

Here is another example of using ncmpi_put_varm_float_all to write – from a transposed, internal array – a subsample of the same netCDF variable, by writing every other point of the netCDF variable:

#include <pnetcdf.h>
   ... 
#define NDIM 2                    /* rank of netCDF variable */
int ncid;                         /* netCDF ID */
int status;                       /* error status */
int rhid;                         /* variable ID */
MPI_Offset start[NDIM] = {0, 0};  /* netCDF variable start point: first element */
MPI_Offset count[NDIM] = {3, 2};  /* size of internal array: entire (subsampled) netCDF variable; order of dimensions corresponds to netCDF variable -- not internal array */
MPI_Offset stride[NDIM] = {2, 2}; /* variable subsampling intervals: sample every other netCDF element */
MPI_Offset imap[NDIM] = {1, 3};   /* internal array inter-element distances; would be {2, 1} if not transposing */
float buf[2][3];                  /* note transposition of (subsampled) netCDF variable dimensions */
   ... 
status = ncmpi_open(MPI_COMM_WORLD, "foo.nc", NC_WRITE, MPI_INFO_NULL,  &ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = ncmpi_inq_varid(ncid, "rh", &rhid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = ncmpi_put_varm_float_all(ncid, rhid, start, count, stride, imap, buf);
if (status != NC_NOERR) handle_error(status);

   In memory:                   In file:
     buf is a memory buffer       rh is a netCDF variable

     buf[2][3] = a b c            rh[6][4] =  a _ d _
                 d e f                        _ _ _ _
                                              b _ e _
                                              _ _ _ _
                                              c _ f _
                                              _ _ _ _

Full example C program


Next: , Previous: , Up: Variables   [Index]