File: multithread.c

package info (click to toggle)
c-blosc2 2.22.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,020 kB
  • sloc: ansic: 46,325; python: 332; lisp: 82; makefile: 63; sh: 3
file content (108 lines) | stat: -rw-r--r-- 2,868 bytes parent folder | download | duplicates (3)
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
/*
  Copyright (c) 2021  Blosc Development Team <blosc@blosc.org>
  https://blosc.org
  License: BSD 3-Clause (see LICENSE.txt)

  Example program demonstrating use of the Blosc filter from C code.

  To compile this program using gcc or clang:

  $ gcc/clang multithread.c -o multithread -lblosc2 -lpthread

  or, if you don't have the blosc library installed:

  $ gcc -O3 -msse2 multithread.c ../blosc/!(*avx2*)*.c  -I../blosc -o multithread -lpthread

  or alternatively:

  $ gcc -O3 -msse2 multithread.c -I../blosc -o multithread -L../build/blosc -lblosc2
  $ export LD_LIBRARY_PATH=../build/blosc

  Using MSVC on Windows:

  $ cl /Ox /Femultithread.exe /Iblosc multithread.c blosc\*.c

  To run:

  $ ./multithread
  Blosc version info: 1.4.2.dev ($Date:: 2014-07-08 #$)
  Using 1 threads (previously using 1)
  Compression: 4000000 -> 158494 (25.2x)
  Successful roundtrip!
  Using 2 threads (previously using 1)
  Compression: 4000000 -> 158494 (25.2x)
  Successful roundtrip!
  Using 3 threads (previously using 2)
  Compression: 4000000 -> 158494 (25.2x)
  Successful roundtrip!
  Using 4 threads (previously using 3)
  Compression: 4000000 -> 158494 (25.2x)
  Successful roundtrip!

*/

#include <stdio.h>
#include <inttypes.h>
#include <blosc2.h>

#define SIZE (1000 * 1000)


int main(void) {
  static float data[SIZE];
  static float data_out[SIZE];
  static float data_dest[SIZE];
  size_t isize = SIZE * sizeof(float), osize = SIZE * sizeof(float);
  int dsize, csize;
  int16_t nthreads, pnthreads;
  int i;

  for (i = 0; i < SIZE; i++) {
    data[i] = (float)i;
  }

  /* Register the filter with the library */
  printf("Blosc version info: %s (%s)\n",
         BLOSC2_VERSION_STRING, BLOSC2_VERSION_DATE);

  /* Initialize the Blosc compressor */
  blosc2_init();

  /* Tell Blosc to use some number of threads */
  for (nthreads = 1; nthreads <= 4; nthreads++) {

    pnthreads = blosc2_set_nthreads(nthreads);
    printf("Using %d threads (previously using %d)\n", nthreads, pnthreads);

    /* Compress with clevel=5 and shuffle active  */
    csize = blosc1_compress(5, 1, sizeof(float), isize, data, data_out, osize);
    if (csize < 0) {
      printf("Compression error.  Error code: %d\n", csize);
      return csize;
    }

    printf("Compression: %" PRId64 " -> %d (%.1fx)\n", (int64_t)isize, csize, (1. * (double)isize) /
            csize);

    /* Decompress  */
    dsize = blosc1_decompress(data_out, data_dest, isize);
    if (dsize < 0) {
      printf("Decompression error.  Error code: %d\n", dsize);
      return dsize;
    }

    for (i = 0; i < SIZE; i++) {
      if (data[i] != data_dest[i]) {
        printf("Decompressed data differs from original!\n");
        return -1;
      }
    }

    printf("Successful roundtrip!\n");
  }

  /* After using it, destroy the Blosc environment */
  blosc2_destroy();

  return 0;
}