-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbw.py
More file actions
46 lines (34 loc) · 1.27 KB
/
bw.py
File metadata and controls
46 lines (34 loc) · 1.27 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
44
45
import numpy as np
from scipy.io import mmread
from scipy.sparse import coo_matrix
def compute_bandwidth_details(matrix):
coo = matrix.tocoo()
n_rows, n_cols = matrix.shape
row_bw = np.zeros(n_rows, dtype=int)
col_bw = np.zeros(n_cols, dtype=int)
for i, j in zip(coo.row, coo.col):
diff = abs(i - j)
row_bw[i] = max(row_bw[i], diff)
col_bw[j] = max(col_bw[j], diff)
max_row_bw = np.max(row_bw)
avg_row_bw = np.mean(row_bw)
max_col_bw = np.max(col_bw)
avg_col_bw = np.mean(col_bw)
bandwidth = np.max(np.abs(coo.row - coo.col))
return {
"max_row_bandwidth": max_row_bw,
"avg_row_bandwidth": avg_row_bw,
"max_col_bandwidth": max_col_bw,
"avg_col_bandwidth": avg_col_bw,
"bandwidth" : bandwidth,
}
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Compute detailed bandwidths of a sparse matrix.")
parser.add_argument("filename", type=str, help="Path to Matrix Market (.mtx) file")
args = parser.parse_args()
matrix = mmread(args.filename)
stats = compute_bandwidth_details(matrix)
print(f"Matrix shape: {matrix.shape}")
for k, v in stats.items():
print(f"{k.replace('_', ' ').capitalize()}: {v:.2f}")