@@ -18,6 +18,7 @@ import shutil
18
18
import tempfile
19
19
from collections import namedtuple
20
20
from glob import glob
21
+ import multiprocessing
21
22
import fasteners
22
23
23
24
@@ -53,6 +54,14 @@ cdef extern from "blosc.h":
53
54
BLOSC_VERSION_STRING,
54
55
BLOSC_VERSION_DATE
55
56
57
+ void blosc_init()
58
+ void blosc_destroy()
59
+ int blosc_set_nthreads(int nthreads)
60
+ int blosc_set_compressor(const char * compname)
61
+ int blosc_compress(int clevel, int doshuffle, size_t typesize,
62
+ size_t nbytes, void * src, void * dest,
63
+ size_t destsize) nogil
64
+ int blosc_decompress(void * src, void * dest, size_t destsize) nogil
56
65
int blosc_compname_to_compcode(const char * compname)
57
66
int blosc_compress_ctx(int clevel, int doshuffle, size_t typesize,
58
67
size_t nbytes, const void * src, void * dest,
@@ -77,6 +86,26 @@ def blosc_version():
77
86
return ver_str, ver_date
78
87
79
88
89
+ def init ():
90
+ blosc_init()
91
+
92
+
93
+ def destroy ():
94
+ blosc_destroy()
95
+
96
+
97
+ _blosc_use_context = False
98
+
99
+
100
+ def set_blosc_options (use_context , nthreads = None ):
101
+ global _blosc_use_context
102
+ _blosc_use_context = use_context
103
+ if not use_context:
104
+ if nthreads is None :
105
+ nthreads = multiprocessing.cpu_count()
106
+ blosc_set_nthreads(nthreads)
107
+
108
+
80
109
# ##############################################################################
81
110
# DEBUG LOGGING #
82
111
# ##############################################################################
@@ -123,7 +152,7 @@ def _normalize_cparams(cname=None, clevel=None, shuffle=None):
123
152
cname = cname.encode()
124
153
# check compressor is available
125
154
if blosc_compname_to_compcode(cname) < 0 :
126
- raise ValueError (" compressor not available: %s " % cname)
155
+ raise ValueError (' compressor not available: %s ' % cname)
127
156
128
157
# determine compression level
129
158
clevel = clevel if clevel is not None else defaults.clevel
@@ -390,8 +419,12 @@ cdef class Chunk(BaseChunk):
390
419
cdef int ret
391
420
392
421
# do decompression
393
- with nogil:
394
- ret = blosc_decompress_ctx(self ._data, dest, self ._nbytes, 1 )
422
+ if _blosc_use_context:
423
+ with nogil:
424
+ ret = blosc_decompress_ctx(self ._data, dest, self ._nbytes, 1 )
425
+ else :
426
+ with nogil:
427
+ ret = blosc_decompress(self ._data, dest, self ._nbytes)
395
428
396
429
# handle errors
397
430
if ret <= 0 :
@@ -409,12 +442,20 @@ cdef class Chunk(BaseChunk):
409
442
dest = < char * > malloc(self ._nbytes + BLOSC_MAX_OVERHEAD)
410
443
411
444
# perform compression
412
- with nogil:
413
- cbytes = blosc_compress_ctx(self ._clevel, self ._shuffle,
414
- self ._itemsize, self ._nbytes,
415
- source, dest,
416
- self ._nbytes + BLOSC_MAX_OVERHEAD,
417
- self ._cname, 0 , 1 )
445
+ if _blosc_use_context:
446
+ with nogil:
447
+ cbytes = blosc_compress_ctx(self ._clevel, self ._shuffle,
448
+ self ._itemsize, self ._nbytes,
449
+ source, dest,
450
+ self ._nbytes + BLOSC_MAX_OVERHEAD,
451
+ self ._cname, 0 , 1 )
452
+ else :
453
+ # compressor should have been checked already
454
+ assert blosc_set_compressor(self ._cname) >= 0
455
+ with nogil:
456
+ cbytes = blosc_compress(self ._clevel, self ._shuffle,
457
+ self ._itemsize, self ._nbytes, source,
458
+ dest, self ._nbytes + BLOSC_MAX_OVERHEAD)
418
459
419
460
# check compression was successful
420
461
if cbytes <= 0 :
@@ -506,8 +547,12 @@ cdef class PersistentChunk(BaseChunk):
506
547
source = PyBytes_AsString(data)
507
548
508
549
# do decompression
509
- with nogil:
510
- ret = blosc_decompress_ctx(source, dest, self ._nbytes, 1 )
550
+ if _blosc_use_context:
551
+ with nogil:
552
+ ret = blosc_decompress_ctx(source, dest, self ._nbytes, 1 )
553
+ else :
554
+ with nogil:
555
+ ret = blosc_decompress(source, dest, self ._nbytes)
511
556
512
557
# handle errors
513
558
if ret <= 0 :
@@ -543,12 +588,20 @@ cdef class PersistentChunk(BaseChunk):
543
588
dest = < char * > malloc(self ._nbytes + BLOSC_MAX_OVERHEAD)
544
589
545
590
# perform compression
546
- with nogil:
547
- cbytes = blosc_compress_ctx(self ._clevel, self ._shuffle,
548
- self ._itemsize, self ._nbytes,
549
- source, dest,
550
- self ._nbytes + BLOSC_MAX_OVERHEAD,
551
- self ._cname, 0 , 1 )
591
+ if _blosc_use_context:
592
+ with nogil:
593
+ cbytes = blosc_compress_ctx(self ._clevel, self ._shuffle,
594
+ self ._itemsize, self ._nbytes,
595
+ source, dest,
596
+ self ._nbytes + BLOSC_MAX_OVERHEAD,
597
+ self ._cname, 0 , 1 )
598
+ else :
599
+ # compressor should have been checked already
600
+ assert blosc_set_compressor(self ._cname) >= 0
601
+ with nogil:
602
+ cbytes = blosc_compress(self ._clevel, self ._shuffle,
603
+ self ._itemsize, self ._nbytes, source,
604
+ dest, self ._nbytes + BLOSC_MAX_OVERHEAD)
552
605
553
606
# check compression was successful
554
607
if cbytes <= 0 :
@@ -1322,6 +1375,7 @@ cdef class SynchronizedPersistentArray(PersistentArray):
1322
1375
# ##############################################################################
1323
1376
1324
1377
1378
+ # noinspection PyUnresolvedReferences,PyProtectedMember
1325
1379
cdef _lazy_get_chunk(BaseArray array, tuple cidx):
1326
1380
try :
1327
1381
chunk = array._cdata[cidx]
0 commit comments