forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testnc3perf.c
496 lines (427 loc) · 12.5 KB
/
testnc3perf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*********************************************************************
* Copyright 1989, University Corporation for Atmospheric Research
* See netcdf/README file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/nctest/nctime.c,v 1.12 1996/04/30 17:56:58 davis Exp $
*********************************************************************/
/*
* This is a standalone benchmark program for timing netCDF hyperslab accesses.
* Once it is built, the benchmarks are run by invoking it with the shape of a
* four-dimensional netCDF variable, e.g.
*
* nctime 10 20 30 40
*
* which will run timing benchmarks accessing 1-, 2-, 3-, and 4-dimensional
* slabs from 10 by 20 by 30 by 40 variables of each type. The first dimension
* varies most slowly and is an unlimited (record) dimension.
*
* This program is especially useful for testing the effect of various compiler
* optimization levels or platform-specific optimizations on the performance of
* netCDF I/O.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/param.h> /* for HZ */
#include <sys/times.h>
#include <assert.h>
#include <time.h>
#ifndef HZ
#ifdef CLK_TCK
#define HZ CLK_TCK
#else
#define HZ 60
#endif
#endif
#include "netcdf.h"
struct ncdim { /* dimension */
char *name;
long size;
};
struct ncvar { /* variable */
char *name;
nc_type type;
int ndims;
int *dims;
int natts;
};
#define LEN_OF(array) ((sizeof array) / (sizeof array[0]))
/* Number of dimensions. Changing this requires other changes as well. */
#define NDIMS 4
#define NVARS 6 /* number of variables, one for each type */
/* Any function that maps dimension values 1-1 to values is OK here */
#define VF(w) 1000*w[0]+100*w[1]+10*w[2]+w[3]
static int DEFAULTDIMS[NDIMS] = {10, 20, 30, 40};
/*
* Fill typed array element with specified value, that is
*
* v[ii] = val;
*/
static void
val_stuff(type, v, ii, val) /* v[ii] = val */
nc_type type; /* netcdf type of v, NC_BYTE, ..., NC_DOUBLE */
void *v; /* array of specified type */
int ii; /* it's v[ii] we want to store into */
long val; /* value to store */
{
union gp {
char cp[1];
short sp[1];
nclong lp[1];
float fp[1];
double dp[1];
} *gp;
gp = (union gp *) v;
switch (type) {
case NC_BYTE:
case NC_CHAR:
gp->cp[ii] = (char) val;
break;
case NC_SHORT:
gp->sp[ii] = (short) val;
break;
case NC_LONG:
gp->lp[ii] = (nclong) val;
break;
case NC_FLOAT:
gp->fp[ii] = (float) val;
break;
case NC_DOUBLE:
gp->dp[ii] = (double) val;
break;
}
}
/*
* Compare typed array element with specified value, that is return
*
* (v[ii] != val)
*
* returns 0 if equal, 1 if not equal
*/
static int
val_diff(type, v, ii, val) /* v[ii] != val */
nc_type type; /* netcdf type of v, NC_BYTE, ..., NC_DOUBLE */
void *v; /* array of specified type */
int ii; /* it's v[ii] we want to compare */
long val; /* value to compare with */
{
union gp {
char cp[1];
short sp[1];
nclong lp[1];
float fp[1];
double dp[1];
} *gp;
gp = (union gp *) v;
switch (type) {
case NC_BYTE:
case NC_CHAR:
return (gp->cp[ii] != (char) val);
case NC_SHORT:
return (gp->sp[ii] != (short) val);
case NC_LONG:
return (gp->lp[ii] != (nclong) val);
case NC_FLOAT:
return (gp->fp[ii] != (float) val);
case NC_DOUBLE:
return (gp->dp[ii] != (double) val);
}
/* NOTREACHED */
return 0;
}
/*
* The following timing macros can be used by including the necessary
* declarations with
*
* TIMING_DECLS ;
*
* and surrounding sections of code to be timed with the "statements"
*
* TIMING_START ;
* [code to be timed goes here]
* TIMING_END ;
*
* (The terminating semicolon is required for TIMING_DECLS and TIMING_END.)
* The macros assume the user has stored a description of what is being timed
* in the user-declared string time_mess, and has included <sys/times.h>
*/
#define TIMING_DECLS \
long TMreps; /* counts repetitions of timed code */ \
long TMrepeats; /* repetitions needed to exceed a second */ \
clock_t TMus, TMsy; /* user and system time in clock ticks */ \
float TMelapsed; /* elapsed time in seconds */ \
struct tms TMru;
#define TIMING_START \
TMrepeats = 1; \
do { /* loop enough times for at least 0.1 second elapsed time */ \
TMrepeats *= 2; \
times(&TMru); \
TMus = TMru.tms_utime; \
TMsy = TMru.tms_stime; \
for(TMreps=0;TMreps < TMrepeats;TMreps++) {
#define TIMING_END \
} \
times(&TMru); \
TMus = TMru.tms_utime - TMus; \
TMsy = TMru.tms_stime - TMsy; \
TMelapsed= (float) (TMus+TMsy) / (float) HZ; \
if (TMreps < TMrepeats) break; \
} while (TMelapsed < 0.1 ); \
printf("time for %-20.20s %10.3f msec\n", \
time_mess, TMelapsed*1000./(TMreps+1))
/*
* For each type of variable, put a four-dimensional hypercube of values
* with a single call to ncvarput. Then use ncvarget to retrieve a single
* value, a vector of values along each of the four dimensions, a plane of
* values along each of the six pairs of dimensions, a cube of values along
* each of the four triples of dimensions, and all the values.
*/
void
test_slabs(ncid, sizes)
int ncid; /* handle of netcdf open and in data mode */
int *sizes; /* dimension sizes */
{
char time_mess[100];
struct ncdim dims[NDIMS];
int dimids[NDIMS]; /* dimension ids */
long corner[NDIMS], edge[NDIMS], point[NDIMS];
static struct ncvar va[NVARS] = { /* variables of all types */
{"byte_var", NC_BYTE, NDIMS, 0, 0},
{"char_var", NC_CHAR, NDIMS, 0, 0},
{"short_var", NC_SHORT, NDIMS, 0, 0},
{"long_var", NC_LONG, NDIMS, 0, 0},
{"float_var", NC_FLOAT, NDIMS, 0, 0},
{"double_var", NC_DOUBLE, NDIMS, 0, 0},
};
void *v;
int varid[NVARS], iv; /* variable id */
int idim, jdim, kdim, ldim;
int iw, ix, iy, iz, ii, jj, kk;
static char* dnames[] = {"w", "x", "y", "z", "u", "v", "a", "b", "c", "d"};
assert(NDIMS <= LEN_OF(dnames));
for (idim = 0; idim < NDIMS; idim++) {
dims[idim].size = sizes[idim];
dims[idim].name = dnames[idim];
}
/* back in define mode OK, now add dimensions */
dimids[0] = ncdimdef(ncid, dims[0].name, NC_UNLIMITED);
if (dimids[0] == -1) {
ncclose(ncid);
return;
}
for (idim = 1; idim < NDIMS; idim++) {
dimids[idim] = ncdimdef(ncid, dims[idim].name, dims[idim].size);
if (dimids[idim] == -1) {
ncclose(ncid);
return;
}
}
/* define a multi-dimensional variable of each type */
for (iv = 0; iv < NVARS; iv++) {
va[iv].dims = (int *) malloc(sizeof(int) * (unsigned)va[iv].ndims);
for (idim = 0; idim < va[iv].ndims; idim++)
va[iv].dims[idim] = dimids[idim];
varid[iv] = ncvardef(ncid, va[iv].name, va[iv].type, va[iv].ndims,
va[iv].dims);
if (varid[iv] == -1) {
ncclose(ncid); return;
}
}
if (ncendef (ncid) == -1) {
ncclose(ncid); return;
}
printf("Note: first ncvarput writes fill values for all variables.\n");
for (iv = 0; iv < NVARS; iv++) { /* test each type of variable */
TIMING_DECLS ;
printf("\n----- %s(%d,%d,%d,%d)\n",
va[iv].name, sizes[0], sizes[1], sizes[2], sizes[3]);
v = (void *) malloc((unsigned)sizes[0]*sizes[1]*sizes[2]*sizes[3]
* nctypelen(va[iv].type));
/* fill it with values using a function of dimension indices */
ii = 0;
for (iw=0; iw < sizes[0]; iw++) {
corner[0] = iw;
for (ix=0; ix < sizes[1]; ix++) {
corner[1] = ix;
for (iy=0; iy < sizes[2]; iy++) {
corner[2] = iy;
for (iz=0; iz < sizes[3]; iz++) {
corner[3] = iz;
/* v[ii++] = VF(corner); */
val_stuff(va[iv].type, v, ii, VF(corner));
ii++;
}
}
}
}
for (idim = 0; idim < NDIMS; idim++) {
corner[idim] = 0;
edge[idim] = dims[idim].size;
}
sprintf(time_mess,"ncvarput %ldx%ldx%ldx%ld",
edge[0], edge[1], edge[2], edge[3]);
TIMING_START ;
/* ncvarput the whole variable */
if (ncvarput(ncid, varid[iv], corner, edge, (void *) v) == -1) {
ncclose(ncid);
return;
}
TIMING_END ;
/*
* For several combinations of fixed dimensions, get a slab and compare
* values to function values.
*/
/* get an interior point */
for (idim=0; idim < NDIMS; idim++) {
corner[idim] = dims[idim].size/2;
edge[idim] = 1;
point[idim] = corner[idim];
}
sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld"
,edge[0],edge[1],edge[2],edge[3]);
TIMING_START ;
if (ncvarget(ncid, varid[iv], corner, edge, (void *) v) == -1)
return;
TIMING_END ;
/* if (v[0] != VF(point)) */
if (val_diff(va[iv].type, v, 0, VF(point)))
fprintf(stderr,"ncvarget got wrong value for point");
/* get a vector in each direction */
for (idim=0; idim < NDIMS; idim++) {
for (jdim=0; jdim < NDIMS; jdim++) {
corner[jdim] = 0;
edge[jdim] = 1;
point[jdim] = corner[jdim];
}
corner[idim] = 0; /* get vector along dimension idim */
edge[idim] = dims[idim].size;
sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld"
,edge[0],edge[1],edge[2],edge[3]);
TIMING_START ;
if (ncvarget(ncid, varid[iv], corner, edge, (void *) v) == -1)
return;
TIMING_END ;
for (ii=corner[idim]; ii < edge[idim]; ii++) {
point[idim] = ii;
/* if (v[ii] != VF(point)) */
if (val_diff(va[iv].type, v, ii, VF(point)))
fprintf(stderr,"ncvarget got wrong value for vector");
}
}
/* get a plane in each direction */
for (idim=0; idim < NDIMS; idim++) {
for (jdim=idim+1; jdim < NDIMS; jdim++) {
for (kdim=0; kdim < NDIMS; kdim++) { /* reset corners and edges */
corner[kdim] = 0;
edge[kdim] = 1;
point[kdim] = corner[kdim];
}
corner[idim] = 0; /* plane along dimensions idim jdim */
corner[jdim] = 0;
edge[idim] = dims[idim].size;
edge[jdim] = dims[jdim].size;
sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld"
,edge[0],edge[1],edge[2],edge[3]);
TIMING_START ;
if (ncvarget(ncid, varid[iv], corner, edge, (void *) v) == -1)
return;
TIMING_END ;
for (ii=corner[idim]; ii < edge[idim]; ii++) {
for (jj=corner[jdim]; jj < edge[jdim]; jj++) {
point[idim] = ii;
point[jdim] = jj;
/* if (v[(ii)*edge[jdim]+jj] != VF(point)) { */
if (val_diff(va[iv].type, v,
(ii)*(int)edge[jdim]+jj, VF(point))) {
fprintf(stderr,
"ncvarget got wrong value in plane");
}
}
}
}
}
/* get a cube in each direction */
for (idim=0; idim < NDIMS; idim++) {
for (jdim=idim+1; jdim < NDIMS; jdim++) {
for (kdim=jdim+1; kdim < NDIMS; kdim++) {
for (ldim=0; ldim < NDIMS; ldim++) { /* reset corners, edges */
corner[ldim] = 0;
edge[ldim] = 1;
point[ldim] = corner[ldim];
}
corner[idim] = 0; /* intr. cube along idim jdim kdim */
corner[jdim] = 0;
corner[kdim] = 0;
edge[idim] = dims[idim].size;
edge[jdim] = dims[jdim].size;
edge[kdim] = dims[kdim].size;
sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld"
,edge[0],edge[1],edge[2],edge[3]);
TIMING_START ;
if (ncvarget(ncid, varid[iv], corner, edge,
(void *) v) == -1)
return;
TIMING_END ;
for (ii=corner[idim]; ii < edge[idim]; ii++) {
for (jj=corner[jdim]; jj < edge[jdim]; jj++) {
for (kk=corner[kdim]; kk < edge[kdim]; kk++) {
point[idim] = ii;
point[jdim] = jj;
point[kdim] = kk;
/* if (v[((ii)*edge[jdim]+jj)*
edge[kdim]+kk] != VF(point)) { */
if (val_diff(va[iv].type,v,
((ii)*(int)edge[jdim]+jj)*
(int)edge[kdim]+kk,VF(point))) {
fprintf(stderr,
"ncvarget - bad value in cube");
}
}
}
}
}
}
}
/* get one 4-D slab of data */
for(idim = 0; idim < NDIMS; idim++) {
corner[idim] = 0;
edge[idim] = dims[idim].size;
}
sprintf(time_mess,"ncvarget %ldx%ldx%ldx%ld"
,edge[0],edge[1],edge[2],edge[3]);
TIMING_START ;
if (ncvarget(ncid, varid[iv], corner, edge, (void *) v) == -1)
return;
TIMING_END ;
free(v);
}
}
void
usage(argv)
char **argv;
{
int i;
fprintf(stderr, "usage: %s ", argv[0]);
for (i=0; i < NDIMS; i++)
fprintf(stderr, "dim%d ", i);
fprintf(stderr, "\n");
}
int
main(argc, argv)
int argc;
char **argv;
{
int ncid;
int i;
int w[NDIMS];
if (argc != NDIMS+1) {
for (i = 0; i < NDIMS; i++)
w[i] = DEFAULTDIMS[i];
} else {
for (i = 0; i < NDIMS; i++)
w[i] = atoi(argv[i+1]);
}
ncid = nccreate("benchmark.nc",NC_CLOBBER);
test_slabs(ncid, w);
ncclose(ncid);
return 0;
}