From 89cc39c906595050db70d54ebcc73829eabc8a84 Mon Sep 17 00:00:00 2001 From: rotormelja98119 Date: Mon, 15 Dec 2025 15:15:46 +0100 Subject: [PATCH] =?UTF-8?q?Test=20wurde=20f=C3=BCr=20kleinere=20Matrizen?= =?UTF-8?q?=20angepasst=20->=20Komponentenweiser=20Vergleich?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parallel2/main.cpp | 147 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 111 insertions(+), 36 deletions(-) diff --git a/parallel2/main.cpp b/parallel2/main.cpp index 2b8a4c5..2c978a3 100644 --- a/parallel2/main.cpp +++ b/parallel2/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -29,7 +30,7 @@ void generateRandomMatrices(int n, } -// Serielle Multiplikation +// Seriell std::vector> matmul_serial(const std::vector>& A, const std::vector>& B) @@ -51,9 +52,9 @@ std::vector> matmul_serial(const std::vector> matmul_row_parallel(const std::vector>& A, - const std::vector>& B, - int numThreads) +std::vector> matmul_parallel_rows(const std::vector>& A, + const std::vector>& B, + int numThreads) { int n = A.size(); int m = B[0].size(); @@ -61,13 +62,19 @@ std::vector> matmul_row_parallel(const std::vector> C(n, std::vector(m, 0.0)); - omp_set_num_threads(numThreads); - - #pragma omp parallel for + #pragma omp parallel for num_threads(numThreads) for (int i = 0; i < n; ++i) + { + std::vector row(m, 0.0); for (int j = 0; j < m; ++j) + { + double sum = 0.0; for (int k = 0; k < p; ++k) - C[i][j] += A[i][k] * B[k][j]; + sum += A[i][k] * B[k][j]; //verhindert überschreibuung + row[j] = sum; + } + C[i] = row; + } return C; } @@ -75,9 +82,9 @@ std::vector> matmul_row_parallel(const std::vector> matmul_col_parallel(const std::vector>& A, - const std::vector>& B, - int numThreads) +std::vector> matmul_parallel_cols(const std::vector>& A, + const std::vector>& B, + int numThreads) { int n = A.size(); int m = B[0].size(); @@ -85,24 +92,28 @@ std::vector> matmul_col_parallel(const std::vector> C(n, std::vector(m, 0.0)); - omp_set_num_threads(numThreads); - - #pragma omp parallel for + #pragma omp parallel for num_threads(numThreads) for (int j = 0; j < m; ++j) + { for (int i = 0; i < n; ++i) + { + double sum = 0.0; for (int k = 0; k < p; ++k) - C[i][j] += A[i][k] * B[k][j]; + sum += A[i][k] * B[k][j]; //Zwischengepeichert + C[i][j] = sum; + } + } return C; } -// Parallele Blockzerlegung +// Blockzerlegung -std::vector> matmul_block_parallel(const std::vector>& A, - const std::vector>& B, - int blockSize, - int numThreads) +std::vector> matmul_block(const std::vector>& A, + const std::vector>& B, + int blockSize, + int numThreads) { int n = A.size(); int m = B[0].size(); @@ -110,54 +121,118 @@ std::vector> matmul_block_parallel(const std::vector> C(n, std::vector(m, 0.0)); - omp_set_num_threads(numThreads); - - #pragma omp parallel for collapse(2) + #pragma omp parallel for collapse(2) num_threads(numThreads) for (int ii = 0; ii < n; ii += blockSize) for (int jj = 0; jj < m; jj += blockSize) + { for (int i = ii; i < std::min(ii + blockSize, n); ++i) for (int j = jj; j < std::min(jj + blockSize, m); ++j) + { + double sum = 0.0; for (int k = 0; k < p; ++k) - C[i][j] += A[i][k] * B[k][j]; + sum += A[i][k] * B[k][j]; + C[i][j] = sum; + } + } return C; } -// Main +// Matrix ausgeben + +void printMatrix(const std::vector>& M, const std::string& name) +{ + std::cout << name << " (" << M.size() << "x" << (M.empty() ? 0 : M[0].size()) << "):\n"; + for (const auto& row : M) + { + for (double val : row) + std::cout << val << "\t"; + std::cout << "\n"; + } + std::cout << "\n"; +} + + +//Matrix Vergleich (Komponentenweise) +bool compareMatrices(const std::vector>& A, + const std::vector>& B) +{ + if (A.size() != B.size() || A[0].size() != B[0].size()) + return false; + + for (size_t i = 0; i < A.size(); ++i) + for (size_t j = 0; j < A[0].size(); ++j) + if (std::abs(A[i][j] - B[i][j]) > 1e-9) + return false; + return true; +} + + + + + + int main() { - int N = 500; // Matrixgröße anpassen - int numThreads = 4; // Threads für OpenMP - int blockSize = 64; // Blockgröße für Blockmultiplikation + int N = 4; // für Ausgabe kleiner Matrizen <5 + int numThreads = omp_get_max_threads(); + if (numThreads == 0) numThreads = 4; std::vector> A, B; generateRandomMatrices(N, A, B); - // Seriell + if (N < 5) + { + printMatrix(A, "Matrix A"); + printMatrix(B, "Matrix B"); + } + + // Serielle Berechnung auto start = std::chrono::steady_clock::now(); auto C_serial = matmul_serial(A, B); auto end = std::chrono::steady_clock::now(); - std::cout << "Seriell: " << std::chrono::duration(end-start).count() << " s\n"; + double timeSerial = std::chrono::duration(end - start).count(); + std::cout << "Seriell: " << timeSerial << " s\n"; + + if (N < 5) + printMatrix(C_serial, "C_serial"); // Zeilenparallel start = std::chrono::steady_clock::now(); - auto C_row = matmul_row_parallel(A, B, numThreads); + auto C_rows = matmul_parallel_rows(A, B, numThreads); end = std::chrono::steady_clock::now(); - std::cout << "Zeilenparallel: " << std::chrono::duration(end-start).count() << " s\n"; + double timeRows = std::chrono::duration(end - start).count(); + std::cout << "Parallel Zeilen: " << timeRows << " s | " + << (compareMatrices(C_serial, C_rows) ? "Match" : "Mismatch") << "\n"; + + if (N < 5) + printMatrix(C_rows, "C_rows"); // Spaltenparallel start = std::chrono::steady_clock::now(); - auto C_col = matmul_col_parallel(A, B, numThreads); + auto C_cols = matmul_parallel_cols(A, B, numThreads); end = std::chrono::steady_clock::now(); - std::cout << "Spaltenparallel: " << std::chrono::duration(end-start).count() << " s\n"; + double timeCols = std::chrono::duration(end - start).count(); + std::cout << "Parallel Spalten: " << timeCols << " s | " + << (compareMatrices(C_serial, C_cols) ? "Match" : "Mismatch") << "\n"; + + if (N < 5) + printMatrix(C_cols, "C_cols"); // Blockparallel + int blockSize = 2; // für kleine N passend start = std::chrono::steady_clock::now(); - auto C_block = matmul_block_parallel(A, B, blockSize, numThreads); + auto C_block = matmul_block(A, B, blockSize, numThreads); end = std::chrono::steady_clock::now(); - std::cout << "Blockparallel: " << std::chrono::duration(end-start).count() << " s\n"; + double timeBlock = std::chrono::duration(end - start).count(); + std::cout << "Parallel Block (" << blockSize << "x" << blockSize << "): " + << timeBlock << " s | " + << (compareMatrices(C_serial, C_block) ? "Match" : "Mismatch") << "\n"; + + if (N < 5) + printMatrix(C_block, "C_block"); return 0; }