forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatmul-dask.py
More file actions
135 lines (120 loc) · 3.73 KB
/
Copy pathmatmul-dask.py
File metadata and controls
135 lines (120 loc) · 3.73 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
import numpy as np
from numcodecs import Blosc
import zarr
import dask.array as da
import dask
import sys
import os
import shutil
import ctypes
from time import time
mkl_rt = ctypes.CDLL("libmkl_rt.dylib")
mkl_set_num_threads = mkl_rt.MKL_Set_Num_Threads
mkl_set_num_threads(1)
persistent = True
shape = (1000, 1000)
chunks = (500, 500)
# shape = (1000, 1000)
# chunks = (500, 500)
if len(sys.argv) == 1:
nthreads = 8
else:
nthreads = int(sys.argv[1])
cname = "lz4"
clevel = 5
shuffle = Blosc.SHUFFLE
aurlpath = "a.zarr"
burlpath = "b.zarr"
curlpath = "c.zarr"
compressor = Blosc(cname=cname, clevel=clevel, shuffle=shuffle)
if persistent:
print("(Re-)Generating operand A")
if not os.path.exists(aurlpath):
azarr = zarr.open(
aurlpath,
mode="w",
shape=shape,
chunks=chunks,
dtype=np.float64,
compressor=compressor,
)
tmp = np.linspace(-1, 1, int(np.prod(shape))).reshape(shape)
azarr[:] = tmp
else:
azarr = zarr.open("a.zarr")
if azarr.shape != shape or azarr.chunks != chunks:
# Ooops, we cannot use the array on-disk. Regenerate it.
if os.path.exists(aurlpath):
shutil.rmtree(aurlpath)
azarr = zarr.open(
aurlpath,
mode="w",
shape=shape,
chunks=chunks,
dtype=np.float64,
compressor=compressor,
)
tmp = np.linspace(-1, 1, int(np.prod(shape))).reshape(shape)
azarr[:] = tmp
print("(Re-)Generating operand B")
if not os.path.exists(burlpath):
bzarr = zarr.open(
burlpath,
mode="w",
shape=shape,
chunks=chunks,
dtype=np.float64,
compressor=compressor,
)
tmp = np.linspace(-1, 1, int(np.prod(shape))).reshape(shape)
bzarr[:] = tmp
else:
print("(Re-)Generating operand B")
bzarr = zarr.open("b.zarr")
if bzarr.shape != shape or bzarr.chunks != chunks:
# Ooops, we cannot use the array on-disk. Regenerate it.
if os.path.exists(burlpath):
shutil.rmtree(burlpath)
bzarr = zarr.open(
burlpath,
mode="w",
shape=shape,
chunks=chunks,
dtype=np.float64,
compressor=compressor,
)
tmp = np.linspace(-1, 1, int(np.prod(shape))).reshape(shape)
bzarr[:] = tmp
else:
azarr = zarr.empty(shape=shape, chunks=chunks, dtype=np.float64, compressor=compressor)
bzarr = zarr.empty(shape=shape, chunks=chunks, dtype=np.float64, compressor=compressor)
if persistent:
if os.path.exists(curlpath):
shutil.rmtree(curlpath)
scheduler = "single-threaded" if nthreads == 1 else "threads"
with dask.config.set(scheduler=scheduler, num_workers=nthreads):
if persistent:
czarr = zarr.open(
curlpath,
mode="w",
shape=shape,
chunks=chunks,
dtype=np.float64,
compressor=compressor,
)
else:
czarr = zarr.empty(shape=shape, chunks=chunks, dtype=np.float64, compressor=compressor)
print(f"Start actual matmul with nthreads = {nthreads}")
t0 = time()
adask = da.from_zarr(azarr)
bdask = da.from_zarr(bzarr)
cdask = da.matmul(adask, bdask)
da.to_zarr(cdask, czarr)
print("Time for iarray matmul:", round((time() - t0), 3))
if persistent:
# if os.path.exists(aurlpath):
# shutil.rmtree(aurlpath)
# if os.path.exists(burlpath):
# shutil.rmtree(burlpath)
if os.path.exists(curlpath):
shutil.rmtree(curlpath)