1 /* Copyright 2007-2018, UCAR/Unidata. See COPYRIGHT file for copying
2 and redistribution conditions.
3
4 This is part of the netCDF package.
5
6 This is a benchmarking program for netCDF-4 parallel I/O.
7 */
8
9 /* Defining USE_MPE causes the MPE trace library to be used (and you
10 * must also relink with -llmpe -lmpe). This causes clog2 output to be
11 * written, which can be converted to slog2 (by the program
12 * clog2TOslog2) and then used in the analysis program jumpshot. */
13 /*#define USE_MPE 1*/
14
15 #include <nc_tests.h>
16 #include "err_macros.h"
17 #include <mpi.h>
18 #ifdef USE_MPE
19 #include <mpe.h>
20 #endif /* USE_MPE */
21
22 #define FILE_NAME "tst_parallel4.nc"
23 #define NDIMS 3
24 #define DIMSIZE 512
25 #define NUM_SLABS 1024
26 #define DIM1_NAME "slab"
27 #define DIM2_NAME "x"
28 #define DIM3_NAME "y"
29 #define VAR_NAME "Bond_James_Bond"
30
31 int
main(int argc,char ** argv)32 main(int argc, char **argv)
33 {
34 /* MPI stuff. */
35 int mpi_namelen;
36 char mpi_name[MPI_MAX_PROCESSOR_NAME];
37 int mpi_size, mpi_rank;
38 MPI_Comm comm = MPI_COMM_WORLD;
39 MPI_Info info = MPI_INFO_NULL;
40 double start_time = 0, total_time;
41
42 /* Netcdf-4 stuff. */
43 int ncid, varid, dimids[NDIMS];
44 size_t start[NDIMS] = {0, 0, 0};
45 size_t count[NDIMS] = {1, DIMSIZE, DIMSIZE};
46 int data[DIMSIZE * DIMSIZE], data_in[DIMSIZE * DIMSIZE];
47 int j, i, ret;
48
49 char file_name[NC_MAX_NAME + 1];
50 int ndims_in, nvars_in, natts_in, unlimdimid_in;
51
52 #ifdef USE_MPE
53 int s_init, e_init, s_define, e_define, s_write, e_write, s_close, e_close;
54 #endif /* USE_MPE */
55
56 /* Initialize MPI. */
57 MPI_Init(&argc,&argv);
58 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
59 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
60 MPI_Get_processor_name(mpi_name, &mpi_namelen);
61 /*printf("mpi_name: %s size: %d rank: %d\n", mpi_name, mpi_size, mpi_rank);*/
62
63 /* Must be able to evenly divide my slabs between processors. */
64 if (NUM_SLABS % mpi_size != 0)
65 {
66 if (!mpi_rank) printf("NUM_SLABS (%d) is not evenly divisible by mpi_size(%d)\n",
67 NUM_SLABS, mpi_size);
68 ERR;
69 }
70
71 #ifdef USE_MPE
72 MPE_Init_log();
73 s_init = MPE_Log_get_event_number();
74 e_init = MPE_Log_get_event_number();
75 s_define = MPE_Log_get_event_number();
76 e_define = MPE_Log_get_event_number();
77 s_write = MPE_Log_get_event_number();
78 e_write = MPE_Log_get_event_number();
79 s_close = MPE_Log_get_event_number();
80 e_close = MPE_Log_get_event_number();
81 s_open = MPE_Log_get_event_number();
82 e_open = MPE_Log_get_event_number();
83 MPE_Describe_state(s_init, e_init, "Init", "red");
84 MPE_Describe_state(s_define, e_define, "Define", "yellow");
85 MPE_Describe_state(s_write, e_write, "Write", "green");
86 MPE_Describe_state(s_close, e_close, "Close", "purple");
87 MPE_Describe_state(s_open, e_open, "Open", "blue");
88 MPE_Start_log();
89 MPE_Log_event(s_init, 0, "start init");
90 #endif /* USE_MPE */
91
92 /* if (!mpi_rank) */
93 /* { */
94 /* printf("\n*** Testing parallel I/O some more.\n"); */
95 /* printf("*** writing a %d x %d x %d file from %d processors...\n", */
96 /* NUM_SLABS, DIMSIZE, DIMSIZE, mpi_size); */
97 /* } */
98
99 /* We will write the same slab over and over. */
100 for (i = 0; i < DIMSIZE * DIMSIZE; i++)
101 data[i] = mpi_rank;
102
103 #ifdef USE_MPE
104 MPE_Log_event(e_init, 0, "end init");
105 MPE_Log_event(s_define, 0, "start define file");
106 #endif /* USE_MPE */
107
108 /* Create a parallel netcdf-4 file. */
109 sprintf(file_name, "%s/%s", TEMP_LARGE, FILE_NAME);
110 if (nc_create_par(file_name, NC_NETCDF4, comm, info, &ncid)) ERR;
111
112 /* A global attribute holds the number of processors that created
113 * the file. */
114 if (nc_put_att_int(ncid, NC_GLOBAL, "num_processors", NC_INT, 1, &mpi_size)) ERR;
115
116 /* Create three dimensions. */
117 if (nc_def_dim(ncid, DIM1_NAME, NUM_SLABS, dimids)) ERR;
118 if (nc_def_dim(ncid, DIM2_NAME, DIMSIZE, &dimids[1])) ERR;
119 if (nc_def_dim(ncid, DIM3_NAME, DIMSIZE, &dimids[2])) ERR;
120
121 /* Create one var. */
122 if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIMS, dimids, &varid)) ERR;
123
124 /* Write metadata to file. */
125 if (nc_enddef(ncid)) ERR;
126
127 #ifdef USE_MPE
128 MPE_Log_event(e_define, 0, "end define file");
129 if (mpi_rank)
130 sleep(mpi_rank);
131 #endif /* USE_MPE */
132
133 /* if (nc_var_par_access(ncid, varid, NC_COLLECTIVE)) ERR;*/
134 /* if (nc_var_par_access(ncid, varid, NC_INDEPENDENT)) ERR;*/
135
136 if (!mpi_rank)
137 start_time = MPI_Wtime();
138
139 /* Write all the slabs this process is responsible for. */
140 for (i = 0; i < NUM_SLABS / mpi_size; i++)
141 {
142 start[0] = NUM_SLABS / mpi_size * mpi_rank + i;
143
144 #ifdef USE_MPE
145 MPE_Log_event(s_write, 0, "start write slab");
146 #endif /* USE_MPE */
147
148 /* Write one slab of data. */
149 if (nc_put_vara_int(ncid, varid, start, count, data)) ERR;
150
151 #ifdef USE_MPE
152 MPE_Log_event(e_write, 0, "end write file");
153 #endif /* USE_MPE */
154 }
155
156 if (!mpi_rank)
157 {
158 total_time = MPI_Wtime() - start_time;
159 /* printf("num_proc\ttime(s)\n");*/
160 printf("%d\t%g\t%g\n", mpi_size, total_time, DIMSIZE * DIMSIZE * NUM_SLABS * sizeof(int) / total_time);
161 }
162
163 #ifdef USE_MPE
164 MPE_Log_event(s_close, 0, "start close file");
165 #endif /* USE_MPE */
166
167 /* Close the netcdf file. */
168 if (nc_close(ncid)) ERR;
169
170 #ifdef USE_MPE
171 MPE_Log_event(e_close, 0, "end close file");
172 #endif /* USE_MPE */
173
174 /* Reopen the file and check it. */
175 if ((ret = nc_open_par(file_name, NC_NOWRITE, comm, info, &ncid)))
176 {
177 printf("ret = %d\n", ret);
178 ERR_RET;
179 }
180 if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
181 if (ndims_in != NDIMS || nvars_in != 1 || natts_in != 1 ||
182 unlimdimid_in != -1) ERR;
183
184 /* Read all the slabs this process is responsible for. */
185 for (i = 0; i < NUM_SLABS / mpi_size; i++)
186 {
187 start[0] = NUM_SLABS / mpi_size * mpi_rank + i;
188
189 #ifdef USE_MPE
190 MPE_Log_event(s_read, 0, "start read slab");
191 #endif /* USE_MPE */
192
193 /* Read one slab of data. */
194 if (nc_get_vara_int(ncid, varid, start, count, data_in)) ERR;
195
196 /* Check data. */
197 for (j = 0; j < DIMSIZE * DIMSIZE; j++)
198 if (data_in[j] != mpi_rank)
199 {
200 ERR;
201 break;
202 }
203
204 #ifdef USE_MPE
205 MPE_Log_event(e_read, 0, "end read file");
206 #endif /* USE_MPE */
207 }
208
209 #ifdef USE_MPE
210 MPE_Log_event(s_close, 0, "start close file");
211 #endif /* USE_MPE */
212
213 /* Close the netcdf file. */
214 if (nc_close(ncid)) ERR;
215
216 #ifdef USE_MPE
217 MPE_Log_event(e_close, 0, "end close file");
218 #endif /* USE_MPE */
219
220 /* Delete this large file. */
221 remove(file_name);
222
223 /* Shut down MPI. */
224 MPI_Finalize();
225
226 /* if (!mpi_rank) */
227 /* { */
228 /* SUMMARIZE_ERR; */
229 /* FINAL_RESULTS; */
230 /* } */
231 return total_err;
232 }
233