-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
43 lines (32 loc) · 1.07 KB
/
benchmark.py
File metadata and controls
43 lines (32 loc) · 1.07 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
import mumpy as mp
import numpy as np
from time import time
class Timer:
def __init__(self, name):
self.name = name
def __enter__(self, *args, **kwargs):
self.t = time()
def __exit__(self, *args, **kwargs):
self.t = time() - self.t
print(f"{self.name} elapsed: {self.t:.02f} sec")
def main():
x = np.random.normal(size=(1024, 1024))
y = np.random.normal(size=(1024, 1024))
with Timer('numpy'):
z_np = x @ y
with Timer('mumpy.matmul_generic'):
z_mp_cpu_gen = mp.matmul_generic(x, y)
print(f"{(z_mp_cpu_gen - z_np).mean()=}")
with Timer('mumpy.matmul_row'):
z_mp_cpu_row = mp.matmul_row(x, y)
print(f"{(z_mp_cpu_row - z_np).mean()=}")
x_col = np.array(x, order='F')
y_col = np.array(y, order='F')
with Timer('mumpy.matmul_col'):
z_mp_cpu_col = mp.matmul_col(x_col, y_col)
print(f"{(z_mp_cpu_col - z_np).mean()=}")
with Timer('mumpy.matmul_cuda'):
z_mp_gpu = mp.matmul_cuda(x, y)
print(f"{(z_mp_gpu - z_np).mean()=}")
if __name__ == '__main__':
main()