Skip to content

Commit

Permalink
style fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
kalhankoul96 committed Jan 9, 2024
1 parent 0dc4a47 commit 1558668
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 150 deletions.
8 changes: 4 additions & 4 deletions count_nnz_tiling.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import glob
import sys


def count_nonzeros(matrix_values_file):
with open(matrix_values_file, 'r') as values_file:
matrix_values = [float(val) for val in values_file.readlines()]
Expand All @@ -18,7 +19,7 @@ def count_nonzeros(matrix_values_file):
sparsity_C = 0
# tilesize=int(sys.argv[1])**2
tot_num_nonzeros = 0
for tile_num in range(0,num_tiles):
for tile_num in range(0, num_tiles):
tot_num_nonzeros = 0

tensor_C_values_file = f'SPARSE_TESTS/MAT_TMP_DIR/tile{tile_num}/tensor_C_mode_vals'
Expand All @@ -39,7 +40,6 @@ def count_nonzeros(matrix_values_file):
print("error! too many nonzeros in INPUT matrices")
raise Exception


if tot_num_nonzeros >= limit:
print("tot_num_nonzeros: ", tot_num_nonzeros)
print("error! too many nonzeros in matrices")
Expand All @@ -49,4 +49,4 @@ def count_nonzeros(matrix_values_file):
sparsity_C /= num_tiles

print("sparsity_B: ", sparsity_B)
print("sparsity_C: ", sparsity_C)
print("sparsity_C: ", sparsity_C)
4 changes: 3 additions & 1 deletion find_max_tilesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import glob


def write_to_line(file_path, line_number, new_content):
with open(file_path, 'r') as file:
lines = file.readlines()
Expand All @@ -15,6 +16,7 @@ def write_to_line(file_path, line_number, new_content):
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')
Expand Down Expand Up @@ -74,5 +76,5 @@ def check_keyword_in_output(command, keyword):
step = 10
else:
break

print("max tile size: ", tile_size)
69 changes: 35 additions & 34 deletions generate_spmv_sparsity_sweep.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#script to generate 50 random 3D tensors (seeded, produces same 50 each time)
# script to generate 50 random 3D tensors (seeded, produces same 50 each time)
import numpy as np
import random
import os
Expand All @@ -7,15 +7,15 @@
# from scipy.io import mmread

# Set the seed value
#previously used to be this: seed_value = 42
# previously used to be this: seed_value = 42
seed_value = 100
random.seed(seed_value)
np.random.seed(seed_value)

#generating matrix dimensions and storing results in an array, array size is 2, 1 matrix and 2 dimensions per matrix
# 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)
#note try to make it so no dimension is 1 or 2 (gives slight issues later, esp 2nd and 3rd dimensions)
# 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 All @@ -31,57 +31,59 @@
dimensions_onematrix[0] = 10
dimensions_onematrix[1] = 10

dimensions[x*3] = dimensions_onematrix[0]
dimensions[(x*3)+1] = dimensions_onematrix[1]
dimensions[x * 3] = dimensions_onematrix[0]
dimensions[(x * 3) + 1] = dimensions_onematrix[1]

dimensions_onematrix[0] = 0
dimensions_onematrix[1] = 0
#print('\n')
# print('\n')


#Generating matrix values based on the dimensions now stored in the dimensions (2 elem) array
#i goes from 0 to __ (before 0 to 49)
# Generating matrix values based on the dimensions now stored in the dimensions (2 elem) array
# i goes from 0 to __ (before 0 to 49)
matrix_num = 1
randomNumber = 0
numToInsert = 0
countnnz = 0
#can add in as many sparsity numbers here (num elements in the sparsities array = num matrices being generated)
# 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 = ''
for i in range(1):
filename = os.path.join(ss_path, "rand_matrix"+str(matrix_num)+".mtx")
filename = os.path.join(ss_path, "rand_matrix" + str(matrix_num) + ".mtx")
sparsity = sparsities[i]
f = open(filename, "w")
f.write('\n')
lineToAddInFile = ""
arr = np.zeros([dimensions[i*3],dimensions[(i*3)+1]], dtype=int)
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
# 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):
numToInsert = random.randint(1,100)
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):
#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])
# print(arr[x][y][z])
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])
countnnz += 1
f.write(lineToAddInFile + '\n')
# writing in first line in file:
with open(filename, 'r') as f:
content = f.read()
updated_content = ""+str(dimensions[i*3]) + " " + str(dimensions[i*3+1]) + " " + str(countnnz) + content
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:
data = file.readlines()

header = data.pop(0)
num_rows, num_cols, num_nonzeros = map(int, header.strip().split())
matrix_data = []
Expand All @@ -93,15 +95,14 @@
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))
output_file = os.path.join(ss_path, "rand_matrix"+str(matrix_num)+".mat")
output_file = os.path.join(ss_path, "rand_matrix" + str(matrix_num) + ".mat")
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")
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)


# f.close()
# a = mmread(filename)
# a.toarray()
Expand All @@ -112,14 +113,14 @@
matrix_num = matrix_num + 1


#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)
#print('dimensions:')
#print(dimensions[0])
#print(dimensions[1])
#print(dimensions[2])
#print('tensor vals')
# 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)
# print('dimensions:')
# print(dimensions[0])
# print(dimensions[1])
# print(dimensions[2])
# print('tensor vals')

"""
arr = np.zeros([dimensions[0],dimensions[1],dimensions[2]], dtype=int)
Expand Down
49 changes: 30 additions & 19 deletions maximum_tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
import os
import re


class EarlyReturn():
pass


def get_files_from_dir(path, operands):
operand_files = {}
for operand in operands:
operand_files[operand] = glob.glob(os.path.join(path, f"*{operand}*.mtx"))

return operand_files


def get_tile_id(string):
indices = [m.start() for m in re.finditer("tile", string)]
if len(indices) >= 2:
Expand All @@ -22,6 +26,7 @@ def get_tile_id(string):
numbers = substring.split("_")
return numbers


def pair_tiles(app_name):
path = f"tiles/{app_name}/mtx"
tile_pairing = {}
Expand All @@ -32,7 +37,7 @@ def pair_tiles(app_name):
operand_files = get_files_from_dir(path, operands)
b_tensors = operand_files["B"]
c_tensors = operand_files["C"]

tile = 0
for b in b_tensors:
for c in c_tensors:
Expand Down Expand Up @@ -93,7 +98,8 @@ def pair_tiles(app_name):

for d in d_tensors:
d_loc = get_tile_id(d)
if(c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2] and b_loc[1] == d_loc[1] and b_loc[3] == d_loc[3] and b_loc[0] == c_loc[0] and b_loc[2] == c_loc[2]):
if (c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2] and b_loc[1] == d_loc[1] and
b_loc[3] == d_loc[3] and b_loc[0] == c_loc[0] and b_loc[2] == c_loc[2]):
tile_pairing[tile] = [b, c, d]
tile += 1
elif "mat_vecmul_iter" in app_name:
Expand Down Expand Up @@ -125,20 +131,20 @@ 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




return tile_pairing


def read_mtx(mtx_path):
matrix = scipy.io.mmread(mtx_path)
arr = np.array(matrix.todense())
return arr


def compute_outputs(tile_pairing, app_name, limit=900):
for key, value in tile_pairing.items():
if "matmul" in app_name:
Expand All @@ -147,7 +153,7 @@ def compute_outputs(tile_pairing, app_name, limit=900):
C_mat = np.transpose(C_mat)
out = np.matmul(B_mat, C_mat)
if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit:
# if np.any(out):
# if np.any(out):
print("tile = ", key)
print("B_tile_ID = ", value[0])
print("C_tile_ID = ", value[1])
Expand All @@ -160,7 +166,7 @@ def compute_outputs(tile_pairing, app_name, limit=900):
out = np.multiply(B_mat, C_mat)
# if np.any(out):
if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit:
# if np.count_nonzero(out) > limit or (np.count_nonzero(B_mat) + np.count_nonzero(C_mat)) > limit:
# if np.count_nonzero(out) > limit or (np.count_nonzero(B_mat) + np.count_nonzero(C_mat)) > limit:
print("tile = ", key)
print("B_tile_ID = ", value[0])
print("C_tile_ID = ", value[1])
Expand All @@ -174,8 +180,9 @@ def compute_outputs(tile_pairing, app_name, limit=900):

out = np.add(np.add(B_mat, C_mat), D_mat)
# if np.any(out):
if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit or np.count_nonzero(D_mat) > limit:
# if np.count_nonzero(out) > limit or (np.count_nonzero(B_mat) + np.count_nonzero(C_mat)) > limit:
if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(
C_mat) > limit or np.count_nonzero(D_mat) > limit:
# if np.count_nonzero(out) > limit or (np.count_nonzero(B_mat) + np.count_nonzero(C_mat)) > limit:
print("tile = ", key)
print("B_tile_ID = ", value[0])
print("C_tile_ID = ", value[1])
Expand All @@ -189,7 +196,8 @@ def compute_outputs(tile_pairing, app_name, limit=900):
D_mat = read_mtx(value[2])
D_mat = np.transpose(D_mat)
out = np.sum(np.multiply(np.matmul(C_mat, D_mat), B_mat))
if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit or np.count_nonzero(D_mat) > limit:
if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(
C_mat) > limit or np.count_nonzero(D_mat) > limit:
print("tile = ", key)
print("B_tile_ID = ", value[0])
print("C_tile_ID = ", value[1])
Expand All @@ -207,7 +215,9 @@ def compute_outputs(tile_pairing, app_name, limit=900):
f_mat = np.transpose(f_mat)
out = np.matmul(np.matmul(np.matmul(np.matmul(B_mat, C_mat), D_mat), E_mat), f_mat)
if np.any(out):
# if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit or np.count_nonzero(D_mat) > limit or np.count_nonzero(E_mat) > limit or np.count_nonzero(f_mat) > limit:
# if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or
# np.count_nonzero(C_mat) > limit or np.count_nonzero(D_mat) > limit or
# np.count_nonzero(E_mat) > limit or np.count_nonzero(f_mat) > limit:
print("tile = ", key)
print("B_tile_ID = ", value[0])
print("C_tile_ID = ", value[1])
Expand All @@ -219,7 +229,8 @@ def compute_outputs(tile_pairing, app_name, limit=900):
breakpoint()
return EarlyReturn()
return None



def find_optimal_tilesize(app_name, datum, initial=30, step_size=10):
tile_size = initial
max_tile_size = initial
Expand All @@ -236,15 +247,15 @@ def find_optimal_tilesize(app_name, datum, initial=30, step_size=10):
if isinstance(exit_status, EarlyReturn):
max_tile_size = tile_size - step_size
return max_tile_size, prev_tile_pairing

tile_size += step_size
print("***********************")
print("tile size = ", tile_size)
print("***********************")
prev_tile_pairing = tile_pairing

return tile_size, prev_tile_pairing


if __name__ == "__main__":
max_list = {}
Expand All @@ -256,15 +267,15 @@ def find_optimal_tilesize(app_name, datum, initial=30, step_size=10):
# compute_outputs(tile_pairing, app_name)

max_tile_size, tile_pairing = find_optimal_tilesize(app_name, datum, initial=40, step_size=10)
print("-"*20)
print("-" * 20)
print(f"MAX TILESIZE for {app_name}, {datum}: {max_tile_size}")
print(f"NUMBER OF TILES: {len(tile_pairing.keys())}")
print("-"*20)
print("-" * 20)

max_list[datum] = [max_tile_size, len(tile_pairing.keys())]

call_tiling = f"python3 setup_tiling_mat.py {app_name} {datum} {max_tile_size} > temp.txt"
os.system(call_tiling)
print(call_tiling)

# print(max_list)
# print(max_list)
4 changes: 2 additions & 2 deletions sam/onyx/synthetic/generate_fixed_nnz_mats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import argparse


def generate_mat(nnz, dim):
return scipy.sparse.random(dim, dim, nnz / (dim**2), data_rvs=np.ones)

Expand All @@ -18,9 +19,8 @@ def write_mtx(path, t):
parser = argparse.ArgumentParser(description="Create some random matrices of given nnz and dim")
parser.add_argument('--nnz', type=int, nargs='+', help='nnz')
parser.add_argument('--dim', type=int, nargs='+', help='dim')
parser.add_argument('--extensor', action='store_true', help='generate extensor dims and nnzs')
parser.add_argument('--extensor', action='store_true', help='generate extensor dims and nnzs')
args = parser.parse_args()


if args.extensor:
dims = list(range(1024, 15721, 1336))
Expand Down
Loading

0 comments on commit 1558668

Please sign in to comment.