Skip to content

Commit

Permalink
more style cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kalhankoul96 committed Jan 9, 2024
1 parent 1558668 commit 2c067d1
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 53 deletions.
18 changes: 8 additions & 10 deletions find_max_tilesize.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import os
import sys
import glob


def write_to_line(file_path, line_number, new_content):
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
lines = file.readlines()

if line_number > len(lines) or line_number < 1:
# Line number is out of range
return

lines[line_number - 1] = new_content + '\n'
lines[line_number - 1] = new_content + "\n"

with open(file_path, 'w') as file:
with open(file_path, "w") as file:
file.writelines(lines)


def check_keyword_in_output(command, keyword):
# Run the command and redirect the output to a file
os.system(f'{command} > output.txt')
os.system(f"{command} > output.txt")

# Read the contents of the file
with open('output.txt', 'r') as file:
with open("output.txt", "r") as file:
output = file.read()

# Check if the keyword is present in the output
if keyword in output:
# Optionally, you can delete the output file
os.remove('output.txt')
os.remove("output.txt")
return True
else:
# Optionally, you can delete the output file
os.remove('output.txt')
os.remove("output.txt")
return False


Expand Down Expand Up @@ -60,7 +58,7 @@ def check_keyword_in_output(command, keyword):
run_count = "python3 count_nnz_tiling.py"
print(run_count)

if (check_keyword_in_output(run_count, "error")) == False:
if (check_keyword_in_output(run_count, "error")) is False:
tile_size += step
step *= 2
else:
Expand Down
45 changes: 30 additions & 15 deletions generate_spmv_sparsity_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import scipy.io as sio
import scipy.sparse as sps

# from scipy.io import mmread

# Set the seed value
Expand All @@ -14,7 +15,8 @@

# generating matrix dimensions and storing results in an array, array size is 2, 1 matrix and 2 dimensions per matrix

# conditions which need to be met for each set of 3 tensor dimensions: no dimension can't be 0, and can't have a tensor with more than 900 elements (meaning dimension1*dimension2*dimension3 <= 900)
# conditions which need to be met for each set of 3 tensor dimensions: no dimension can't be 0,
# and can't have a tensor with more than 900 elements (meaning dimension1*dimension2*dimension3 <= 900)
# note try to make it so no dimension is 1 or 2 (gives slight issues later, esp 2nd and 3rd dimensions)
dimensions = [0] * 2
dimensions_onematrix = [0] * 2
Expand Down Expand Up @@ -48,40 +50,50 @@
# can add in as many sparsity numbers here (num elements in the sparsities array = num matrices being generated)
sparsities = [0.5]
# NEED TO CHANGE suitesparse_path for this to work: frostt_path = os.environ['FROSTT_PATH']
ss_path = ''
ss_path = ""
for i in range(1):
filename = os.path.join(ss_path, "rand_matrix" + str(matrix_num) + ".mtx")
sparsity = sparsities[i]
f = open(filename, "w")
f.write('\n')
f.write("\n")
lineToAddInFile = ""
arr = np.zeros([dimensions[i * 3], dimensions[(i * 3) + 1]], dtype=int)
for x in range(len(arr)):
for y in range(len(arr[x])):
# TO CHANGE SPARSITY: generate random number from 1 to 9; if 1,2,3,7,8,9 don't add a num in, only add if 4,5,6
# randomNumber = random.randint(1,9)
randomNumber = random.random()
if (randomNumber > sparsity):
if randomNumber > sparsity:
numToInsert = random.randint(1, 100)
arr[x][y] = numToInsert
numToInsert = 0
randomNumber = 0
# print(arr[x][y][z])
if (arr[x][y] != 0):
if arr[x][y] != 0:
# tensor files are not 0 indexed - say want to insert a point at (0,0,0),
# then need to feed in (1,1,1) to the tensor file to insert at the (0,0,0)
# location
lineToAddInFile = "" + str(x + 1) + " " + str(y + 1) + " " + str(arr[x][y])
lineToAddInFile = (
"" + str(x + 1) + " " + str(y + 1) + " " + str(arr[x][y])
)
countnnz += 1
f.write(lineToAddInFile + '\n')
f.write(lineToAddInFile + "\n")
# writing in first line in file:
with open(filename, 'r') as f:
with open(filename, "r") as f:
content = f.read()
updated_content = "" + str(dimensions[i * 3]) + " " + str(dimensions[i * 3 + 1]) + " " + str(countnnz) + content
with open(filename, 'w') as f:
updated_content = (
"" +
str(dimensions[i * 3]) +
" " +
str(dimensions[i * 3 + 1]) +
" " +
str(countnnz) +
content
)
with open(filename, "w") as f:
f.write(updated_content)

with open(filename, 'r') as file:
with open(filename, "r") as file:
data = file.readlines()

header = data.pop(0)
Expand All @@ -94,14 +106,16 @@
row_indices.append(int(row) - 1) # Convert to 0-based indexing
col_indices.append(int(col) - 1) # Convert to 0-based indexing
matrix_data.append(value)
matrix = sps.coo_matrix((matrix_data, (row_indices, col_indices)), shape=(num_rows, num_cols))
matrix = sps.coo_matrix(
(matrix_data, (row_indices, col_indices)), shape=(num_rows, num_cols)
)
output_file = os.path.join(ss_path, "rand_matrix" + str(matrix_num) + ".mat")
sio.savemat(output_file, {'matrix': matrix}, do_compression=True)
sio.savemat(output_file, {"matrix": matrix}, do_compression=True)

# vec = sps.random(dimensions[i*3+1], 1, 0, data_rvs=np.ones)
vec = np.ones(dimensions[i * 3 + 1])
output_file1 = os.path.join(ss_path, "rand_vector" + str(matrix_num) + ".mat")
sio.savemat(output_file1, {'vector': vec}, do_compression=True)
sio.savemat(output_file1, {"vector": vec}, do_compression=True)

# f.close()
# a = mmread(filename)
Expand All @@ -115,7 +129,8 @@

# first step: one randomly generated 3D tensor given first set dimensions
# Note: generally if 2/3 elems in a tensor is 0, it can be considered sparse
# approach: 2/3 of the time add in a 0, 1/3 of the time add in an integer from 0 to 100 (use randint to generate num from 1 to 9 inclusive, and depending on where the num is, insert number or not)
# approach: 2/3 of the time add in a 0, 1/3 of the time add in an integer from 0 to 100
# (use randint to generate num from 1 to 9 inclusive, and depending on where the num is, insert number or not)
# print('dimensions:')
# print(dimensions[0])
# print(dimensions[1])
Expand Down
6 changes: 4 additions & 2 deletions maximum_tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ def pair_tiles(app_name):
continue
for f in f_tensors:
f_loc = get_tile_id(f)
if (d_loc[1] == e_loc[0] and d_loc[3] == e_loc[2] and c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2] and
b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2] and e_loc[1] == f_loc[0] and e_loc[3] == f_loc[1]):
if (d_loc[1] == e_loc[0] and d_loc[3] == e_loc[2] and
c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2] and
b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2] and
e_loc[1] == f_loc[0] and e_loc[3] == f_loc[1]):
tile_pairing[tile] = [b, c, d, e, f]
tile += 1

Expand Down
26 changes: 14 additions & 12 deletions sam/sim/src/tiling/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@
custom_path = '/home/avb03/sam'
sys.path.append(custom_path)

SAM_STRS = {"matmul_kij": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss:1,0 -f=C:ss -s=reorder(k,i,j)",
"matmul_ikj": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss -f=C:ss -s=reorder(i,k,j)",
"matmul_ijk": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)",
"mat_elemadd": "X(i,j)=B(i,j)+C(i,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)",
"mat_elemmul": "X(i,j)=B(i,j)*C(i,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)",
"mat_mattransmul": "X(i,j)=B(j,i)*c(j)+d(i) -f=X:ss -f=B:ss -f=c:ss:0 -f=d:ss:0 -s=reorder(i,j)",
"mat_vecmul_ij": "X(i,j)=B(i,j)*c(j) -f=X:ss -f=B:ss -f=c:ss:0 -s=reorder(i,j)",
"mat_residual": "X(i,j)=b(i)-C(i,j)*d(j) -f=X:ss -f=C:ss -f=b:ss:0 -f=d:ss:0 -s=reorder(i,j)",
"mat_sddmm": "X(i,j)=B(i,j)*C(i,k)*D(k,j) -f=X:ss -f=B:ss -f=C:dd -f=D:dd:1,0 -s=reorder(i,j,k)",
"mat_elemadd3": "X(i,j)=B(i,j)+C(i,j)+D(i,j) -f=X:ss -f=B:ss -f=C:ss -f=D:ss",
"mat_mask_tri": "X(i,j)=B(i,j)*C(i,k)*D(k,j) -f=X:ss -f=B:ss -f=C:ss -f=D:ss:1,0 -s=reorder(i,j,k)",
"mat_vecmul_iter": "X(i,j)=B(i,j)*C(j,k)*D(k,l)*E(l,m)*f(m) -f=X:ss -f=B:ss -f=C:ss -f=D:ss -f=E:ss -f=f:s -s=reorder(i,j,k,l,m)"}
SAM_STRS = {
"matmul_kij": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss:1,0 -f=C:ss -s=reorder(k,i,j)",
"matmul_ikj": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss -f=C:ss -s=reorder(i,k,j)",
"matmul_ijk": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)",
"mat_elemadd": "X(i,j)=B(i,j)+C(i,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)",
"mat_elemmul": "X(i,j)=B(i,j)*C(i,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)",
"mat_mattransmul": "X(i,j)=B(j,i)*c(j)+d(i) -f=X:ss -f=B:ss -f=c:ss:0 -f=d:ss:0 -s=reorder(i,j)",
"mat_vecmul_ij": "X(i,j)=B(i,j)*c(j) -f=X:ss -f=B:ss -f=c:ss:0 -s=reorder(i,j)",
"mat_residual": "X(i,j)=b(i)-C(i,j)*d(j) -f=X:ss -f=C:ss -f=b:ss:0 -f=d:ss:0 -s=reorder(i,j)",
"mat_sddmm": "X(i,j)=B(i,j)*C(i,k)*D(k,j) -f=X:ss -f=B:ss -f=C:dd -f=D:dd:1,0 -s=reorder(i,j,k)",
"mat_elemadd3": "X(i,j)=B(i,j)+C(i,j)+D(i,j) -f=X:ss -f=B:ss -f=C:ss -f=D:ss",
"mat_mask_tri": "X(i,j)=B(i,j)*C(i,k)*D(k,j) -f=X:ss -f=B:ss -f=C:ss -f=D:ss:1,0 -s=reorder(i,j,k)",
"mat_vecmul_iter": "X(i,j)=B(i,j)*C(j,k)*D(k,l)*E(l,m)*f(m) -f=X:ss -f=B:ss -f=C:ss -f=D:ss -f=E:ss -f=f:s -s=reorder(i,j,k,l,m)"
}


def print_dict(dd):
Expand Down
17 changes: 11 additions & 6 deletions scripts/tiling/generate_gold_mattransmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ def generate_gold_mattransmul_tiled(tile_crd_b, tile_crd_c, tile_crd_d, dirname,
# print(gold_nd)

gold_out = gold_nd.tocoo()
assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[1] and tile_crd_b[0] == tile_crd_d[0] and tile_crd_b[2] == tile_crd_d[1]
assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[1]
and tile_crd_b[0] == tile_crd_d[0] and tile_crd_b[2] == tile_crd_d[1]
# assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[2]
scipy.io.mmwrite(
dirname + "out_" + str(tile_crd_b[0]) + "_" + str(tile_crd_b[1]) + "_" + str(tile_crd_b[3]) + "_" + str(tile_crd_b[2]) + "_" + str(
tile_crd_c[0]) + "_" + str(tile_crd_c[1]) + "_" + str(tile_crd_d[0]) + "_" + str(tile_crd_d[1]) + ".mtx", gold_out)
dirname + "out_" + str(tile_crd_b[0]) + "_" + str(tile_crd_b[1]) + "_" + str(tile_crd_b[3]) + "_"
+ str(tile_crd_b[2]) + "_" + str(tile_crd_c[0]) + "_" + str(tile_crd_c[1])
+ "_" + str(tile_crd_d[0]) + "_" + str(tile_crd_d[1]) + ".mtx", gold_out)
elif os.path.exists(d_filename):
d_scipy = scipy.io.mmread(d_filename)
# print("\nd_scipy: ", d_scipy)
Expand Down Expand Up @@ -125,11 +127,14 @@ def generate_gold_mattransmul_tiled(tile_crd_b, tile_crd_c, tile_crd_d, dirname,
return

gold_out = gold_nd.tocoo()
# assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[1] and tile_crd_b[0] == tile_crd_d[0] and tile_crd_b[2] == tile_crd_d[1]
# assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[1]
# and tile_crd_b[0] == tile_crd_d[0] and tile_crd_b[2] == tile_crd_d[1]
# assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[2]
scipy.io.mmwrite(
dirname + "out_" + str(tile_crd_b[0]) + "_" + str(tile_crd_b[1]) + "_" + str(tile_crd_b[3]) + "_" + str(tile_crd_b[2]) + "_" + str(
tile_crd_c[0]) + "_" + str(tile_crd_c[1]) + "_" + str(tile_crd_d[0]) + "_" + str(tile_crd_d[1]) + ".mtx", gold_out)
dirname + "out_" + str(tile_crd_b[0]) + "_" + str(tile_crd_b[1])
+ "_" + str(tile_crd_b[3]) + "_" + str(tile_crd_b[2]) + "_"
+ str(tile_crd_c[0]) + "_" + str(tile_crd_c[1]) + "_"
+ str(tile_crd_d[0]) + "_" + str(tile_crd_d[1]) + ".mtx", gold_out)


if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions setup_tiling_mat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
# Usage: python3 setup_tiling_mat.py <app_name> <data_file> <tile_size> <docker_path>


## PARAMS ######################################################################
# PARAMS
data = [sys.argv[2]]
tilesizes = [int(sys.argv[3])]
app_name = sys.argv[1]
docker_path = sys.argv[4]

print("TILESIZES: ", tilesizes)
print("DATA: ", data)
###############################################################################


def write_to_line(file_path, line_number, new_content):
Expand Down
4 changes: 2 additions & 2 deletions setup_tiling_tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import shutil
from scripts.util.util import FormatWriter, InputCacheSuiteSparse

#### PARAMS ####
# PARAMS
tile = True
app_name = "tensor3_ttv"
vector_names = ['c']
##############


tiled_tensors = glob.glob(f"tiles/{app_name}/mtx/*.tns")
formatwriter = FormatWriter()
Expand Down
4 changes: 0 additions & 4 deletions tile_pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@
# "fast"],
# text=True)

# shutil.copy("/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/output_gold.npy", "/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/bin")
# shutil.copytree("/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/bin", f"/aha/garnet/SPARSE_TESTS/{tile_str}")
tile = tile + 1
# print("we are on tile ", tile)
elif app_name == "mat_mattransmul":
Expand Down Expand Up @@ -489,8 +487,6 @@
# "fast"],
# text=True)

# shutil.copy("/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/output_gold.npy", "/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/bin")
# shutil.copytree("/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/bin", f"/aha/garnet/SPARSE_TESTS/{tile_str}")
tile = tile + 1
# print("we are on tile ", tile)

Expand Down

0 comments on commit 2c067d1

Please sign in to comment.