From 6e87997a8ab2b569b560ffd0fba156c8e9534a37 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Mon, 16 Sep 2024 12:54:55 -0400 Subject: [PATCH 01/47] 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 199a46a319d2915e939932d5759a166dd747b753 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Mon, 16 Sep 2024 12:58:27 -0400 Subject: [PATCH 02/47] Update scatter.cpp --- src/scatter.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/scatter.cpp b/src/scatter.cpp index 8b13789..3d151d7 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -1 +1,79 @@ +#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 > ijkalgorithm(vector< vector > A) { + int n = A.size(); + + // initialise C with 0s + vector tmp(n, 0); + vector< vector > C(n, tmp); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + for (int k = 0; k < n; k++) { + C[i][j] += A[i][k] * B[k][j]; + } + } + } + return C; +} + +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 = ijkalgorithm(result.A); + parsec_roi_end(); + return 0; +} From b22128c08a14a160e8c9c45c865162d9105e8860 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Mon, 16 Sep 2024 13:01:32 -0400 Subject: [PATCH 03/47] Update random_matrix_generator.py --- utils/random_matrix_generator.py | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/utils/random_matrix_generator.py b/utils/random_matrix_generator.py index f57d0c8..52552fd 100644 --- a/utils/random_matrix_generator.py +++ b/utils/random_matrix_generator.py @@ -27,7 +27,7 @@ def saveMatrix(matrixA, matrixB, 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,6 @@ def main(): outpath = args.dump #Create dense matrix matrixA = createRandomMatrix(n) - matrixB = createRandomMatrix(n) #print(matrixA) #Convert to sparse matrix by replacing value below threshold to 0 if (args.sparsity): @@ -71,27 +70,13 @@ def main(): size=int(matrixA.size * (args.sparsity/100))) 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, - size=int(matrixB.size * (args.sparsity/100))) - flatB = matrixB.flatten() - flatB[indicesB] = 0 #Reshape it back to square matrix flatA = flatA.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) matrixA = flatA.tolist() - matrixB = flatB.tolist() csr_Amatrix = "csrA_"+args.dump - csr_Bmatrix = "csrB_"+args.dump saveCSRMatrix(matrixA_csr, csr_Amatrix) - saveCSRMatrix(matrixB_csr, csr_Bmatrix) - #print(matrixA) - saveMatrix(matrixA, matrixB, args.dump) + saveMatrix(matrixA, args.dump) if __name__ == '__main__': main() From 06c541d2cc644d03fe928f6dcb91690132681c41 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Mon, 16 Sep 2024 13:06:32 -0400 Subject: [PATCH 04/47] Update scatter.cpp --- src/scatter.cpp | 89 ++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 3d151d7..9062fc0 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -4,6 +4,7 @@ #include #include #include +#include // For rand() and srand() using namespace std; @@ -17,63 +18,61 @@ 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++; - } + 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; + infile.close(); + ab.A = A; + return ab; } -vector< vector > ijkalgorithm(vector< vector > A) { - int n = A.size(); +vector< vector > scatter(vector< vector > A) { + int n = A.size(); + + // Generate 1,000 random indices and 1,000 random values + for (int i = 0; i < 1000; i++) { + int row = rand() % n; // Random row index + int col = rand() % n; // Random column index + int value = rand(); // Random value without bounds - // initialise C with 0s - vector tmp(n, 0); - vector< vector > C(n, tmp); + // Scatter the random value into the matrix + A[row][col] = value; + } - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - for (int k = 0; k < n; k++) { - C[i][j] += A[i][k] * B[k][j]; - } - } - } - return C; + return A; } -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); +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 = ijkalgorithm(result.A); + vector< vector > C = scatter(result.A); parsec_roi_end(); - return 0; + return 0; } From 5cf80e5170e3f267f946e397a9f44375d42d6e12 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 13:18:02 -0400 Subject: [PATCH 05/47] Create gather.cpp --- src/gather.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/gather.cpp diff --git a/src/gather.cpp b/src/gather.cpp new file mode 100644 index 0000000..6f16d3f --- /dev/null +++ b/src/gather.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +void parsec_roi_begin() +{ + // Start of Region of Interest +} + +void parsec_roi_end() +{ + // End of Region of Interest +} + +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 gather(const vector< vector >& A) { + int n = A.size(); + vector gathered_values; + + for (int i = 0; i < 1000; i++) { + int row = rand() % n; + int col = rand() % n; + + gathered_values.push_back(A[row][col]); + } + + return gathered_values; +} + +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 gathered = gather(result.A); + parsec_roi_end(); + + for (int value : gathered) { + cout << value << " "; + } + cout << endl; + + return 0; +} From 2198d0f2b59a1a7b1e17895c756b5ff953d17d36 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 16:08:31 -0400 Subject: [PATCH 06/47] 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 52552fd..2e8ed23 100644 --- a/utils/random_matrix_generator.py +++ b/utils/random_matrix_generator.py @@ -76,7 +76,7 @@ def main(): matrixA = flatA.tolist() csr_Amatrix = "csrA_"+args.dump saveCSRMatrix(matrixA_csr, csr_Amatrix) - saveMatrix(matrixA, args.dump) + saveMatrix(matrixA, args.dump, "input_matrix.in") if __name__ == '__main__': main() From b3a4ff26742d807987a55c829683843478c17915 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 16:10:31 -0400 Subject: [PATCH 07/47] Update cache_simulator.py --- Simulator/src/cache_simulator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Simulator/src/cache_simulator.py b/Simulator/src/cache_simulator.py index 21e0b78..51d94ce 100755 --- a/Simulator/src/cache_simulator.py +++ b/Simulator/src/cache_simulator.py @@ -38,7 +38,7 @@ def main(): logger.info('Loading config...') config_file = open(arguments['config_file']) - configs = yaml.load(config_file) + configs = yaml.load(config_file, Loader=yaml.FullLoader) hierarchy = build_hierarchy(configs, logger) logger.info('Memory hierarchy built.') From 73296ec7b0a7479a49a896d28c82a0de8fbd8e6a Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 16:52:04 -0400 Subject: [PATCH 08/47] Update scatter.cpp --- src/scatter.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 9062fc0..434050a 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -10,12 +10,12 @@ using namespace std; void parsec_roi_begin() { - + // Placeholder for PIN tool region start } void parsec_roi_end() { - + // Placeholder for PIN tool region end } struct Result { @@ -46,9 +46,23 @@ Result read(string filename) { return ab; } +void printMatrix(const vector>& matrix) { + for (const auto& row : matrix) { + for (int val : row) { + cout << val << " "; + } + cout << endl; + } +} + vector< vector > scatter(vector< vector > A) { int n = A.size(); + if (n == 0) { // Check for empty matrix + cerr << "Error: Matrix is empty." << endl; + return A; + } + // Generate 1,000 random indices and 1,000 random values for (int i = 0; i < 1000; i++) { int row = rand() % n; // Random row index @@ -65,14 +79,19 @@ vector< vector > scatter(vector< vector > A) { int main(int argc, char* argv[]) { srand(time(0)); string filename; - if (argc < 3) { + if (argc < 2) { // Corrected condition filename = "2000.in"; } else { - filename = argv[2]; + filename = argv[1]; // Use argv[1] for the first argument after the executable name } + Result result = read(filename); parsec_roi_begin(); vector< vector > C = scatter(result.A); parsec_roi_end(); + + // Optional: Print the modified matrix after scatter + printMatrix(C); + return 0; } From 10789ddd0fe1c2a8dea9eec4be04004c9938e042 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 17:10:06 -0400 Subject: [PATCH 09/47] Update scatter.cpp --- src/scatter.cpp | 110 ++++++++++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 40 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 434050a..f988919 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include // For rand() and srand() @@ -10,60 +11,84 @@ using namespace std; void parsec_roi_begin() { - // Placeholder for PIN tool region start + // Placeholder for profiling region start } void parsec_roi_end() { - // Placeholder for PIN tool region end + // Placeholder for profiling region end } -struct Result { - vector< vector > A; -}; - -Result read(string filename) { - vector< vector > A; - Result ab; +// Get matrix size from input file +int getMatrixSize(string filename) { string line; ifstream infile; - infile.open(filename.c_str()); + infile.open (filename.c_str()); + getline(infile, line); + return count(line.begin(), line.end(), '\t') + 1; +} + +// Read matrices from input file +void read(string filename, vector< vector > &A, vector< vector > &B) { + string line; + FILE* matrixfile = freopen(filename.c_str(), "r", stdin); + + int i = 0, j, a; + while (getline(cin, line) && !line.empty()) { + istringstream iss(line); + j = 0; + while (iss >> a) { + A[i][j] = a; + j++; + } + i++; + } - int i = 0; - while (getline(infile, line) && !line.empty()) { + i = 0; + while (getline(cin, line)) { istringstream iss(line); - A.resize(A.size() + 1); - int a, j = 0; + j = 0; while (iss >> a) { - A[i].push_back(a); + B[i][j] = a; j++; } i++; } - infile.close(); - ab.A = A; - return ab; + fclose (matrixfile); } -void printMatrix(const vector>& matrix) { - for (const auto& row : matrix) { - for (int val : row) { - cout << val << " "; +// Matrix multiplication using IKJ algorithm +vector< vector > ikjalgorithm(vector< vector > A, + vector< vector > B, + vector< vector > &C, int n) { + for (register int i = 0; i < n; i++) { + for (register int k = 0; k < n; k++) { + for (register int j = 0; j < n; j++) { + C[i][j] += A[i][k] * B[k][j]; + } } - cout << endl; } + return C; } -vector< vector > scatter(vector< vector > A) { - int n = A.size(); - - if (n == 0) { // Check for empty matrix - cerr << "Error: Matrix is empty." << endl; - return A; +// Print the matrix +void printMatrix(vector< vector > matrix, int n) { + for (int i=0; i < n; i++) { + for (int j=0; j < n; j++) { + if (j != 0) { + cout << "\t"; + } + cout << matrix[i][j]; + } + cout << endl; } +} + +// Scatter operation: Generate 1,000 random indices and values, scatter them into matrix A +void scatter(vector< vector > &A, int n) { + srand(time(0)); // Seed for random number generation - // Generate 1,000 random indices and 1,000 random values for (int i = 0; i < 1000; i++) { int row = rand() % n; // Random row index int col = rand() % n; // Random column index @@ -72,26 +97,31 @@ vector< vector > scatter(vector< vector > A) { // Scatter the random value into the matrix A[row][col] = value; } - - return A; } -int main(int argc, char* argv[]) { - srand(time(0)); +int main (int argc, char* argv[]) { string filename; - if (argc < 2) { // Corrected condition + if (argc < 3) { filename = "2000.in"; } else { - filename = argv[1]; // Use argv[1] for the first argument after the executable name + filename = argv[2]; } - Result result = read(filename); + // Determine matrix size (should be 128 or 150 based on input file) + int n = getMatrixSize(filename); + vector inner (n); + vector< vector > A(n, inner), B(n, inner), C(n, inner); + read(filename, A, B); + + // Perform scatter operation before IKJ algorithm + scatter(A, n); // Scatter random values into matrix A + parsec_roi_begin(); - vector< vector > C = scatter(result.A); + ikjalgorithm(A, B, C, n); parsec_roi_end(); - // Optional: Print the modified matrix after scatter - printMatrix(C); + // Optional: Print matrix C to verify results + // printMatrix(C, n); return 0; } From e84624eac9778cbd10f84d5d4648b41c425b503c Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 17:54:01 -0400 Subject: [PATCH 10/47] Update scatter.cpp --- src/scatter.cpp | 105 +++++++++++++----------------------------------- 1 file changed, 29 insertions(+), 76 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index f988919..236aaa8 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include // For rand() and srand() @@ -11,84 +10,46 @@ using namespace std; void parsec_roi_begin() { - // Placeholder for profiling region start + } void parsec_roi_end() { - // Placeholder for profiling region end -} -// Get matrix size from input file -int getMatrixSize(string filename) { - string line; - ifstream infile; - infile.open (filename.c_str()); - getline(infile, line); - return count(line.begin(), line.end(), '\t') + 1; } -// Read matrices from input file -void read(string filename, vector< vector > &A, vector< vector > &B) { - string line; - FILE* matrixfile = freopen(filename.c_str(), "r", stdin); +struct Result { + vector< vector > A; +}; - int i = 0, j, a; - while (getline(cin, line) && !line.empty()) { - istringstream iss(line); - j = 0; - while (iss >> a) { - A[i][j] = a; - j++; - } - i++; - } +Result read(string filename) { + vector< vector > A; + Result ab; + string line; + ifstream infile; + infile.open(filename.c_str()); - i = 0; - while (getline(cin, line)) { + int i = 0; + while (getline(infile, line) && !line.empty()) { istringstream iss(line); - j = 0; + A.resize(A.size() + 1); + int a, j = 0; while (iss >> a) { - B[i][j] = a; + A[i].push_back(a); j++; } i++; } - fclose (matrixfile); + infile.close(); + ab.A = A; + return ab; } -// Matrix multiplication using IKJ algorithm -vector< vector > ikjalgorithm(vector< vector > A, - vector< vector > B, - vector< vector > &C, int n) { - for (register int i = 0; i < n; i++) { - for (register int k = 0; k < n; k++) { - for (register int j = 0; j < n; j++) { - C[i][j] += A[i][k] * B[k][j]; - } - } - } - return C; -} - -// Print the matrix -void printMatrix(vector< vector > matrix, int n) { - for (int i=0; i < n; i++) { - for (int j=0; j < n; j++) { - if (j != 0) { - cout << "\t"; - } - cout << matrix[i][j]; - } - cout << endl; - } -} - -// Scatter operation: Generate 1,000 random indices and values, scatter them into matrix A -void scatter(vector< vector > &A, int n) { - srand(time(0)); // Seed for random number generation +vector< vector > scatter(vector< vector > A) { + int n = A.size(); + // Generate 1,000 random indices and 1,000 random values for (int i = 0; i < 1000; i++) { int row = rand() % n; // Random row index int col = rand() % n; // Random column index @@ -97,31 +58,23 @@ void scatter(vector< vector > &A, int n) { // Scatter the random value into the matrix A[row][col] = value; } + + return A; } -int main (int argc, char* argv[]) { +int main(int argc, char* argv[]) { + srand(time(0)); string filename; if (argc < 3) { filename = "2000.in"; } else { filename = argv[2]; } - - // Determine matrix size (should be 128 or 150 based on input file) - int n = getMatrixSize(filename); - vector inner (n); - vector< vector > A(n, inner), B(n, inner), C(n, inner); - read(filename, A, B); - - // Perform scatter operation before IKJ algorithm - scatter(A, n); // Scatter random values into matrix A - + // Result result = read(filename); + int size = 128; + vector< vector > A(size, vector(size, 0)); parsec_roi_begin(); - ikjalgorithm(A, B, C, n); + vector< vector > C = scatter(A); parsec_roi_end(); - - // Optional: Print matrix C to verify results - // printMatrix(C, n); - return 0; } From 7317eae27fe5b6e95e1d947fa1adf114f69bdf3e Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 18:08:27 -0400 Subject: [PATCH 11/47] Update scatter.cpp --- src/scatter.cpp | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 236aaa8..a2f32aa 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -1,6 +1,3 @@ -#include -#include -#include #include #include #include @@ -8,16 +5,6 @@ using namespace std; -void parsec_roi_begin() -{ - -} - -void parsec_roi_end() -{ - -} - struct Result { vector< vector > A; }; @@ -47,7 +34,7 @@ Result read(string filename) { } vector< vector > scatter(vector< vector > A) { - int n = A.size(); + int n = A.size(); // Assuming A is a square matrix (n x n) // Generate 1,000 random indices and 1,000 random values for (int i = 0; i < 1000; i++) { @@ -70,11 +57,18 @@ int main(int argc, char* argv[]) { } else { filename = argv[2]; } - // Result result = read(filename); - int size = 128; - vector< vector > A(size, vector(size, 0)); - parsec_roi_begin(); - vector< vector > C = scatter(A); - parsec_roi_end(); + + Result result = read(filename); + + vector< vector > C = scatter(result.A); + + // Display or process matrix C as needed + for (const auto& row : C) { + for (const auto& val : row) { + cout << val << " "; + } + cout << endl; + } + return 0; } From d7487648fb4269e7ab8e464620f4d544445793d0 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 19:02:03 -0400 Subject: [PATCH 12/47] Update scatter.cpp --- src/scatter.cpp | 111 +++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index a2f32aa..61926c0 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -1,74 +1,81 @@ #include #include -#include -#include // For rand() and srand() +#include using namespace std; -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++; - } +void parsec_roi_begin() +{ + +} - infile.close(); - ab.A = A; - return ab; +void parsec_roi_end() +{ + } -vector< vector > scatter(vector< vector > A) { - int n = A.size(); // Assuming A is a square matrix (n x n) - // Generate 1,000 random indices and 1,000 random values - for (int i = 0; i < 1000; i++) { - int row = rand() % n; // Random row index - int col = rand() % n; // Random column index - int value = rand(); // Random value without bounds +void generateRandomIndicesAndData(int max_x, int max_y, vector>& indices, vector& data) { + random_device rd; + mt19937 gen(rd()); + uniform_int_distribution<> dis_x(0, max_x - 1); + uniform_int_distribution<> dis_y(0, max_y - 1); + uniform_int_distribution<> dis_data(1, 100); // Random data between 1 and 100 - // Scatter the random value into the matrix - A[row][col] = value; + for (int i = 0; i < 1000; i++) { + int x = dis_x(gen); + int y = dis_y(gen); + indices.push_back(make_pair(x, y)); + data.push_back(dis_data(gen)); } - - return A; } -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); +void scatter(vector>& matrix, const vector>& indices, const vector& data) { + for (int i = 0; i < indices.size(); i++) { + int x = indices[i].first; + int y = indices[i].second; + matrix[x][y] = data[i]; + } +} - vector< vector > C = scatter(result.A); - // Display or process matrix C as needed - for (const auto& row : C) { - for (const auto& val : row) { - cout << val << " "; +void printMatrix(const vector>& matrix) { + for (const auto& row : matrix) { + for (const auto& elem : row) { + cout << elem << "\t"; } cout << endl; } +} + +int main() { + // Step 1: Initialize matrices of size 128x128 and 150x150 + vector> matrix_128(128, vector(128, 0)); + vector> matrix_150(150, vector(150, 0)); + + // Step 2: Generate random indices and data samples for 128x128 matrix + vector> indices_128; + vector data_128; + generateRandomIndicesAndData(128, 128, indices_128, data_128); + + // Step 3: Generate random indices and data samples for 150x150 matrix + vector> indices_150; + vector data_150; + generateRandomIndicesAndData(150, 150, indices_150, data_150); + + // Step 4: Perform scatter operation on the matrices + parsec_roi_begin(); // Start ROI + scatter(matrix_128, indices_128, data_128); + scatter(matrix_150, indices_150, data_150); + parsec_roi_end(); // End ROI + + // Optional: Print the resulting matrices + cout << "Scattered Matrix (128 x 128):" << endl; + printMatrix(matrix_128); + + cout << "\nScattered Matrix (150 x 150):" << endl; + printMatrix(matrix_150); return 0; } From 453eda0bbdc2eee0f51ff4f4ab2c3a521e2a6989 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 19:12:20 -0400 Subject: [PATCH 13/47] Update scatter.cpp --- src/scatter.cpp | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 61926c0..43baafe 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -6,15 +6,15 @@ using namespace std; void parsec_roi_begin() { - + // Begin region of interest for performance measurement } void parsec_roi_end() { - + // End region of interest for performance measurement } - +// Function to generate random indices and data samples for scattering void generateRandomIndicesAndData(int max_x, int max_y, vector>& indices, vector& data) { random_device rd; mt19937 gen(rd()); @@ -30,25 +30,15 @@ void generateRandomIndicesAndData(int max_x, int max_y, vector>& } } - +// Function to perform scatter operation on a matrix void scatter(vector>& matrix, const vector>& indices, const vector& data) { - for (int i = 0; i < indices.size(); i++) { + for (size_t i = 0; i < indices.size(); i++) { // Changed int to size_t to match indices.size() type int x = indices[i].first; int y = indices[i].second; matrix[x][y] = data[i]; } } - -void printMatrix(const vector>& matrix) { - for (const auto& row : matrix) { - for (const auto& elem : row) { - cout << elem << "\t"; - } - cout << endl; - } -} - int main() { // Step 1: Initialize matrices of size 128x128 and 150x150 vector> matrix_128(128, vector(128, 0)); @@ -70,12 +60,5 @@ int main() { scatter(matrix_150, indices_150, data_150); parsec_roi_end(); // End ROI - // Optional: Print the resulting matrices - cout << "Scattered Matrix (128 x 128):" << endl; - printMatrix(matrix_128); - - cout << "\nScattered Matrix (150 x 150):" << endl; - printMatrix(matrix_150); - return 0; } From b4925cf4057b251bb46c3987e7c91a50f119a733 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 19:37:12 -0400 Subject: [PATCH 14/47] Update scatter.cpp --- src/scatter.cpp | 60 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 43baafe..7cbbda1 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -1,17 +1,39 @@ #include #include #include +#include +#include using namespace std; void parsec_roi_begin() { - // Begin region of interest for performance measurement + // Placeholder for beginning of region of interest for performance measurement } void parsec_roi_end() { - // End region of interest for performance measurement + // Placeholder for end of region of interest for performance measurement +} + +// Function to read a matrix from a file +vector> readMatrixFromFile(const string& filename) { + ifstream infile(filename); + vector> matrix; + string line; + + while (getline(infile, line)) { + istringstream iss(line); + vector row; + int value; + while (iss >> value) { + row.push_back(value); + } + matrix.push_back(row); + } + + infile.close(); + return matrix; } // Function to generate random indices and data samples for scattering @@ -32,33 +54,31 @@ void generateRandomIndicesAndData(int max_x, int max_y, vector>& // Function to perform scatter operation on a matrix void scatter(vector>& matrix, const vector>& indices, const vector& data) { - for (size_t i = 0; i < indices.size(); i++) { // Changed int to size_t to match indices.size() type + for (size_t i = 0; i < indices.size(); i++) { int x = indices[i].first; int y = indices[i].second; matrix[x][y] = data[i]; } } -int main() { - // Step 1: Initialize matrices of size 128x128 and 150x150 - vector> matrix_128(128, vector(128, 0)); - vector> matrix_150(150, vector(150, 0)); +int main(int argc, char* argv[]) { + if (argc < 2) { + cerr << "Usage: " << argv[0] << " input_file" << endl; + return 1; + } - // Step 2: Generate random indices and data samples for 128x128 matrix - vector> indices_128; - vector data_128; - generateRandomIndicesAndData(128, 128, indices_128, data_128); + // Read the generated matrix from the file + vector> matrix = readMatrixFromFile(argv[1]); - // Step 3: Generate random indices and data samples for 150x150 matrix - vector> indices_150; - vector data_150; - generateRandomIndicesAndData(150, 150, indices_150, data_150); + // Generate random indices and data samples for the scatter operation + vector> indices; + vector data; + generateRandomIndicesAndData(matrix.size(), matrix[0].size(), indices, data); - // Step 4: Perform scatter operation on the matrices - parsec_roi_begin(); // Start ROI - scatter(matrix_128, indices_128, data_128); - scatter(matrix_150, indices_150, data_150); - parsec_roi_end(); // End ROI + // Perform scatter operation on the matrix + parsec_roi_begin(); + scatter(matrix, indices, data); + parsec_roi_end(); return 0; } From 83c9f375645a0322891dd5b86642aa5c4c6079ce Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 20:17:29 -0400 Subject: [PATCH 15/47] Update scatter.cpp --- src/scatter.cpp | 130 +++++++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 58 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 7cbbda1..9015fab 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -1,84 +1,98 @@ +#include +#include +#include #include #include -#include -#include -#include using namespace std; void parsec_roi_begin() { - // Placeholder for beginning of region of interest for performance measurement + } void parsec_roi_end() { - // Placeholder for end of region of interest for performance measurement + } -// Function to read a matrix from a file -vector> readMatrixFromFile(const string& filename) { - ifstream infile(filename); - vector> matrix; - string line; - - while (getline(infile, line)) { - istringstream iss(line); - vector row; - int value; - while (iss >> value) { - row.push_back(value); - } - matrix.push_back(row); - } - infile.close(); - return matrix; -} +struct Result { + vector< vector > A; + +}; -// Function to generate random indices and data samples for scattering -void generateRandomIndicesAndData(int max_x, int max_y, vector>& indices, vector& data) { - random_device rd; - mt19937 gen(rd()); - uniform_int_distribution<> dis_x(0, max_x - 1); - uniform_int_distribution<> dis_y(0, max_y - 1); - uniform_int_distribution<> dis_data(1, 100); // Random data between 1 and 100 +Result read(string filename) { + vector< vector > A; + Result ab; + string line; + ifstream infile; + infile.open (filename.c_str()); - for (int i = 0; i < 1000; i++) { - int x = dis_x(gen); - int y = dis_y(gen); - indices.push_back(make_pair(x, y)); - data.push_back(dis_data(gen)); - } -} + 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++; + } -// Function to perform scatter operation on a matrix -void scatter(vector>& matrix, const vector>& indices, const vector& data) { - for (size_t i = 0; i < indices.size(); i++) { - int x = indices[i].first; - int y = indices[i].second; - matrix[x][y] = data[i]; - } + + + infile.close(); + ab.A = A; + return ab; } -int main(int argc, char* argv[]) { - if (argc < 2) { - cerr << "Usage: " << argv[0] << " input_file" << endl; - return 1; +vector> scatter(vector>& A) { + int rows = A.size(); + if (rows == 0) return A; // Return if matrix is empty + + int cols = A[0].size(); + + // Seed the random number generator + srand(static_cast(time(0))); + + // Scatter: Assign random values to 100 random indices + for (int i = 0; i < 1000; i++) { + int x = rand() % rows; // Random row index + int y = rand() % cols; // Random column index + int new_value = (rand() % 1000) + 1; // Random value between 1 and 100 + A[x][y] = new_value; // Assign the random value to the random index } - // Read the generated matrix from the file - vector> matrix = readMatrixFromFile(argv[1]); + return A; +} - // Generate random indices and data samples for the scatter operation - vector> indices; - vector data; - generateRandomIndicesAndData(matrix.size(), matrix[0].size(), indices, 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; + } +} - // Perform scatter operation on the matrix +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(); - scatter(matrix, indices, data); + vector< vector > C = scatter(result.A); parsec_roi_end(); - - return 0; + //printMatrix(C); + return 0; } From 3ce9edab98572654b3894d0ad286f44bafbe4b12 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Tue, 17 Sep 2024 23:02:02 -0400 Subject: [PATCH 16/47] Update scatter.cpp --- src/scatter.cpp | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index 9015fab..c41d1b5 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace std; @@ -13,13 +14,11 @@ void parsec_roi_begin() void parsec_roi_end() { - + } - struct Result { vector< vector > A; - }; Result read(string filename) { @@ -40,32 +39,25 @@ Result read(string filename) { } i++; } - - - infile.close(); ab.A = A; return ab; } -vector> scatter(vector>& A) { - int rows = A.size(); - if (rows == 0) return A; // Return if matrix is empty - - int cols = A[0].size(); - - // Seed the random number generator - srand(static_cast(time(0))); - - // Scatter: Assign random values to 100 random indices - for (int i = 0; i < 1000; i++) { - int x = rand() % rows; // Random row index - int y = rand() % cols; // Random column index - int new_value = (rand() % 1000) + 1; // Random value between 1 and 100 - A[x][y] = new_value; // Assign the random value to the random index - } - - return A; +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) { @@ -83,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 42cfcb528202fb689252e96f4cf4bf7b0f7c521c Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 00:22:15 -0400 Subject: [PATCH 17/47] Update gather.cpp --- src/gather.cpp | 107 +++++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index 6f16d3f..53d1f5f 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -3,80 +3,91 @@ #include #include #include -#include -#include using namespace std; void parsec_roi_begin() { - // Start of Region of Interest + } void parsec_roi_end() { - // End of Region of Interest + } + 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()); - - 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++; - } + 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; + infile.close(); + ab.A = A; + return ab; } -vector gather(const vector< vector >& A) { +vector< vector > scatter(vector< vector > A) +{ int n = A.size(); - vector gathered_values; + vector B(1000); + // Generate 1,000 random indices and 1,000 random values for (int i = 0; i < 1000; i++) { - int row = rand() % n; - int col = rand() % n; + int row = rand() % n; // Random row index + int col = rand() % n; // Random column index - gathered_values.push_back(A[row][col]); + // Scatter the random value into the matrix + B[i] = A[row][col]; } + return A; +} + - return gathered_values; +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); +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 gathered = gather(result.A); + vector< vector > C = scatter(result.A); parsec_roi_end(); - - for (int value : gathered) { - cout << value << " "; - } - cout << endl; - - return 0; + //printMatrix(C); + return 0; } From a6aff31d39de0240014bdcb6be2f476b578b790f Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 00:33:12 -0400 Subject: [PATCH 18/47] Update gather.cpp --- src/gather.cpp | 122 ++++++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index 53d1f5f..e8965d3 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -3,91 +3,91 @@ #include #include #include +#include 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()); - - 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 > 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) -{ +vector gather(const vector>& A) { int n = A.size(); - vector B(1000); - - // Generate 1,000 random indices and 1,000 random values - for (int i = 0; i < 1000; i++) { - int row = rand() % n; // Random row index - int col = rand() % n; // Random column index - - // Scatter the random value into the matrix - B[i] = A[row][col]; + 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 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[]) { - 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 = scatter(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 04c2a550c4dd1aa63d4e70e0e3d10419c1a31333 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 01:58:53 -0400 Subject: [PATCH 19/47] Update gather.cpp --- src/gather.cpp | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index e8965d3..246478c 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -1,3 +1,5 @@ +// Updated code with additional checks + #include #include #include @@ -7,13 +9,9 @@ using namespace std; -void parsec_roi_begin() -{ -} +void parsec_roi_begin() {} -void parsec_roi_end() -{ -} +void parsec_roi_end() {} struct Result { vector< vector > A; @@ -24,7 +22,12 @@ Result read(string filename) { Result ab; string line; ifstream infile; - infile.open (filename.c_str()); + infile.open(filename.c_str()); + + if (!infile.is_open()) { // Check if file opened successfully + cerr << "Error opening file: " << filename << endl; + return ab; // Return empty Result + } int i = 0; while (getline(infile, line) && !line.empty()) { @@ -44,6 +47,11 @@ Result read(string filename) { vector gather(const vector>& A) { int n = A.size(); + if (n == 0) { + cerr << "Error: Matrix is empty!" << endl; + return {}; + } + int number_of_indices = 1000; vector indices(number_of_indices), gathered_data(number_of_indices); @@ -56,23 +64,18 @@ vector gather(const vector>& A) { for(int i = 0; i < number_of_indices; i++) { int row = indices[i] / n; // Compute row int col = indices[i] % n; // Compute column + + if (row < 0 || row >= n || col < 0 || col >= A[row].size()) { // Check bounds + cerr << "Error: Index out of bounds at " << row << ", " << col << endl; + continue; + } + gathered_data[i] = A[row][col]; // Gather the element } return gathered_data; // Return the gathered data } -// 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; @@ -82,6 +85,11 @@ int main (int argc, char* argv[]) { filename = argv[2]; } Result result = read (filename); + if (result.A.empty()) { // Check if matrix read is successful + cerr << "Error: Matrix read unsuccessful or empty!" << endl; + return 1; + } + parsec_roi_begin(); vector C = gather(result.A); // Corrected to vector parsec_roi_end(); From 5cf536828390a6eee825bf0f30e26b8d0fbe5a8b Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:02:39 -0400 Subject: [PATCH 20/47] Update gather.cpp --- src/gather.cpp | 144 +++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 76 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index 246478c..fa4a21e 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -1,101 +1,93 @@ -// Updated code with additional checks - #include #include #include #include #include -#include using namespace std; -void parsec_roi_begin() {} +void parsec_roi_begin() +{ + +} + +void parsec_roi_end() +{ + +} -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()); - - if (!infile.is_open()) { // Check if file opened successfully - cerr << "Error opening file: " << filename << endl; - return ab; // Return empty Result - } + 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) { +vector< vector > gather(vector< vector > A) +{ int n = A.size(); - if (n == 0) { - cerr << "Error: Matrix is empty!" << endl; - return {}; - } - - 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 - - if (row < 0 || row >= n || col < 0 || col >= A[row].size()) { // Check bounds - cerr << "Error: Index out of bounds at " << row << ", " << col << endl; - continue; - } - - gathered_data[i] = A[row][col]; // Gather the element + vector B(1000); + + // Generate 1,000 random indices and 1,000 random values + for (int i = 0; i < 1000; i++) { + int row = rand() % n; // Random row index + int col = rand() % n; // Random column index + + // Scatter the random value into the matrix + B[i] = A[row][col]; } - - return gathered_data; // Return the gathered data + return A; } -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); - if (result.A.empty()) { // Check if matrix read is successful - cerr << "Error: Matrix read unsuccessful or empty!" << endl; - return 1; - } +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 C = gather(result.A); // Corrected to vector + vector< vector > C = gather(result.A); parsec_roi_end(); - - // Optionally print the gathered data as a 1D vector - //printVector(C); - - return 0; + //printMatrix(C); + return 0; } From 9d998381fc119342bea5a053d8976daaa3ddc7ad Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:05:51 -0400 Subject: [PATCH 21/47] Update gather.cpp --- src/gather.cpp | 122 ++++++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index fa4a21e..e8965d3 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -3,91 +3,91 @@ #include #include #include +#include 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()); - - 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 > 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) -{ +vector gather(const vector>& A) { int n = A.size(); - vector B(1000); - - // Generate 1,000 random indices and 1,000 random values - for (int i = 0; i < 1000; i++) { - int row = rand() % n; // Random row index - int col = rand() % n; // Random column index - - // Scatter the random value into the matrix - B[i] = A[row][col]; + 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 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[]) { - 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 e7ffa7b1dccfcbe71541b66ed98b7c56faab8064 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:13:09 -0400 Subject: [PATCH 22/47] Update gather.cpp --- src/gather.cpp | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index e8965d3..25eb969 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -7,13 +7,9 @@ using namespace std; -void parsec_roi_begin() -{ -} +void parsec_roi_begin() {} -void parsec_roi_end() -{ -} +void parsec_roi_end() {} struct Result { vector< vector > A; @@ -24,7 +20,12 @@ Result read(string filename) { Result ab; string line; ifstream infile; - infile.open (filename.c_str()); + infile.open(filename.c_str()); + + if (!infile.is_open()) { // Check if file opened successfully + cerr << "Error opening file: " << filename << endl; + return ab; // Return empty Result + } int i = 0; while (getline(infile, line) && !line.empty()) { @@ -44,6 +45,11 @@ Result read(string filename) { vector gather(const vector>& A) { int n = A.size(); + if (n == 0) { + cerr << "Error: Matrix is empty!" << endl; + return {}; + } + int number_of_indices = 1000; vector indices(number_of_indices), gathered_data(number_of_indices); @@ -55,39 +61,36 @@ vector gather(const vector>& A) { // 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 + size_t col = indices[i] % n; // Compute column, using size_t to match A[row].size() type + + if (row < 0 || row >= n || col >= A[row].size()) { // Check bounds + cerr << "Error: Index out of bounds at " << row << ", " << col << endl; + continue; + } + gathered_data[i] = A[row][col]; // Gather the element } return gathered_data; // Return the gathered data } -// 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"; + filename = "input_matrix.in"; // Corrected filename to match generated file } else { filename = argv[2]; } Result result = read (filename); + if (result.A.empty()) { // Check if matrix read is successful + cerr << "Error: Matrix read unsuccessful or empty!" << endl; + return 1; + } + parsec_roi_begin(); vector C = gather(result.A); // Corrected to vector parsec_roi_end(); - // Optionally print the gathered data as a 1D vector - //printVector(C); - return 0; } From ad50e6e43f7a16b021f9f4aa8eb4025eadd58e8c Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:17:27 -0400 Subject: [PATCH 23/47] Update gather.cpp --- src/gather.cpp | 80 ++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index 25eb969..b0a91c5 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -7,37 +7,41 @@ using namespace std; -void parsec_roi_begin() {} +void parsec_roi_begin() +{ +} -void parsec_roi_end() {} +void parsec_roi_end() +{ +} struct Result { - vector< vector > A; + vector> A; }; Result read(string filename) { - vector< vector > A; + vector> A; Result ab; string line; ifstream infile; infile.open(filename.c_str()); - if (!infile.is_open()) { // Check if file opened successfully + if (!infile.is_open()) { cerr << "Error opening file: " << filename << endl; - return ab; // Return empty Result + return ab; } int i = 0; while (getline(infile, line) && !line.empty()) { istringstream iss(line); A.resize(A.size() + 1); - int a, j = 0; + int a; while (iss >> a) { A[i].push_back(a); - j++; } i++; } + infile.close(); ab.A = A; return ab; @@ -45,52 +49,50 @@ Result read(string filename) { vector gather(const vector>& A) { int n = A.size(); - if (n == 0) { - cerr << "Error: Matrix is empty!" << endl; - return {}; - } - - 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 + vector gathered_data(1000); + + // Generate 1,000 random indices and gather values + for (int i = 0; i < 1000; i++) { + int row = rand() % n; // Random row index + int col = rand() % n; // Random column index + + gathered_data[i] = A[row][col]; // Gather the value into the 1D vector } - - // Gather elements at the random indices - for(int i = 0; i < number_of_indices; i++) { - int row = indices[i] / n; // Compute row - size_t col = indices[i] % n; // Compute column, using size_t to match A[row].size() type - - if (row < 0 || row >= n || col >= A[row].size()) { // Check bounds - cerr << "Error: Index out of bounds at " << row << ", " << col << endl; - continue; + return gathered_data; +} + +void printVector(const vector& vec) { + for (size_t i = 0; i < vec.size(); i++) { + cout << vec[i]; + if (i + 1 != vec.size()) { + cout << "\t"; } - - gathered_data[i] = A[row][col]; // Gather the element } - - return gathered_data; // Return the gathered data + cout << endl; } -int main (int argc, char* argv[]) { - srand(time(0)); +int main(int argc, char* argv[]) { + srand(time(0)); // Seed the random number generator + string filename; if (argc < 3) { - filename = "input_matrix.in"; // Corrected filename to match generated file + filename = "2000.in"; } else { filename = argv[2]; } - Result result = read (filename); - if (result.A.empty()) { // Check if matrix read is successful + + Result result = read(filename); + if (result.A.empty()) { cerr << "Error: Matrix read unsuccessful or empty!" << endl; return 1; } parsec_roi_begin(); - vector C = gather(result.A); // Corrected to vector + vector C = gather(result.A); parsec_roi_end(); - + + // Optionally print the gathered data as a 1D vector + // printVector(C); + return 0; } From 220e424273c4545caddf96a505e7c9558001cf1d Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:33:04 -0400 Subject: [PATCH 24/47] Update gather.cpp --- src/gather.cpp | 61 +++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index b0a91c5..e8965d3 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -16,32 +16,27 @@ void parsec_roi_end() } struct Result { - vector> A; + vector< vector > A; }; Result read(string filename) { - vector> A; + vector< vector > A; Result ab; string line; ifstream infile; - infile.open(filename.c_str()); - - if (!infile.is_open()) { - cerr << "Error opening file: " << filename << endl; - return ab; - } + infile.open (filename.c_str()); int i = 0; while (getline(infile, line) && !line.empty()) { istringstream iss(line); A.resize(A.size() + 1); - int a; + int a, j = 0; while (iss >> a) { A[i].push_back(a); + j++; } i++; } - infile.close(); ab.A = A; return ab; @@ -49,18 +44,25 @@ Result read(string filename) { vector gather(const vector>& A) { int n = A.size(); - vector gathered_data(1000); - - // Generate 1,000 random indices and gather values - for (int i = 0; i < 1000; i++) { - int row = rand() % n; // Random row index - int col = rand() % n; // Random column index - - gathered_data[i] = A[row][col]; // Gather the value into the 1D vector + 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 gathered_data; // Return the gathered data } +// Updated to print a 1D vector void printVector(const vector& vec) { for (size_t i = 0; i < vec.size(); i++) { cout << vec[i]; @@ -71,28 +73,21 @@ void printVector(const vector& vec) { cout << endl; } -int main(int argc, char* argv[]) { - srand(time(0)); // Seed the random number generator - +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); - if (result.A.empty()) { - cerr << "Error: Matrix read unsuccessful or empty!" << endl; - return 1; - } - + Result result = read (filename); parsec_roi_begin(); - vector C = gather(result.A); + vector C = gather(result.A); // Corrected to vector parsec_roi_end(); - + // Optionally print the gathered data as a 1D vector - // printVector(C); - + //printVector(C); + return 0; } From 2eeb35dee495486260651c4fadca51e3be052c51 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:41:44 -0400 Subject: [PATCH 25/47] Update gather.cpp --- src/gather.cpp | 70 +++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index e8965d3..661e03b 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -9,60 +9,59 @@ using namespace std; void parsec_roi_begin() { + // Mark the beginning of the region of interest } void parsec_roi_end() { + // Mark the end of the region of interest } struct Result { - vector< vector > A; + vector> A; // Corrected to store a 2D vector }; +// Function to read the matrix from a file Result read(string filename) { - vector< vector > A; + vector> A; Result ab; string line; - ifstream infile; - infile.open (filename.c_str()); + ifstream infile(filename.c_str()); + + if (!infile.is_open()) { + cerr << "Error opening file: " << filename << endl; + return ab; // Return an empty Result + } - int i = 0; while (getline(infile, line) && !line.empty()) { istringstream iss(line); A.resize(A.size() + 1); - int a, j = 0; + int a; while (iss >> a) { - A[i].push_back(a); - j++; + A.back().push_back(a); // Push back into the last row } - i++; } + infile.close(); ab.A = A; return ab; } +// Gather function that returns a 1D vector of 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 + vector B(1000); + + // Generate 1,000 random indices and gather values + for (int i = 0; i < 1000; i++) { + int row = rand() % n; // Random row index + int col = rand() % n; // Random column index + B[i] = A[row][col]; // Gather the value from the matrix } - - return gathered_data; // Return the gathered data + return B; } -// Updated to print a 1D vector +// Function to print a 1D vector void printVector(const vector& vec) { for (size_t i = 0; i < vec.size(); i++) { cout << vec[i]; @@ -73,21 +72,28 @@ void printVector(const vector& vec) { cout << endl; } -int main (int argc, char* argv[]) { - srand(time(0)); +int main(int argc, char* argv[]) { + srand(time(0)); // Seed the random number generator + string filename; if (argc < 3) { - filename = "2000.in"; + filename = "2000.in"; // Default filename } else { filename = argv[2]; } - Result result = read (filename); + + Result result = read(filename); + if (result.A.empty()) { // Check if matrix read is successful + cerr << "Error: Matrix read unsuccessful or empty!" << endl; + return 1; + } + parsec_roi_begin(); - vector C = gather(result.A); // Corrected to vector + vector C = gather(result.A); // Gather values from the matrix parsec_roi_end(); - + // Optionally print the gathered data as a 1D vector //printVector(C); - + return 0; } From c46fbf83b18d9be2c17fd3aaa30f1f1c10beb9ea Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:44:55 -0400 Subject: [PATCH 26/47] Update gather.cpp --- src/gather.cpp | 69 +++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index 661e03b..9f35752 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -9,56 +9,58 @@ using namespace std; void parsec_roi_begin() { - // Mark the beginning of the region of interest } void parsec_roi_end() { - // Mark the end of the region of interest } struct Result { - vector> A; // Corrected to store a 2D vector + vector< vector > A; }; -// Function to read the matrix from a file Result read(string filename) { - vector> A; + vector< vector > A; Result ab; string line; - ifstream infile(filename.c_str()); - - if (!infile.is_open()) { - cerr << "Error opening file: " << filename << endl; - return ab; // Return an empty Result - } + 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; + int a, j = 0; while (iss >> a) { - A.back().push_back(a); // Push back into the last row + A[i].push_back(a); + j++; } + i++; } - infile.close(); ab.A = A; return ab; } -// Gather function that returns a 1D vector of gathered data vector gather(const vector>& A) { - int n = A.size(); - vector B(1000); - - // Generate 1,000 random indices and gather values - for (int i = 0; i < 1000; i++) { - int row = rand() % n; // Random row index - int col = rand() % n; // Random column index - B[i] = A[row][col]; // Gather the value from the matrix + int n = A.size(); // Number of rows in the matrix + int number_of_indices = 1000; // Number of random elements to gather + vector indices(number_of_indices); // Vector to store random indices + vector gathered_data(number_of_indices); // Vector to store gathered elements + + // Generate 1,000 random indices and gather elements + for (int i = 0; i < number_of_indices; i++) { + int row, col; + do { + int random_index = rand() % (n * n); // Random index within the flattened matrix + row = random_index / n; // Compute row index + col = random_index % n; // Compute column index + } while (row >= A.size() || col >= A[row].size()); // Ensure indices are within bounds + + gathered_data[i] = A[row][col]; // Gather the element from the matrix } - return B; + + return gathered_data; // Return the gathered data } // Function to print a 1D vector @@ -73,27 +75,20 @@ void printVector(const vector& vec) { } int main(int argc, char* argv[]) { - srand(time(0)); // Seed the random number generator - + srand(time(0)); string filename; if (argc < 3) { - filename = "2000.in"; // Default filename + filename = "2000.in"; } else { filename = argv[2]; } - Result result = read(filename); - if (result.A.empty()) { // Check if matrix read is successful - cerr << "Error: Matrix read unsuccessful or empty!" << endl; - return 1; - } - parsec_roi_begin(); - vector C = gather(result.A); // Gather values from the matrix + vector C = gather(result.A); // Gather random elements from the matrix parsec_roi_end(); - + // Optionally print the gathered data as a 1D vector - //printVector(C); - + // printVector(C); + return 0; } From 6c679bc32096bbd0687f76c7f7d264ad1feffc51 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:46:41 -0400 Subject: [PATCH 27/47] Update gather.cpp --- src/gather.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index 9f35752..c6aee03 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -43,14 +43,14 @@ Result read(string filename) { } vector gather(const vector>& A) { - int n = A.size(); // Number of rows in the matrix + size_t n = A.size(); // Number of rows in the matrix (size_t for consistency) int number_of_indices = 1000; // Number of random elements to gather vector indices(number_of_indices); // Vector to store random indices vector gathered_data(number_of_indices); // Vector to store gathered elements // Generate 1,000 random indices and gather elements for (int i = 0; i < number_of_indices; i++) { - int row, col; + size_t row, col; // Changed to size_t to match the type of vector size do { int random_index = rand() % (n * n); // Random index within the flattened matrix row = random_index / n; // Compute row index From 2c5eba041ce7ba9a14b4f3d96b45400b2210eedf Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:50:52 -0400 Subject: [PATCH 28/47] Update gather.cpp --- src/gather.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index c6aee03..27f026a 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -16,16 +16,22 @@ void parsec_roi_end() } struct Result { - vector< vector > A; + vector> A; }; +// Function to read the matrix from a file Result read(string filename) { - vector< vector > A; + vector> A; Result ab; string line; ifstream infile; infile.open(filename.c_str()); + if (!infile) { + cerr << "Error: Unable to open file " << filename << endl; + exit(1); + } + int i = 0; while (getline(infile, line) && !line.empty()) { istringstream iss(line); @@ -37,29 +43,55 @@ Result read(string filename) { } i++; } + infile.close(); ab.A = A; + + // Debugging: Print the matrix size and content + cout << "Matrix read from file: " << filename << " (" << A.size() << " rows)" << endl; + for (const auto& row : A) { + for (int val : row) { + cout << val << " "; + } + cout << endl; + } + return ab; } +// Function to gather random elements from the matrix vector gather(const vector>& A) { - size_t n = A.size(); // Number of rows in the matrix (size_t for consistency) + size_t n = A.size(); // Number of rows in the matrix + + // Check if matrix is empty + if (n == 0) { + cerr << "Error: Matrix is empty." << endl; + exit(1); + } + int number_of_indices = 1000; // Number of random elements to gather vector indices(number_of_indices); // Vector to store random indices vector gathered_data(number_of_indices); // Vector to store gathered elements - + // Generate 1,000 random indices and gather elements for (int i = 0; i < number_of_indices; i++) { size_t row, col; // Changed to size_t to match the type of vector size do { int random_index = rand() % (n * n); // Random index within the flattened matrix + + // Check for potential division by zero error + if (n == 0) { + cerr << "Error: Division by zero in index calculation. Matrix size is zero." << endl; + exit(1); + } + row = random_index / n; // Compute row index col = random_index % n; // Compute column index } while (row >= A.size() || col >= A[row].size()); // Ensure indices are within bounds - + gathered_data[i] = A[row][col]; // Gather the element from the matrix } - + return gathered_data; // Return the gathered data } @@ -76,12 +108,23 @@ void printVector(const vector& vec) { int main(int argc, char* argv[]) { srand(time(0)); + + // Debugging: Print arguments received + cout << "Program arguments received: "; + for (int i = 0; i < argc; ++i) { + cout << argv[i] << " "; + } + cout << endl; + string filename; if (argc < 3) { filename = "2000.in"; + cout << "No filename provided, using default: " << filename << endl; } else { filename = argv[2]; + cout << "Filename provided: " << filename << endl; } + Result result = read(filename); parsec_roi_begin(); vector C = gather(result.A); // Gather random elements from the matrix @@ -89,6 +132,6 @@ int main(int argc, char* argv[]) { // Optionally print the gathered data as a 1D vector // printVector(C); - + return 0; } From 12466679e09005526fb9243ae5716b21599183d6 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 02:53:43 -0400 Subject: [PATCH 29/47] Update gather.cpp --- src/gather.cpp | 90 +++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 67 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index 27f026a..e8965d3 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -16,21 +16,15 @@ void parsec_roi_end() } struct Result { - vector> A; + vector< vector > A; }; -// Function to read the matrix from a file Result read(string filename) { - vector> A; + vector< vector > A; Result ab; string line; ifstream infile; - infile.open(filename.c_str()); - - if (!infile) { - cerr << "Error: Unable to open file " << filename << endl; - exit(1); - } + infile.open (filename.c_str()); int i = 0; while (getline(infile, line) && !line.empty()) { @@ -43,59 +37,32 @@ Result read(string filename) { } i++; } - infile.close(); ab.A = A; - - // Debugging: Print the matrix size and content - cout << "Matrix read from file: " << filename << " (" << A.size() << " rows)" << endl; - for (const auto& row : A) { - for (int val : row) { - cout << val << " "; - } - cout << endl; - } - return ab; } -// Function to gather random elements from the matrix vector gather(const vector>& A) { - size_t n = A.size(); // Number of rows in the matrix - - // Check if matrix is empty - if (n == 0) { - cerr << "Error: Matrix is empty." << endl; - exit(1); + 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 } - - int number_of_indices = 1000; // Number of random elements to gather - vector indices(number_of_indices); // Vector to store random indices - vector gathered_data(number_of_indices); // Vector to store gathered elements - - // Generate 1,000 random indices and gather elements - for (int i = 0; i < number_of_indices; i++) { - size_t row, col; // Changed to size_t to match the type of vector size - do { - int random_index = rand() % (n * n); // Random index within the flattened matrix - - // Check for potential division by zero error - if (n == 0) { - cerr << "Error: Division by zero in index calculation. Matrix size is zero." << endl; - exit(1); - } - - row = random_index / n; // Compute row index - col = random_index % n; // Compute column index - } while (row >= A.size() || col >= A[row].size()); // Ensure indices are within bounds - - gathered_data[i] = A[row][col]; // Gather the element from the 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 } -// Function to print a 1D vector +// Updated to print a 1D vector void printVector(const vector& vec) { for (size_t i = 0; i < vec.size(); i++) { cout << vec[i]; @@ -106,32 +73,21 @@ void printVector(const vector& vec) { cout << endl; } -int main(int argc, char* argv[]) { +int main (int argc, char* argv[]) { srand(time(0)); - - // Debugging: Print arguments received - cout << "Program arguments received: "; - for (int i = 0; i < argc; ++i) { - cout << argv[i] << " "; - } - cout << endl; - string filename; if (argc < 3) { filename = "2000.in"; - cout << "No filename provided, using default: " << filename << endl; } else { filename = argv[2]; - cout << "Filename provided: " << filename << endl; } - - Result result = read(filename); + Result result = read (filename); parsec_roi_begin(); - vector C = gather(result.A); // Gather random elements from the matrix + vector C = gather(result.A); // Corrected to vector parsec_roi_end(); // Optionally print the gathered data as a 1D vector - // printVector(C); - + //printVector(C); + return 0; } From d09aaf837f802ec88d7e5477968962918971c4c3 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 03:41:09 -0400 Subject: [PATCH 30/47] Create convolve.cpp --- src/convolve.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/convolve.cpp diff --git a/src/convolve.cpp b/src/convolve.cpp new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/convolve.cpp @@ -0,0 +1 @@ + From 0525baa5af687df65b1067011987fcac992aea9b Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 03:47:38 -0400 Subject: [PATCH 31/47] Update convolve.cpp --- src/convolve.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/src/convolve.cpp b/src/convolve.cpp index 8b13789..b9ef22c 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -1 +1,103 @@ +#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; +} + +// Define the 3x3 Gaussian kernel with integer values +vector> gaussianKernel() { + return { + {1, 2, 1}, + {2, 4, 2}, + {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 < rows; ++i) { + for (int j = 0; j < cols; ++j) { + padded[i + 1][j + 1] = matrix[i][j]; + } + } + return padded; +} + +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)); + + for (int i = 1; i < rows - 1; ++i) { + for (int j = 1; j < cols - 1; ++j) { + int sum = 0; + 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 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); + parsec_roi_begin(); + vector< vector > C = convolve(result.A); + parsec_roi_end(); + return 0; +} From a39ab40fb268aa8b0cade90e8113300ccf39d620 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 04:04:14 -0400 Subject: [PATCH 32/47] Update convolve.cpp --- src/convolve.cpp | 97 ++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index b9ef22c..a117e23 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -3,7 +3,6 @@ #include #include #include -#include using namespace std; @@ -14,9 +13,10 @@ void parsec_roi_begin() void parsec_roi_end() { - + } + struct Result { vector< vector > A; }; @@ -33,71 +33,78 @@ Result read(string filename) { istringstream iss(line); A.resize(A.size() + 1); int a, j = 0; - while (iss >> a) { + while (iss >> a) + { A[i].push_back(a); j++; } i++; } + infile.close(); ab.A = A; return ab; } -// Define the 3x3 Gaussian kernel with integer values -vector> gaussianKernel() { - return { - {1, 2, 1}, - {2, 4, 2}, - {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 < rows; ++i) { - for (int j = 0; j < cols; ++j) { - padded[i + 1][j + 1] = matrix[i][j]; - } +vector> initializeIdentityKernel(int size) { + vector> kernel(size, vector(size, 0)); + for (int i = 0; i < size; ++i) { + kernel[i][i] = 1; } - return padded; + return kernel; } -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)); + vector> convolve(const vector>& matrix, const vector>& kernel) { +{ + int n = matrix.size(); + int k = kernel.size(); + int outputSize = (n - k) + 1; + + vector> output(outputSize, vector(outputSize, 0)); - for (int i = 1; i < rows - 1; ++i) { - for (int j = 1; j < cols - 1; ++j) { + for (int i = 0; i <= n - k; i += 1) { + for (int j = 0; j <= n - k; j += 1) { int sum = 0; - 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]; + for (int ki = 0; ki < k; ++ki) { + for (int kj = 0; kj < k; ++kj) { + sum += matrix[i + ki][j + kj] * kernel[ki][kj]; } } - result[i - 1][j - 1] = sum; + output[i ][j ] = sum; } } - return result; + + return output; +} +} + + +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); + string filename; + if (argc < 3) { + filename = "2000.in"; + } else { + filename = argv[2]; + } + Result result = read (filename); + vector> kernel = initializeIdentityKernel(7); //used identity matrix for 7X7 parsec_roi_begin(); - vector< vector > C = convolve(result.A); + vector< vector > C = convolve(result.A,kernel); parsec_roi_end(); - return 0; + //printMatrix(C); + return 0; } From ba5f8f617ce6e7d098280a9d56f92e742e98c5df Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 04:18:44 -0400 Subject: [PATCH 33/47] Update convolve.cpp --- src/convolve.cpp | 110 ++++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index a117e23..fbde60d 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -16,76 +16,65 @@ void parsec_roi_end() } - -struct Result { - vector< vector > A; -}; - -Result read(string filename) { +vector< vector > read(string filename) { vector< vector > A; - Result ab; string line; ifstream infile; - infile.open (filename.c_str()); + infile.open(filename.c_str()); int i = 0; - while (getline(infile, line) && !line.empty()) { + while (getline(infile, line)) { istringstream iss(line); A.resize(A.size() + 1); - int a, j = 0; - while (iss >> a) - { + int a; + while (iss >> a) { A[i].push_back(a); - j++; } i++; } infile.close(); - ab.A = A; - return ab; + return A; } -vector> initializeIdentityKernel(int size) { - vector> kernel(size, vector(size, 0)); - for (int i = 0; i < size; ++i) { - kernel[i][i] = 1; - } - return kernel; -} +// Perform 2D convolution with a 3x3 kernel +vector< vector > convolution2D(vector< vector > A, vector< vector > kernel) { + int n = A.size(); + int m = A[0].size(); + + // Initialize output matrix C with the same size as A + vector tmp(m, 0); + vector< vector > C(n, tmp); + + int kernelSize = kernel.size(); + int offset = kernelSize / 2; + + // Perform convolution + for (int i = offset; i < n - offset; i++) { + for (int j = offset; j < m - offset; j++) { + float sum = 0.0; + // Convolution operation + for (int ki = 0; ki < kernelSize; ki++) { + for (int kj = 0; kj < kernelSize; kj++) { + int ni = i + (ki - offset); // Neighbor index for matrix A + int nj = j + (kj - offset); // Neighbor index for matrix A + sum += A[ni][nj] * kernel[ki][kj]; + } + } + C[i][j] = static_cast(sum); // Store the result in C + } + } - vector> convolve(const vector>& matrix, const vector>& kernel) { -{ - int n = matrix.size(); - int k = kernel.size(); - int outputSize = (n - k) + 1; - - vector> output(outputSize, vector(outputSize, 0)); - - for (int i = 0; i <= n - k; i += 1) { - for (int j = 0; j <= n - k; j += 1) { - int sum = 0; - for (int ki = 0; ki < k; ++ki) { - for (int kj = 0; kj < k; ++kj) { - sum += matrix[i + ki][j + kj] * kernel[ki][kj]; - } - } - output[i ][j ] = sum; - } - } - - return output; + return C; } -} - void printMatrix(vector< vector > matrix) { vector< vector >::iterator it; vector::iterator inner; - for (it=matrix.begin(); it != matrix.end(); it++) { + for (it = matrix.begin(); it != matrix.end(); it++) { for (inner = it->begin(); inner != it->end(); inner++) { cout << *inner; - if(inner+1 != it->end()) { + if (inner + 1 != it->end()) { cout << "\t"; } } @@ -93,18 +82,31 @@ void printMatrix(vector< vector > matrix) { } } -int main (int argc, char* argv[]) { +int main(int argc, char* argv[]) { string filename; if (argc < 3) { filename = "2000.in"; } else { filename = argv[2]; } - Result result = read (filename); - vector> kernel = initializeIdentityKernel(7); //used identity matrix for 7X7 - parsec_roi_begin(); - vector< vector > C = convolve(result.A,kernel); - parsec_roi_end(); - //printMatrix(C); + + // Read matrix A from file + vector< vector > A = read(filename); + + // Define a 3x3 Gaussian kernel + vector< vector > gaussianKernel = { + {1 / 16.0, 2 / 16.0, 1 / 16.0}, + {2 / 16.0, 4 / 16.0, 2 / 16.0}, + {1 / 16.0, 2 / 16.0, 1 / 16.0} + }; + + // Perform convolution on matrix A + parsec_roi_begin(); + vector< vector > C = convolution2D(A, gaussianKernel); + parsec_roi_end(); + + // Uncomment the following line to print the result + // printMatrix(C); + return 0; } From c8852861c4e838b20fc609998cea150a08f4fe99 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 04:24:55 -0400 Subject: [PATCH 34/47] Update convolve.cpp --- src/convolve.cpp | 148 ++++++++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 71 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index fbde60d..dfe1cc6 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace std; @@ -13,11 +14,16 @@ void parsec_roi_begin() void parsec_roi_end() { - + } -vector< vector > read(string filename) { +struct Result { + vector< vector > A; +}; + +Result read(string filename) { vector< vector > A; + Result ab; string line; ifstream infile; infile.open(filename.c_str()); @@ -32,81 +38,81 @@ vector< vector > read(string filename) { } i++; } - infile.close(); - return A; + ab.A = A; + return ab; } -// Perform 2D convolution with a 3x3 kernel -vector< vector > convolution2D(vector< vector > A, vector< vector > kernel) { - int n = A.size(); - int m = A[0].size(); - - // Initialize output matrix C with the same size as A - vector tmp(m, 0); - vector< vector > C(n, tmp); - - int kernelSize = kernel.size(); - int offset = kernelSize / 2; - - // Perform convolution - for (int i = offset; i < n - offset; i++) { - for (int j = offset; j < m - offset; j++) { - float sum = 0.0; - // Convolution operation - for (int ki = 0; ki < kernelSize; ki++) { - for (int kj = 0; kj < kernelSize; kj++) { - int ni = i + (ki - offset); // Neighbor index for matrix A - int nj = j + (kj - offset); // Neighbor index for matrix A - sum += A[ni][nj] * kernel[ki][kj]; - } - } - C[i][j] = static_cast(sum); // Store the result in C - } - } - - return C; +// Define the 7x7 Gaussian kernel +vector> gaussianKernel7x7() { + return { + {0, 0, 1, 2, 1, 0, 0}, + {0, 3, 13, 22, 13, 3, 0}, + {1, 13, 59, 97, 59, 13, 1}, + {2, 22, 97, 159, 97, 22, 2}, + {1, 13, 59, 97, 59, 13, 1}, + {0, 3, 13, 22, 13, 3, 0}, + {0, 0, 1, 2, 1, 0, 0} + }; } -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; - } +// Pad the matrix with zeros around the border (for 7x7 kernel) +vector> padMatrix(const vector>& matrix) { + int rows = matrix.size(); + int cols = matrix[0].size(); + vector> padded(rows + 6, vector(cols + 6, 0)); + + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + padded[i + 3][j + 3] = matrix[i][j]; // Pad with 3 rows and columns (half of 7-1) + } + } + return padded; } -int main(int argc, char* argv[]) { - string filename; - if (argc < 3) { - filename = "2000.in"; - } else { - filename = argv[2]; - } - - // Read matrix A from file - vector< vector > A = read(filename); - - // Define a 3x3 Gaussian kernel - vector< vector > gaussianKernel = { - {1 / 16.0, 2 / 16.0, 1 / 16.0}, - {2 / 16.0, 4 / 16.0, 2 / 16.0}, - {1 / 16.0, 2 / 16.0, 1 / 16.0} - }; - - // Perform convolution on matrix A - parsec_roi_begin(); - vector< vector > C = convolution2D(A, gaussianKernel); - parsec_roi_end(); - - // Uncomment the following line to print the result - // printMatrix(C); +// Perform convolution using the 7x7 Gaussian kernel +vector> convolve7x7(const vector>& matrix) { + vector> kernel = gaussianKernel7x7(); + vector> padded = padMatrix(matrix); + int rows = matrix.size(); + int cols = matrix[0].size(); + vector> result(rows, vector(cols, 0)); + + for (int i = 3; i < rows + 3; ++i) { // Start at index 3 due to padding + for (int j = 3; j < cols + 3; ++j) { + int sum = 0; + for (int ki = -3; ki <= 3; ++ki) { + for (int kj = -3; kj <= 3; ++kj) { + sum += padded[i + ki][j + kj] * kernel[ki + 3][kj + 3]; // Shift by 3 for 7x7 kernel + } + } + result[i - 3][j - 3] = sum; + } + } + return result; +} - return 0; +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 = convolve7x7(result.A); + parsec_roi_end(); + + // Optionally print the result + // for (const auto& row : C) { + // for (const auto& elem : row) { + // cout << elem << " "; + // } + // cout << endl; + // } + + return 0; } From cb0f20182ac99344e25b767d1a1b93fb5b978aa3 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 04:26:30 -0400 Subject: [PATCH 35/47] Update convolve.cpp --- src/convolve.cpp | 61 ++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index dfe1cc6..b9ef22c 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -26,15 +26,16 @@ Result read(string filename) { Result ab; string line; ifstream infile; - infile.open(filename.c_str()); + infile.open (filename.c_str()); int i = 0; - while (getline(infile, line)) { + while (getline(infile, line) && !line.empty()) { istringstream iss(line); A.resize(A.size() + 1); - int a; + int a, j = 0; while (iss >> a) { A[i].push_back(a); + j++; } i++; } @@ -43,50 +44,44 @@ Result read(string filename) { return ab; } -// Define the 7x7 Gaussian kernel -vector> gaussianKernel7x7() { +// Define the 3x3 Gaussian kernel with integer values +vector> gaussianKernel() { return { - {0, 0, 1, 2, 1, 0, 0}, - {0, 3, 13, 22, 13, 3, 0}, - {1, 13, 59, 97, 59, 13, 1}, - {2, 22, 97, 159, 97, 22, 2}, - {1, 13, 59, 97, 59, 13, 1}, - {0, 3, 13, 22, 13, 3, 0}, - {0, 0, 1, 2, 1, 0, 0} + {1, 2, 1}, + {2, 4, 2}, + {1, 2, 1} }; } -// Pad the matrix with zeros around the border (for 7x7 kernel) vector> padMatrix(const vector>& matrix) { int rows = matrix.size(); int cols = matrix[0].size(); - vector> padded(rows + 6, vector(cols + 6, 0)); + vector> padded(rows + 2, vector(cols + 2, 0)); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { - padded[i + 3][j + 3] = matrix[i][j]; // Pad with 3 rows and columns (half of 7-1) + padded[i + 1][j + 1] = matrix[i][j]; } } return padded; } -// Perform convolution using the 7x7 Gaussian kernel -vector> convolve7x7(const vector>& matrix) { - vector> kernel = gaussianKernel7x7(); +vector> convolve(const vector>& matrix) { + vector> kernel = gaussianKernel(); vector> padded = padMatrix(matrix); - int rows = matrix.size(); - int cols = matrix[0].size(); - vector> result(rows, vector(cols, 0)); + int rows = padded.size(); + int cols = padded[0].size(); + vector> result(matrix.size(), vector(matrix[0].size(), 0)); - for (int i = 3; i < rows + 3; ++i) { // Start at index 3 due to padding - for (int j = 3; j < cols + 3; ++j) { + for (int i = 1; i < rows - 1; ++i) { + for (int j = 1; j < cols - 1; ++j) { int sum = 0; - for (int ki = -3; ki <= 3; ++ki) { - for (int kj = -3; kj <= 3; ++kj) { - sum += padded[i + ki][j + kj] * kernel[ki + 3][kj + 3]; // Shift by 3 for 7x7 kernel + 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 - 3][j - 3] = sum; + result[i - 1][j - 1] = sum; } } return result; @@ -100,19 +95,9 @@ int main (int argc, char* argv[]) { } else { filename = argv[2]; } - Result result = read(filename); parsec_roi_begin(); - vector< vector > C = convolve7x7(result.A); + vector< vector > C = convolve(result.A); parsec_roi_end(); - - // Optionally print the result - // for (const auto& row : C) { - // for (const auto& elem : row) { - // cout << elem << " "; - // } - // cout << endl; - // } - return 0; } From ca0090f53a8b96bc835a4325aac17c5b5e900286 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 04:28:19 -0400 Subject: [PATCH 36/47] Update convolve.cpp --- src/convolve.cpp | 52 +++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index b9ef22c..81ca3cc 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -26,16 +26,15 @@ Result read(string filename) { Result ab; string line; ifstream infile; - infile.open (filename.c_str()); + infile.open(filename.c_str()); int i = 0; - while (getline(infile, line) && !line.empty()) { + while (getline(infile, line)) { istringstream iss(line); A.resize(A.size() + 1); - int a, j = 0; + int a; while (iss >> a) { A[i].push_back(a); - j++; } i++; } @@ -44,44 +43,50 @@ Result read(string filename) { return ab; } -// Define the 3x3 Gaussian kernel with integer values -vector> gaussianKernel() { +// Define the 7x7 Gaussian kernel +vector> gaussianKernel7x7() { return { - {1, 2, 1}, - {2, 4, 2}, - {1, 2, 1} + {0, 0, 1, 2, 1, 0, 0}, + {0, 3, 13, 22, 13, 3, 0}, + {1, 13, 59, 97, 59, 13, 1}, + {2, 22, 97, 159, 97, 22, 2}, + {1, 13, 59, 97, 59, 13, 1}, + {0, 3, 13, 22, 13, 3, 0}, + {0, 0, 1, 2, 1, 0, 0} }; } +// Pad the matrix with zeros around the border (for 7x7 kernel) vector> padMatrix(const vector>& matrix) { int rows = matrix.size(); int cols = matrix[0].size(); - vector> padded(rows + 2, vector(cols + 2, 0)); + vector> padded(rows + 6, vector(cols + 6, 0)); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { - padded[i + 1][j + 1] = matrix[i][j]; + padded[i + 3][j + 3] = matrix[i][j]; // Pad with 3 rows and columns (half of 7-1) } } return padded; } -vector> convolve(const vector>& matrix) { - vector> kernel = gaussianKernel(); +// Perform convolution using the 7x7 Gaussian kernel +vector> convolve7x7(const vector>& matrix) { + vector> kernel = gaussianKernel7x7(); vector> padded = padMatrix(matrix); - int rows = padded.size(); - int cols = padded[0].size(); - vector> result(matrix.size(), vector(matrix[0].size(), 0)); + int rows = matrix.size(); + int cols = matrix[0].size(); + vector> result(rows, vector(cols, 0)); - for (int i = 1; i < rows - 1; ++i) { - for (int j = 1; j < cols - 1; ++j) { + for (int i = 3; i < rows + 3; ++i) { // Start at index 3 due to padding + for (int j = 3; j < cols + 3; ++j) { int sum = 0; - 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]; + for (int ki = -3; ki <= 3; ++ki) { + for (int kj = -3; kj <= 3; ++kj) { + sum += padded[i + ki][j + kj] * kernel[ki + 3][kj + 3]; // Shift by 3 for 7x7 kernel } } - result[i - 1][j - 1] = sum; + result[i - 3][j - 3] = sum; } } return result; @@ -95,9 +100,10 @@ int main (int argc, char* argv[]) { } else { filename = argv[2]; } + Result result = read(filename); parsec_roi_begin(); - vector< vector > C = convolve(result.A); + vector< vector > C = convolve7x7(result.A); parsec_roi_end(); return 0; } From 12777039c197455e83705bf2620e2d8a5af92ece Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 04:33:25 -0400 Subject: [PATCH 37/47] Update convolve.cpp --- src/convolve.cpp | 53 ++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index 81ca3cc..2cb89e4 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -21,26 +21,19 @@ 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)) { - istringstream iss(line); - A.resize(A.size() + 1); - int a; - while (iss >> a) { - A[i].push_back(a); - } - i++; - } - infile.close(); - ab.A = A; - return ab; +// Function to generate a matrix with the first dimension as a power of 2 (e.g., 64) +// and the second dimension not (e.g., 80) +Result generateMatrix(int rows, int cols) { + Result ab; + ab.A.resize(rows, vector(cols, 0)); + + // Fill the matrix with some random values + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + ab.A[i][j] = rand() % 100; // Random values between 0 and 99 + } + } + return ab; } // Define the 7x7 Gaussian kernel @@ -94,16 +87,22 @@ vector> convolve7x7(const vector>& matrix) { 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); + int rows = 64; // Power of 2 + int cols = 80; // Not a power of 2 + + Result result = generateMatrix(rows, cols); // Generate matrix with the given dimensions parsec_roi_begin(); vector< vector > C = convolve7x7(result.A); parsec_roi_end(); + + // Optionally print the result + // for (const auto& row : C) { + // for (const auto& elem : row) { + // cout << elem << " "; + // } + // cout << endl; + // } + return 0; } From 6c7a144ce0ef04592b76b0e1ddcd75747ea1575d Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 04:36:53 -0400 Subject: [PATCH 38/47] Update convolve.cpp --- src/convolve.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index 2cb89e4..b4bf5d4 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -64,7 +64,7 @@ vector> padMatrix(const vector>& matrix) { } // Perform convolution using the 7x7 Gaussian kernel -vector> convolve7x7(const vector>& matrix) { +vector> convolve(const vector>& matrix) { vector> kernel = gaussianKernel7x7(); vector> padded = padMatrix(matrix); int rows = matrix.size(); @@ -93,7 +93,7 @@ int main (int argc, char* argv[]) { Result result = generateMatrix(rows, cols); // Generate matrix with the given dimensions parsec_roi_begin(); - vector< vector > C = convolve7x7(result.A); + vector< vector > C = convolve(result.A); parsec_roi_end(); // Optionally print the result From 84677509b2fd999348eb568fc6adbe50e817f865 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 21:52:33 -0400 Subject: [PATCH 39/47] Update convolve.cpp --- src/convolve.cpp | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index b4bf5d4..0555f3c 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -21,18 +21,29 @@ struct Result { vector< vector > A; }; -// Function to generate a matrix with the first dimension as a power of 2 (e.g., 64) -// and the second dimension not (e.g., 80) -Result generateMatrix(int rows, int cols) { +// Function to read the matrix from a file (as specified in the bash script) +Result readMatrixFromFile(string filename) { + vector< vector > A; Result ab; - ab.A.resize(rows, vector(cols, 0)); + string line; + ifstream infile(filename); - // Fill the matrix with some random values - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - ab.A[i][j] = rand() % 100; // Random values between 0 and 99 + if (!infile.is_open()) { + cerr << "Error opening file: " << filename << endl; + exit(1); + } + + while (getline(infile, line)) { + istringstream iss(line); + vector row; + int value; + while (iss >> value) { + row.push_back(value); } + A.push_back(row); } + infile.close(); + ab.A = A; return ab; } @@ -88,10 +99,13 @@ vector> convolve(const vector>& matrix) { int main (int argc, char* argv[]) { srand(time(0)); - int rows = 64; // Power of 2 - int cols = 80; // Not a power of 2 + if (argc < 2) { + cerr << "Usage: " << argv[0] << " input_file" << endl; + return 1; + } - Result result = generateMatrix(rows, cols); // Generate matrix with the given dimensions + string input_filename = argv[1]; // Use the input file specified in the command line + Result result = readMatrixFromFile(input_filename); // Read the matrix from the file parsec_roi_begin(); vector< vector > C = convolve(result.A); parsec_roi_end(); From 229f529dcfce698e8e5d22dc40c86cf79a4c60f5 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 22:15:11 -0400 Subject: [PATCH 40/47] Update convolve.cpp --- src/convolve.cpp | 156 ++++++++++++++++++++++------------------------- 1 file changed, 72 insertions(+), 84 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index 0555f3c..a117e23 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -3,7 +3,6 @@ #include #include #include -#include using namespace std; @@ -14,109 +13,98 @@ void parsec_roi_begin() void parsec_roi_end() { - + } + struct Result { vector< vector > A; }; -// Function to read the matrix from a file (as specified in the bash script) -Result readMatrixFromFile(string filename) { - vector< vector > A; - Result ab; - string line; - ifstream infile(filename); - - if (!infile.is_open()) { - cerr << "Error opening file: " << filename << endl; - exit(1); - } - - while (getline(infile, line)) { - istringstream iss(line); - vector row; - int value; - while (iss >> value) { - row.push_back(value); - } - A.push_back(row); - } - infile.close(); - ab.A = A; - return ab; -} - -// Define the 7x7 Gaussian kernel -vector> gaussianKernel7x7() { - return { - {0, 0, 1, 2, 1, 0, 0}, - {0, 3, 13, 22, 13, 3, 0}, - {1, 13, 59, 97, 59, 13, 1}, - {2, 22, 97, 159, 97, 22, 2}, - {1, 13, 59, 97, 59, 13, 1}, - {0, 3, 13, 22, 13, 3, 0}, - {0, 0, 1, 2, 1, 0, 0} - }; +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; } -// Pad the matrix with zeros around the border (for 7x7 kernel) -vector> padMatrix(const vector>& matrix) { - int rows = matrix.size(); - int cols = matrix[0].size(); - vector> padded(rows + 6, vector(cols + 6, 0)); - - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - padded[i + 3][j + 3] = matrix[i][j]; // Pad with 3 rows and columns (half of 7-1) - } +vector> initializeIdentityKernel(int size) { + vector> kernel(size, vector(size, 0)); + for (int i = 0; i < size; ++i) { + kernel[i][i] = 1; } - return padded; + return kernel; } -// Perform convolution using the 7x7 Gaussian kernel -vector> convolve(const vector>& matrix) { - vector> kernel = gaussianKernel7x7(); - vector> padded = padMatrix(matrix); - int rows = matrix.size(); - int cols = matrix[0].size(); - vector> result(rows, vector(cols, 0)); + vector> convolve(const vector>& matrix, const vector>& kernel) { +{ + int n = matrix.size(); + int k = kernel.size(); + int outputSize = (n - k) + 1; + + vector> output(outputSize, vector(outputSize, 0)); - for (int i = 3; i < rows + 3; ++i) { // Start at index 3 due to padding - for (int j = 3; j < cols + 3; ++j) { + for (int i = 0; i <= n - k; i += 1) { + for (int j = 0; j <= n - k; j += 1) { int sum = 0; - for (int ki = -3; ki <= 3; ++ki) { - for (int kj = -3; kj <= 3; ++kj) { - sum += padded[i + ki][j + kj] * kernel[ki + 3][kj + 3]; // Shift by 3 for 7x7 kernel + for (int ki = 0; ki < k; ++ki) { + for (int kj = 0; kj < k; ++kj) { + sum += matrix[i + ki][j + kj] * kernel[ki][kj]; } } - result[i - 3][j - 3] = sum; + output[i ][j ] = sum; } } - return result; + + return output; +} } -int main (int argc, char* argv[]) { - srand(time(0)); - - if (argc < 2) { - cerr << "Usage: " << argv[0] << " input_file" << endl; - return 1; - } - string input_filename = argv[1]; // Use the input file specified in the command line - Result result = readMatrixFromFile(input_filename); // Read the matrix from the file +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); + vector> kernel = initializeIdentityKernel(7); //used identity matrix for 7X7 parsec_roi_begin(); - vector< vector > C = convolve(result.A); + vector< vector > C = convolve(result.A,kernel); parsec_roi_end(); - - // Optionally print the result - // for (const auto& row : C) { - // for (const auto& elem : row) { - // cout << elem << " "; - // } - // cout << endl; - // } - - return 0; + //printMatrix(C); + return 0; } From 6494aa0550407541e051277f22559e17223deb6e Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 22:33:08 -0400 Subject: [PATCH 41/47] Update convolve.cpp --- src/convolve.cpp | 82 ++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index a117e23..0b9d359 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -16,27 +16,25 @@ void parsec_roi_end() } - struct Result { vector< vector > A; }; +// Function to read matrix from a file Result read(string filename) { vector< vector > A; Result ab; string line; ifstream infile; - infile.open (filename.c_str()); + 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) - { + int a; + while (iss >> a) { A[i].push_back(a); - j++; } i++; } @@ -46,48 +44,53 @@ Result read(string filename) { return ab; } -vector> initializeIdentityKernel(int size) { - vector> kernel(size, vector(size, 0)); - for (int i = 0; i < size; ++i) { - kernel[i][i] = 1; - } - return kernel; +// Define the 7x7 Gaussian kernel +vector> gaussianKernel7x7() { + return { + {0, 0, 1, 2, 1, 0, 0}, + {0, 3, 13, 22, 13, 3, 0}, + {1, 13, 59, 97, 59, 13, 1}, + {2, 22, 97, 159, 97, 22, 2}, + {1, 13, 59, 97, 59, 13, 1}, + {0, 3, 13, 22, 13, 3, 0}, + {0, 0, 1, 2, 1, 0, 0} + }; } - vector> convolve(const vector>& matrix, const vector>& kernel) { -{ +// Perform convolution +vector> convolve(const vector>& matrix, const vector>& kernel) { int n = matrix.size(); int k = kernel.size(); int outputSize = (n - k) + 1; + // If the matrix is too small for the kernel, return an empty output + if (outputSize <= 0) { + cout << "Error: Input matrix is too small for the kernel size." << endl; + exit(1); + } + vector> output(outputSize, vector(outputSize, 0)); - for (int i = 0; i <= n - k; i += 1) { - for (int j = 0; j <= n - k; j += 1) { + for (int i = 0; i <= n - k; i++) { + for (int j = 0; j <= n - k; j++) { int sum = 0; for (int ki = 0; ki < k; ++ki) { for (int kj = 0; kj < k; ++kj) { sum += matrix[i + ki][j + kj] * kernel[ki][kj]; } } - output[i ][j ] = sum; + output[i][j] = sum; } } return output; } -} - -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"; - } +// Function to print a matrix +void printMatrix(const vector>& matrix) { + for (const auto& row : matrix) { + for (const auto& elem : row) { + cout << elem << "\t"; } cout << endl; } @@ -95,16 +98,27 @@ void printMatrix(vector< vector > matrix) { int main (int argc, char* argv[]) { string filename; - if (argc < 3) { + if (argc < 2) { filename = "2000.in"; } else { - filename = argv[2]; + filename = argv[1]; // Corrected argument indexing } - Result result = read (filename); - vector> kernel = initializeIdentityKernel(7); //used identity matrix for 7X7 + + // Read input matrix from file + Result result = read(filename); + + // Use Gaussian kernel (size 7x7) + vector> kernel = gaussianKernel7x7(); + parsec_roi_begin(); - vector< vector > C = convolve(result.A,kernel); + + // Perform convolution + vector> C = convolve(result.A, kernel); + parsec_roi_end(); - //printMatrix(C); + + // Uncomment to print the resulting matrix + // printMatrix(C); + return 0; } From 994fad2d60eea9f6838f704f5a3693486312ce75 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Wed, 18 Sep 2024 22:45:40 -0400 Subject: [PATCH 42/47] Update convolve.cpp --- src/convolve.cpp | 82 ++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 48 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index 0b9d359..a117e23 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -16,25 +16,27 @@ void parsec_roi_end() } + struct Result { vector< vector > A; }; -// Function to read matrix from a file Result read(string filename) { vector< vector > A; Result ab; string line; ifstream infile; - infile.open(filename.c_str()); + infile.open (filename.c_str()); int i = 0; while (getline(infile, line) && !line.empty()) { istringstream iss(line); A.resize(A.size() + 1); - int a; - while (iss >> a) { + int a, j = 0; + while (iss >> a) + { A[i].push_back(a); + j++; } i++; } @@ -44,53 +46,48 @@ Result read(string filename) { return ab; } -// Define the 7x7 Gaussian kernel -vector> gaussianKernel7x7() { - return { - {0, 0, 1, 2, 1, 0, 0}, - {0, 3, 13, 22, 13, 3, 0}, - {1, 13, 59, 97, 59, 13, 1}, - {2, 22, 97, 159, 97, 22, 2}, - {1, 13, 59, 97, 59, 13, 1}, - {0, 3, 13, 22, 13, 3, 0}, - {0, 0, 1, 2, 1, 0, 0} - }; +vector> initializeIdentityKernel(int size) { + vector> kernel(size, vector(size, 0)); + for (int i = 0; i < size; ++i) { + kernel[i][i] = 1; + } + return kernel; } -// Perform convolution -vector> convolve(const vector>& matrix, const vector>& kernel) { + vector> convolve(const vector>& matrix, const vector>& kernel) { +{ int n = matrix.size(); int k = kernel.size(); int outputSize = (n - k) + 1; - // If the matrix is too small for the kernel, return an empty output - if (outputSize <= 0) { - cout << "Error: Input matrix is too small for the kernel size." << endl; - exit(1); - } - vector> output(outputSize, vector(outputSize, 0)); - for (int i = 0; i <= n - k; i++) { - for (int j = 0; j <= n - k; j++) { + for (int i = 0; i <= n - k; i += 1) { + for (int j = 0; j <= n - k; j += 1) { int sum = 0; for (int ki = 0; ki < k; ++ki) { for (int kj = 0; kj < k; ++kj) { sum += matrix[i + ki][j + kj] * kernel[ki][kj]; } } - output[i][j] = sum; + output[i ][j ] = sum; } } return output; } +} + -// Function to print a matrix -void printMatrix(const vector>& matrix) { - for (const auto& row : matrix) { - for (const auto& elem : row) { - cout << elem << "\t"; +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; } @@ -98,27 +95,16 @@ void printMatrix(const vector>& matrix) { int main (int argc, char* argv[]) { string filename; - if (argc < 2) { + if (argc < 3) { filename = "2000.in"; } else { - filename = argv[1]; // Corrected argument indexing + filename = argv[2]; } - - // Read input matrix from file - Result result = read(filename); - - // Use Gaussian kernel (size 7x7) - vector> kernel = gaussianKernel7x7(); - + Result result = read (filename); + vector> kernel = initializeIdentityKernel(7); //used identity matrix for 7X7 parsec_roi_begin(); - - // Perform convolution - vector> C = convolve(result.A, kernel); - + vector< vector > C = convolve(result.A,kernel); parsec_roi_end(); - - // Uncomment to print the resulting matrix - // printMatrix(C); - + //printMatrix(C); return 0; } From ea7bf90e6ea52dc4c37f44a8d7e24b6812f00b52 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Thu, 19 Sep 2024 00:32:49 -0400 Subject: [PATCH 43/47] Update convolve.cpp --- src/convolve.cpp | 97 ++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index a117e23..b9ef22c 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace std; @@ -13,10 +14,9 @@ void parsec_roi_begin() void parsec_roi_end() { - + } - struct Result { vector< vector > A; }; @@ -33,78 +33,71 @@ Result read(string filename) { istringstream iss(line); A.resize(A.size() + 1); int a, j = 0; - while (iss >> a) - { + while (iss >> a) { A[i].push_back(a); j++; } i++; } - infile.close(); ab.A = A; return ab; } -vector> initializeIdentityKernel(int size) { - vector> kernel(size, vector(size, 0)); - for (int i = 0; i < size; ++i) { - kernel[i][i] = 1; +// Define the 3x3 Gaussian kernel with integer values +vector> gaussianKernel() { + return { + {1, 2, 1}, + {2, 4, 2}, + {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 < rows; ++i) { + for (int j = 0; j < cols; ++j) { + padded[i + 1][j + 1] = matrix[i][j]; + } } - return kernel; + return padded; } - vector> convolve(const vector>& matrix, const vector>& kernel) { -{ - int n = matrix.size(); - int k = kernel.size(); - int outputSize = (n - k) + 1; - - vector> output(outputSize, vector(outputSize, 0)); +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)); - for (int i = 0; i <= n - k; i += 1) { - for (int j = 0; j <= n - k; j += 1) { + for (int i = 1; i < rows - 1; ++i) { + for (int j = 1; j < cols - 1; ++j) { int sum = 0; - for (int ki = 0; ki < k; ++ki) { - for (int kj = 0; kj < k; ++kj) { - sum += matrix[i + ki][j + kj] * kernel[ki][kj]; + 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]; } } - output[i ][j ] = sum; + result[i - 1][j - 1] = sum; } } - - return output; -} -} - - -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[]) { - string filename; - if (argc < 3) { - filename = "2000.in"; - } else { - filename = argv[2]; - } - Result result = read (filename); - vector> kernel = initializeIdentityKernel(7); //used identity matrix for 7X7 + 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,kernel); + vector< vector > C = convolve(result.A); parsec_roi_end(); - //printMatrix(C); - return 0; + return 0; } From 9e352f3dab2a42cd527a166bedb403a63257ab7d Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Thu, 19 Sep 2024 00:41:41 -0400 Subject: [PATCH 44/47] Update convolve.cpp --- src/convolve.cpp | 97 ++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index b9ef22c..a117e23 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -3,7 +3,6 @@ #include #include #include -#include using namespace std; @@ -14,9 +13,10 @@ void parsec_roi_begin() void parsec_roi_end() { - + } + struct Result { vector< vector > A; }; @@ -33,71 +33,78 @@ Result read(string filename) { istringstream iss(line); A.resize(A.size() + 1); int a, j = 0; - while (iss >> a) { + while (iss >> a) + { A[i].push_back(a); j++; } i++; } + infile.close(); ab.A = A; return ab; } -// Define the 3x3 Gaussian kernel with integer values -vector> gaussianKernel() { - return { - {1, 2, 1}, - {2, 4, 2}, - {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 < rows; ++i) { - for (int j = 0; j < cols; ++j) { - padded[i + 1][j + 1] = matrix[i][j]; - } +vector> initializeIdentityKernel(int size) { + vector> kernel(size, vector(size, 0)); + for (int i = 0; i < size; ++i) { + kernel[i][i] = 1; } - return padded; + return kernel; } -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)); + vector> convolve(const vector>& matrix, const vector>& kernel) { +{ + int n = matrix.size(); + int k = kernel.size(); + int outputSize = (n - k) + 1; + + vector> output(outputSize, vector(outputSize, 0)); - for (int i = 1; i < rows - 1; ++i) { - for (int j = 1; j < cols - 1; ++j) { + for (int i = 0; i <= n - k; i += 1) { + for (int j = 0; j <= n - k; j += 1) { int sum = 0; - 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]; + for (int ki = 0; ki < k; ++ki) { + for (int kj = 0; kj < k; ++kj) { + sum += matrix[i + ki][j + kj] * kernel[ki][kj]; } } - result[i - 1][j - 1] = sum; + output[i ][j ] = sum; } } - return result; + + return output; +} +} + + +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); + string filename; + if (argc < 3) { + filename = "2000.in"; + } else { + filename = argv[2]; + } + Result result = read (filename); + vector> kernel = initializeIdentityKernel(7); //used identity matrix for 7X7 parsec_roi_begin(); - vector< vector > C = convolve(result.A); + vector< vector > C = convolve(result.A,kernel); parsec_roi_end(); - return 0; + //printMatrix(C); + return 0; } From 27d3603ffd86f51593572ccd804c92399ce985b2 Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Thu, 19 Sep 2024 23:23:39 -0400 Subject: [PATCH 45/47] Update scatter.cpp --- src/scatter.cpp | 116 +++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 56 deletions(-) diff --git a/src/scatter.cpp b/src/scatter.cpp index c41d1b5..091b3b8 100644 --- a/src/scatter.cpp +++ b/src/scatter.cpp @@ -14,78 +14,82 @@ 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< 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; + int n = A.size(); + int numElements = 1000; + vector indices(numElements); + vector values(numElements); + + for(int i = 0; i < numElements; i++) { + int randomIndex = rand() % (n * n); + indices[i] = randomIndex; + values[i] = rand(); + } + + for(int i = 0; i < numElements; i++) { + int row = indices[i] / n; + int column = indices[i] % n; + A[row][column] = values[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; - } + 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); + 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); + vector< vector > C = scatter(result.A); parsec_roi_end(); - //printMatrix(C); - return 0; + return 0; } From 598f537b0af5327d7cffe01014b6f92e961ad60c Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Thu, 19 Sep 2024 23:26:29 -0400 Subject: [PATCH 46/47] Update gather.cpp --- src/gather.cpp | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/gather.cpp b/src/gather.cpp index e8965d3..584f953 100644 --- a/src/gather.cpp +++ b/src/gather.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using namespace std; @@ -44,25 +45,30 @@ Result read(string filename) { vector gather(const vector>& A) { int n = A.size(); - int number_of_indices = 1000; - vector indices(number_of_indices), gathered_data(number_of_indices); + int elements = 1000; + vector indices(elements), result(elements); + unordered_set unique_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 < elements; ++i) { + int random_index; + do { + random_index = rand() % (n * n); + } while(unique_indices.find(random_index) != unique_indices.end()); + + indices[i] = random_index; + unique_indices.insert(random_index); } - - // 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 + + int offset = 5; + for(int idx = 0; idx < elements; ++idx) { + int rowIndex = indices[idx] / n; + int columnIndex = indices[idx] % n; + result[idx] = A[rowIndex][columnIndex] + offset; } - return gathered_data; // Return the gathered data + return result; } -// Updated to print a 1D vector void printVector(const vector& vec) { for (size_t i = 0; i < vec.size(); i++) { cout << vec[i]; @@ -83,11 +89,7 @@ int main (int argc, char* argv[]) { } Result result = read (filename); parsec_roi_begin(); - vector C = gather(result.A); // Corrected to vector + vector C = gather(result.A); parsec_roi_end(); - - // Optionally print the gathered data as a 1D vector - //printVector(C); - return 0; } From 33ad09e66830bc48a1cfa134d08dcdd3f9a1409a Mon Sep 17 00:00:00 2001 From: gouthamdeepak1705 Date: Thu, 19 Sep 2024 23:29:43 -0400 Subject: [PATCH 47/47] Update convolve.cpp --- src/convolve.cpp | 55 ++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/src/convolve.cpp b/src/convolve.cpp index a117e23..1add983 100644 --- a/src/convolve.cpp +++ b/src/convolve.cpp @@ -8,15 +8,12 @@ using namespace std; void parsec_roi_begin() { - } void parsec_roi_end() { - } - struct Result { vector< vector > A; }; @@ -26,21 +23,19 @@ Result read(string filename) { Result ab; string line; ifstream infile; - infile.open (filename.c_str()); + 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) - { + while (iss >> a) { A[i].push_back(a); j++; } i++; } - infile.close(); ab.A = A; return ab; @@ -54,38 +49,35 @@ vector> initializeIdentityKernel(int size) { return kernel; } - vector> convolve(const vector>& matrix, const vector>& kernel) { -{ - int n = matrix.size(); - int k = kernel.size(); - int outputSize = (n - k) + 1; +vector> convolve(const vector>& matrix, const vector>& kernel) { + int matrixSize = matrix.size(); + int kernelSize = kernel.size(); + int outputSize = matrixSize - kernelSize + 1; - vector> output(outputSize, vector(outputSize, 0)); - - for (int i = 0; i <= n - k; i += 1) { - for (int j = 0; j <= n - k; j += 1) { - int sum = 0; - for (int ki = 0; ki < k; ++ki) { - for (int kj = 0; kj < k; ++kj) { - sum += matrix[i + ki][j + kj] * kernel[ki][kj]; + vector> result(outputSize, vector(outputSize, 0)); + + for (int row = 0; row <= matrixSize - kernelSize; ++row) { + for (int col = 0; col <= matrixSize - kernelSize; ++col) { + int convSum = 0; + for (int i = 0; i < kernelSize; ++i) { + for (int j = 0; j < kernelSize; ++j) { + convSum += matrix[row + i][col + j] * kernel[i][j]; } } - output[i ][j ] = sum; + result[row][col] = convSum; } } - return output; + return result; } -} - 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++) { + for (it = matrix.begin(); it != matrix.end(); ++it) { + for (inner = it->begin(); inner != it->end(); ++inner) { cout << *inner; - if(inner+1 != it->end()) { + if (inner + 1 != it->end()) { cout << "\t"; } } @@ -93,18 +85,17 @@ void printMatrix(vector< vector > matrix) { } } -int main (int argc, char* argv[]) { +int main(int argc, char* argv[]) { string filename; if (argc < 3) { filename = "2000.in"; } else { filename = argv[2]; } - Result result = read (filename); - vector> kernel = initializeIdentityKernel(7); //used identity matrix for 7X7 + Result result = read(filename); + vector> kernel = initializeIdentityKernel(7); parsec_roi_begin(); - vector< vector > C = convolve(result.A,kernel); + vector< vector > C = convolve(result.A, kernel); parsec_roi_end(); - //printMatrix(C); return 0; }