@@ -485,6 +485,12 @@ cdef extern from "b2nd.h":
485
485
int b2nd_from_schunk(blosc2_schunk * schunk, b2nd_array_t ** array)
486
486
487
487
void blosc2_unidim_to_multidim(uint8_t ndim, int64_t * shape, int64_t i, int64_t * index)
488
+ int b2nd_copy_buffer(int8_t ndim,
489
+ uint8_t itemsize,
490
+ const void * src, const int64_t * src_pad_shape,
491
+ const int64_t * src_start, const int64_t * src_stop,
492
+ void * dst, const int64_t * dst_pad_shape,
493
+ const int64_t * dst_start);
488
494
489
495
490
496
ctypedef struct user_filters_udata:
@@ -508,6 +514,8 @@ ctypedef struct numba_udata:
508
514
int32_t * blockshape
509
515
int32_t * chunkshape_ndim
510
516
int64_t * shape
517
+ int64_t * ext_shape
518
+ b2nd_array_t * array
511
519
512
520
MAX_TYPESIZE = BLOSC_MAX_TYPESIZE
513
521
MAX_BUFFERSIZE = BLOSC2_MAX_BUFFERSIZE
@@ -1566,31 +1574,40 @@ cdef int general_numba(blosc2_prefilter_params *params):
1566
1574
cdef int64_t chunks_in_array[B2ND_MAX_DIM]
1567
1575
for i in range (nd):
1568
1576
# Canviar-ho a extshape i chunkshape normal
1569
- chunks_in_array[i] = udata.shape [i] // udata.chunkshape_ndim[i]
1577
+ chunks_in_array[i] = udata.ext_shape [i] // udata.chunkshape_ndim[i]
1570
1578
blosc2_unidim_to_multidim(nd, chunks_in_array, params.nchunk, chunk_ndim)
1571
1579
# print("nchunk ", params.nchunk)
1572
1580
1573
1581
cdef int64_t block_ndim[B2ND_MAX_DIM]
1574
1582
cdef int64_t blocks_in_chunk[B2ND_MAX_DIM]
1575
1583
for i in range (nd):
1576
- # Canviar-ho a extshape i chunkshape normal
1577
1584
blocks_in_chunk[i] = udata.chunkshape_ndim[i] // udata.blockshape[i]
1578
1585
blosc2_unidim_to_multidim(nd, chunks_in_array, params.nblock, block_ndim)
1579
1586
1580
- cdef int64_t offset_ndim [B2ND_MAX_DIM]
1587
+ cdef int64_t start_ndim [B2ND_MAX_DIM]
1581
1588
for i in range (nd):
1582
- offset_ndim [i] = chunk_ndim[i] * udata.chunkshape_ndim[i] + block_ndim[i] * udata.blockshape[i]
1583
- # print("offset_ndim [", i, "] = ", offset_ndim [i])
1589
+ start_ndim [i] = chunk_ndim[i] * udata.chunkshape_ndim[i] + block_ndim[i] * udata.blockshape[i]
1590
+ # print("start_ndim [", i, "] = ", start_ndim [i])
1584
1591
1592
+ padding = False
1593
+ blockshape = []
1594
+ for i in range (nd):
1595
+ if start_ndim[i] + udata.blockshape[i] > udata.shape[i]:
1596
+ padding = True
1597
+ blockshape.append(udata.shape[i] - start_ndim[i])
1598
+ else :
1599
+ blockshape.append(udata.blockshape[i])
1585
1600
cdef np.npy_intp dims[B2ND_MAX_DIM]
1586
- # params.output_size // params.output_typesize
1587
1601
for i in range (nd):
1588
- dims[i] = udata.blockshape[i] # Açò canviarà quan hi haja padding
1589
- output = np.PyArray_SimpleNewFromData(nd, dims, udata.output_cdtype, < void * > params.output)
1602
+ dims[i] = blockshape[i]
1590
1603
1604
+ if padding:
1605
+ output = np.empty(blockshape, udata.array.dtype) # igual toca crear un py dtype
1606
+ print (output.dtype)
1607
+ else :
1608
+ output = np.PyArray_SimpleNewFromData(nd, dims, udata.output_cdtype, < void * > params.output)
1591
1609
inputs_tuple = _ctypes.PyObj_FromPtr(udata.inputs_id)
1592
1610
inputs = []
1593
- blockshape = [udata.blockshape[i] for i in range (nd)]
1594
1611
if nd == 1 :
1595
1612
# Enviar-ho a fer la mà quan nd = 1 ? o ho puc suportar sense problemes??
1596
1613
for obj, dtype in inputs_tuple:
@@ -1608,8 +1625,8 @@ cdef int general_numba(blosc2_prefilter_params *params):
1608
1625
# Get tuple of slices
1609
1626
l = []
1610
1627
for i in range (nd):
1611
- # print(" slice dim ", i, " = inici = ", offset_ndim [i], " final = ", offset_ndim [i] + udata. blockshape[i])
1612
- l.append( slice (offset_ndim [i], offset_ndim [i] + udata. blockshape[i]) )
1628
+ l.append( slice (start_ndim [i], start_ndim [i] + blockshape[i]) )
1629
+ # print(" slice dim ", i, " = inici = ", start_ndim [i], " final = ", start_ndim [i] + blockshape[i])
1613
1630
slices = tuple (l)
1614
1631
for obj, dtype in inputs_tuple:
1615
1632
if isinstance (obj, blosc2.SChunk):
@@ -1624,14 +1641,24 @@ cdef int general_numba(blosc2_prefilter_params *params):
1624
1641
raise ValueError (" Unsupported operand" )
1625
1642
1626
1643
func_id = udata.py_func.decode(" utf-8" )
1627
- # out = np.empty(blockshape, dtype)
1628
1644
blosc2.prefilter_funcs[func_id](tuple (inputs), output, offset)
1629
1645
1630
- # Tornarem a intentar-ho amb el capi ndarray sembla que les posicions no es formategen bé
1631
- # cdef Py_buffer *buf = <Py_buffer *> malloc(sizeof(Py_buffer))
1632
- # PyObject_GetBuffer(out, buf, PyBUF_SIMPLE)
1633
- # memcpy(params.output, buf.buf, buf.len)
1634
- # PyBuffer_Release(buf)
1646
+ cdef int64_t start[B2ND_MAX_DIM]
1647
+ cdef int64_t slice_shape[B2ND_MAX_DIM]
1648
+ cdef int64_t blockshape_int64[B2ND_MAX_DIM]
1649
+
1650
+ cdef Py_buffer * buf
1651
+ if padding:
1652
+ for i in range (nd):
1653
+ start[i] = 0
1654
+ slice_shape[i] = blockshape[i]
1655
+ blockshape_int64[i] = udata.blockshape[i]
1656
+ buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1657
+ PyObject_GetBuffer(output, buf, PyBUF_SIMPLE)
1658
+ b2nd_copy_buffer(udata.ndim, params.output_typesize,
1659
+ buf.buf, slice_shape, start, slice_shape,
1660
+ params.output, blockshape_int64, start)
1661
+ PyBuffer_Release(buf)
1635
1662
1636
1663
return 0
1637
1664
@@ -2282,6 +2309,8 @@ cdef class NDArray:
2282
2309
pref_udata.blockshape = self .array.blockshape
2283
2310
pref_udata.shape = self .array.shape
2284
2311
pref_udata.chunkshape_ndim = self .array.chunkshape
2312
+ pref_udata.ext_shape = self .array.extshape
2313
+ pref_udata.array = self .array
2285
2314
2286
2315
preparams.user_data = pref_udata
2287
2316
cparams.preparams = preparams
0 commit comments