Skip to content

Commit fcf1a8a

Browse files
committed
example
1 parent 5a24541 commit fcf1a8a

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
2+
# See file LICENSE for terms.
3+
4+
import cupy
5+
import numpy
6+
import zarr
7+
8+
import kvikio
9+
import kvikio.zarr
10+
11+
12+
def main(path):
13+
a = cupy.arange(20)
14+
15+
# Let's use KvikIO's convenience function `open_cupy_array()` to create
16+
# a new Zarr file on disk. Its semantic is the same as `zarr.open_array()`
17+
# but uses a GDS file store, nvCOMP compression, and CuPy arrays.
18+
z = kvikio.zarr.open_cupy_array(store=path, mode="w", shape=(20,), chunks=(5,))
19+
20+
# `z` is a regular Zarr Array that we can write to as usual
21+
z[0:10] = numpy.arange(0, 10)
22+
# but it also support direct reads and writes of CuPy arrays
23+
z[10:20] = numpy.arange(10, 20)
24+
25+
# Reading `z` returns a CuPy array
26+
assert isinstance(z[:], cupy.ndarray)
27+
assert (a == z[:]).all()
28+
29+
# By default, `open_cupy_array()` uses nvCOMP's `lz4` GPU compression, which is
30+
# compatible with NumCodecs's `lz4` CPU compression (CPU). Normally, it is not
31+
# possible to change which decompressor to use when reading a Zarr file. The
32+
# decompressor specified in the Zarr file's metadata is always used. However,
33+
# `open_cupy_array()` makes it possible to overwrite the metadata on-the-fly
34+
# without having to modify the Zarr file on disk. In fact, the Zarr file written
35+
# above appears, in the metadata, as if it was written by NumCodecs's `lz4` CPU
36+
# compression. Thus, we can open the file using Zarr's regular API and the CPU.
37+
z = zarr.open_array(path)
38+
# `z` is now read as a regular NumPy array
39+
assert isinstance(z[:], numpy.ndarray)
40+
assert (a.get() == z[:]).all()
41+
# and we can write to is as usual
42+
z[:] = numpy.arange(20, 40)
43+
44+
# Let's read the Zarr file back into a CuPy array
45+
z = kvikio.zarr.open_cupy_array(store=path, mode="r")
46+
assert isinstance(z[:], cupy.ndarray)
47+
assert (cupy.arange(20, 40) == z[:]).all()
48+
49+
50+
if __name__ == "__main__":
51+
main("/tmp/zarr-cupy-nvcomp")

python/tests/test_examples.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
1+
# Copyright (c) 2021-2023, NVIDIA CORPORATION. All rights reserved.
22
# See file LICENSE for terms.
33

44
import os
@@ -16,3 +16,13 @@ def test_hello_world(tmp_path, monkeypatch):
1616

1717
monkeypatch.syspath_prepend(str(examples_path))
1818
import_module("hello_world").main(tmp_path / "test-file")
19+
20+
21+
def test_zarr_cupy_nvcomp(tmp_path, monkeypatch):
22+
"""Test examples/zarr_cupy_nvcomp.py"""
23+
24+
# `examples/zarr_cupy_nvcomp.py` requires the Zarr submodule
25+
pytest.importorskip("kvikio.zarr")
26+
27+
monkeypatch.syspath_prepend(str(examples_path))
28+
import_module("zarr_cupy_nvcomp").main(tmp_path / "test-file")

0 commit comments

Comments
 (0)