source: trunk/src/bench.c @ 93

Revision 93, 8.1 KB checked in by faltet, 3 years ago (diff)

Include more cases for the benchmark suite.

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:
9
10    gcc -O3 -msse2 -o bench bench.c blosc.c blosclz.c shuffle.c
11
12  I'm collecting speeds for different machines, so the output of your
13  benchmarks and your processor specifications are welcome!
14
15  Author: Francesc Alted (faltet@pytables.org)
16
17  See LICENSES/BLOSC.txt for details about copyright and rights to use.
18**********************************************************************/
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#ifdef _WIN32
27  #include <time.h>
28#else
29  #include <unistd.h>
30  #include <sys/time.h>
31#endif
32#include <math.h>
33#include "blosc.h"
34
35#define KB  1024
36#define MB  (1024*1024)
37
38/* #define NCHUNKS (100) */
39/* #define NITER  (10)               /\* Number of iterations *\/ */
40#define NCHUNKS (128)
41#define NITER  (10)               /* Number of iterations */
42
43
44#ifdef _WIN32
45#include <windows.h>
46#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
47  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
48#else
49  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
50#endif
51
52struct timezone
53{
54  int  tz_minuteswest; /* minutes W of Greenwich */
55  int  tz_dsttime;     /* type of dst correction */
56};
57
58int gettimeofday(struct timeval *tv, struct timezone *tz)
59{
60  FILETIME ft;
61  unsigned __int64 tmpres = 0;
62  static int tzflag;
63
64  if (NULL != tv)
65  {
66    GetSystemTimeAsFileTime(&ft);
67
68    tmpres |= ft.dwHighDateTime;
69    tmpres <<= 32;
70    tmpres |= ft.dwLowDateTime;
71
72    /*converting file time to unix epoch*/
73    tmpres -= DELTA_EPOCH_IN_MICROSECS;
74    tmpres /= 10;  /*convert into microseconds*/
75    tv->tv_sec = (long)(tmpres / 1000000UL);
76    tv->tv_usec = (long)(tmpres % 1000000UL);
77  }
78
79  if (NULL != tz)
80  {
81    if (!tzflag)
82    {
83      _tzset();
84      tzflag++;
85    }
86    tz->tz_minuteswest = _timezone / 60;
87    tz->tz_dsttime = _daylight;
88  }
89
90  return 0;
91}
92#endif   /* _WIN32 */
93
94
95/* Given two timeval stamps, return the difference in seconds */
96float getseconds(struct timeval last, struct timeval current) {
97  int sec, usec;
98
99  sec = current.tv_sec - last.tv_sec;
100  usec = current.tv_usec - last.tv_usec;
101  return (float)(((double)sec + usec*1e-6)/((double)NITER*NCHUNKS)*1e6);
102}
103
104
105int get_value(int i, int rshift) {
106  int v;
107
108  v = (i<<26)^(i<<18)^(i<<11)^(i<<3)^i;
109  if (rshift < 32) {
110    v &= (1 << rshift) - 1;
111  }
112  return v;
113}
114
115
116void init_buffer(void *src, int size, int rshift) {
117  unsigned int i;
118  int *_src = (int *)src;
119
120  /* To have reproducible results */
121  srand(1);
122
123  /* Initialize the original buffer */
124  for (i = 0; i < size/sizeof(int); ++i) {
125    /* Choose one below */
126    //_src[i] = 0;
127    //_src[i] = 0x01010101;
128    //_src[i] = 0x01020304;
129    //_src[i] = i * 1/.3;
130    //_src[i] = i;
131    //_src[i] = rand() >> (32-rshift);
132    _src[i] = get_value(i, rshift);
133  }
134}
135
136
137do_bench(int nthreads, unsigned int size, int elsize, int rshift) {
138  void *src, *srccpy;
139  void **dest[NCHUNKS], *dest2;
140  int nbytes, cbytes;
141  size_t i, j;
142  struct timeval last, current;
143  float tmemcpy, tshuf, tunshuf;
144  int clevel, doshuffle=1;
145  unsigned char *orig, *round;
146
147  blosc_set_nthreads(nthreads);
148
149  /* Initialize buffers */
150  src = malloc(size);
151  srccpy = malloc(size);
152  dest2 = malloc(size);
153  init_buffer(src, size, rshift);
154  memcpy(srccpy, src, size);
155  for (j = 0; j < NCHUNKS; j++) {
156    dest[j] = malloc(size);
157  }
158
159  printf("********************** Run info ******************************\n");
160  printf("Blosc version: %s (%s)\n", BLOSC_VERSION_STRING, BLOSC_VERSION_DATE);
161  printf("Using random data with %d significant bits (out of 32)\n", rshift);
162  printf("Dataset size: %d bytes\tType size: %d bytes\n", size, elsize);
163  printf("Working set: %.1f MB\t\t", (size*NCHUNKS) / (float)MB);
164  printf("Number of threads: %d\n", nthreads);
165  printf("********************** Running benchmarks *********************\n");
166
167  gettimeofday(&last, NULL);
168  for (i = 0; i < NITER; i++) {
169    for (j = 0; j < NCHUNKS; j++) {
170      memcpy(dest[j], src, size);
171    }
172  }
173  gettimeofday(&current, NULL);
174  tmemcpy = getseconds(last, current);
175  printf("memcpy(write):\t\t %6.1f us, %.1f MB/s\n",
176         tmemcpy, size/(tmemcpy*MB/1e6));
177
178  gettimeofday(&last, NULL);
179  for (i = 0; i < NITER; i++) {
180    for (j = 0; j < NCHUNKS; j++) {
181      memcpy(dest2, dest[j], size);
182    }
183  }
184  gettimeofday(&current, NULL);
185  tmemcpy = getseconds(last, current);
186  printf("memcpy(read):\t\t %6.1f us, %.1f MB/s\n",
187         tmemcpy, size/(tmemcpy*MB/1e6));
188
189  for (clevel=1; clevel<10; clevel++) {
190
191    printf("Compression level: %d\n", clevel);
192
193    gettimeofday(&last, NULL);
194    for (i = 0; i < NITER; i++) {
195      for (j = 0; j < NCHUNKS; j++) {
196        cbytes = blosc_compress(clevel, doshuffle, elsize, size, src, dest[j]);
197      }
198    }
199    gettimeofday(&current, NULL);
200    tshuf = getseconds(last, current);
201    printf("comp(write):\t %6.1f us, %.1f MB/s\t  ",
202           tshuf, size/(tshuf*MB/1e6));
203    printf("Final bytes: %d  ", cbytes);
204    if (cbytes > 0) {
205      printf("Ratio: %3.2f", size/(float)cbytes);
206    }
207    printf("\n");
208
209    /* Compressor was unable to compress.  Copy the buffer manually. */
210    if (cbytes == 0) {
211      for (j = 0; j < NCHUNKS; j++) {
212        memcpy(dest[j], src, size);
213      }
214    }
215
216    gettimeofday(&last, NULL);
217    for (i = 0; i < NITER; i++) {
218      for (j = 0; j < NCHUNKS; j++) {
219        if (cbytes == 0) {
220          memcpy(dest2, dest[j], size);
221          nbytes = size;
222        }
223        else {
224          nbytes = blosc_decompress(dest[j], dest2, size);
225        }
226      }
227    }
228    gettimeofday(&current, NULL);
229    tunshuf = getseconds(last, current);
230    printf("decomp(read):\t %6.1f us, %.1f MB/s\t  ",
231           tunshuf, nbytes/(tunshuf*MB/1e6));
232    if (nbytes < 0) {
233      printf("FAILED.  Error code: %d\n", nbytes);
234    }
235    /* printf("Orig bytes: %d\tFinal bytes: %d\n", cbytes, nbytes); */
236
237    /* Check if data has had a good roundtrip */
238    orig = (unsigned char *)srccpy;
239    round = (unsigned char *)dest2;
240    for(i = 0; i<size; ++i){
241      if (orig[i] != round[i]) {
242        printf("\nError: Original data and round-trip do not match in pos %d\n",
243               (int)i);
244        printf("Orig--> %x, round-trip--> %x\n", orig[i], round[i]);
245        break;
246      }
247    }
248
249    if (i == size) printf("OK\n");
250
251  } /* End clevel loop */
252
253  free(src); free(srccpy); free(dest2);
254  for (i = 0; i < NCHUNKS; i++) {
255    free(dest[i]);
256  }
257
258}
259
260
261int main(int argc, char *argv[]) {
262  int suite = 0;
263  int hard_suite = 0;
264  int nthreads = 1;                /* The number of threads */
265  unsigned int size = 1024*1024;   /* Buffer size */
266  unsigned int elsize = 4;         /* Datatype size */
267  int rshift = 20;                 /* Significant bits */
268
269  if ((argc == 2) && strcmp(argv[1], "suite") == 0) {
270    suite = 1;
271  }
272  else if ((argc == 2) && strcmp(argv[1], "hard_suite") == 0) {
273    hard_suite = 1;
274  }
275  else if (argc >= 2) {
276    nthreads = atoi(argv[1]);
277  }
278 
279  if (argc >= 3) {
280    size = atoi(argv[2])*1024;
281    if (size > 2*1024*1024) {
282      printf("The test is going to require more than 256 MB of RAM!\n");
283    }
284  }
285  if (argc >= 4) {
286    elsize = atoi(argv[3]);
287  }
288  if (argc >= 5) {
289    rshift = atoi(argv[4]);
290  }
291  if (argc >= 6) {
292    printf("Usage: bench 'suite' | 'hard_suite' | [nthreads [bufsize(KB) [typesize [sbits ]]]]\n");
293    exit(1);
294  }
295
296  if (suite) {
297    for (nthreads=1; nthreads <= 10; nthreads++) {
298      for (size=32*KB; size <= 8*MB; size *=2) {
299        for (elsize=1; elsize <= 32; elsize *=2) {
300          do_bench(nthreads, size, elsize, rshift);
301        }
302      }
303    }
304  }
305  else if (hard_suite) {
306    for (nthreads=1; nthreads < 10; nthreads++) {
307      for (size=32*KB; size < 8*MB; size *=2) {
308        for (elsize=1; elsize < 32; elsize *=2) {
309          for (rshift=0; rshift < 32; rshift++) {
310            do_bench(nthreads, size, elsize, rshift);
311          }
312        }
313      }
314    }
315  }
316  else {
317    do_bench(nthreads, size, elsize, rshift);
318  }
319
320  /* Free blosc resources */
321  blosc_free_resources();
322
323  return 0;
324}
Note: See TracBrowser for help on using the repository browser.