diff --git a/Matrix-based Benchmark/MatrixBench.py b/Matrix-based Benchmark/NumPy/MatrixBench-NumPy.py similarity index 90% rename from Matrix-based Benchmark/MatrixBench.py rename to Matrix-based Benchmark/NumPy/MatrixBench-NumPy.py index 2a8b5c5..cd6ca21 100644 --- a/Matrix-based Benchmark/MatrixBench.py +++ b/Matrix-based Benchmark/NumPy/MatrixBench-NumPy.py @@ -1,5 +1,4 @@ import numpy as np -from tqdm import tqdm from time import time class MatrixBench(): @@ -32,7 +31,9 @@ class MatrixBench(): def run(self): print("# Linpack-like Matrix-based Floating Point Benchmark #") - for i in tqdm(range(self.iters), desc="Running Matrix-based Benchmark..."): + print("# Using NumPy as Core #") + for i in range(0, self.iters+1): + print(f"=> Epoch #{str(i).zfill(len(str(self.iters)))}", end="\r") self.create_matrices() self.do_operation() self.do_calculate() diff --git a/Matrix-based Benchmark/README.md b/Matrix-based Benchmark/README.md new file mode 100644 index 0000000..782cabb --- /dev/null +++ b/Matrix-based Benchmark/README.md @@ -0,0 +1,7 @@ +# Matrix-based CPU Benchmark # + +## Contains two options: ## +* NumPy script (which requires numpy lib) +* SciPy script (which requires scipy lib) + +## Warning: SciPy based scipt kinda buggy and produces many warnings (the warnings already suppresed for visual)## diff --git a/Matrix-based Benchmark/SciPy/MatrixBench-SciPy.py b/Matrix-based Benchmark/SciPy/MatrixBench-SciPy.py new file mode 100644 index 0000000..3db61b6 --- /dev/null +++ b/Matrix-based Benchmark/SciPy/MatrixBench-SciPy.py @@ -0,0 +1,64 @@ +import warnings + +# Suppress all warnings +warnings.filterwarnings("ignore") + + +import sys +import numpy as np +import scipy as sp +from scipy.stats import uniform +from time import time + + +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 + 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) + + def do_operation(self): + # Do Floating Point Operation + self.start_time = time() + _ = sp.linalg.solve(self.matrix_A, self.matrix_B) + 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 #") + print("# Using SciPy as Core (contains Warn: Ill-conditioned Matrix) #") + for i in range(0, self.iters + 1): + print(f"=> Epoch #{str(i).zfill(len(str(self.iters)))}", end="\r") + 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") + + +def main(): + # Adjust Matrix Size + size = 192 + # Adjust Iteration Time + iters = 10000 + # Run Benchmark + bench = MatrixBench(iters=iters, size=size) + bench.run() + + +main() diff --git a/README.md b/README.md index 3bece63..a91a11e 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,5 @@ This repository contains useful programs written in **Python 3.9**. Each folder * **Benchmark Program** : using **psutil** to obtain computer performance * **Pickle Compress Program** : compress variable dumps using **gzip**, **bzip2**, **lzma**, or **no compression at all** * **FastAPI Simple Example** : contains API standard operations (Normal Request, GET Request, HTML Request) -* **Matrix-based Bencmark** : contains a class to recreate Linpack (Matrix-based) Floating-point Per Second (FLOPS) Benchmark. Based on **numpy** lib +* **Matrix-based Bencmark** : contains a class to recreate Linpack (Matrix-based) Floating-point Per Second (FLOPS) Benchmark. Based on **numpy** or **scipy** libs * *adding more soon*