1 /*
2 Copyright 2007-2019, UCAR/Unidata
3 See COPYRIGHT file for copying and redistribution conditions.
4
5 This program creates some test files which are used for performance
6 testing.
7
8 Ed Hartnett
9 */
10 #include <config.h>
11 #include <nc_tests.h>
12 #include "err_macros.h"
13 #include <netcdf.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include "ncdispatch.h"
17 #include <math.h>
18
19 /* We will create this file. */
20 #define FILE_NAME "tst_floats_1D.nc"
21
22 int
main(int argc,char ** argv)23 main(int argc, char **argv)
24 {
25 printf("\n*** Create some files for testing benchmarks.\n");
26
27 #ifdef LARGE_FILE_TESTS
28 printf("*** Creating very large 3D file...");
29 {
30 #define LARGE_VAR_NAME "Really_Large_Variable"
31 #define LARGE_FILE "tst_large.nc"
32 #define NDIMS3 3
33 #define D0 400
34 #define D1 2000
35 #define D2 2000
36
37 int ncid, dimids[NDIMS3], varid;
38 size_t start[NDIMS3], count[NDIMS3];
39 size_t dim_len[NDIMS3] = {D0, D1, D2};
40 size_t chunk_sizes[NDIMS3] = {1, D1, D2};
41 float *data;
42 char file_name[NC_MAX_NAME * 2 + 1];
43 int d, i;
44
45 /* Initialize the data to random floats. */
46 if (!(data = (float *)malloc(D1 * D2 * sizeof(float)))) ERR;
47 for (i = 0; i < D1 * D2; i++)
48 data[i] = (float)rand();
49
50 /* User TEMP_LARGE as the directory. */
51 if (strlen(TEMP_LARGE) + strlen(LARGE_FILE) > NC_MAX_NAME * 2) ERR;
52 sprintf(file_name, "%s/%s", TEMP_LARGE, LARGE_FILE);
53
54 /* Create file with 3 dims, one variable. */
55 if (nc_create(file_name, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
56 if (nc_def_dim(ncid, "d0", D0, &dimids[0])) ERR;
57 if (nc_def_dim(ncid, "d1", D1, &dimids[1])) ERR;
58 if (nc_def_dim(ncid, "d2", D2, &dimids[2])) ERR;
59 if (nc_def_var(ncid, LARGE_VAR_NAME, NC_FLOAT, NDIMS3, dimids, &varid)) ERR;
60 #ifndef NOBUG
61 {int status;
62 status = nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunk_sizes);
63 if(status)
64 printf("nc_def_var_chunking fail: %d: %s\n",status,nc_strerror(status));
65 }
66 #else
67 if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunk_sizes)) ERR;
68 #endif
69 if (nc_enddef(ncid)) ERR;
70
71 /* Write the data one slice at a time. */
72 start[0] = 0;
73 count[0] = 1;
74 for (d = 1; d < NDIMS3; d++)
75 {
76 start[d] = 0;
77 count[d] = dim_len[d];
78 }
79 for ( ; start[0] < D0; (start[0])++)
80 if (nc_put_vara_float(ncid, varid, start, count, (const float *) data))
81 ERR_RET;
82
83 /* Close up shop. */
84 if (nc_close(ncid)) ERR;
85 free(data);
86 }
87 SUMMARIZE_ERR;
88 #endif /* LARGE_FILE_TESTS */
89
90 printf("*** Creating a file with floats...");
91 {
92 #define DIM_NAME "stupidity"
93 #define NUMDIMS 1
94 #define DIMLEN 10000
95
96 int ncid, dimids[NUMDIMS], varid;
97 char var_name[NC_MAX_NAME + 1] = {"Billy-Bob"};
98 int ndims, nvars, natts, unlimdimid;
99 nc_type xtype;
100 char name_in[NC_MAX_NAME + 1];
101 size_t len;
102 float data[DIMLEN], data_in[DIMLEN];
103 int i;
104
105 for (i = 0; i < DIMLEN; i++)
106 data[i] = ((float)rand() / (float)(RAND_MAX));
107
108 /* Create a netCDF netCDF-4/HDF5 format file, with 1 var. */
109 /* if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;*/
110 if (nc_create(FILE_NAME, 0, &ncid)) ERR;
111 if (nc_def_dim(ncid, DIM_NAME, DIMLEN, dimids)) ERR;
112 if (nc_def_var(ncid, var_name, NC_FLOAT, NUMDIMS, dimids, &varid)) ERR;
113 if (nc_enddef(ncid)) ERR;
114 if (nc_put_var_float(ncid, varid, data)) ERR;
115 if (nc_close(ncid)) ERR;
116
117 /* Reopen and check the file. */
118 if (nc_open(FILE_NAME, 0, &ncid)) ERR;
119 if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
120 if (ndims != NUMDIMS || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
121 if (nc_inq_dimids(ncid, &ndims, dimids, 1)) ERR;
122 if (ndims != 1 || dimids[0] != 0) ERR;
123 if (nc_inq_dim(ncid, 0, name_in, &len)) ERR;
124 if (strcmp(name_in, DIM_NAME) || len != DIMLEN) ERR;
125 if (nc_inq_var(ncid, 0, name_in, &xtype, &ndims, dimids, &natts)) ERR;
126 if (strcmp(name_in, var_name) || xtype != NC_FLOAT || ndims != 1 ||
127 dimids[0] != 0 || natts != 0) ERR;
128 if (nc_get_var_float(ncid, 0, data_in)) ERR;
129 for (i = 0; i < DIMLEN; i++)
130 if (data_in[i] != data[i]) ERR;
131
132 if (nc_close(ncid)) ERR;
133 }
134
135 SUMMARIZE_ERR;
136 printf("*** Creating files of various dimensions with various types...");
137 {
138 #define TOTAL_SIZE 100000
139 #define MAX_DIMS 6
140 #define VAR_NAME "Unimaginatively_Named_Variable"
141 #define MAX_TYPES 3
142
143 int ncid, dimids[MAX_DIMS], varid;
144 char dim_name[NC_MAX_NAME + 1], file_name[NC_MAX_NAME + 1];
145 char type_name[MAX_TYPES][NC_MAX_NAME + 1] = {"floats", "ints", "shorts"};
146 int typeid[MAX_TYPES] = {NC_FLOAT, NC_INT, NC_SHORT};
147 size_t len;
148 float fdata[TOTAL_SIZE];
149 int idata[TOTAL_SIZE];
150 short sdata[TOTAL_SIZE];
151 void *data[MAX_TYPES];
152 int ndims;
153 int i, d, t;
154
155 /* Initialize the data to random floats. */
156 for (i = 0; i < TOTAL_SIZE; i++)
157 {
158 fdata[i] = (float)rand();
159 idata[i] = rand();
160 sdata[i] = (short)rand();
161 }
162 data[0] = fdata;
163 data[1] = idata;
164 data[2] = sdata;
165
166 /* Do the file creation process below for each type. */
167 for (t = 0; t < MAX_TYPES; t++)
168 {
169 /* Create MAX_DIMS files, each with different number of
170 * dimensions. */
171 for (ndims = 1; ndims <= MAX_DIMS; ndims++)
172 {
173 sprintf(file_name, "tst_%s2_%dD.nc", type_name[t], ndims);
174 if (nc_create(file_name, 0, &ncid)) ERR;
175 for (len = pow(TOTAL_SIZE, (float)1/ndims), d = 0; d < ndims; d++)
176 {
177 sprintf(dim_name, "dim_%d", d);
178 if (nc_def_dim(ncid, dim_name, len, &dimids[d])) ERR;
179 }
180 if (nc_def_var(ncid, VAR_NAME, typeid[t], ndims, dimids, &varid)) ERR;
181 if (nc_enddef(ncid)) ERR;
182 if (nc_put_var(ncid, varid, data[t])) ERR;
183 if (nc_close(ncid)) ERR;
184 } /* next ndims. */
185 } /* next type */
186 }
187
188 SUMMARIZE_ERR;
189 printf("*** Creating file like in http://hdfeos.org/workshops/ws06/presentations/Pourmal/HDF5_IO_Perf.pdf...");
190 {
191 #define XLEN 256
192 #define YLEN 256
193 #define ZLEN 1024
194 #define NDIMS 3
195 #define E_VAR_NAME "Like_Elenas_Benchmark"
196 #define ELENA_FILE_NAME "tst_elena_int_3D.nc"
197 #define E_TYPE_SIZE 4
198
199 int ncid, dimids[NDIMS], varid;
200 int *idata;
201 int i;
202
203 /* Initialize data to random int between 0 and 255. */
204 if (!(idata = malloc(XLEN * YLEN * ZLEN * E_TYPE_SIZE)))
205 return NC_ENOMEM;
206 for (i = 0; i < XLEN * YLEN * ZLEN; i++)
207 idata[i] = rand() % 255;
208
209 /* Create a 3D file with one var. */
210 if (nc_create(ELENA_FILE_NAME, NC_CLOBBER, &ncid)) ERR;
211 if (nc_def_dim(ncid, "z", ZLEN, &dimids[0])) ERR;
212 if (nc_def_dim(ncid, "y", YLEN, &dimids[1])) ERR;
213 if (nc_def_dim(ncid, "x", XLEN, &dimids[2])) ERR;
214 if (nc_def_var(ncid, E_VAR_NAME, NC_INT, NDIMS, dimids, &varid)) ERR;
215 if (nc_enddef(ncid)) ERR;
216
217 /* Write the data. */
218 if (nc_put_var(ncid, varid, idata)) ERR;
219 if (nc_close(ncid)) ERR;
220 }
221
222 SUMMARIZE_ERR;
223 printf("*** Creating super simple file to test non-sequential reads...");
224 {
225 #define DIM_LEN 10
226 #define NDIMS1 1
227 #define S_VAR_NAME "My_Favorite_Numbers_in_order"
228 #define S_FILE_NAME "tst_simple.nc"
229
230 int ncid, dimids[NDIMS1], varid;
231 int data[DIM_LEN];
232 int i;
233
234 /* Initialize data to my favorite numbers. */
235 for (i = 0; i < DIM_LEN; i++)
236 data[i] = i;
237
238 /* Create a file with one var. */
239 if (nc_create(S_FILE_NAME, NC_CLOBBER, &ncid)) ERR;
240 if (nc_def_dim(ncid, "a", DIM_LEN, &dimids[0])) ERR;
241 if (nc_def_var(ncid, S_VAR_NAME, NC_INT, NDIMS1, dimids, &varid)) ERR;
242 if (nc_enddef(ncid)) ERR;
243
244 /* Write the data. */
245 if (nc_put_var(ncid, varid, data)) ERR;
246 if (nc_close(ncid)) ERR;
247 }
248
249 SUMMARIZE_ERR;
250 printf("*** Creating very simple 3D file...");
251 {
252 #define SIMPLE_VAR_NAME "Paul_Mau_Dib"
253 #define MAX_TYPES 3
254
255 int ncid, dimids[MAX_DIMS], varid;
256 char dim_name[NC_MAX_NAME + 1], file_name[NC_MAX_NAME + 1];
257 char type_name[MAX_TYPES][NC_MAX_NAME + 1] = {"floats", "ints", "shorts"};
258 int typeid[MAX_TYPES] = {NC_FLOAT, NC_INT, NC_SHORT};
259 size_t len;
260 float fdata[TOTAL_SIZE];
261 int idata[TOTAL_SIZE];
262 short sdata[TOTAL_SIZE];
263 void *data[MAX_TYPES];
264 int ndims;
265 int i, d, t;
266
267 /* Initialize the data to random floats. */
268 for (i = 0; i < TOTAL_SIZE; i++)
269 {
270 fdata[i] = (float)rand();
271 idata[i] = rand();
272 sdata[i] = (short)rand();
273 }
274 data[0] = fdata;
275 data[1] = idata;
276 data[2] = sdata;
277
278 /* Do the file creation process below for each type. */
279 for (t = 0; t < MAX_TYPES; t++)
280 {
281 /* Create MAX_DIMS files, each with different number of
282 * dimensions. */
283 for (ndims = 1; ndims <= MAX_DIMS; ndims++)
284 {
285 sprintf(file_name, "tst_%s2_%dD.nc", type_name[t], ndims);
286 if (nc_create(file_name, 0, &ncid)) ERR;
287 for (len = pow(TOTAL_SIZE, (float)1/ndims), d = 0; d < ndims; d++)
288 {
289 sprintf(dim_name, "dim_%d", d);
290 if (nc_def_dim(ncid, dim_name, len, &dimids[d])) ERR;
291 }
292 if (nc_def_var(ncid, SIMPLE_VAR_NAME, typeid[t], ndims, dimids, &varid)) ERR;
293 if (nc_enddef(ncid)) ERR;
294 if (nc_put_var(ncid, varid, data[t])) ERR;
295 if (nc_close(ncid)) ERR;
296 } /* next ndims. */
297 } /* next type */
298 }
299 SUMMARIZE_ERR;
300
301 FINAL_RESULTS;
302 }
303