forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression-var3.py
More file actions
137 lines (112 loc) · 3.58 KB
/
Copy pathexpression-var3.py
File metadata and controls
137 lines (112 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Comparison of complex expressions performance when using 1 or 3 variables
import os
from time import time
import numexpr as ne
import numpy as np
import iarray as ia
from iarray import udf
from iarray.udf import Array
from iarray.py2llvm import float64, int64
NITER = 3 # number of iterations per benchmark
PROFILE = False
NVARS = 3 # number of variables in expression (only 1 or 3)
assert NVARS in (1, 3)
expr1 = "(x - 1.35) * (x - 4.45) * (x - 8.5)"
expr3 = "(x - 1.35) * (y - 4.45) * (z - 8.5)"
# Define array params
shape = [20_000_000]
#chunkshape, blockshape = [2_000_000], [20_000]
#chunkshape, blockshape = [2**24], [2**16]
chunkshape, blockshape = None, None # uncomment for automatic partitioning
dtype = np.float64
nthreads = 8
dtshape = ia.DTShape(shape=shape, dtype=dtype)
# Set global defaults
ia.set_config(chunkshape=chunkshape, blockshape=blockshape,
favor=ia.Favors.SPEED)
# Output required precision (in significant bits for the mantissa)
#out_prec = 0
out_prec = 30
@udf.jit(verbose=0)
def f1(out: Array(float64, 1), x: Array(float64, 1)) -> int64:
n = out.shape[0]
for i in range(n):
out[i] = (x[i] - 1.35) * (x[i] - 4.45) * (x[i] - 8.5)
return 0
# Version with 3 parameters
@udf.jit(verbose=0)
def f3(
out: Array(float64, 1), x: Array(float64, 1), y: Array(float64, 1), z: Array(float64, 1)
) -> int64:
n = out.shape[0]
for i in range(n):
out[i] = (x[i] - 1.35) * (y[i] - 4.45) * (z[i] - 8.5)
return 0
# Create initial containers
if PROFILE:
a1_storage = None # avoid a warning
a1_fname = "a1.iarray"
if not os.path.isfile(a1_fname):
print(f"Creating {a1_fname}")
a1_storage = ia.Storage(urlpath=a1_fname)
a1 = ia.linspace(dtshape, 0, 10, storage=a1_storage)
else:
print(f"Reading {a1_fname}")
a1 = ia.load(a1_fname)
else:
a1 = ia.linspace(dtshape, 0, 10)
a2 = np.linspace(0, 10, shape[0], dtype=dtype).reshape(shape)
x = a2.copy()
y = a2.copy()
z = a2.copy()
t0 = time()
for i in range(NITER):
if NVARS == 1:
bn = eval(expr1, {"x": x})
else:
bn = eval(expr3, {"x": x, "y": y, "z": z})
print("Time for numpy eval:", round((time() - t0) / NITER, 3))
# print(bn)
ne.set_num_threads(nthreads)
t0 = time()
for i in range(NITER):
if NVARS == 1:
bne = ne.evaluate(expr1, {"x": x})
else:
bne = ne.evaluate(expr3, {"x": x, "y": y, "z": z})
print("Time for numexpr eval:", round((time() - t0) / NITER, 3))
# print(bne)
print("iarray evaluation starts...")
print("Operands cratio:", round(a1.cratio, 2))
iax = a1.copy()
iay = a1.copy()
iaz = a1.copy()
if NVARS == 1:
expr = f1.create_expr([iax], fp_mantissa_bits=out_prec)
else:
expr = f3.create_expr([iax, iay, iaz], fp_mantissa_bits=out_prec)
b1 = None # avoid a warning
t0 = time()
for i in range(NITER):
b1 = expr.eval()
print("Time for udf eval engine:", round((time() - t0) / NITER, 3))
b1_n = ia.iarray2numpy(b1)
# print(b1_n)
b2 = None # avoid a warning
t0 = time()
if NVARS == 1:
expr = ia.expr_from_string(expr1, {"x": iax}, fp_mantissa_bits=out_prec)
else:
expr = ia.expr_from_string(expr3, {"x": iax, "y": iay, "z": iaz}, fp_mantissa_bits=out_prec)
for i in range(NITER):
b2 = expr.eval()
print("Time for internal eval engine:", round((time() - t0) / NITER, 3))
b2_n = ia.iarray2numpy(b2)
# print(b2_n)
print("Output cratio:", round(b2.cratio, 2))
try:
np.testing.assert_almost_equal(bn, b1_n, decimal=4)
np.testing.assert_almost_equal(bn, b2_n, decimal=4)
print("OK. Results are the same.")
except AssertionError:
print("ERROR. Results are different.")