Skip to content

Commit 56e3cec

Browse files
Chaluvadisyurkevi
authored andcommitted
added getting-started examples
1 parent 2967856 commit 56e3cec

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

examples/getting_started/convolve.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2024, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
import sys
13+
from array import array
14+
15+
# from time import time
16+
from timeit import timeit
17+
18+
import arrayfire as af
19+
from arrayfire.library.signal_processing import convolve2, convolve2_separable
20+
21+
22+
def af_assert(left, right, eps=1e-6):
23+
if af.max(af.abs(left - right)) > eps:
24+
raise ValueError("Arrays not within dictated precision")
25+
return
26+
27+
28+
if __name__ == "__main__":
29+
if len(sys.argv) > 1:
30+
af.set_device(int(sys.argv[1]))
31+
af.info()
32+
33+
h_dx = array("f", (1.0 / 12, -8.0 / 12, 0, 8.0 / 12, 1.0 / 12))
34+
h_spread = array("f", (1.0 / 5, 1.0 / 5, 1.0 / 5, 1.0 / 5, 1.0 / 5))
35+
36+
img = af.randu((640, 480))
37+
dx = af.Array(obj=h_dx, shape=(5, 1))
38+
spread = af.Array(obj=h_spread, shape=(1, 5))
39+
kernel = af.matmul(dx, spread)
40+
41+
full_res = convolve2(img, kernel)
42+
sep_res = convolve2_separable(dx, spread, img)
43+
44+
af_assert(full_res, sep_res)
45+
46+
time_convolve2 = timeit(lambda: convolve2(img, kernel), number=1000)
47+
print(f"full 2D convolution time: {time_convolve2 * 1000:.5f} ms")
48+
49+
time_convolve2_sep = timeit(lambda: convolve2_separable(dx, spread, img), number=1000)
50+
print(f"full separable 2D convolution time: {time_convolve2_sep * 1000:.5f} ms")

examples/getting_started/intro.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2024, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
import sys
13+
from array import array
14+
15+
import arrayfire as af
16+
from arrayfire.array_object import _array_as_str
17+
18+
if __name__ == "__main__":
19+
if len(sys.argv) > 1:
20+
af.set_device(int(sys.argv[1]))
21+
af.info()
22+
23+
print("\n---- Intro to ArrayFire using unsigned(s32) arrays ----\n")
24+
25+
h_A = array("i", (1, 2, 4, -1, 2, 0, 4, 2, 3))
26+
h_B = array("i", (2, 3, 5, 6, 0, 10, -12, 0, 1))
27+
28+
A = af.Array(obj=h_A, shape=(3, 3), dtype=af.int32)
29+
B = af.Array(obj=h_B, shape=(3, 3), dtype=af.int32)
30+
31+
print("\n---- Sub referencing and sub assignment\n")
32+
print(_array_as_str(A))
33+
print(_array_as_str(A[0, :]))
34+
print(_array_as_str(A[:, 0]))
35+
A[0, 0] = 11
36+
A[1] = 100
37+
print(_array_as_str(A))
38+
print(_array_as_str(B))
39+
A[1, :] = B[2, :]
40+
print(_array_as_str(A))
41+
42+
b_A = array("I", (1, 1, 1, 0, 1, 1, 0, 0, 0))
43+
b_B = array("I", (1, 0, 1, 0, 1, 0, 1, 0, 1))
44+
45+
C = af.Array(obj=b_A, shape=(3, 3), dtype=af.uint32)
46+
D = af.Array(obj=b_B, shape=(3, 3), dtype=af.uint32)
47+
print(_array_as_str(C))
48+
print(_array_as_str(D))
49+
50+
print("\n---- Bitwise operations\n")
51+
print(_array_as_str(af.bitand(C, D)))
52+
print(_array_as_str(af.bitor(C, D)))
53+
print(_array_as_str(af.bitxor(C, D)))
54+
55+
print("\n---- Transpose\n")
56+
print(_array_as_str(A))
57+
print(_array_as_str(af.transpose(A)))
58+
59+
print("\n---- Flip Vertically / Horizontally\n")
60+
print(_array_as_str(A))
61+
print(_array_as_str(af.flip(A, axis=0)))
62+
print(_array_as_str(af.flip(A, axis=1)))
63+
64+
print("\n---- Sum, Min, Max along row / columns\n")
65+
print(_array_as_str(A))
66+
print(_array_as_str(af.min(A, axis=0))) # type: ignore[arg-type]
67+
print(_array_as_str(af.max(A, axis=0))) # type: ignore[arg-type]
68+
69+
print(_array_as_str(af.min(A, axis=1))) # type: ignore[arg-type]
70+
print(_array_as_str(af.max(A, axis=1))) # type: ignore[arg-type]
71+
72+
print(_array_as_str(af.sum(A, axis=0))) # type: ignore[arg-type]
73+
print(_array_as_str(af.sum(A, axis=1))) # type: ignore[arg-type]
74+
75+
print("\n---- Get minimum with index\n")
76+
(min_val, min_idx) = af.imin(A, axis=0)
77+
print(_array_as_str(min_val)) # type: ignore[arg-type]
78+
print(_array_as_str(min_idx)) # type: ignore[arg-type]

0 commit comments

Comments
 (0)