-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
44 lines (37 loc) · 1.24 KB
/
utils.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
import struct
from dataclasses import dataclass
def load_float32_array(filepath):
''' Load array data from binary file in Python list.\n
Caller must interpret the data as intended by caller
'''
arr = []
with open(filepath, 'rb') as f:
A_data = f.read()
for i in range(0, len(A_data), 4):
element_data = A_data[i:i+4]
assert len(element_data) == 4
arr.append(struct.unpack('f', element_data)[0])
return arr
@dataclass
class MatmalSpeedInfo:
'''
Data class to hold matrix multiplication speed information
'''
# number of floating point operations for matmul in GFLOPs
FLOPs: float
# number of floating point operations per second in GFLOPS
FLOPS: float
# elapsed time in seconds
elapsed: float
# row of first matrix
M: int
# column of the second matrix
N: int
# column of the first matrix and row of the second matrix
K: int
def get_speed_informaiton(M, N, K, start, end) -> MatmalSpeedInfo:
''' Get speed information for matrix multiplication
'''
elapsed = (end - start) / 1e9
GFLOPs = (2 * K - 1) * M * N / 1e9
return MatmalSpeedInfo(FLOPs=GFLOPs, FLOPS=GFLOPs / elapsed, elapsed=elapsed, M=M, N=N, K=K)