netcdf-c/nc_test/tst_parallel2.c

257 lines
7.0 KiB
C
Raw Normal View History

2018-12-07 05:56:42 +08:00
/*! \file
Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
2015, 2016, 2017, 2018
University Corporation for Atmospheric Research/Unidata.
See \ref copyright file for more info.
*/
/* This is a benchmarking program for netCDF-4 parallel I/O. */
2010-06-03 21:24:43 +08:00
/* Defining USE_MPE causes the MPE trace library to be used (and you
* must also relink with -llmpe -lmpe). This causes clog2 output to be
* written, which can be converted to slog2 (by the program
* clog2TOslog2) and then used in the analysis program jumpshot. */
/*#define USE_MPE 1*/
#include <nc_tests.h>
#include "err_macros.h"
2010-06-03 21:24:43 +08:00
#include <mpi.h>
#include <pnetcdf.h>
#ifdef USE_MPE
#include <mpe.h>
#endif /* USE_MPE */
#undef DEBUG
2010-06-03 21:24:43 +08:00
#define FILE_NAME "tst_parallel2.nc"
#define NDIMS 3
#define DIMSIZE 8
#define NUM_SLABS 8
#define DIM1_NAME "slab"
#define DIM2_NAME "x"
#define DIM3_NAME "y"
#define VAR_NAME "Bond_James_Bond"
int
main(int argc, char **argv)
{
/* MPI stuff. */
2018-12-07 05:56:42 +08:00
int mpi_namelen;
2010-06-03 21:24:43 +08:00
char mpi_name[MPI_MAX_PROCESSOR_NAME];
int mpi_size, mpi_rank;
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Info info = MPI_INFO_NULL;
double start_time = 0, total_time;
/* Netcdf-4 stuff. */
int ncid, varid, dimids[NDIMS];
size_t start[NDIMS] = {0, 0, 0};
size_t count[NDIMS] = {1, DIMSIZE, DIMSIZE};
int data[DIMSIZE * DIMSIZE], data_in[DIMSIZE * DIMSIZE];
int j, i;
2018-12-07 05:56:42 +08:00
2010-06-03 21:24:43 +08:00
char file_name[NC_MAX_NAME + 1];
int ndims_in, nvars_in, natts_in, unlimdimid_in;
#ifdef USE_MPE
int s_init, e_init, s_define, e_define, s_write, e_write, s_close, e_close;
#endif /* USE_MPE */
/* 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);
#ifdef DEBUG
printf("mpi_name: %s size: %d rank: %d\n", mpi_name, mpi_size, mpi_rank);
#endif
2010-06-03 21:24:43 +08:00
/* Must be able to evenly divide my slabs between processors. */
if (NUM_SLABS % mpi_size != 0)
{
2018-12-07 05:56:42 +08:00
if (!mpi_rank) printf("NUM_SLABS (%d) is not evenly divisible by mpi_size(%d)\n",
2010-06-03 21:24:43 +08:00
NUM_SLABS, mpi_size);
ERR;
}
#ifdef USE_MPE
MPE_Init_log();
2018-12-07 05:56:42 +08:00
s_init = MPE_Log_get_event_number();
e_init = MPE_Log_get_event_number();
s_define = MPE_Log_get_event_number();
e_define = MPE_Log_get_event_number();
s_write = MPE_Log_get_event_number();
e_write = MPE_Log_get_event_number();
s_close = MPE_Log_get_event_number();
e_close = MPE_Log_get_event_number();
s_open = MPE_Log_get_event_number();
e_open = MPE_Log_get_event_number();
2010-06-03 21:24:43 +08:00
MPE_Describe_state(s_init, e_init, "Init", "red");
MPE_Describe_state(s_define, e_define, "Define", "yellow");
MPE_Describe_state(s_write, e_write, "Write", "green");
MPE_Describe_state(s_close, e_close, "Close", "purple");
MPE_Describe_state(s_open, e_open, "Open", "blue");
MPE_Start_log();
MPE_Log_event(s_init, 0, "start init");
#endif /* USE_MPE */
#ifdef DEBUG
if (!mpi_rank)
{
printf("\n*** Testing parallel I/O some more.\n");
printf("*** writing a %d x %d x %d file from %d processors...\n",
NUM_SLABS, DIMSIZE, DIMSIZE, mpi_size);
}
#endif
2010-06-03 21:24:43 +08:00
/* We will write the same slab over and over. */
for (i = 0; i < DIMSIZE * DIMSIZE; i++)
data[i] = mpi_rank;
#ifdef USE_MPE
MPE_Log_event(e_init, 0, "end init");
MPE_Log_event(s_define, 0, "start define file");
#endif /* USE_MPE */
/* Create a parallel netcdf-4 file. */
sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME);
#ifdef DEBUG
fprintf(stderr,"create: file_name=%s\n",file_name);
#endif
2018-09-23 09:22:34 +08:00
if (nc_create_par(file_name, 0, comm, info, &ncid)) ERR;
2010-06-03 21:24:43 +08:00
/* A global attribute holds the number of processors that created
* the file. */
if (nc_put_att_int(ncid, NC_GLOBAL, "num_processors", NC_INT, 1, &mpi_size)) ERR;
/* Create three dimensions. */
if (nc_def_dim(ncid, DIM1_NAME, NUM_SLABS, dimids)) ERR;
if (nc_def_dim(ncid, DIM2_NAME, DIMSIZE, &dimids[1])) ERR;
if (nc_def_dim(ncid, DIM3_NAME, DIMSIZE, &dimids[2])) ERR;
/* Create one var. */
if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIMS, dimids, &varid)) ERR;
/* Write metadata to file. */
if (nc_enddef(ncid)) ERR;
#ifdef USE_MPE
MPE_Log_event(e_define, 0, "end define file");
if (mpi_rank)
sleep(mpi_rank);
#endif /* USE_MPE */
#ifdef USE_PNETCDF
/* if (nc_var_par_access(ncid, NC_GLOBAL, NC_COLLECTIVE)) ERR;*/
if (nc_var_par_access(ncid, NC_GLOBAL, NC_INDEPENDENT)) ERR;
#else
2010-06-03 21:24:43 +08:00
/* if (nc_var_par_access(ncid, varid, NC_COLLECTIVE)) ERR;*/
if (nc_var_par_access(ncid, varid, NC_INDEPENDENT)) ERR;
#endif
2010-06-03 21:24:43 +08:00
if (!mpi_rank)
start_time = MPI_Wtime();
/* Write all the slabs this process is responsible for. */
for (i = 0; i < NUM_SLABS / mpi_size; i++)
{
start[0] = NUM_SLABS / mpi_size * mpi_rank + i;
#ifdef USE_MPE
MPE_Log_event(s_write, 0, "start write slab");
#endif /* USE_MPE */
/* Write one slab of data. */
if (nc_put_vara_int(ncid, varid, start, count, data)) ERR;
#ifdef USE_MPE
MPE_Log_event(e_write, 0, "end write file");
#endif /* USE_MPE */
}
if (!mpi_rank)
{
total_time = MPI_Wtime() - start_time;
/* printf("num_proc\ttime(s)\n");*/
printf("%d\t%g\t%g\n", mpi_size, total_time, DIMSIZE * DIMSIZE * NUM_SLABS * sizeof(int) / total_time);
}
#ifdef USE_MPE
MPE_Log_event(s_close, 0, "start close file");
#endif /* USE_MPE */
/* Close the netcdf file. */
if (nc_close(ncid)) ERR;
2018-12-07 05:56:42 +08:00
2010-06-03 21:24:43 +08:00
#ifdef USE_MPE
MPE_Log_event(e_close, 0, "end close file");
#endif /* USE_MPE */
/* Reopen the file and check it. */
#ifdef DEBUG
fprintf(stderr,"open: file_name=%s\n",file_name);
#endif
2018-09-23 09:22:34 +08:00
if (nc_open_par(file_name, NC_NOWRITE, comm, info, &ncid)) ERR;
2010-06-03 21:24:43 +08:00
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
2018-12-07 05:56:42 +08:00
if (ndims_in != NDIMS || nvars_in != 1 || natts_in != 1 ||
2010-06-03 21:24:43 +08:00
unlimdimid_in != -1) ERR;
/* Read all the slabs this process is responsible for. */
for (i = 0; i < NUM_SLABS / mpi_size; i++)
{
start[0] = NUM_SLABS / mpi_size * mpi_rank + i;
#ifdef USE_MPE
MPE_Log_event(s_read, 0, "start read slab");
#endif /* USE_MPE */
/* Read one slab of data. */
if (nc_get_vara_int(ncid, varid, start, count, data_in)) ERR;
2018-12-07 05:56:42 +08:00
2010-06-03 21:24:43 +08:00
/* Check data. */
for (j = 0; j < DIMSIZE * DIMSIZE; j++)
2018-12-07 05:56:42 +08:00
if (data_in[j] != mpi_rank)
2010-06-03 21:24:43 +08:00
{
ERR;
break;
}
#ifdef USE_MPE
MPE_Log_event(e_read, 0, "end read file");
#endif /* USE_MPE */
}
#ifdef USE_MPE
MPE_Log_event(s_close, 0, "start close file");
#endif /* USE_MPE */
/* Close the netcdf file. */
if (nc_close(ncid)) ERR;
2018-12-07 05:56:42 +08:00
2010-06-03 21:24:43 +08:00
#ifdef USE_MPE
MPE_Log_event(e_close, 0, "end close file");
#endif /* USE_MPE */
2017-10-26 03:07:17 +08:00
MPI_Barrier(MPI_COMM_WORLD);
if (mpi_rank == 0)
remove(file_name);
/* Shut down MPI. */
MPI_Finalize();
#ifdef DEBUG
if (!mpi_rank)
{
SUMMARIZE_ERR;
FINAL_RESULTS;
}
#endif
2010-06-03 21:24:43 +08:00
return total_err;
}