/********************************************************************* * * Copyright (C) 2014, Northwestern University and Argonne National Laboratory * See COPYRIGHT notice in top-level directory. * *********************************************************************/ /* $Id$ */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This example shows how to use the vard API ncmpi_put_vard() and * ncmpi_get_vard() to write and read 2D record and fixed-size variables. * * To compile: * mpicc -O2 vard_int.c -o vard_int -lpnetcdf * * Example commands for MPI run and outputs from running ncmpidump on the * NC file produced by this example program: * * % mpiexec -n 4 ./vard_int /pvfs2/wkliao/testfile.nc * * The expected results from the output file contents are: * * % ncmpidump /pvfs2/wkliao/testfile.nc * netcdf testfile { * // file format: CDF-1 * dimensions: * REC_DIM = UNLIMITED ; // (2 currently) * X = 12 ; * FIX_DIM = 2 ; * variables: * int rec_var(REC_DIM, X) ; * int fix_var(FIX_DIM, X) ; * data: * * rec_var = * 0, 1, 2, 100, 101, 102, 200, 201, 202, 300, 301, 302, * 10, 11, 12, 110, 111, 112, 210, 211, 212, 310, 311, 312 ; * * fix_var = * 0, 1, 2, 100, 101, 102, 200, 201, 202, 300, 301, 302, * 10, 11, 12, 110, 111, 112, 210, 211, 212, 310, 311, 312 ; * } * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include /* strcpy(), strncpy() */ #include /* getopt() */ #include #include #define NY 2 #define NX 3 #define ERR {if(err!=NC_NOERR){printf("Error at line %d in %s: %s\n", __LINE__,__FILE__, ncmpi_strerror(err));nerrs++;}} static void usage(char *argv0) { char *help = "Usage: %s [-h] | [-q] [file_name]\n" " [-h] Print help\n" " [-q] Quiet mode (reports when fail)\n" " [filename] output netCDF file name\n"; fprintf(stderr, help, argv0); } /*----< main() >------------------------------------------------------------*/ int main(int argc, char **argv) { extern int optind; char filename[256]; int i, j, err, nerrs=0, ncid, verbose=1, varid0, varid1, dimids[2]; int rank, nprocs, array_of_blocklengths[2], buf[NY][NX]; int array_of_sizes[2], array_of_subsizes[2], array_of_starts[2]; MPI_Offset start[2], count[2], recsize, bufcount, len; MPI_Aint array_of_displacements[2]; MPI_Datatype buftype, rec_filetype, fix_filetype; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* get command-line arguments */ while ((i = getopt(argc, argv, "hq")) != EOF) switch(i) { case 'q': verbose = 0; break; case 'h': default: if (rank==0) usage(argv[0]); MPI_Finalize(); return 1; } if (argv[optind] == NULL) strcpy(filename, "testfile.nc"); else snprintf(filename, 256, "%s", argv[optind]); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (verbose && rank == 0) printf("%s: example of using vard APIs\n",__FILE__); start[0] = 0; start[1] = NX*rank; count[0] = 2; count[1] = NX; /* create a new file for write */ err = ncmpi_create(MPI_COMM_WORLD, filename, NC_CLOBBER, MPI_INFO_NULL, &ncid); ERR /* define a 2D array */ err = ncmpi_def_dim(ncid, "REC_DIM", NC_UNLIMITED, &dimids[0]); ERR err = ncmpi_def_dim(ncid, "X", NX*nprocs, &dimids[1]); ERR err = ncmpi_def_var(ncid, "rec_var", NC_INT, 2, dimids, &varid0); ERR err = ncmpi_def_dim(ncid, "FIX_DIM", 2, &dimids[0]); ERR err = ncmpi_def_var(ncid, "fix_var", NC_INT, 2, dimids, &varid1); ERR err = ncmpi_enddef(ncid); ERR /* create a file type for the record variable */ err = ncmpi_inq_recsize(ncid, &recsize); for (i=0; i 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Finalize(); return (nerrs > 0); }