From e832e35e0742d0376318d1ce629c17d84f8e2089 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 00:35:33 -0400 Subject: [PATCH 01/19] Update random_matrix_generator.py --- utils/random_matrix_generator.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/utils/random_matrix_generator.py b/utils/random_matrix_generator.py index f57d0c8..78e3c8b 100644 --- a/utils/random_matrix_generator.py +++ b/utils/random_matrix_generator.py @@ -21,13 +21,13 @@ def createRandomMatrix(n): return matrix -def saveMatrix(matrixA, matrixB, filename): +def saveMatrix(matrixA, filename): if os.path.exists(filename): os.remove(filename) else: print("New file created: ",filename) f = open(filename, "w") - for i, matrix in enumerate([matrixA, matrixB]): + for i, matrix in enumerate([matrixA]): if i != 0: f.write("\n") for line in matrix: @@ -61,7 +61,7 @@ def main(): outpath = args.dump #Create dense matrix matrixA = createRandomMatrix(n) - matrixB = createRandomMatrix(n) + #matrixB = createRandomMatrix(n) #print(matrixA) #Convert to sparse matrix by replacing value below threshold to 0 if (args.sparsity): @@ -72,26 +72,26 @@ def main(): flatA = matrixA.flatten() flatA[indicesA] = 0 #Replace random x %element to 0 in matrixB - matrixB = np.asarray(matrixB) - indicesB = np.random.choice(np.arange(matrixB.size), replace=False, + #matrixB = np.asarray(matrixB) + #indicesB = np.random.choice(np.arange(matrixB.size), replace=False, size=int(matrixB.size * (args.sparsity/100))) - flatB = matrixB.flatten() - flatB[indicesB] = 0 + #flatB = matrixB.flatten() + #flatB[indicesB] = 0 #Reshape it back to square matrix flatA = flatA.reshape(n,n) - flatB = flatB.reshape(n,n) + #flatB = flatB.reshape(n,n) #print(flatA) matrixA_csr = sparse.csr_matrix(flatA) #print(matrixA_csr) - matrixB_csr = sparse.csr_matrix(flatB) + #matrixB_csr = sparse.csr_matrix(flatB) matrixA = flatA.tolist() - matrixB = flatB.tolist() + #matrixB = flatB.tolist() csr_Amatrix = "csrA_"+args.dump - csr_Bmatrix = "csrB_"+args.dump + #csr_Bmatrix = "csrB_"+args.dump saveCSRMatrix(matrixA_csr, csr_Amatrix) - saveCSRMatrix(matrixB_csr, csr_Bmatrix) + #saveCSRMatrix(matrixB_csr, csr_Bmatrix) #print(matrixA) - saveMatrix(matrixA, matrixB, args.dump) + saveMatrix(matrixA, args.dump) if __name__ == '__main__': main() From 66d98247ff1da8a7f4415cf1032d91dd0a501684 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 00:42:00 -0400 Subject: [PATCH 02/19] Update random_matrix_generator.py --- utils/random_matrix_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/random_matrix_generator.py b/utils/random_matrix_generator.py index 78e3c8b..4739372 100644 --- a/utils/random_matrix_generator.py +++ b/utils/random_matrix_generator.py @@ -74,7 +74,7 @@ def main(): #Replace random x %element to 0 in matrixB #matrixB = np.asarray(matrixB) #indicesB = np.random.choice(np.arange(matrixB.size), replace=False, - size=int(matrixB.size * (args.sparsity/100))) + #size=int(matrixB.size * (args.sparsity/100))) #flatB = matrixB.flatten() #flatB[indicesB] = 0 #Reshape it back to square matrix From fe965240af8b668a65c7d799cc194739dcdcb816 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:54:02 -0400 Subject: [PATCH 03/19] Create scatter.cpp --- src/scatter.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/scatter.cpp diff --git a/src/scatter.cpp b/src/scatter.cpp new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/scatter.cpp @@ -0,0 +1 @@ + From 183036da1e9d6a6b2052ce19a3288cac6e7cebce Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 13:12:50 -0400 Subject: [PATCH 04/19] Update scatter.cpp --- src/scatter.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/scatter.cpp b/src/scatter.cpp index 8b13789..30d9bf2 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -1 +1,89 @@ +#include +#include +#include +#include +#include +using namespace std; + +void parsec_roi_begin() +{ + +} + +void parsec_roi_end() +{ + +} + +struct Result { + vector< vector > A; +}; + +Result read(string filename) { + vector< vector > A; + Result ab; + string line; + ifstream infile; + infile.open (filename.c_str()); + + int i = 0; + while (getline(infile, line) && !line.empty()) { + istringstream iss(line); + A.resize(A.size() + 1); + int a, j = 0; + while (iss >> a) { + A[i].push_back(a); + j++; + } + i++; + } + infile.close(); + ab.A = A; + return ab; +} + +vector< vector > scatter(vector< vector > A) { + int n = A.size(); + int num_of_indices = 1000; + vector indices(number_of_indices), data(number_of_indices); + for(int i = 0; i < number_of_indices ; i++){ + indices[i] = rand() % (number_of_indices); + data[i] = rand(); + } + for(int i = 0 ; i < number_of_indices ; i++){ + int row = indices[i]/n; + int col = indices[i]%n; + A[row][col] = data[i]; + } + return A; +} + +void printMatrix(vector< vector > matrix) { + vector< vector >::iterator it; + vector::iterator inner; + for (it=matrix.begin(); it != matrix.end(); it++) { + for (inner = it->begin(); inner != it->end(); inner++) { + cout << *inner; + if(inner+1 != it->end()) { + cout << "\t"; + } + } + cout << endl; + } +} + +int main (int argc, char* argv[]) { + string filename; + if (argc < 3) { + filename = "2000.in"; + } else { + filename = argv[2]; + } + Result result = read (filename); + parsec_roi_begin(); + vector< vector > C = scatter(result.A); + parsec_roi_end(); + //printMatrix(C); + return 0; +} From 032bae8f8bbd1237eed8869b847f5b0a3c1140c9 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:14:32 -0400 Subject: [PATCH 05/19] Update scatter.cpp --- src/scatter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 30d9bf2..625069d 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -45,7 +45,7 @@ Result read(string filename) { vector< vector > scatter(vector< vector > A) { int n = A.size(); - int num_of_indices = 1000; + int number_of_indices = 1000; vector indices(number_of_indices), data(number_of_indices); for(int i = 0; i < number_of_indices ; i++){ indices[i] = rand() % (number_of_indices); From 0cd05c611e1bd61fc762be5eca4103bb0185d6e6 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:37:00 -0400 Subject: [PATCH 06/19] Update scatter.cpp --- src/scatter.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 625069d..c41d1b5 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace std; @@ -48,7 +49,7 @@ vector< vector > scatter(vector< vector > A) { int number_of_indices = 1000; vector indices(number_of_indices), data(number_of_indices); for(int i = 0; i < number_of_indices ; i++){ - indices[i] = rand() % (number_of_indices); + indices[i] = rand() % (n*n); data[i] = rand(); } for(int i = 0 ; i < number_of_indices ; i++){ @@ -74,6 +75,7 @@ void printMatrix(vector< vector > matrix) { } int main (int argc, char* argv[]) { + srand(time(0)); string filename; if (argc < 3) { filename = "2000.in"; From c655771614ab3e5e3504563f62ac977f6446f614 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:38:24 -0400 Subject: [PATCH 07/19] Update cache_simulator.py --- Simulator/src/cache_simulator.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Simulator/src/cache_simulator.py b/Simulator/src/cache_simulator.py index 21e0b78..3869df7 100755 --- a/Simulator/src/cache_simulator.py +++ b/Simulator/src/cache_simulator.py @@ -188,6 +188,10 @@ def build_hierarchy(configs, logger): main_memory = build_cache(configs, 'mem', None, logger) prev_level = main_memory hierarchy['mem'] = main_memory + if 'cache_4' in list(configs.keys()): + cache_4 = build_cache(configs, 'cache_4', prev_level, logger) + prev_level = cache_4 + hierarchy['cache_4'] = cache_4 if 'cache_3' in list(configs.keys()): cache_3 = build_cache(configs, 'cache_3', prev_level, logger) prev_level = cache_3 From d54c9452e0d7b440716272163f83092986722031 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:39:34 -0400 Subject: [PATCH 08/19] Update config_simple_multilevel --- Simulator/config/config_simple_multilevel | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Simulator/config/config_simple_multilevel b/Simulator/config/config_simple_multilevel index fc35d35..7ec2e34 100644 --- a/Simulator/config/config_simple_multilevel +++ b/Simulator/config/config_simple_multilevel @@ -20,6 +20,11 @@ cache_3: associativity: 8 hit_time: 100 +cache_4: + blocks: 512 + associativity: 8 + hit_time: 150 + mem: #required hit_time: 1000 #cycles From b154049dc195529f681ff7198aeb0206a0278d13 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:40:44 -0400 Subject: [PATCH 09/19] Update config_simple_multilevel --- Simulator/config/config_simple_multilevel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Simulator/config/config_simple_multilevel b/Simulator/config/config_simple_multilevel index 7ec2e34..a38fc19 100644 --- a/Simulator/config/config_simple_multilevel +++ b/Simulator/config/config_simple_multilevel @@ -22,7 +22,7 @@ cache_3: cache_4: blocks: 512 - associativity: 8 + associativity: 16 hit_time: 150 mem: #required From 0d931396ea65bd967732daa220bab3d16a56c8bd Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 22:41:02 -0400 Subject: [PATCH 10/19] Create gather.cpp --- src/gather.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/gather.cpp diff --git a/src/gather.cpp b/src/gather.cpp new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/gather.cpp @@ -0,0 +1 @@ + From b9d12ea5a851b7500ab0577649771eba218fc28d Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 22:42:47 -0400 Subject: [PATCH 11/19] Update gather.cpp --- src/gather.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/gather.cpp b/src/gather.cpp index 8b13789..e03e3ec 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -1 +1,91 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +void parsec_roi_begin() +{ + +} + +void parsec_roi_end() +{ + +} + +struct Result { + vector< vector > A; +}; + +Result read(string filename) { + vector< vector > A; + Result ab; + string line; + ifstream infile; + infile.open (filename.c_str()); + + int i = 0; + while (getline(infile, line) && !line.empty()) { + istringstream iss(line); + A.resize(A.size() + 1); + int a, j = 0; + while (iss >> a) { + A[i].push_back(a); + j++; + } + i++; + } + infile.close(); + ab.A = A; + return ab; +} + +vector< vector > gather(vector< vector > A) { + int n = A.size(); + int number_of_indices = 1000; + vector indices(number_of_indices), data(number_of_indices); + for(int i = 0; i < number_of_indices ; i++){ + indices[i] = rand() % (n*n); + data[i] = rand(); + } + for(int i = 0 ; i < number_of_indices ; i++){ + int row = indices[i]/n; + int col = indices[i]%n; + A[row][col] = data[i]; + } + return A; +} + +void printMatrix(vector< vector > matrix) { + vector< vector >::iterator it; + vector::iterator inner; + for (it=matrix.begin(); it != matrix.end(); it++) { + for (inner = it->begin(); inner != it->end(); inner++) { + cout << *inner; + if(inner+1 != it->end()) { + cout << "\t"; + } + } + cout << endl; + } +} + +int main (int argc, char* argv[]) { + srand(time(0)); + string filename; + if (argc < 3) { + filename = "2000.in"; + } else { + filename = argv[2]; + } + Result result = read (filename); + parsec_roi_begin(); + vector< vector > C = gather(result.A); + parsec_roi_end(); + //printMatrix(C); + return 0; +} From 15bae9e34d733f9d1f1916324927077f9653ac75 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 22:46:11 -0400 Subject: [PATCH 12/19] Update gather.cpp --- src/gather.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index e03e3ec..9c216ee 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -44,22 +44,27 @@ Result read(string filename) { return ab; } -vector< vector > gather(vector< vector > A) { +vector gather(const vector>& A) { int n = A.size(); int number_of_indices = 1000; - vector indices(number_of_indices), data(number_of_indices); - for(int i = 0; i < number_of_indices ; i++){ - indices[i] = rand() % (n*n); - data[i] = rand(); + vector indices(number_of_indices), gathered_data(number_of_indices); + + // Generate 1,000 random indices + for(int i = 0; i < number_of_indices; i++) { + indices[i] = rand() % (n * n); // Random index within the flattened matrix } - for(int i = 0 ; i < number_of_indices ; i++){ - int row = indices[i]/n; - int col = indices[i]%n; - A[row][col] = data[i]; + + // Gather elements at the random indices + for(int i = 0; i < number_of_indices; i++) { + int row = indices[i] / n; // Compute row + int col = indices[i] % n; // Compute column + gathered_data[i] = A[row][col]; // Gather the element } - return A; + + return gathered_data; } + void printMatrix(vector< vector > matrix) { vector< vector >::iterator it; vector::iterator inner; From bc74820e037c9b7abb60ca0afde04caf1f2b87d3 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 22:49:30 -0400 Subject: [PATCH 13/19] Update gather.cpp --- src/gather.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gather.cpp b/src/gather.cpp index 9c216ee..eb415b3 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -44,7 +44,7 @@ Result read(string filename) { return ab; } -vector gather(const vector>& A) { +vector< vector > gather(vector< vector > A) { int n = A.size(); int number_of_indices = 1000; vector indices(number_of_indices), gathered_data(number_of_indices); From c226999dad3a4164ff977caf00199407121ed576 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 23:00:14 -0400 Subject: [PATCH 14/19] Update gather.cpp --- src/gather.cpp | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index eb415b3..d3a1b06 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -44,27 +44,28 @@ Result read(string filename) { return ab; } -vector< vector > gather(vector< vector > A) { - int n = A.size(); - int number_of_indices = 1000; - vector indices(number_of_indices), gathered_data(number_of_indices); - - // Generate 1,000 random indices - for(int i = 0; i < number_of_indices; i++) { - indices[i] = rand() % (n * n); // Random index within the flattened matrix - } - - // Gather elements at the random indices - for(int i = 0; i < number_of_indices; i++) { - int row = indices[i] / n; // Compute row - int col = indices[i] % n; // Compute column - gathered_data[i] = A[row][col]; // Gather the element - } - - return gathered_data; +vector gather(const vector>& A) { + int n = A.size(); + int number_of_indices = 1000; + vector indices(number_of_indices), gathered_data(number_of_indices); + + // Generate 1,000 random indices + for(int i = 0; i < number_of_indices; i++) { + indices[i] = rand() % (n * n); // Random index within the flattened matrix + } + + // Gather elements at the random indices + for(int i = 0; i < number_of_indices; i++) { + int row = indices[i] / n; // Compute row + int col = indices[i] % n; // Compute column + gathered_data[i] = A[row][col]; // Gather the element + } + + return gathered_data; // Return the gathered data } + void printMatrix(vector< vector > matrix) { vector< vector >::iterator it; vector::iterator inner; From 7e9fdfb82e4d11e7c70d9384a5c965a664a58a96 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 23:06:18 -0400 Subject: [PATCH 15/19] Update gather.cpp --- src/gather.cpp | 90 ++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index d3a1b06..e8965d3 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -9,39 +9,37 @@ using namespace std; void parsec_roi_begin() { - } void parsec_roi_end() { - } struct Result { - vector< vector > A; + vector< vector > A; }; Result read(string filename) { - vector< vector > A; - Result ab; - string line; - ifstream infile; - infile.open (filename.c_str()); + vector< vector > A; + Result ab; + string line; + ifstream infile; + infile.open (filename.c_str()); - int i = 0; - while (getline(infile, line) && !line.empty()) { - istringstream iss(line); - A.resize(A.size() + 1); - int a, j = 0; - while (iss >> a) { - A[i].push_back(a); - j++; - } - i++; - } - infile.close(); - ab.A = A; - return ab; + int i = 0; + while (getline(infile, line) && !line.empty()) { + istringstream iss(line); + A.resize(A.size() + 1); + int a, j = 0; + while (iss >> a) { + A[i].push_back(a); + j++; + } + i++; + } + infile.close(); + ab.A = A; + return ab; } vector gather(const vector>& A) { @@ -64,34 +62,32 @@ vector gather(const vector>& A) { return gathered_data; // Return the gathered data } - - -void printMatrix(vector< vector > matrix) { - vector< vector >::iterator it; - vector::iterator inner; - for (it=matrix.begin(); it != matrix.end(); it++) { - for (inner = it->begin(); inner != it->end(); inner++) { - cout << *inner; - if(inner+1 != it->end()) { - cout << "\t"; - } - } - cout << endl; - } +// Updated to print a 1D vector +void printVector(const vector& vec) { + for (size_t i = 0; i < vec.size(); i++) { + cout << vec[i]; + if (i + 1 != vec.size()) { + cout << "\t"; + } + } + cout << endl; } int main (int argc, char* argv[]) { - srand(time(0)); - string filename; - if (argc < 3) { - filename = "2000.in"; - } else { - filename = argv[2]; - } - Result result = read (filename); + srand(time(0)); + string filename; + if (argc < 3) { + filename = "2000.in"; + } else { + filename = argv[2]; + } + Result result = read (filename); parsec_roi_begin(); - vector< vector > C = gather(result.A); + vector C = gather(result.A); // Corrected to vector parsec_roi_end(); - //printMatrix(C); - return 0; + + // Optionally print the gathered data as a 1D vector + //printVector(C); + + return 0; } From b96bad607b637e3992cbc2b947ddcc81aa324f52 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Fri, 13 Sep 2024 23:18:16 -0400 Subject: [PATCH 16/19] Update config_simple_multilevel --- Simulator/config/config_simple_multilevel | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Simulator/config/config_simple_multilevel b/Simulator/config/config_simple_multilevel index a38fc19..3b269c9 100644 --- a/Simulator/config/config_simple_multilevel +++ b/Simulator/config/config_simple_multilevel @@ -20,10 +20,10 @@ cache_3: associativity: 8 hit_time: 100 -cache_4: - blocks: 512 - associativity: 16 - hit_time: 150 +#cache_4: +# blocks: 512 +# associativity: 16 +# hit_time: 150 mem: #required hit_time: 1000 #cycles From 07caf9f2a45d140582c19a596fb4dc32dc7373f1 Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Sun, 15 Sep 2024 19:44:47 -0400 Subject: [PATCH 17/19] Create convolve.cpp --- src/convolve.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/convolve.cpp diff --git a/src/convolve.cpp b/src/convolve.cpp new file mode 100644 index 0000000..c41d1b5 --- /dev/null +++ b/src/convolve.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +void parsec_roi_begin() +{ + +} + +void parsec_roi_end() +{ + +} + +struct Result { + vector< vector > A; +}; + +Result read(string filename) { + vector< vector > A; + Result ab; + string line; + ifstream infile; + infile.open (filename.c_str()); + + int i = 0; + while (getline(infile, line) && !line.empty()) { + istringstream iss(line); + A.resize(A.size() + 1); + int a, j = 0; + while (iss >> a) { + A[i].push_back(a); + j++; + } + i++; + } + infile.close(); + ab.A = A; + return ab; +} + +vector< vector > scatter(vector< vector > A) { + int n = A.size(); + int number_of_indices = 1000; + vector indices(number_of_indices), data(number_of_indices); + for(int i = 0; i < number_of_indices ; i++){ + indices[i] = rand() % (n*n); + data[i] = rand(); + } + for(int i = 0 ; i < number_of_indices ; i++){ + int row = indices[i]/n; + int col = indices[i]%n; + A[row][col] = data[i]; + } + return A; +} + +void printMatrix(vector< vector > matrix) { + vector< vector >::iterator it; + vector::iterator inner; + for (it=matrix.begin(); it != matrix.end(); it++) { + for (inner = it->begin(); inner != it->end(); inner++) { + cout << *inner; + if(inner+1 != it->end()) { + cout << "\t"; + } + } + cout << endl; + } +} + +int main (int argc, char* argv[]) { + srand(time(0)); + string filename; + if (argc < 3) { + filename = "2000.in"; + } else { + filename = argv[2]; + } + Result result = read (filename); + parsec_roi_begin(); + vector< vector > C = scatter(result.A); + parsec_roi_end(); + //printMatrix(C); + return 0; +} From 84d5e8b4ec4f66289486c4e1624370eb8893ac2c Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Sun, 15 Sep 2024 19:46:05 -0400 Subject: [PATCH 18/19] Update convolve.cpp --- src/convolve.cpp | 59 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index c41d1b5..2b4a1f8 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -44,20 +44,49 @@ Result read(string filename) { return ab; } -vector< vector > scatter(vector< vector > A) { - int n = A.size(); - int number_of_indices = 1000; - vector indices(number_of_indices), data(number_of_indices); - for(int i = 0; i < number_of_indices ; i++){ - indices[i] = rand() % (n*n); - data[i] = rand(); - } - for(int i = 0 ; i < number_of_indices ; i++){ - int row = indices[i]/n; - int col = indices[i]%n; - A[row][col] = data[i]; - } - return A; +vector> convolve(vector> A) +{ + // Gaussian filter + vector> filter = { + {1, 2, 1}, + {2, 4, 2}, + {1, 2, 1}}; + int n = A.size(); + + for (int i = 0; i < n; i++) + { + A[i].insert(A[i].begin(), 0); + A[i].insert(A[i].end(), 0); + } + vector temp(n+2, 0); + A.insert(A.begin(), temp); + A.insert(A.end(), temp); + + vector> res(n * n, vector (n,0)); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + // res[i][j] + + // 00 01 02 x-1, y-1 x-1,y x-1,y+1 + // 10 11 12 x,y-1 x, y x, y+1 + // 20 21 22 x+1, y-1 x+1,y x+1, y+1 + int sum = 0; + sum += A[i - 1][j - 1] * filter[0][0]; + sum += A[i - 1][j] * filter[0][1]; + sum += A[i - 1][j + 1] * filter[0][2]; + sum += A[i][j - 1] * filter[1][0]; + sum += A[i][j] * filter[1][1]; + sum += A[i][j + 1] * filter[1][2]; + sum += A[i + 1][j - 1] * filter[2][0]; + sum += A[i + 1][j] * filter[2][1]; + sum += A[i + 1][j + 1] * filter[2][2]; + sum = sum / 16; + res[i - 1][j - 1] = (int)sum; + } + } + return res; } void printMatrix(vector< vector > matrix) { @@ -84,7 +113,7 @@ int main (int argc, char* argv[]) { } Result result = read (filename); parsec_roi_begin(); - vector< vector > C = scatter(result.A); + vector< vector > C = convolve(result.A); parsec_roi_end(); //printMatrix(C); return 0; From fb6256d2ed92d973a15dc4f4cbac8f7e8d14a1fc Mon Sep 17 00:00:00 2001 From: Ronit26x <150147594+Ronit26x@users.noreply.github.com> Date: Sun, 15 Sep 2024 20:41:50 -0400 Subject: [PATCH 19/19] Update convolve.cpp --- src/convolve.cpp | 101 ++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 59 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index 2b4a1f8..b9ef22c 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -44,77 +44,60 @@ Result read(string filename) { return ab; } -vector> convolve(vector> A) -{ - // Gaussian filter - vector> filter = { +// Define the 3x3 Gaussian kernel with integer values +vector> gaussianKernel() { + return { {1, 2, 1}, {2, 4, 2}, - {1, 2, 1}}; - int n = A.size(); + {1, 2, 1} + }; +} + +vector> padMatrix(const vector>& matrix) { + int rows = matrix.size(); + int cols = matrix[0].size(); + vector> padded(rows + 2, vector(cols + 2, 0)); - for (int i = 0; i < n; i++) - { - A[i].insert(A[i].begin(), 0); - A[i].insert(A[i].end(), 0); + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + padded[i + 1][j + 1] = matrix[i][j]; + } } - vector temp(n+2, 0); - A.insert(A.begin(), temp); - A.insert(A.end(), temp); + return padded; +} - vector> res(n * n, vector (n,0)); - for (int i = 1; i <= n; i++) - { - for (int j = 1; j <= n; j++) - { - // res[i][j] +vector> convolve(const vector>& matrix) { + vector> kernel = gaussianKernel(); + vector> padded = padMatrix(matrix); + int rows = padded.size(); + int cols = padded[0].size(); + vector> result(matrix.size(), vector(matrix[0].size(), 0)); - // 00 01 02 x-1, y-1 x-1,y x-1,y+1 - // 10 11 12 x,y-1 x, y x, y+1 - // 20 21 22 x+1, y-1 x+1,y x+1, y+1 + for (int i = 1; i < rows - 1; ++i) { + for (int j = 1; j < cols - 1; ++j) { int sum = 0; - sum += A[i - 1][j - 1] * filter[0][0]; - sum += A[i - 1][j] * filter[0][1]; - sum += A[i - 1][j + 1] * filter[0][2]; - sum += A[i][j - 1] * filter[1][0]; - sum += A[i][j] * filter[1][1]; - sum += A[i][j + 1] * filter[1][2]; - sum += A[i + 1][j - 1] * filter[2][0]; - sum += A[i + 1][j] * filter[2][1]; - sum += A[i + 1][j + 1] * filter[2][2]; - sum = sum / 16; - res[i - 1][j - 1] = (int)sum; + for (int ki = -1; ki <= 1; ++ki) { + for (int kj = -1; kj <= 1; ++kj) { + sum += padded[i + ki][j + kj] * kernel[ki + 1][kj + 1]; + } + } + result[i - 1][j - 1] = sum; } } - return res; -} - -void printMatrix(vector< vector > matrix) { - vector< vector >::iterator it; - vector::iterator inner; - for (it=matrix.begin(); it != matrix.end(); it++) { - for (inner = it->begin(); inner != it->end(); inner++) { - cout << *inner; - if(inner+1 != it->end()) { - cout << "\t"; - } - } - cout << endl; - } + return result; } int main (int argc, char* argv[]) { - srand(time(0)); - string filename; - if (argc < 3) { - filename = "2000.in"; - } else { - filename = argv[2]; - } - Result result = read (filename); + srand(time(0)); + string filename; + if (argc < 3) { + filename = "2000.in"; + } else { + filename = argv[2]; + } + Result result = read(filename); parsec_roi_begin(); - vector< vector > C = convolve(result.A); + vector< vector > C = convolve(result.A); parsec_roi_end(); - //printMatrix(C); - return 0; + return 0; }