forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare-evaluators.py
More file actions
252 lines (215 loc) · 8.47 KB
/
Copy pathcompare-evaluators.py
File metadata and controls
252 lines (215 loc) · 8.47 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Comparison of different array evaluators (numpy, numexpr, numba, iarray...)
# It looks like you need to set envvar KMP_DUPLICATE_LIB_OK=TRUE manually in order to run this.
from itertools import zip_longest
from time import time
import os
import numba as nb
import numexpr as ne
import numpy as np
import iarray as ia
from iarray import udf
from iarray.udf import float64, int64
# Numba uses OpemMP, and this collides with the libraries in ironArray.
# Using the next envvar seems to fix the issue (bar a small printed info line).
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
# Number of iterations per benchmark
NITER = 4
# Vector sizes and chunking
shape = [100 * 1000 * 1000]
N = int(np.prod(shape))
# chunks, blocks = None, None # use automatic partition advice
# chunks, blocks = [400 * 1000], [16 * 1000] # user-defined partitions
chunks, blocks = [1 * 1000 * 1000], [20 * 1000] # user-defined partitions
expression = "(x - 1.35) * (x - 4.45) * (x - 8.5)"
clevel = 9 # compression level
nthreads = 8 # number of threads for the evaluation and/or compression
def poly_python(x):
y = np.empty(x.shape, x.dtype)
for i in range(len(x)):
y[i] = (x[i] - 1.35) * (x[i] - 4.45) * (x[i] - 8.5)
return y
@nb.jit(nopython=True, cache=True, parallel=True)
def poly_numba(x):
y = np.empty(x.shape, x.dtype)
for i in nb.prange(len(x)):
y[i] = (x[i] - 1.35) * (x[i] - 4.45) * (x[i] - 8.5)
return y
@nb.jit(nopython=True, cache=True, parallel=True)
def poly_numba2(x, y):
for i in nb.prange(len(x)):
y[i] = (x[i] - 1.35) * (x[i] - 4.45) * (x[i] - 8.5)
@udf.jit
def poly_llvm(out: udf.Array(float64, 1), x: udf.Array(float64, 1)) -> udf.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
def do_regular_evaluation():
print(f"Regular evaluation of the expression: {expression} with {N} elements")
x = np.linspace(0, 10, N, dtype=np.double).reshape(shape)
# Reference to compare to
y0 = eval(expression)
# print(y0, y0.shape)
if N <= 2e6:
t0 = time()
y1 = poly_python(x)
print("Regular evaluate via python:", round(time() - t0, 4))
np.testing.assert_almost_equal(y0, y1)
y1 = None # shut-up warnings about variables possibly referenced before assignment
t0 = time()
for i in range(NITER):
y1 = eval(expression)
print("Regular evaluate via numpy:", round((time() - t0) / NITER, 4))
# np.testing.assert_almost_equal(y0, y1)
# t0 = time()
# ne.set_num_threads(1)
# for i in range(NITER):
# y1 = ne.evaluate(expression, local_dict={"x": x})
# print("Regular evaluate via numexpr:", round((time() - t0) / NITER, 4))
# np.testing.assert_almost_equal(y0, y1)
t0 = time()
# ne.set_num_threads(nthreads)
for i in range(NITER):
y1 = ne.evaluate(expression, local_dict={"x": x})
print("Regular evaluate via numexpr (multi-thread):", round((time() - t0) / NITER, 4))
# np.testing.assert_almost_equal(y0, y1)
# nb.set_num_threads(1)
# t0 = time()
# for i in range(NITER):
# y1 = poly_numba(x)
# print("Regular evaluate via numba:", round((time() - t0) / NITER, 4))
# np.testing.assert_almost_equal(y0, y1)
# t0 = time()
# for i in range(NITER):
# y1 = poly_numba(x)
# print("Regular evaluate via numba (II):", round((time() - t0) / NITER, 4))
# np.testing.assert_almost_equal(y0, y1)
# nb.set_num_threads(nthreads)
t0 = time()
for i in range(NITER):
y1 = poly_numba(x)
print("Regular evaluate via numba (multi-thread):", round((time() - t0) / NITER, 4))
# np.testing.assert_almost_equal(y0, y1)
# t0 = time()
# for i in range(NITER):
# y1 = ia.ext.poly_cython(x)
# print("Regular evaluate via cython:", round((time() - t0) / NITER, 4))
# np.testing.assert_almost_equal(y0, y1)
#
# t0 = time()
# for i in range(NITER):
# y1 = ia.ext.poly_cython_nogil(x)
# print("Regular evaluate via cython (nogil):", round((time() - t0) / NITER, 4))
# np.testing.assert_almost_equal(y0, y1)
def do_block_evaluation():
print(f"Block evaluation")
cfg = ia.Config(chunks=chunks, blocks=blocks)
# ia.set_config_defaults(codec=ia.Codec.LZ4, clevel=clevel, nthreads=nthreads, chunks=chunks, blocks=blocks)
# The latest versions of BTune work much better for 1-dim arrays
ia.set_config_defaults(cfg=cfg, favor=ia.Favor.SPEED)
print(ia.get_config_defaults())
xa = ia.linspace(0.0, 10.0, int(np.prod(shape)), shape=shape)
# x = np.linspace(0, 10, N).reshape(shape)
#
# print("Operand cratio:", round(xa.cratio, 2))
#
# # Reference to compare to
# y0 = eval(expression)
#
# ya = ia.empty(shape)
#
# t0 = time()
# for i in range(NITER):
# ya = ia.empty(shape)
# for ((j, x), (k, y)) in zip_longest(xa.iter_read_block(), ya.iter_write_block()):
# y[:] = eval(expression)
# print("Block evaluate via numpy:", round((time() - t0) / NITER, 4))
#
# y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
#
# ne.set_num_threads(1)
# t0 = time()
# for i in range(NITER):
# ya = ia.empty(shape)
# for ((j, x), (k, y)) in zip_longest(xa.iter_read_block(), ya.iter_write_block()):
# ne.evaluate(expression, local_dict={"x": x}, out=y)
# print("Block evaluate via numexpr:", round((time() - t0) / NITER, 4))
# y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
#
# ne.set_num_threads(nthreads)
# t0 = time()
# for i in range(NITER):
# ya = ia.empty(shape)
# for ((j, x), (k, y)) in zip_longest(xa.iter_read_block(), ya.iter_write_block()):
# ne.evaluate(expression, local_dict={"x": x}, out=y)
# print("Block evaluate via numexpr (multi-thread):", round((time() - t0) / NITER, 4))
# y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
#
# nb.set_num_threads(1)
# t0 = time()
# for i in range(NITER):
# ya = ia.empty(shape)
# for ((j, x), (k, y)) in zip_longest(xa.iter_read_block(), ya.iter_write_block()):
# # y[:] = poly_numba(x)
# poly_numba2(x, y)
# print("Block evaluate via numba (II):", round((time() - t0) / NITER, 4))
# y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
#
# nb.set_num_threads(nthreads)
# t0 = time()
# for i in range(NITER):
# ya = ia.empty(shape)
# for ((j, x), (k, y)) in zip_longest(xa.iter_read_block(), ya.iter_write_block()):
# # y[:] = poly_numba(x)
# poly_numba2(x, y)
# print("Block evaluate via numba (II, multi-thread):", round((time() - t0) / NITER, 4))
# y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
#
# t0 = time()
# for i in range(NITER):
# ya = ia.empty(shape)
# for ((j, x), (k, y)) in zip_longest(xa.iter_read_block(), ya.iter_write_block()):
# y[:] = ia.ext.poly_cython(x)
# print("Block evaluate via cython:", round((time() - t0) / NITER, 4))
# y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
#
# t0 = time()
# for i in range(NITER):
# ya = ia.empty(shape)
# for ((j, x), (k, y)) in zip_longest(xa.iter_read_block(), ya.iter_write_block()):
# y[:] = ia.ext.poly_cython_nogil(x)
# print("Block evaluate via cython (nogil):", round((time() - t0) / NITER, 4))
# y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
for engine in ("internal", "udf"):
t0 = time()
if engine == "internal":
expr = ia.expr_from_string(expression, {"x": xa})
else:
expr = ia.expr_from_udf(poly_llvm, [xa])
for i in range(NITER):
ya = expr.eval()
avg = round((time() - t0) / NITER, 4)
print(f"Block evaluate via iarray.eval (engine: {engine}): {avg:.4f}")
y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
t0 = time()
x = xa
for i in range(NITER):
ya = eval(expression, {"x": x})
ya = ya.eval()
avg = round((time() - t0) / NITER, 4)
print(f"Block evaluate via iarray.LazyExpr.eval (engine: internal): {avg:.4f}")
y1 = ia.iarray2numpy(ya)
# np.testing.assert_almost_equal(y0, y1)
print("Result cratio:", round(ya.cratio, 2))
if __name__ == "__main__":
do_regular_evaluation()
print("-*-" * 10)
do_block_evaluation()