diff --git a/find_max_tilesize.py b/find_max_tilesize.py index e2c21812..ef81fbd1 100644 --- a/find_max_tilesize.py +++ b/find_max_tilesize.py @@ -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 @@ -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: diff --git a/generate_spmv_sparsity_sweep.py b/generate_spmv_sparsity_sweep.py index 165e6894..7437d023 100644 --- a/generate_spmv_sparsity_sweep.py +++ b/generate_spmv_sparsity_sweep.py @@ -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 @@ -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 @@ -48,12 +50,12 @@ # 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)): @@ -61,27 +63,37 @@ # 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) @@ -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) @@ -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]) diff --git a/maximum_tiling.py b/maximum_tiling.py index 578e42ac..33933671 100644 --- a/maximum_tiling.py +++ b/maximum_tiling.py @@ -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 diff --git a/sam/sim/src/tiling/tile.py b/sam/sim/src/tiling/tile.py index b605c2b3..96d4f96f 100644 --- a/sam/sim/src/tiling/tile.py +++ b/sam/sim/src/tiling/tile.py @@ -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): diff --git a/scripts/tiling/generate_gold_mattransmul.py b/scripts/tiling/generate_gold_mattransmul.py index a298e2bb..43ee33e0 100644 --- a/scripts/tiling/generate_gold_mattransmul.py +++ b/scripts/tiling/generate_gold_mattransmul.py @@ -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) @@ -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__": diff --git a/setup_tiling_mat.py b/setup_tiling_mat.py index febdfc87..1aa26241 100644 --- a/setup_tiling_mat.py +++ b/setup_tiling_mat.py @@ -10,7 +10,7 @@ # Usage: python3 setup_tiling_mat.py -## PARAMS ###################################################################### +# PARAMS data = [sys.argv[2]] tilesizes = [int(sys.argv[3])] app_name = sys.argv[1] @@ -18,7 +18,6 @@ print("TILESIZES: ", tilesizes) print("DATA: ", data) -############################################################################### def write_to_line(file_path, line_number, new_content): diff --git a/setup_tiling_tensors.py b/setup_tiling_tensors.py index 25642121..a704b765 100644 --- a/setup_tiling_tensors.py +++ b/setup_tiling_tensors.py @@ -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() diff --git a/tile_pairing.py b/tile_pairing.py index d2a5c40f..bf65eabc 100644 --- a/tile_pairing.py +++ b/tile_pairing.py @@ -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": @@ -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)