-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathray_vs_pyarraypool.py
80 lines (53 loc) · 1.7 KB
/
ray_vs_pyarraypool.py
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
import contextlib
import numpy as np
import ray
import multiprocessing
import pyarraypool
import time
TEST_COUNT = 100
@ray.remote
def sum_matrix(matrix):
return int(np.sum(matrix))
def sum_matrix_pyarraypool(matrix):
return int(np.sum(matrix))
def bench(f):
@contextlib.wraps(f)
def wrapper(*args, **kwargs):
results = np.zeros(TEST_COUNT)
for test_id in range(TEST_COUNT):
t_start = time.perf_counter() * 1000.0
f(*args, **kwargs)
t_end = time.perf_counter() * 1000.0
results[test_id] = t_end - t_start
print(f"Duration for {f.__name__}: {results.mean():.3f}ms")
return wrapper
@contextlib.contextmanager
def warmup_pool():
ray.get(sum_matrix.remote(np.ones((100, 100))))
with multiprocessing.get_context("spawn").Pool() as pool:
pool.apply(sum_matrix_pyarraypool, args=(np.ones(10),))
yield pool
@bench
def bench_litteral_argument_value(data):
_ = ray.get(sum_matrix.remote(data))
@bench
def bench_large_array_object_store(data):
matrix_ref = ray.put(data)
_ = ray.get(sum_matrix.remote(matrix_ref))
@bench
def bench_pyarraypool_mp(data, pool):
matrix_ref = pyarraypool.make_transferable(data)
_ = pool.apply(sum_matrix_pyarraypool, args=(matrix_ref,))
@bench
def bench_pyarraypool_ray(data):
matrix_ref = pyarraypool.make_transferable(data)
_ = ray.get(sum_matrix.remote(matrix_ref))
def main():
data = np.ones((1000, 1000))
with warmup_pool() as pool:
bench_litteral_argument_value(data)
bench_large_array_object_store(data)
bench_pyarraypool_mp(data, pool)
bench_pyarraypool_ray(data)
if __name__ == "__main__":
main()