source: trunk/bench/bench.c @ 114

Revision 114, 8.5 KB checked in by faltet, 4 years ago (diff)

Fixes for supporting MINGW compiler.

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