forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst_files3.c
232 lines (198 loc) · 7.24 KB
/
tst_files3.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
/* This is part of the netCDF package. Copyright 2005-2018 University
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
conditions of use.
This is a benchmark program which tests file writes with compressed
data.
Ed Hartnett
*/
#include <nc_tests.h>
#include "err_macros.h"
#include <hdf5.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h> /* Extra high precision time info. */
#define NDIMS1 1
#define NDIMS 3
#define FILE_NAME "tst_files3.nc"
#define X_LEN 120
#define Y_LEN 64
#define Z_LEN 128
#define NUM_TRIES 200
/* Prototype from tst_utils.c. */
int nc4_timeval_subtract(struct timeval *result, struct timeval *x,
struct timeval *y);
int dump_file2(const float *data, int docompression, int usedefdim)
{
int ncid, dimids[NDIMS], var;
size_t start[NDIMS] = {0, 0, 0};
size_t count[NDIMS] = {1, 1, Z_LEN};
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR_RET;
if (nc_def_dim(ncid, "time", X_LEN, &dimids[0])) ERR_RET;
if (nc_def_dim(ncid, "lat", Y_LEN, &dimids[1])) ERR_RET;
if (nc_def_dim(ncid, "lon", Z_LEN, &dimids[2])) ERR_RET;
if (nc_def_var(ncid, "test", NC_FLOAT, NDIMS, dimids, &var)) ERR_RET;
if (docompression)
if (nc_def_var_deflate(ncid, var, 1, 1, 1)) ERR_RET;
if (nc_enddef(ncid)) ERR_RET;
for (start[0] = 0; start[0] < X_LEN; start[0]++)
for (start[1] = 0; start[1] < Y_LEN; start[1]++)
if (nc_put_vara_float(ncid, var, start, count, data)) ERR_RET;
if (nc_close(ncid)) ERR_RET;
return 0;
}
int dump_file(const float *data, int docompression, int usedefdim)
{
int ncmode, ncid, dimids[NDIMS], var;
size_t start[NDIMS] = {0, 0, 0}, count[NDIMS] = {X_LEN, Y_LEN, Z_LEN};
ptrdiff_t stride[NDIMS] = {1, 1, 1};
ncmode = NC_CLOBBER|NC_NETCDF4;
if (nc_create(FILE_NAME, ncmode, &ncid)) ERR_RET;
if (nc_def_dim(ncid, "time", X_LEN, &dimids[0])) ERR_RET;
if (nc_def_dim(ncid, "lat", Y_LEN, &dimids[1])) ERR_RET;
if (nc_def_dim(ncid, "lon", Z_LEN, &dimids[2])) ERR_RET;
if (nc_def_var(ncid, "test", NC_FLOAT, NDIMS, dimids, &var)) ERR_RET;
if (docompression)
if (nc_def_var_deflate(ncid, var, 1, 1, 1)) ERR_RET;
if (nc_enddef(ncid)) ERR_RET;
if (nc_put_vars_float(ncid, var, start, count, stride, data)) ERR_RET;
if (nc_close(ncid)) ERR_RET;
return 0;
}
int dump_file3(const float *data, int docompression, int usedefdim)
{
int ncmode, ncid, dimids[NDIMS], var;
size_t start[NDIMS] = {0, 0, 0}, count[NDIMS] = {X_LEN, Y_LEN, Z_LEN};
ptrdiff_t stride[NDIMS] = {1, 1, 1};
ncmode = NC_CLOBBER|NC_NETCDF4;
if (nc_create(FILE_NAME, ncmode, &ncid)) ERR_RET;
if (nc_def_dim(ncid, "time", X_LEN, &dimids[0])) ERR_RET;
if (nc_def_dim(ncid, "lat", Y_LEN, &dimids[1])) ERR_RET;
if (nc_def_dim(ncid, "lon", Z_LEN, &dimids[2])) ERR_RET;
if (nc_def_var(ncid, "test", NC_FLOAT, NDIMS, dimids, &var)) ERR_RET;
if (docompression)
if (nc_def_var_deflate(ncid, var, 1, 1, 1)) ERR_RET;
if (nc_enddef(ncid)) ERR_RET;
if (nc_put_vars_float(ncid, var, start, count, stride, data)) ERR_RET;
if (nc_close(ncid)) ERR_RET;
return 0;
}
int dump_hdf_file(const float *data, int docompression)
{
hid_t file_id, dataset_id, propid;
hid_t file_spaceid, mem_spaceid, access_plistid, xfer_plistid;
hsize_t dims[NDIMS] = {X_LEN, Y_LEN, Z_LEN};
hsize_t start[NDIMS] = {0, 0, 0};
hsize_t count[NDIMS] = {1, 1, Z_LEN};
/* create file */
file_id = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC,
H5P_DEFAULT, H5P_DEFAULT);
/* create property for dataset */
propid = H5Pcreate(H5P_DATASET_CREATE);
if (docompression)
{
if (H5Pset_layout(propid, H5D_CHUNKED) < 0) ERR;
if (H5Pset_chunk(propid, NDIMS, dims) < 0) ERR;
/* values[0]=9; */
/* status = H5Pset_filter(propid, H5Z_FILTER_DEFLATE,0,1,&values[0]); */
/* printf("deflat estatus is: %i\n",status); */
/* sets defalte level */
if (H5Pset_deflate(propid, 1)) ERR;
}
if ((file_spaceid = H5Screate_simple(NDIMS, dims, dims)) < 0) ERR;
/* Set up the cache. */
if ((access_plistid = H5Pcreate(H5P_DATASET_ACCESS)) < 0) ERR;
if (H5Pset_chunk_cache(access_plistid, CHUNK_CACHE_NELEMS,
CHUNK_CACHE_SIZE, CHUNK_CACHE_PREEMPTION) < 0) ERR;
/* Create the dataset. */
if ((dataset_id = H5Dcreate2(file_id, "dset", H5T_NATIVE_FLOAT, file_spaceid,
H5P_DEFAULT, propid, access_plistid)) < 0) ERR;
/* if ((file_spaceid = H5Dget_space(dataset_id)) < 0) ERR;*/
if ((mem_spaceid = H5Screate_simple(NDIMS, count, NULL)) < 0) ERR;
if ((xfer_plistid = H5Pcreate(H5P_DATASET_XFER)) < 0) ERR;
/* Write the dataset. */
for (start[0] = 0; start[0] < X_LEN; start[0]++)
for (start[1] = 0; start[1] < Y_LEN; start[1]++)
{
if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL,
count, NULL) < 0) ERR_RET;
if (H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, mem_spaceid, file_spaceid,
xfer_plistid, data) < 0) ERR_RET;
}
/* Close property lists. */
if (H5Pclose(propid) < 0) ERR;
if (H5Pclose(access_plistid) < 0) ERR;
if (H5Pclose(xfer_plistid) < 0) ERR;
/* Close spaces. */
if (H5Sclose(file_spaceid) < 0) ERR;
if (H5Sclose(mem_spaceid) < 0) ERR;
/* End access to the dataset and release resources used by it. */
if (H5Dclose(dataset_id) < 0) ERR;
/* close file */
if (H5Fclose(file_id) < 0) ERR;
return 0;
}
void
get_mem_used2(int *mem_used)
{
char buf[30];
FILE *pf;
snprintf(buf, 30, "/proc/%u/statm", (unsigned)getpid());
pf = NCfopen(buf, "r");
if (pf) {
unsigned size; /* total program size */
unsigned resident;/* resident set size */
unsigned share;/* shared pages */
unsigned text;/* text (code) */
unsigned lib;/* library */
unsigned data;/* data/stack */
/*unsigned dt; dirty pages (unused in Linux 2.6)*/
fscanf(pf, "%u %u %u %u %u %u", &size, &resident, &share,
&text, &lib, &data);
*mem_used = data;
}
else
*mem_used = -1;
fclose(pf);
}
int main(void)
{
float data[X_LEN * Y_LEN * Z_LEN];
int i;
printf("\n*** Testing netcdf-4 file functions with caching.\n");
/* Initialize data. */
for (i = 0; i < (X_LEN * Y_LEN * Z_LEN); i++)
data[i] = i;
printf("*** testing a bunch of file writes with compressed data...\n");
{
int mem_used, mem_used1;
printf("*** testing netcdf-4 writes...\n");
for (i = 0; i < NUM_TRIES; i++)
{
get_mem_used2(&mem_used);
if (dump_file3(data, 1, 0)) ERR_RET;
get_mem_used2(&mem_used1);
if (mem_used1 - mem_used)
printf("delta %d bytes of memory for try %d\n", mem_used1 - mem_used, i);
}
printf("*** testing HDF5 writes...\n");
for (i = 0; i < NUM_TRIES; i++)
{
get_mem_used2(&mem_used);
if (dump_hdf_file(data, 1)) ERR_RET;
get_mem_used2(&mem_used1);
if (mem_used1 - mem_used)
printf("delta %d bytes of memory for try %d\n", mem_used1 - mem_used, i);
}
printf("*** testing netcdf-4 writes again...\n");
for (i = 0; i < NUM_TRIES; i++)
{
get_mem_used2(&mem_used);
if (dump_file2(data, 1, 0)) ERR_RET;
get_mem_used2(&mem_used1);
if (mem_used1 - mem_used)
printf("delta %d bytes of memory for try %d\n", mem_used1 - mem_used, i);
}
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}