1 /*
2 Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
3 LLNL-CODE-425250.
4 All rights reserved.
5 
6 This file is part of Silo. For details, see silo.llnl.gov.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11 
12    * Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the disclaimer below.
14    * Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the disclaimer (as noted
16      below) in the documentation and/or other materials provided with
17      the distribution.
18    * Neither the name of the LLNS/LLNL nor the names of its
19      contributors may be used to endorse or promote products derived
20      from this software without specific prior written permission.
21 
22 THIS SOFTWARE  IS PROVIDED BY  THE COPYRIGHT HOLDERS  AND CONTRIBUTORS
23 "AS  IS" AND  ANY EXPRESS  OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT
24 LIMITED TO, THE IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A  PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN  NO  EVENT SHALL  LAWRENCE
26 LIVERMORE  NATIONAL SECURITY, LLC,  THE U.S.  DEPARTMENT OF  ENERGY OR
27 CONTRIBUTORS BE LIABLE FOR  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR  CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED TO,
29 PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS  OF USE,  DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER  IN CONTRACT, STRICT LIABILITY,  OR TORT (INCLUDING
32 NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT  OF THE USE  OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35 This work was produced at Lawrence Livermore National Laboratory under
36 Contract No.  DE-AC52-07NA27344 with the DOE.
37 
38 Neither the  United States Government nor  Lawrence Livermore National
39 Security, LLC nor any of  their employees, makes any warranty, express
40 or  implied,  or  assumes  any  liability or  responsibility  for  the
41 accuracy, completeness,  or usefulness of  any information, apparatus,
42 product, or  process disclosed, or  represents that its use  would not
43 infringe privately-owned rights.
44 
45 Any reference herein to  any specific commercial products, process, or
46 services by trade name,  trademark, manufacturer or otherwise does not
47 necessarily  constitute or imply  its endorsement,  recommendation, or
48 favoring  by  the  United  States  Government  or  Lawrence  Livermore
49 National Security,  LLC. The views  and opinions of  authors expressed
50 herein do not necessarily state  or reflect those of the United States
51 Government or Lawrence Livermore National Security, LLC, and shall not
52 be used for advertising or product endorsement purposes.
53 */
54 
55 #include "silo.h"               /*include public silo           */
56 
57 #include <math.h>
58 #include <string.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 
62 #if !defined(_WIN32)
63 #include <sys/time.h>
64 #include <unistd.h>
65 #endif
66 #include <stdlib.h>
67 
68 #define ONE_MEG 1048576
69 #define INTERATE 50
70 
71 #include <std.c>
72 
73 /*-------------------------------------------------------------------------
74  * Function:        main
75  *
76  * Purpose:
77  *
78  * Return:        0
79  *
80  * Programmer:
81  *      Thomas R. Treadway, Mon Mar 12 14:13:51 PDT 2007
82  *      Test of HDF5 compression.
83  *
84  * Modifications:
85  *
86  *-------------------------------------------------------------------------
87  */
88 int
main(int argc,char * argv[])89 main(int argc, char *argv[])
90 {
91 
92     int            nerrors = 0;
93     int            verbose = 0;
94     int            usefloat = 0;
95     int            readonly = 0;
96     int            i, j, ndims=1;
97     int            fdims[]={ONE_MEG/sizeof(float)};
98     int            ddims[]={ONE_MEG/sizeof(double)};
99     float          *fval;
100     float          *frval;
101     double         *dval;
102     double         *drval;
103     int            driver=DB_HDF5;
104     char          *filename="compression.h5";
105     char          *ptr;
106     char           tmpname[64];
107     DBfile        *dbfile;
108 #if !defined(_WIN32)
109     struct         timeval tim;
110     double         t1, t2;
111 #endif
112     struct stat    buffer;
113     off_t          fsize;
114     int            has_loss = 0;
115     int            show_errors = DB_TOP;
116 
117     /* Parse command-line */
118     for (i=1; i<argc; i++) {
119        if (!strncmp(argv[i], "DB_PDB",6)) {
120           fprintf(stderr, "This test only supported on HDF5 driver\n");
121           exit(1);
122        } else if (!strncmp(argv[i], "DB_HDF5", 7)) {
123           driver = StringToDriver(argv[i]);
124           filename = "compression.h5";
125        } else if (!strcmp(argv[i], "compress")) {
126           if ((i+1<argc) && ((ptr=strstr(argv[i+1], "METHOD=")) != NULL))
127           {
128             DBSetCompression(argv[i+1]);
129             i++;
130           }
131           else
132             DBSetCompression("METHOD=GZIP");
133        } else if (!strcmp(argv[i], "szip")) {
134           DBSetCompression("METHOD=SZIP");
135        } else if (!strcmp(argv[i], "gzip")) {
136           DBSetCompression("METHOD=GZIP");
137        } else if (!strcmp(argv[i], "fpzip")) {
138           DBSetCompression("METHOD=FPZIP");
139        } else if (!strcmp(argv[i], "single")) {
140           usefloat = 1;
141        } else if (!strcmp(argv[i], "verbose")) {
142           verbose = 1;
143        } else if (!strcmp(argv[i], "lossy1")) {
144           DBSetCompression("METHOD=FPZIP LOSS=1");
145           has_loss = 1;
146        } else if (!strcmp(argv[i], "lossy2")) {
147           DBSetCompression("METHOD=FPZIP LOSS=2");
148           has_loss = 1;
149        } else if (!strcmp(argv[i], "lossy3")) {
150           DBSetCompression("METHOD=FPZIP LOSS=3");
151           has_loss = 1;
152        } else if (!strcmp(argv[i], "minratio1000")) {
153           DBSetCompression("ERRMODE=FAIL MINRATIO=1000 METHOD=FPZIP");
154        } else if (!strcmp(argv[i], "minratio1001")) {
155           DBSetCompression("ERRMODE=FALLBACK MINRATIO=1000 METHOD=FPZIP");
156        } else if (!strcmp(argv[i], "readonly")) {
157           readonly = 1;
158        } else if (!strcmp(argv[i], "help")) {
159           printf("Usage: %s [compress [\"METHOD=...\"]|single|verbose|readonly]\n",argv[0]);
160           printf("Where: compress - enables compression, followed by compression information string\n");
161           printf("                  default is compress \"METHOD=GZIP LEVEL=1\"\n");
162           printf("       single   - writes data as floats not doubles\n");
163           printf("       verbose  - displays more feedback\n");
164           printf("       readonly - checks an existing file (used for cross platform test)\n");
165           printf("       DB_HDF5  - enable HDF5 driver, the default\n");
166           return (0);
167        } else if (!strcmp(argv[i], "show-all-errors")) {
168           show_errors = DB_ALL_AND_DRVR;
169        } else if (argv[i][0] != '\0') {
170           fprintf(stderr, "%s: ignored argument `%s'\n", argv[0], argv[i]);
171        }
172     }
173 
174     /* get some temporary memory */
175     fval = (float*) malloc(ONE_MEG);
176     frval = (float*) malloc(ONE_MEG);
177     dval = (double*) malloc(ONE_MEG);
178     drval = (double*) malloc(ONE_MEG);
179 
180     DBShowErrors(show_errors, 0);
181 
182     if (!readonly)
183     {
184       /*
185        * Create a file that contains a simple variables.
186        */
187       if (verbose)
188          printf("Creating file: `%s'\n", filename);
189       dbfile = DBCreate(filename, 0, DB_LOCAL, "Compression Test", driver);
190 
191 #if !defined(_WIN32)
192       gettimeofday(&tim, NULL);
193       t1=tim.tv_sec+(tim.tv_usec/1000000.0);
194 #endif
195       if (usefloat)
196       {
197          for (j = 0; j < INTERATE; j++)
198          {
199             if (verbose)
200              if (j % 100 == 0)
201                printf("Iterations %04d to %04d of %04d\n", j,j+100-1,INTERATE);
202 
203             sprintf(tmpname, "compression_%04d", j);
204 
205             for (i = 0; i < fdims[0]; i++)
206                fval[i] = (float) fdims[0] * j + i;
207 
208             if (DBWrite(dbfile, tmpname, fval, fdims, ndims, DB_FLOAT) < 0)
209             {
210                 nerrors++;
211                 break;
212             }
213          }
214       }
215       else
216       {
217          for (j = 0; j < INTERATE; j++)
218          {
219             if (verbose)
220                if (j % 100 == 0)
221                  printf("Iterations %04d to %04d of %04d\n",j,j+100-1,INTERATE);
222 
223             sprintf(tmpname, "compression_%04d", j);
224 
225             for (i = 0; i < ddims[0]; i++)
226                dval[i] = (double) ddims[0] * j + i;
227 
228             if (DBWrite(dbfile, tmpname, dval, ddims, ndims, DB_DOUBLE) < 0)
229             {
230                 nerrors++;
231                 break;
232             }
233          }
234       }
235 #if !defined(_WIN32)
236       gettimeofday(&tim, NULL);
237       t2=tim.tv_sec+(tim.tv_usec/1000000.0);
238       stat(filename, &buffer);
239       fsize = buffer.st_size;
240       printf("Write took %.6lf seconds and %.6g bytes/second\n",
241          t2-t1,fsize/(t2-t1));
242 #endif
243 
244       DBClose(dbfile);
245     }
246     else
247     {
248       stat(filename, &buffer);
249       fsize = buffer.st_size;
250     }
251 
252     if (nerrors)
253         return nerrors;
254 
255     /*
256      * Now try opening the file again and verify the simple
257      * variable.
258      */
259     if (verbose)
260         printf("Reopening `%s'\n", filename);
261     dbfile = DBOpen(filename, driver, DB_READ);
262 
263     if (dbfile == 0)
264     {
265         printf("Unable to Open file for reading\n");
266         exit(1);
267     }
268 
269 #if !defined(_WIN32)
270     gettimeofday(&tim, NULL);
271     t1=tim.tv_sec+(tim.tv_usec/1000000.0);
272 #endif
273     if (usefloat)
274     {
275        for (j = 0; j < INTERATE; j++)
276        {
277           if (verbose)
278              if (j % 100 == 0)
279                 printf("Iterations %04d to %04d of %04d\n", j,j+100-1,INTERATE);
280 
281           sprintf(tmpname, "compression_%04d", j);
282 
283           if (DBReadVar(dbfile, tmpname, frval) < 0)
284           {
285              if (!has_loss) nerrors++;
286              if (!has_loss && nerrors <= 10) printf("DBReadVar for \"%s\" failed\n", tmpname);
287              if (!has_loss && nerrors == 10) printf("Further errors will be suppressed\n");
288           }
289           for (i = 0; i < fdims[0]; i++)
290           {
291              fval[i] = (float) fdims[0] * j + i;
292              if (fval[i] != frval[i])
293              {
294                 if (!has_loss) nerrors++;
295                 if (!has_loss && nerrors <= 10)
296                     printf("Read error in \"%s\" at position %04d. Expected %f, got %f\n",
297                     tmpname, i, fval[i], frval[i]);
298                 if (!has_loss && nerrors == 10) printf("Further errors will be suppressed\n");
299                 break;
300              }
301           }
302        }
303     }
304     else
305     {
306        for (j = 0; j < INTERATE; j++)
307        {
308           if (verbose)
309              if (j % 100 == 0)
310                 printf("Iterations %04d to %04d of %04d\n",j,j+100-1,INTERATE);
311 
312           sprintf(tmpname, "compression_%04d", j);
313 
314           if (DBReadVar(dbfile, tmpname, drval) < 0)
315           {
316              if (!has_loss) nerrors++;
317              if (!has_loss && nerrors <= 10) printf("DBReadVar for \"%s\" failed\n", tmpname);
318              if (!has_loss && nerrors == 10) printf("Further errors will be suppressed\n");
319           }
320           for (i = 0; i < ddims[0]; i++)
321           {
322              dval[i] = (double) ddims[0] * j + i;
323              if (dval[i] != drval[i])
324              {
325                 if (!has_loss) nerrors++;
326                 if (!has_loss && nerrors <= 10)
327                     printf("Read error in \"%s\" at position %04d. Expected %f, got %f\n",
328                     tmpname, i, dval[i], drval[i]);
329                 if (!has_loss && nerrors == 10) printf("Further errors will be suppressed\n");
330                 break;
331              }
332           }
333        }
334     }
335 #if !defined(_WIN32)
336     gettimeofday(&tim, NULL);
337     t2=tim.tv_sec+(tim.tv_usec/1000000.0);
338     printf("Read took %.6lf seconds and %.6g bytes/second\n",
339        t2-t1,fsize/(t2-t1));
340 #endif
341     DBClose(dbfile);
342 
343     free(fval);
344     free(frval);
345     free(dval);
346     free(drval);
347 
348     CleanupDriverStuff();
349     return nerrors;
350 }
351