Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assignment1/Untitled 1.odt
Binary file not shown.
Binary file added assignment1/assignment1.odt
Binary file not shown.
4,946 changes: 4,946 additions & 0 deletions assignment1/flamegraph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 21 additions & 3 deletions assignment1/matmul_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import sys
from typing import List
import time

Matrix = List[List[float]]

Expand All @@ -26,6 +27,7 @@ def zero_matrix(n: int) -> Matrix:

#Reorder loops to improve locality
def matmul_fast1(a: Matrix, b: Matrix, c: Matrix, n: int) -> None:
time_start=time.perf_counter()
for i in range(n):
row_ai = a[i]
row_ci = c[i]
Expand All @@ -34,9 +36,11 @@ def matmul_fast1(a: Matrix, b: Matrix, c: Matrix, n: int) -> None:
for k in range(n):
total += row_ai[k] * b[k][j]
row_ci[j] = total
print(f"Algo1 takes: {time.perf_counter()-time_start}")

#Reorder loops to reduce inner loops
def matmul_fast2(a: Matrix, b: Matrix, c: Matrix, n: int) -> None:
time_start=time.perf_counter()
for i in range(n):
row_ai = a[i]
row_ci = c[i]
Expand All @@ -47,15 +51,17 @@ def matmul_fast2(a: Matrix, b: Matrix, c: Matrix, n: int) -> None:
row_bk = b[k]
for j in range(n):
row_ci[j] += aik * row_bk[j]

print(f"Algo2 takes: {time.perf_counter()-time_start}")

def transpose(m: Matrix) -> Matrix:
n = len(m)
return [[m[i][j] for i in range(n)] for j in range(n)]

#Matrix transpose method
def matmul_fast3(a: Matrix, b: Matrix, c: Matrix, n: int) -> None:
time_start=time.perf_counter()

bt = transpose(b)

for i in range(n):
row_ai = a[i]
row_ci = c[i]
Expand All @@ -65,6 +71,8 @@ def matmul_fast3(a: Matrix, b: Matrix, c: Matrix, n: int) -> None:
for k in range(n):
total += row_ai[k] * row_btj[k]
row_ci[j] = total

print(f"Algo3 takes: {time.perf_counter()-time_start}")


def checksum(m: Matrix, n: int) -> float:
Expand Down Expand Up @@ -111,9 +119,19 @@ def main(argv: list[str]) -> int:
c = zero_matrix(n)

for _ in range(reps):
matmul_fast3(a, b, c, n)
matmul_fast1(a, b, c, n)
print(f"n={n} reps={reps} checksum={checksum(c, n):.6f}")

for _ in range(reps):
matmul_fast2(a, b, c, n)
print(f"n={n} reps={reps} checksum={checksum(c, n):.6f}")

for _ in range(reps):
matmul_fast3(a, b, c, n)
print(f"n={n} reps={reps} checksum={checksum(c, n):.6f}")



return 0


Expand Down
16 changes: 15 additions & 1 deletion assignment1/matmul_slow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import sys
from typing import List
import time

Matrix = List[List[float]]

Expand All @@ -28,13 +29,26 @@ def zero_matrix(n: int) -> Matrix:
# Intentionally simple O(n^3) matrix multiplication.
# This loop order is correct but cache-unfriendly for matrix B.
def matmul_slow(a: Matrix, b: Matrix, c: Matrix, n: int) -> None:
# For duration calcs
function_start=time.perf_counter()
cell_times=[]

# Actual function
for i in range(n):
for j in range(n):
cell_start=time.perf_counter() # For duration calcs
total = 0.0
for k in range(n):
total += a[i][k] * b[k][j]
c[i][j] = total


# For duration calcs
cell_time=time.perf_counter()-cell_start
cell_time.append((i,j,cell_time))
function_time=time.perf_counter()-function_start
print("cell calculation times")
print(cell_time)
print(f"function calc. time:{function_time}")

def checksum(m: Matrix, n: int) -> float:
total = 0.0
Expand Down
Binary file added assignment2/assignment2.odt
Binary file not shown.
7 changes: 7 additions & 0 deletions assignment2/flamegraph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions assignment2/rdtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ def get_min_time_diff(num,filename):

if __name__ == "__main__":
filename = "time_py.txt"
num = 100
num = 1000000
counter = get_cpu_time_counter()
print(f"CPU time counter: {counter} ns")
counter2 = get_cpu_time_counter()
print(f"CPU time diff: {counter2-counter} ns")

## Uncomment the lines below to run multiple measurement iterations
#min_diff=get_min_time_diff(num,filename)
#print(f"CPU min diff time: {min_diff} ns")
min_diff=get_min_time_diff(num,filename)
print(f"CPU min diff time: {min_diff} ns")
Loading