Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/blosc2/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,19 @@ def check_contiguity(container, part):
return False
return builtins.all(size % unit == 0 for size, unit in zip(container[:-1], part[:-1], strict=True))

return check_contiguity(chunks, blocks) and check_contiguity(shape, chunks)
def blocks_contiguous(chunks, blocks):
# A block is a C-contiguous run of its chunk only if its leading dims are 1,
# then one dim divides the chunk's, and the rest are equal to the chunk's.
# Same rule as nchunk_fastpath() in C-Blosc2's b2nd.c.
k = 0
for i, (size, unit) in enumerate(zip(chunks, blocks, strict=True)):
if size % unit != 0 or (i > k and size != unit):
return False
if i == k and unit == 1:
k += 1
return True

return blocks_contiguous(chunks, blocks) and check_contiguity(shape, chunks)


def get_flat_slices_orig(shape: tuple[int], s: tuple[slice, ...]) -> list[slice]:
Expand Down
22 changes: 22 additions & 0 deletions tests/ndarray/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ def test_asarray_chunks_larger_than_shape(shape, chunks, blocks):
assert not blosc2.are_partitions_behaved(shape, chunks, blocks)


@pytest.mark.parametrize(
("shape", "chunks", "blocks", "behaved"),
[
# blocks divide the chunk but are not C-contiguous runs of it
((256, 32, 16, 32), (256, 32, 16, 32), (32, 1, 16, 32), False),
((64, 64, 64, 16), (64, 64, 64, 16), (8, 2, 64, 16), False),
# contiguous blocks keep the fast path
((256, 32, 16, 32), (256, 32, 16, 32), (1, 4, 16, 32), True),
],
)
def test_asarray_blocks_not_contiguous_in_chunk(shape, chunks, blocks, behaved):
# Above 16 MB, asarray copies whole chunks with update_data, which is only
# valid when every block is a C-contiguous run of its chunk
a = np.arange(math.prod(shape), dtype=np.float64).reshape(shape)
assert a.nbytes > 2**24

b = blosc2.asarray(a, chunks=chunks, blocks=blocks)

np.testing.assert_array_equal(b[:], a)
assert blosc2.are_partitions_behaved(shape, chunks, blocks) == behaved


def test_asarray_persists_copy_with_urlpath(tmp_path):
array = blosc2.asarray(np.arange(10, dtype=np.int64), chunks=(5,), blocks=(2,))
path = tmp_path / "persisted_copy.b2nd"
Expand Down
Loading