-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrassen_testing.py
333 lines (207 loc) · 10.2 KB
/
strassen_testing.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import strassen_algorithms as s_a
import numpy as nmp #per l'implementazione delle matrici
import random #per generare i numeri casuali
import time #per tracciare i tempi
#############################################################################
################## FUNZIONI PER IL TESTING DEGLI ALGORITMI ##################
#############################################################################
def generate_2_random_matrix(a,b,c):
A = nmp.random.randint(low=-100, high=100, size=(a,b), dtype='l')
B = nmp.random.randint(low=-100, high=100, size=(b,c), dtype='l')
return A, B
def different_q(dim_min: int, dim_max: int, n_test:int, file_name:str, qmin, qmax):
random.seed(1234)
with open(file_name, "w") as f:
f.write(f"ID,tipo,Q,a,b,c,riga_per_colonna,dynamic_time,static_time,peeling_time\n")
for i in range(0,n_test):
a = random.randint(dim_min,dim_max)
b = random.randint(dim_min,dim_max)
c = random.randint(dim_min,dim_max)
A, B = generate_2_random_matrix(a,b,c)
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
for q in range(qmin,qmax):
Q = 2**q
print(f"\n---Q={Q}---")
f.write(f"{i},rettangolare,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
f.close()
def generate_sparse_matrix(rows, cols):
total_elements = rows * cols
num_nonzero_elements = int(total_elements * 0.5)
random_values = nmp.random.randint(low=-100, high=100, size=num_nonzero_elements, dtype='l')
ris = nmp.zeros((rows, cols))
indices = nmp.random.choice(range(total_elements), size=num_nonzero_elements, replace=False)
ris.flat[indices] = random_values
return ris
def sparse_dense_matrix(dim_min: int, dim_max: int, Q:int, n_test:int):
random.seed(1234)
with open("dataset\sparse_vs_dense.txt", "w") as f:
f.write(f"ID,tipo,Q,a,b,c,riga_per_colonna,dynamic_time,static_time,peeling_time\n")
print("Matrici dense")
for i in range(0,n_test):
a = random.randint(dim_min, dim_max)
b = random.randint(dim_min, dim_max)
c = random.randint(dim_min, dim_max)
A, B = generate_2_random_matrix(a,b,c)
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
f.write(f"{i},densa,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
print("Matrici sparse")
for i in range(0,n_test):
a = random.randint(dim_min, dim_max)
b = random.randint(dim_min, dim_max)
c = random.randint(dim_min, dim_max)
A = generate_sparse_matrix(a,b)
B = generate_sparse_matrix(b,c)
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
f.write(f"{n_test+i},sparsa,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
f.close()
def lunghe_vs_larghe(dim_min: int, dim_max: int, Q:int, n_test:int):
random.seed(1234)
with open("dataset\lunghe_vs_larghe.txt", "w") as f:
f.write(f"ID,tipo,Q,a,b,c,riga_per_colonna,dynamic_time,static_time,peeling_time\n")
print("########## a piccolo ##########")
for i in range(0,n_test):
a = random.randint(dim_min//10, dim_max//10)
b = random.randint(dim_min, dim_max)
c = random.randint(dim_min, dim_max)
A, B = generate_2_random_matrix(a,b,c)
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
f.write(f"{i},a_piccolo,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
print("\n########## b piccolo ##########")
for i in range(0,n_test):
a = random.randint(dim_min, dim_max)
b = random.randint(dim_min//10, dim_max//10)
c = random.randint(dim_min, dim_max)
A, B = generate_2_random_matrix(a,b,c)
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
f.write(f"{i},b_piccolo,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
print("\n########## c piccolo ##########")
for i in range(0,n_test):
a = random.randint(dim_min, dim_max)
b = random.randint(dim_min, dim_max)
c = random.randint(dim_min//10, dim_max//10)
A, B = generate_2_random_matrix(a,b,c)
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
f.write(f"{i},c_piccolo,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
f.close()
def increasing_dimension(dim_min0: int, dim_max0: int, Q:int, n_test:int):
random.seed(1234)
with open("dataset\dimensioni.txt", "w") as f:
f.write(f"ID,tipo,Q,a,b,c,riga_per_colonna,dynamic_time,static_time,peeling_time\n")
for i in range(0,n_test):
dim_min0 += 10
dim_max0 += 10
a = random.randint(dim_min0, dim_max0)
b = random.randint(dim_min0, dim_max0)
c = random.randint(dim_min0, dim_max0)
A, B = generate_2_random_matrix(a,b,c)
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
f.write(f"{i},dimensioni_crescenti,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
def floating_time(dim_min0: int, dim_max0: int, Q:int, n_test:int):
random.seed(1234)
with open("dataset\on_floating.txt", "w") as f:
f.write(f"ID,tipo,Q,a,b,c,riga_per_colonna,dynamic_time,static_time,peeling_time\n")
for i in range(0,n_test):
a = random.randint(dim_min0, dim_max0)
b = random.randint(dim_min0, dim_max0)
c = random.randint(dim_min0, dim_max0)
A = nmp.random.uniform(low=-100, high=100, size=(a,b))
B = nmp.random.uniform(low=-100, high=100, size=(b,c))
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
f.write(f"{i},floating,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
def floating_error(dim_min0: int, dim_max0: int, Q:int, n_test:int, floating_accuracy):
random.seed(1234)
with open("dataset\error_floating.txt", "w") as f:
f.write(f"ID,tipo,Q,a,b,c,riga_per_colonna,dynamic_time,static_time,peeling_time\n")
for i in range(0,n_test):
a = random.randint(dim_min0, dim_max0)
b = random.randint(dim_min0, dim_max0)
c = random.randint(dim_min0, dim_max0)
A = nmp.random.uniform(low=-100, high=100, size=(a,b))
B = nmp.random.uniform(low=-100, high=100, size=(b,c))
print("\n-----------Matrice numero", i+1, end="-----------\n")
print("Prodotto riga per colonna...")
start = time.process_time()
nmp.matmul(A,B)
end = time.process_time()
rbc_time = max(end - start, 1e-6)
f.write(f"{i},floating,{Q},{a},{b},{c},{rbc_time},")
Strassens_speed_test(A=A,B=B,f=f, Q=(Q))
def Strassens_speed_test(A,B,f,Q):
print("Strassen padding dinamico...")
start = time.process_time()
s_a.Strassen_dynamic_padding(A,B,Q)
end = time.process_time()
dynamic_time = max(end - start, 1e-6)
print("Strassen padding statico...")
start = time.process_time()
s_a.Strassen_static_padding(A,B,Q)
end = time.process_time()
static_time = max(end - start, 1e-6)
print("Strassen peeling dinamico...")
start = time.process_time()
s_a.Strassen_dynamic_peeling(A,B,Q)
end = time.process_time()
peeling_time = max(end - start, 1e-6)
f.write(f"{dynamic_time},{static_time},{peeling_time}\n")
def Strassens_results(A,B,Q):
C_RBC = nmp.matmul(A,B)
C_DPA = s_a.Strassen_dynamic_padding(A,B,Q)
C_SPA = s_a.Strassen_static_padding(A,B,Q)
C_DPE = s_a.Strassen_dynamic_peeling(A,B,Q)
return C_RBC, C_DPA, C_SPA, C_DPE
#############################################################################
if __name__ == "__main__":
#different_q(600, 1000, n_test=30, file_name="dataset\q_test.txt", qmin=4, qmax=10)
#sparse_dense_matrix(600, 1000, 128, 40)
lunghe_vs_larghe(800, 1200, 64, 60)
#increasing_dimension(200,400,128,75)
#floating_time(600, 1000, 128, 40)
#floating_error(600, 1000, 128, 40, 8)