Skip to content

Commit

Permalink
merge in tiling branch
Browse files Browse the repository at this point in the history
  • Loading branch information
kalhankoul96 committed Dec 18, 2023
2 parents 2c699f9 + a1440cd commit f308aed
Show file tree
Hide file tree
Showing 33 changed files with 2,730 additions and 188 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ suitesparse-bench/
sam-outputs/
compiler/benchmark/

# Generated SAM simulator tests
*/sim/test/apps/test_*.py

# Tensor files
*.mtx
*.tns

# Temporary or generated tensor directories
tmp_mat*/
tiles/
Expand Down
32 changes: 12 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ endif

ifeq ("$(NEVA)","ON")
CMD := OMP_PROC_BIND=true LD_LIBRARY_PATH=compiler/build/lib/:$(LD_LIBRARY_PATH) numactl -C 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 -m 0 compiler/build/taco-bench $(BENCHFLAGS)
export SUITESPARSE_PATH=/nobackup/owhsu/sparse-datasets/suitesparse/
export FROSTT_PATH=/nobackup/owhsu/sparse-datasets/frostt/
export SUITESPARSE_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/suitesparse-formatted
export FROSTT_FORMATTED_TACO_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted/taco-tensor
export FROSTT_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted
export TACO_TENSOR_PATH=/nobackup/owhsu/sparse-datasets
# export SUITESPARSE_PATH=/nobackup/owhsu/sparse-datasets/suitesparse/
# export FROSTT_PATH=/nobackup/owhsu/sparse-datasets/frostt/
# export SUITESPARSE_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/suitesparse-formatted
# export FROSTT_FORMATTED_TACO_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted/taco-tensor
# export FROSTT_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted
# export TACO_TENSOR_PATH=/nobackup/owhsu/sparse-datasets
else ifeq ("$(LANKA)", "ON")
CMD := OMP_PROC_BIND=true LD_LIBRARY_PATH=compiler/build/lib/:$(LD_LIBRARY_PATH) numactl -C 0,2,4,6,8,10,24,26,28,30,32,34 -m 0 compiler/build/taco-bench $(BENCHFLAGS)
export SUITESPARSE_PATH=/data/scratch/changwan/florida_all
export FROSTT_PATH=/data/scratch/owhsu/datasets/frostt
export TACO_TENSOR_PATH=/data/scratch/owhsu/datasets
export SUITESPARSE_FORMATTED_PATH=/data/scratch/owhsu/datasets/suitesparse-formatted
export FROSTT_FORMATTED_TACO_PATH=/data/scratch/owhsu/datasets/frostt-formatted/taco-tensor
export FROSTT_FORMATTED_PATH=/data/scratch/owhsu/datasets/frostt-formatted
# export SUITESPARSE_PATH=/data/scratch/changwan/florida_all
# export FROSTT_PATH=/data/scratch/owhsu/datasets/frostt
# export TACO_TENSOR_PATH=/data/scratch/owhsu/datasets
# export SUITESPARSE_FORMATTED_PATH=/data/scratch/owhsu/datasets/suitesparse-formatted
# export FROSTT_FORMATTED_TACO_PATH=/data/scratch/owhsu/datasets/frostt-formatted/taco-tensor
# export FROSTT_FORMATTED_PATH=/data/scratch/owhsu/datasets/frostt-formatted
else
CMD := LD_LIBRARY_PATH=compiler/build/lib/:$(LD_LIBRARY_PATH) compiler/build/taco-bench $(BENCHFLAGS)
endif
Expand Down Expand Up @@ -114,14 +114,6 @@ suitesparse-formats: guard-SUITESPARSE_FORMATTED_PATH guard-SUITESPARSE_PATH
frostt-formats: taco/build guard-FROSTT_FORMATTED_PATH guard-FROSTT_PATH
./scripts/formatting/generate_frostt_formats.sh

.PHONY: env
env:
export SUITESPARSE_PATH=/nobackup/owhsu/sparse-datasets/suitesparse/
export FROSTT_PATH=/nobackup/owhsu/sparse-datasets/frostt/
export SUITESPARSE_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/suitesparse-formatted
export FROSTT_FORMATTED_TACO_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted/taco-tensor
export FROSTT_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted

.PHONY: pydepends
pydepends:
conda env export > environment.yml
Expand Down
14 changes: 13 additions & 1 deletion compiler/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ enum SuiteSparseOp {
SDDMM = 4,
MATTRANSMUL = 5,
RESIDUAL = 6,
MMADD = 7
MMADD = 7,
MMMUL = 8
};

std::string opName(SuiteSparseOp op) {
Expand All @@ -341,6 +342,9 @@ std::string opName(SuiteSparseOp op) {
case MMADD: {
return "mmadd";
}
case MMMUL: {
return "mmmul"
}
default:
return "";
}
Expand Down Expand Up @@ -467,6 +471,13 @@ static void bench_suitesparse(benchmark::State &state, SuiteSparseOp op, int fil
result(i, j) = ssTensor(i, j) + otherShifted(i, j);
break;
}
case MMMUL: {
result = Tensor<int64_t>("result", ssTensor.getDimensions(), ssTensor.getFormat(), fill_value);

IndexVar i, j, k;
result(i, j) = ssTensor(i, j) * otherShifted(i, j);
break;
}
case MATTRANSMUL: {
result = Tensor<int64_t>("result", {DIM1}, Format(Sparse), fill_value);

Expand Down Expand Up @@ -516,4 +527,5 @@ static void bench_suitesparse(benchmark::State &state, SuiteSparseOp op, int fil
// TODO: need to fix for DCSC for this
TACO_BENCH_ARGS(bench_suitesparse, mat_mattransmul, MATTRANSMUL);
TACO_BENCH_ARGS(bench_suitesparse, matmul_spmm, SPMM);
TACO_BENCH_ARGS(bench_suitesparse, mat_elemmul, MMMUL);

52 changes: 52 additions & 0 deletions count_nnz_tiling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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()]

nonzeros = sum(1 for val in matrix_values if val != 0)

return nonzeros


tile_dirs = glob.glob("SPARSE_TESTS/MAT_TMP_DIR/tile*")
num_tiles = len(tile_dirs)
limit = 900
print("there are ", num_tiles, "tiles")

sparsity_B = 0
sparsity_C = 0
# tilesize=int(sys.argv[1])**2
tot_num_nonzeros = 0
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'

num_nonzeros = count_nonzeros(tensor_C_values_file)
tot_num_nonzeros += num_nonzeros
if num_nonzeros >= limit:
print("num_nonzeros: ", num_nonzeros)
print("error! too many nonzeros in INPUT matrices")
raise Exception

tensor_C_values_file = f'SPARSE_TESTS/MAT_TMP_DIR/tile{tile_num}/tensor_B_mode_vals'

num_nonzeros = count_nonzeros(tensor_C_values_file)
tot_num_nonzeros += num_nonzeros
if num_nonzeros >= limit:
print("num_nonzeros: ", num_nonzeros)
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")
raise Exception

sparsity_B /= num_tiles
sparsity_C /= num_tiles

print("sparsity_B: ", sparsity_B)
print("sparsity_C: ", sparsity_C)
78 changes: 78 additions & 0 deletions find_max_tilesize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
import sys
import glob

def write_to_line(file_path, line_number, new_content):
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'

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')

# Read the contents of the 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')
return True
else:
# Optionally, you can delete the output file
os.remove('output.txt')
return False


tile_size = 300
step = 10

for _ in range(20):
print("********************")
print("tile size: ", tile_size)
print("step: ", step)

yaml_file = "sam/sim/src/tiling/memory_config_onyx.yaml"
mem_tile_line = f"Mem_tile_size: {tile_size}"
print(mem_tile_line)
write_to_line(yaml_file, 19, mem_tile_line)

run_setup_script = "python3 setup_tiling_mat.py > temp.txt"
os.system(run_setup_script)
print(run_setup_script)

run_tile_pairing = "python3 tile_pairing.py > temp.txt"
os.system(run_tile_pairing)
print(run_tile_pairing)

run_count = "python3 count_nnz_tiling.py"
print(run_count)

if (check_keyword_in_output(run_count, "error")) == False:
tile_size += step
step *= 2
else:
print("****************Tile broken!")
tile_size -= step
step //= 2

if tile_size == 450:
break

if step == 0:
if _ >= 15:
step = 10
else:
break

print("max tile size: ", tile_size)
164 changes: 164 additions & 0 deletions generate_spmv_sparsity_sweep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#script to generate 50 random 3D tensors (seeded, produces same 50 each time)
import numpy as np
import random
import os
import scipy.io as sio
import scipy.sparse as sps
# from scipy.io import mmread

# Set the seed value
#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

#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

# x goes from 0 to __ (before 0 to 49)
for x in range(1):
# dimensions_onematrix[0] = random.randint(1,60)
# dimensions_onematrix[1] = random.randint(3,60)

# while((dimensions_onetensor[0]*dimensions_onetensor[1]*dimensions_onetensor[2])>901):
# dimensions_onematrix[0] = random.randint(1,60)
# dimensions_onematrix[1] = random.randint(3,60)
# dimensions_onematrix[2] = random.randint(3,60)
dimensions_onematrix[0] = 10
dimensions_onematrix[1] = 10

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

dimensions_onematrix[0] = 0
dimensions_onematrix[1] = 0
#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)
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)
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")
sparsity = sparsities[i]
f = open(filename, "w")
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):
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])
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
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 = []
row_indices = []
col_indices = []
for line in data:
row, col, value = map(float, line.strip().split())
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))
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")
sio.savemat(output_file1, {'vector': vec}, do_compression=True)


# f.close()
# a = mmread(filename)
# a.toarray()
# scipy.io.savemat("rand_matrix"+str(matrix_num)+".mat", {'mydata': a})

# f.write(""+str(dimensions[i*3]) + " " + str(dimensions[i*3+1]) + " " + str(countnnz))
# f.write("\n")
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')

"""
arr = np.zeros([dimensions[0],dimensions[1],dimensions[2]], dtype=int)
randomNumber = 0
numToInsert = 0
for x in range(len(arr)):
for y in range(len(arr[x])):
for z in range(len(arr[x][y])):
#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)
if(randomNumber==4 or randomNumber==5 or randomNumber==6):
numToInsert = random.randint(1,100)
arr[x][y][z] = numToInsert
numToInsert = 0
print(arr[x][y][z])
#lineToAddInFile="" + str(x) + " " + str(y) + " " + str(z) + " " + str(arr[x][y][z])
#f.write(lineToAddInFile + '\n')
print('dimensions:')
print(dimensions[3])
print(dimensions[4])
print(dimensions[5])
print('tensor vals')
arr = np.zeros([dimensions[3],dimensions[4],dimensions[5]], dtype=int)
randomNumber = 0
numToInsert = 0
for x in range(len(arr)):
for y in range(len(arr[x])):
for z in range(len(arr[x][y])):
#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)
if(randomNumber==4 or randomNumber==5 or randomNumber==6):
numToInsert = random.randint(1,100)
arr[x][y][z] = numToInsert
numToInsert = 0
randomNumber = 0
print(arr[x][y][z])
#lineToAddInFile="" + str(x) + " " + str(y) + " " + str(z) + " " + str(arr[x][y][z])
#f.write(lineToAddInFile + '\n')
"""
Loading

0 comments on commit f308aed

Please sign in to comment.