2024-06-16 15:13:17 +02:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
# Suppress all warnings
|
|
|
|
warnings.filterwarnings("ignore")
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
2024-06-16 10:06:22 +02:00
|
|
|
import numpy as np
|
2024-06-16 15:13:17 +02:00
|
|
|
import scipy as sp
|
|
|
|
from scipy.stats import uniform
|
2024-06-16 10:06:22 +02:00
|
|
|
from time import time
|
|
|
|
|
2024-06-16 15:13:17 +02:00
|
|
|
|
2024-06-16 10:06:22 +02:00
|
|
|
class MatrixBench():
|
|
|
|
def __init__(self, iters: int, size: int):
|
|
|
|
self.iters = iters
|
|
|
|
self.size = size
|
|
|
|
self.average = 0
|
|
|
|
self.start_time = 0
|
|
|
|
self.end_time = 0
|
|
|
|
self.elapsed_time = 0
|
|
|
|
self.flops:float = 0.0
|
|
|
|
|
|
|
|
def create_matrices(self):
|
|
|
|
# Create num x num matrix and an num x 1 vector with random floating point numbers
|
2024-06-16 15:13:17 +02:00
|
|
|
self.matrix_A = uniform.rvs(size=(self.size, self.size)).astype(np.float32)
|
|
|
|
self.matrix_B = uniform.rvs(size=(self.size, 1)).astype(np.float32)
|
2024-06-16 10:06:22 +02:00
|
|
|
|
|
|
|
def do_operation(self):
|
|
|
|
# Do Floating Point Operation
|
|
|
|
self.start_time = time()
|
2024-06-16 15:13:17 +02:00
|
|
|
_ = sp.linalg.solve(self.matrix_A, self.matrix_B)
|
2024-06-16 10:06:22 +02:00
|
|
|
self.end_time = time()
|
|
|
|
|
|
|
|
def do_calculate(self):
|
|
|
|
# Calculate Floating Point Operation per Second (FLOPS)
|
|
|
|
self.elapsed_time = self.end_time - self.start_time
|
|
|
|
self.num_operations = (2 / 3) * self.size **3
|
|
|
|
self.flops = self.num_operations / self.elapsed_time
|
|
|
|
self.average += self.flops
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
print("# Linpack-like Matrix-based Floating Point Benchmark #")
|
2024-06-16 15:13:17 +02:00
|
|
|
print("# Using SciPy as Core (contains Warn: Ill-conditioned Matrix) #")
|
2024-06-16 15:54:03 +02:00
|
|
|
for i in range(1, self.iters + 1):
|
2024-06-16 15:13:17 +02:00
|
|
|
print(f"=> Epoch #{str(i).zfill(len(str(self.iters)))}", end="\r")
|
2024-06-16 10:06:22 +02:00
|
|
|
self.create_matrices()
|
|
|
|
self.do_operation()
|
|
|
|
self.do_calculate()
|
|
|
|
self.avg_flops: float = (self.average/self.iters) / 1000000
|
|
|
|
print(f"Result : {self.avg_flops:.2f} MFLOPS")
|
|
|
|
|
2024-06-16 15:13:17 +02:00
|
|
|
|
2024-06-16 10:06:22 +02:00
|
|
|
def main():
|
|
|
|
# Adjust Matrix Size
|
|
|
|
size = 192
|
|
|
|
# Adjust Iteration Time
|
|
|
|
iters = 10000
|
|
|
|
# Run Benchmark
|
|
|
|
bench = MatrixBench(iters=iters, size=size)
|
|
|
|
bench.run()
|
|
|
|
|
|
|
|
|
|
|
|
main()
|