Skip to content

Commit 0ce9c30

Browse files
committed
Examples on how to use the jit decorator (for both expressions and reductions)
1 parent 77cc75e commit 0ce9c30

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

examples/ndarray/jit-expr.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#######################################################################
2+
# Copyright (c) 2019-present, Blosc Development Team <[email protected]>
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under a BSD-style license (found in the
6+
# LICENSE file in the root directory of this source tree)
7+
#######################################################################
8+
9+
# Examples of using the jit decorator with expressions
10+
# You can find benchmarks for this example in the bench/ndarray directory
11+
12+
import blosc2
13+
import numpy as np
14+
15+
# Example 1: Basic usage of the jit decorator
16+
@blosc2.jit
17+
def expr_jit(a, b, c):
18+
# This function computes a boolean array where the condition is met
19+
return ((a ** 3 + np.sin(a * 2)) < c) & (b > 0)
20+
21+
# Create some sample data
22+
a = blosc2.linspace(0, 1, 10 * 100, dtype="float32", shape=(10, 100))
23+
b = blosc2.linspace(1, 2, 10 * 100, dtype="float32", shape=(10, 100))
24+
c = blosc2.linspace(-10, 10, 10, dtype="float32", shape=(100,))
25+
26+
# Call the function with the jit decorator
27+
result = expr_jit(a, b, c)
28+
print(result[1,:10])
29+
30+
# Example 2: Using the jit decorator with an out parameter
31+
out = blosc2.zeros((10, 100), dtype=np.bool_)
32+
33+
@blosc2.jit(out=out)
34+
def expr_jit_out(a, b, c):
35+
# This function computes a boolean array and stores the result in the 'out' array
36+
return ((a ** 3 + np.sin(a * 2)) < c) & (b > 0)
37+
38+
# Call the function with the jit decorator and out parameter
39+
result_out = expr_jit_out(a, b, c)
40+
print(result_out[1,:10])
41+
print(out[1,:10]) # The 'out' array should now contain the same result
42+
43+
# Example 3: Using the jit decorator with additional keyword arguments
44+
cparams = blosc2.CParams(clevel=1, codec=blosc2.Codec.LZ4, filters=[blosc2.Filter.BITSHUFFLE])
45+
46+
@blosc2.jit(cparams=cparams)
47+
def expr_jit_cparams(a, b, c):
48+
# This function computes a boolean array with custom compression parameters
49+
return ((a ** 3 + np.sin(a * 2)) < c) & (b > 0)
50+
51+
# Call the function with the jit decorator and custom parameters
52+
result_cparams = expr_jit_cparams(a, b, c)
53+
print(result_cparams[1,:10])

examples/ndarray/jit-reduc.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#######################################################################
2+
# Copyright (c) 2019-present, Blosc Development Team <[email protected]>
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under a BSD-style license (found in the
6+
# LICENSE file in the root directory of this source tree)
7+
#######################################################################
8+
9+
# Examples of using the jit decorator with reductions
10+
# You can find benchmarks for this example in the bench/ndarray directory
11+
12+
import blosc2
13+
import numpy as np
14+
15+
# Example 1: Basic usage of the jit decorator with reduction
16+
@blosc2.jit
17+
def expr_jit(a, b, c):
18+
# This function computes a sum reduction along axis 1
19+
return np.sum(((a ** 3 + np.sin(a * 2)) < c) & (b > 0), axis=1)
20+
21+
# Create some sample data
22+
a = blosc2.linspace(0, 1, 10 * 100, dtype="float32", shape=(10, 100))
23+
b = blosc2.linspace(1, 2, 10 * 100, dtype="float32", shape=(10, 100))
24+
c = blosc2.linspace(-10, 10, 10, dtype="float32", shape=(100,))
25+
26+
# Call the function with the jit decorator
27+
result = expr_jit(a, b, c)
28+
print("Example 1 result:", result)
29+
30+
# Example 2: Using the jit decorator with an out parameter for reduction
31+
out = np.zeros((10,), dtype=np.int64)
32+
33+
@blosc2.jit
34+
def expr_jit_out(a, b, c):
35+
# This function computes a sum reduction along axis 1 and stores the result in the 'out' array
36+
return np.sum(((a ** 3 + np.sin(a * 2)) < c) & (b > 0), axis=1, out=out)
37+
38+
# Call the function with the jit decorator and out parameter
39+
result_out = expr_jit_out(a, b, c)
40+
print("Example 2 result:", result_out)
41+
print("Example 2 out array:", out) # The 'out' array should now contain the same result
42+
43+
# Example 3: Using the jit decorator with additional keyword arguments for reduction
44+
cparams = blosc2.CParams(clevel=1, codec=blosc2.Codec.LZ4, filters=[blosc2.Filter.BITSHUFFLE])
45+
out_cparams = blosc2.zeros((10,), dtype=np.int64, cparams=cparams)
46+
47+
@blosc2.jit
48+
def expr_jit_cparams(a, b, c):
49+
# This function computes a sum reduction along axis 1 with custom compression parameters
50+
return np.sum(((a ** 3 + np.sin(a * 2)) < c) & (b > 0), axis=1, out=out_cparams)
51+
52+
# Call the function with the jit decorator and custom parameters
53+
result_cparams = expr_jit_cparams(a, b, c)
54+
print("Example 3 result:", result_cparams[...])
55+
print("Example 3 out array:", out_cparams[...]) # The 'out_cparams' array should now contain the same result

0 commit comments

Comments
 (0)