Created
May 12, 2023 13:46
-
-
Save AbhimanyuAryan/deee0cbdb5ca97234f478b8241eda3f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from timeit import timeit | |
def matmul_python(C, A, B): | |
for m in range(C.rows): | |
for n in range(C.cols): | |
for k in range(A.cols): | |
C[m, n] += A[m, k] * B[k, n] | |
class Matrix: | |
def __init__(self, value, rows, cols): | |
self.value = value | |
self.rows = rows | |
self.cols = cols | |
def __getitem__(self, idxs): | |
return self.value[idxs[0]][idxs[1]] | |
def __setitem__(self, idxs, value): | |
self.value[idxs[0]][idxs[1]] = value | |
def benchmark_matmul_python(M, N, K): | |
A = Matrix(list(np.random.rand(M, K)), M, K) | |
B = Matrix(list(np.random.rand(K, N)), K, N) | |
C = Matrix(list(np.zeros((M, N))), M, N) | |
secs = timeit(lambda: matmul_python(C, A, B), number=2)/2 | |
print(secs) | |
gflops = ((2*M*N*K)/secs) / 1e9 | |
print(gflops, "GFLOP/s") | |
return gflops | |
python_gflops = benchmark_matmul_python(128, 128, 128).to_f64() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment