multithread.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright (C) 2014 Francesc Alted
  3. http://blosc.org
  4. License: MIT (see LICENSE.txt)
  5. Example program demonstrating use of the Blosc filter from C code.
  6. To compile this program using gcc or clang:
  7. gcc/clang multithread.c -o multithread -lblosc -lpthread
  8. or, if you don't have the blosc library installed:
  9. gcc -O3 -msse2 multithread.c ../blosc/!(*avx2*)*.c -I../blosc -o multithread -lpthread
  10. Using MSVC on Windows:
  11. cl /Ox /Femultithread.exe /Iblosc multithread.c blosc\*.c
  12. To run:
  13. $ ./multithread
  14. Blosc version info: 1.4.2.dev ($Date:: 2014-07-08 #$)
  15. Using 1 threads (previously using 1)
  16. Compression: 4000000 -> 158494 (25.2x)
  17. Succesful roundtrip!
  18. Using 2 threads (previously using 1)
  19. Compression: 4000000 -> 158494 (25.2x)
  20. Succesful roundtrip!
  21. Using 3 threads (previously using 2)
  22. Compression: 4000000 -> 158494 (25.2x)
  23. Succesful roundtrip!
  24. Using 4 threads (previously using 3)
  25. Compression: 4000000 -> 158494 (25.2x)
  26. Succesful roundtrip!
  27. */
  28. #include <stdio.h>
  29. #include <blosc.h>
  30. #define SIZE 1000*1000
  31. int main(){
  32. static float data[SIZE];
  33. static float data_out[SIZE];
  34. static float data_dest[SIZE];
  35. int isize = SIZE*sizeof(float), osize = SIZE*sizeof(float);
  36. int dsize = SIZE*sizeof(float), csize;
  37. int nthreads, pnthreads, i;
  38. for(i=0; i<SIZE; i++){
  39. data[i] = i;
  40. }
  41. /* Register the filter with the library */
  42. printf("Blosc version info: %s (%s)\n",
  43. BLOSC_VERSION_STRING, BLOSC_VERSION_DATE);
  44. /* Initialize the Blosc compressor */
  45. blosc_init();
  46. /* Tell Blosc to use some number of threads */
  47. for (nthreads=1; nthreads <= 4; nthreads++) {
  48. pnthreads = blosc_set_nthreads(nthreads);
  49. printf("Using %d threads (previously using %d)\n", nthreads, pnthreads);
  50. /* Compress with clevel=5 and shuffle active */
  51. csize = blosc_compress(5, 1, sizeof(float), isize, data, data_out, osize);
  52. if (csize < 0) {
  53. printf("Compression error. Error code: %d\n", csize);
  54. return csize;
  55. }
  56. printf("Compression: %d -> %d (%.1fx)\n", isize, csize, (1.*isize) / csize);
  57. /* Decompress */
  58. dsize = blosc_decompress(data_out, data_dest, dsize);
  59. if (dsize < 0) {
  60. printf("Decompression error. Error code: %d\n", dsize);
  61. return dsize;
  62. }
  63. for(i=0;i<SIZE;i++){
  64. if(data[i] != data_dest[i]) {
  65. printf("Decompressed data differs from original!\n");
  66. return -1;
  67. }
  68. }
  69. printf("Succesful roundtrip!\n");
  70. }
  71. /* After using it, destroy the Blosc environment */
  72. blosc_destroy();
  73. return 0;
  74. }