-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmatrix.py
More file actions
248 lines (196 loc) · 7.52 KB
/
Copy pathmatrix.py
File metadata and controls
248 lines (196 loc) · 7.52 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
import numpy as np
from numpy import int32
from pyfaasm.config import (
MATRIX_CONF_STATE_KEY,
SUBMATRICES_KEY_A,
SUBMATRICES_KEY_B,
MatrixConf,
RESULT_MATRIX_KEY,
)
from pyfaasm.core import (
write_state,
read_state,
chain,
await_call,
)
from pyfaasm.matrix_data import do_subdivide_matrix, do_reconstruct_matrix
def write_matrix_params_to_state(matrix_size, n_splits):
params = np.array((matrix_size, n_splits), dtype=int32)
write_state(MATRIX_CONF_STATE_KEY, params.tobytes())
def load_matrix_conf_from_state():
# Params are ints so need to work out what size they are
dummy = np.array((1, 2), dtype=int32)
param_len = len(dummy.tobytes())
param_bytes = read_state(MATRIX_CONF_STATE_KEY, param_len)
params = np.frombuffer(param_bytes, dtype=int32)
matrix_size = params[0]
n_splits = params[1]
conf = MatrixConf(matrix_size, n_splits)
return conf
def random_matrix(size):
return np.random.rand(size, size).astype(np.float32)
# Split up the original matrix into square submatrices and write to state
def subdivide_matrix_into_state(conf, mat, key_prefix):
def _write_submatrix_to_state(sm_bytes, row_idx, col_idx):
full_key = conf.get_submatrix_key(
key_prefix, conf.n_splits, row_idx, col_idx
)
write_state(full_key, sm_bytes)
do_subdivide_matrix(conf, mat, _write_submatrix_to_state)
# Reads a given submatrix from the input
def read_input_submatrix(conf, key_prefix, row_idx, col_idx):
sm_bytes = conf.get_bytes_per_submatrix(conf.n_splits)
sm_size = conf.get_submatrix_size(conf.n_splits)
full_key = conf.get_submatrix_key(
key_prefix, conf.n_splits, row_idx, col_idx
)
sub_mat_data = read_state(full_key, sm_bytes)
return np.frombuffer(sub_mat_data, dtype=np.float32).reshape(
sm_size, sm_size
)
# Rebuilds a matrix from its submatrices in state
def reconstruct_matrix_from_submatrices(conf, key_prefix):
def _read_submatrix_from_state(row_idx, col_idx):
full_key = conf.get_submatrix_key(
key_prefix, conf.n_splits, row_idx, col_idx
)
sm_bytes = conf.get_bytes_per_submatrix(conf.n_splits)
return read_state(full_key, sm_bytes)
return do_reconstruct_matrix(conf, _read_submatrix_from_state)
# This is the distributed worker that will be invoked by faasm
def distributed_divide_and_conquer(input_bytes):
conf = load_matrix_conf_from_state()
input_args = np.frombuffer(input_bytes, dtype=int32)
split_level = input_args[0]
row_a = input_args[1]
col_a = input_args[2]
row_b = input_args[3]
col_b = input_args[4]
# If we're at the target number of splits, do the work
if split_level == conf.n_splits:
# Read in the relevant submatrices of each input matrix
mat_a = read_input_submatrix(conf, SUBMATRICES_KEY_A, row_a, col_a)
mat_b = read_input_submatrix(conf, SUBMATRICES_KEY_B, row_b, col_b)
# Do the multiplication in memory
result = np.dot(mat_a, mat_b)
else:
# Recursively kick off more divide and conquer
result = chain_multiplications(
conf, split_level, row_a, col_a, row_b, col_b
)
# Write the result
result_key = conf.get_intermediate_result_key(
split_level, row_a, col_a, row_b, col_b
)
write_state(result_key, result.tobytes())
def divide_and_conquer():
conf = load_matrix_conf_from_state()
print(
"Running divide and conquer for {}x{} matrix with {} splits".format(
conf.matrix_size, conf.matrix_size, conf.n_splits
)
)
# Short-cut for no splits
if conf.n_splits == 0:
# Read in the relevant submatrices of each input matrix
mat_a = read_input_submatrix(conf, SUBMATRICES_KEY_A, 0, 0)
mat_b = read_input_submatrix(conf, SUBMATRICES_KEY_B, 0, 0)
# Do the multiplication in memory
result = np.dot(mat_a, mat_b)
else:
# Kick off the basic multiplication jobs
result = chain_multiplications(conf, 0, 0, 0, 0, 0)
# Write final result
write_state(RESULT_MATRIX_KEY, result.tobytes())
def get_addition_result(conf, split_level, addition_def):
sm_size = conf.get_submatrix_size(split_level)
sm_byte_size = conf.get_bytes_per_submatrix(split_level)
key_a = conf.get_intermediate_result_key(
split_level,
addition_def[0][0][0],
addition_def[0][0][1],
addition_def[0][1][0],
addition_def[0][1][1],
)
key_b = conf.get_intermediate_result_key(
split_level,
addition_def[1][0][0],
addition_def[1][0][1],
addition_def[1][1][0],
addition_def[1][1][1],
)
bytes_a = read_state(key_a, sm_byte_size)
mat_a = np.frombuffer(bytes_a, dtype=np.float32).reshape(sm_size, sm_size)
bytes_b = read_state(key_b, sm_byte_size)
mat_b = np.frombuffer(bytes_b, dtype=np.float32).reshape(sm_size, sm_size)
return mat_a + mat_b
def chain_multiplications(conf, split_level, row_a, col_a, row_b, col_b):
"""
Spawns 8 workers to do the relevant multiplication in parallel.
- split level is how many times we've split the original matrix
- row_a, col_a is the chunk of matrix A
- row_b, col_b is the chunk of matrix B
The row/ col values will specify which chunk of the current split level, not
actual indices in the final input matrices. Those must only be calculated
when the final multiplication is done.
"""
call_ids = []
# Next split down we'll double the number of submatrices
next_split_level = split_level + 1
next_row_a = 2 * row_a
next_row_b = 2 * row_b
next_col_a = 2 * col_a
next_col_b = 2 * col_b
# Splitting submatrix A into four, A11, A12, A21, A22 and same with B
a11 = [next_row_a, next_col_a]
a12 = [next_row_a, next_col_a + 1]
a21 = [next_row_a + 1, next_col_a]
a22 = [next_row_a + 1, next_col_a + 1]
b11 = [next_row_b, next_col_b]
b12 = [next_row_b, next_col_b + 1]
b21 = [next_row_b + 1, next_col_b]
b22 = [next_row_b + 1, next_col_b + 1]
# Define the relevant multiplications and additions for these submatrices
additions = [
[(a11, b11), (a12, b21)],
[(a11, b12), (a12, b22)],
[(a21, b11), (a22, b21)],
[(a21, b12), (a22, b22)],
]
# Build a list of all the required multiplications
multiplications = list()
for mult_one, mult_two in additions:
multiplications.append(mult_one)
multiplications.append(mult_two)
# Kick off the multiplications in parallel
for submatrix_a, submatrix_b in multiplications:
inputs_a = np.array(
[
next_split_level,
submatrix_a[0],
submatrix_a[1],
submatrix_b[0],
submatrix_b[1],
],
dtype=int32,
)
call_ids.append(
chain(distributed_divide_and_conquer, inputs_a.tobytes())
)
# Await completion
for call_id in call_ids:
await_call(call_id)
# Go through and get the results
r_1 = get_addition_result(conf, next_split_level, additions[0])
r_2 = get_addition_result(conf, next_split_level, additions[1])
r_3 = get_addition_result(conf, next_split_level, additions[2])
r_4 = get_addition_result(conf, next_split_level, additions[3])
# Reconstitute the result
result = np.concatenate(
(
np.concatenate((r_1, r_2), axis=1),
np.concatenate((r_3, r_4), axis=1),
),
axis=0,
)
return result