source: trunk/bench/bench.c @ 138

Revision 138, 11.4 KB checked in by faltet, 3 years ago (diff)

Version set to 0.9.4.dev.

Line 
1/*********************************************************************
2  Small benchmark for testing basic capabilities of Blosc.
3
4  You can select different degrees of 'randomness' in input buffer, as
5  well as external datafiles (uncomment the lines after "For data
6  coming from a file" comment).
7
8  To compile using GCC, stay in this directory and do:
9
10    gcc -O3 -msse2 -o bench bench.c \
11        ../src/blosc.c ../src/blosclz.c ../src/shuffle.c -lpthread
12
13  I'm collecting speeds for different machines, so the output of your
14  benchmarks and your processor specifications are welcome!
15
16  Author: Francesc Alted (faltet@pytables.org)
17
18  See LICENSES/BLOSC.txt for details about copyright and rights to use.
19**********************************************************************/
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#if defined(_WIN32) && !defined(__MINGW32__)
28  #include <time.h>
29#else
30  #include <unistd.h>
31  #include <sys/time.h>
32#endif
33#include <math.h>
34#include "../src/blosc.h"
35
36#define KB  1024
37#define MB  (1024*KB)
38#define GB  (1024*MB)
39
40#define NCHUNKS (32*1024)       /* maximum number of chunks */
41
42
43int nchunks = NCHUNKS;
44int niter = 3;                  /* default number of iterations */
45float totalsize = 0.;           /* total compressed/decompressed size */
46
47#if defined(_WIN32) && !defined(__MINGW32__)
48#include <windows.h>
49#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
50  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
51#else
52  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
53#endif
54
55struct timezone
56{
57  int  tz_minuteswest; /* minutes W of Greenwich */
58  int  tz_dsttime;     /* type of dst correction */
59};
60
61int gettimeofday(struct timeval *tv, struct timezone *tz)
62{
63  FILETIME ft;
64  unsigned __int64 tmpres = 0;
65  static int tzflag;
66
67  if (NULL != tv)
68  {
69    GetSystemTimeAsFileTime(&ft);
70
71    tmpres |= ft.dwHighDateTime;
72    tmpres <<= 32;
73    tmpres |= ft.dwLowDateTime;
74
75    /*converting file time to unix epoch*/
76    tmpres -= DELTA_EPOCH_IN_MICROSECS;
77    tmpres /= 10;  /*convert into microseconds*/
78    tv->tv_sec = (long)(tmpres / 1000000UL);
79    tv->tv_usec = (long)(tmpres % 1000000UL);
80  }
81
82  if (NULL != tz)
83  {
84    if (!tzflag)
85    {
86      _tzset();
87      tzflag++;
88    }
89    tz->tz_minuteswest = _timezone / 60;
90    tz->tz_dsttime = _daylight;
91  }
92
93  return 0;
94}
95#endif   /* _WIN32 */
96
97
98/* Given two timeval stamps, return the difference in seconds */
99float getseconds(struct timeval last, struct timeval current) {
100  int sec, usec;
101
102  sec = current.tv_sec - last.tv_sec;
103  usec = current.tv_usec - last.tv_usec;
104  return (float)(((double)sec + usec*1e-6));
105}
106
107/* Given two timeval stamps, return the time per chunk in usec */
108float get_usec_chunk(struct timeval last, struct timeval current) {
109  return (float)(getseconds(last, current)/(niter*nchunks)*1e6);
110}
111
112
113int get_value(int i, int rshift) {
114  int v;
115
116  v = (i<<26)^(i<<18)^(i<<11)^(i<<3)^i;
117  if (rshift < 32) {
118    v &= (1 << rshift) - 1;
119  }
120  return v;
121}
122
123
124void init_buffer(void *src, int size, int rshift) {
125  unsigned int i;
126  int *_src = (int *)src;
127
128  /* To have reproducible results */
129  srand(1);
130
131  /* Initialize the original buffer */
132  for (i = 0; i < size/sizeof(int); ++i) {
133    /* Choose one below */
134    //_src[i] = 0;
135    //_src[i] = 0x01010101;
136    //_src[i] = 0x01020304;
137    //_src[i] = i * 1/.3;
138    //_src[i] = i;
139    //_src[i] = rand() >> (32-rshift);
140    _src[i] = get_value(i, rshift);
141  }
142}
143
144
145do_bench(int nthreads, unsigned int size, int elsize, int rshift) {
146  void *src, *srccpy;
147  void **dest[NCHUNKS], *dest2;
148  int nbytes, cbytes;
149  size_t i, j;
150  struct timeval last, current;
151  float tmemcpy, tshuf, tunshuf;
152  int clevel, doshuffle=1;
153  unsigned char *orig, *round;
154
155  blosc_set_nthreads(nthreads);
156
157  /* Initialize buffers */
158  src = malloc(size);
159  srccpy = malloc(size);
160  dest2 = malloc(size);
161  /* zero src to initialize byte on it, and not only multiples of 4 */
162  memset(src, 0, size);
163  init_buffer(src, size, rshift);
164  memcpy(srccpy, src, size);
165  for (j = 0; j < nchunks; j++) {
166    dest[j] = malloc(size);
167  }
168
169  /* Warm destination memory (memcpy() will go a bit faster later on) */
170  for (j = 0; j < nchunks; j++) {
171    memcpy(dest[j], src, size);
172  }
173
174  printf("--> %d, %d, %d, %d\n", nthreads, size, elsize, rshift);
175  printf("********************** Run info ******************************\n");
176  printf("Blosc version: %s (%s)\n", BLOSC_VERSION_STRING, BLOSC_VERSION_DATE);
177  printf("Using synthetic data with %d significant bits (out of 32)\n", rshift);
178  printf("Dataset size: %d bytes\tType size: %d bytes\n", size, elsize);
179  printf("Working set: %.1f MB\t\t", (size*nchunks) / (float)MB);
180  printf("Number of threads: %d\n", nthreads);
181  printf("********************** Running benchmarks *********************\n");
182
183  gettimeofday(&last, NULL);
184  for (i = 0; i < niter; i++) {
185    for (j = 0; j < nchunks; j++) {
186      memcpy(dest[j], src, size);
187    }
188  }
189  gettimeofday(&current, NULL);
190  tmemcpy = get_usec_chunk(last, current);
191  printf("memcpy(write):\t\t %6.1f us, %.1f MB/s\n",
192         tmemcpy, size/(tmemcpy*MB/1e6));
193
194  gettimeofday(&last, NULL);
195  for (i = 0; i < niter; i++) {
196    for (j = 0; j < nchunks; j++) {
197      memcpy(dest2, dest[j], size);
198    }
199  }
200  gettimeofday(&current, NULL);
201  tmemcpy = get_usec_chunk(last, current);
202  printf("memcpy(read):\t\t %6.1f us, %.1f MB/s\n",
203         tmemcpy, size/(tmemcpy*MB/1e6));
204
205  for (clevel=1; clevel<10; clevel++) {
206
207    printf("Compression level: %d\n", clevel);
208
209    gettimeofday(&last, NULL);
210    for (i = 0; i < niter; i++) {
211      for (j = 0; j < nchunks; j++) {
212        cbytes = blosc_compress(clevel, doshuffle, elsize, size, src, dest[j]);
213      }
214    }
215    gettimeofday(&current, NULL);
216    tshuf = get_usec_chunk(last, current);
217    printf("comp(write):\t %6.1f us, %.1f MB/s\t  ",
218           tshuf, size/(tshuf*MB/1e6));
219    printf("Final bytes: %d  ", cbytes);
220    if (cbytes > 0) {
221      printf("Ratio: %3.2f", size/(float)cbytes);
222    }
223    printf("\n");
224
225    /* Compressor was unable to compress.  Copy the buffer manually. */
226    if (cbytes == 0) {
227      for (j = 0; j < nchunks; j++) {
228        memcpy(dest[j], src, size);
229      }
230    }
231
232    gettimeofday(&last, NULL);
233    for (i = 0; i < niter; i++) {
234      for (j = 0; j < nchunks; j++) {
235        if (cbytes == 0) {
236          memcpy(dest2, dest[j], size);
237          nbytes = size;
238        }
239        else {
240          nbytes = blosc_decompress(dest[j], dest2, size);
241        }
242      }
243    }
244    gettimeofday(&current, NULL);
245    tunshuf = get_usec_chunk(last, current);
246    printf("decomp(read):\t %6.1f us, %.1f MB/s\t  ",
247           tunshuf, nbytes/(tunshuf*MB/1e6));
248    if (nbytes < 0) {
249      printf("FAILED.  Error code: %d\n", nbytes);
250    }
251    /* printf("Orig bytes: %d\tFinal bytes: %d\n", cbytes, nbytes); */
252
253    /* Check if data has had a good roundtrip */
254    orig = (unsigned char *)srccpy;
255    round = (unsigned char *)dest2;
256    for(i = 0; i<size; ++i){
257      if (orig[i] != round[i]) {
258        printf("\nError: Original data and round-trip do not match in pos %d\n",
259               (int)i);
260        printf("Orig--> %x, round-trip--> %x\n", orig[i], round[i]);
261        break;
262      }
263    }
264
265    if (i == size) printf("OK\n");
266
267  } /* End clevel loop */
268
269
270  /* To compute the totalsize, we should take into account the 9
271     compression levels */
272  totalsize += (size * nchunks * niter * 9.);
273
274  free(src); free(srccpy); free(dest2);
275  for (i = 0; i < nchunks; i++) {
276    free(dest[i]);
277  }
278
279}
280
281
282/* Compute a sensible value for nchunks */
283int get_nchunks(int size_, int ws) {
284  int nchunks;
285
286  nchunks = ws / size_;
287  if (nchunks > NCHUNKS) nchunks = NCHUNKS;
288  if (nchunks < 1) nchunks = 1;
289  return nchunks;
290}
291
292
293int main(int argc, char *argv[]) {
294  int single = 1;
295  int suite = 0;
296  int hard_suite = 0;
297  int extreme_suite = 0;
298  int nthreads = 1;                     /* The number of threads */
299  int size = 2*MB;                      /* Buffer size */
300  int elsize = 8;                       /* Datatype size */
301  int rshift = 19;                      /* Significant bits */
302  int workingset = 256*MB;              /* The maximum allocated memory */
303  int nthreads_, size_, elsize_, rshift_, i;
304  struct timeval last, current;
305  float totaltime;
306  char *usage = "Usage: bench ['single' | 'suite' | 'hardsuite' | 'extremesuite'] [nthreads [bufsize(bytes) [typesize [sbits ]]]]";
307
308
309  if (strcmp(argv[1], "single") == 0) {
310    single = 1;
311  }
312  else if (strcmp(argv[1], "suite") == 0) {
313    suite = 1;
314  }
315  else if (strcmp(argv[1], "hardsuite") == 0) {
316    hard_suite = 1;
317    workingset = 64*MB;
318    /* Values here are ending points for loops */
319    nthreads = 2;
320    size = 8*MB;
321    elsize = 32;
322    rshift = 32;
323  }
324  else if (strcmp(argv[1], "extremesuite") == 0) {
325    extreme_suite = 1;
326    workingset = 32*MB;
327    niter = 1;
328    /* Warning: values here are starting points for loops.  This is
329       useful for debugging. */
330    nthreads = 1;
331    size = 16*KB;
332    elsize = 1;
333    rshift = 0;
334  }
335  else {
336    printf("%s\n", usage);
337    exit(1);
338  }
339
340  if (argc >= 3) {
341    nthreads = atoi(argv[2]);
342  }
343  if (argc >= 4) {
344    size = atoi(argv[3]);
345  }
346  if (argc >= 5) {
347    elsize = atoi(argv[4]);
348  }
349  if (argc >= 6) {
350    rshift = atoi(argv[5]);
351  }
352
353  if ((argc >= 7) || !(single || suite || hard_suite || extreme_suite)) {
354    printf("%s\n", usage);
355    exit(1);
356  }
357
358  nchunks = get_nchunks(size, workingset);
359  gettimeofday(&last, NULL);
360
361  if (suite) {
362    for (nthreads_=1; nthreads_ <= nthreads; nthreads_++) {
363        do_bench(nthreads_, size, elsize, rshift);
364    }
365  }
366  else if (hard_suite) {
367    for (rshift_ = 0; rshift_ < rshift; rshift_ += 5) {
368      for (elsize_ = 1; elsize_ <= elsize; elsize_ *= 2) {
369        /* The next loop is for getting sizes that are not power of 2 */
370        for (i = -elsize_; i <= elsize_; i += elsize_) {
371          for (size_ = 32*KB; size_ <= size; size_ *= 2) {
372            nchunks = get_nchunks(size_+i, workingset);
373            niter = 1;
374            for (nthreads_ = 1; nthreads_ <= nthreads; nthreads_++) {
375              do_bench(nthreads_, size_+i, elsize_, rshift_);
376              gettimeofday(&current, NULL);
377              totaltime = getseconds(last, current);
378              printf("Elapsed time:\t %6.1f s.  Processed data: %.1f GB\n",
379                     totaltime, totalsize / GB);
380            }
381          }
382        }
383      }
384    }
385  }
386  else if (extreme_suite) {
387    for (rshift_ = rshift; rshift_ <= 32; rshift_++) {
388      for (elsize_ = elsize; elsize_ <= 32; elsize_++) {
389        /* The next loop is for getting sizes that are not power of 2 */
390        for (i = -elsize_*2; i <= elsize_*2; i += elsize_) {
391          for (size_ = size; size_ <= 16*MB; size_ *= 2) {
392            nchunks = get_nchunks(size_+i, workingset);
393            for (nthreads_ = nthreads; nthreads_ <= 4; nthreads_++) {
394              do_bench(nthreads_, size_+i, elsize_, rshift_);
395              gettimeofday(&current, NULL);
396              totaltime = getseconds(last, current);
397              printf("Elapsed time:\t %6.1f s.  Processed data: %.1f GB\n",
398                     totaltime, totalsize / GB);
399            }
400          }
401        }
402      }
403    }
404  }
405  /* Single mode */
406  else {
407    do_bench(nthreads, size, elsize, rshift);
408  }
409
410  /* Print out some statistics */
411  gettimeofday(&current, NULL);
412  totaltime = getseconds(last, current);
413  printf("\nRound-trip compr/decompr on %.1f GB\n", totalsize / GB);
414  printf("Elapsed time:\t %6.1f s, %.1f MB/s\n",
415         totaltime, totalsize*2*1.1/(MB*totaltime));
416
417  /* Free blosc resources */
418  blosc_free_resources();
419
420  return 0;
421}
Note: See TracBrowser for help on using the repository browser.