2012 Unidata NetCDF Workshop > Parallel I/O with NetCDF
35.7 Parallel I/O Example
This example is from nc_test4/tst_parallel.c, and is run by make check
on platforms with MPI/IO support.
/* Initialize MPI. */
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
MPI_Get_processor_name(mpi_name, &mpi_namelen);
if (mpi_rank == 1)
printf("\n*** tst_parallel testing very basic parallel access.\n");
/* Create a parallel netcdf-4 file. */
if ((res = nc_create_par(FILE, NC_NETCDF4|NC_MPIIO, comm,
info, &ncid))) ERR;
/* Create two dimensions. */
if ((res = nc_def_dim(ncid, "d1", DIMSIZE, dimids))) ERR;
if ((res = nc_def_dim(ncid, "d2", DIMSIZE, &dimids[1]))) ERR;
/* Create one var. */
if ((res = nc_def_var(ncid, "v1", NC_INT, NDIMS, dimids, &v1id))) ERR;
if ((res = nc_enddef(ncid))) ERR;
/* Set up slab for this process. */
start[0] = mpi_rank * DIMSIZE/mpi_size;
start[1] = 0;
count[0] = DIMSIZE/mpi_size;
count[1] = DIMSIZE;
/* Create phoney data. We're going to write a 24x24 array of ints,
in 4 sets of 144. */
for (i=mpi_rank*QTR_DATA; i < (mpi_rank+1)*QTR_DATA; i++)
data[i] = mpi_rank;
/*if ((res = nc_var_par_access(ncid, v1id, NC_COLLECTIVE)))
ERR;*/
if ((res = nc_var_par_access(ncid, v1id, NC_INDEPENDENT))) ERR;
/* Write slabs of phoney data. */
if ((res = nc_put_vara_int(ncid, v1id, start, count,
&data[mpi_rank*QTR_DATA]))) ERR;
/* Close the netcdf file. */
if ((res = nc_close(ncid))) ERR;
/* Shut down MPI. */
MPI_Finalize();
if (mpi_rank == 1)
{
SUMMARIZE_ERR;
FINAL_RESULTS;
}
return 0;
2012 Unidata NetCDF Workshop > Parallel I/O with NetCDF