From b829d1b6993d6f7450a387a09cbeede47472e09c Mon Sep 17 00:00:00 2001 From: ehriger31415 Date: Mon, 22 Dec 2025 16:52:08 +0100 Subject: [PATCH] commit --- parallele_zeilenverlegung.cpp | 80 +++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/parallele_zeilenverlegung.cpp b/parallele_zeilenverlegung.cpp index 5a04805..3348465 100644 --- a/parallele_zeilenverlegung.cpp +++ b/parallele_zeilenverlegung.cpp @@ -2,43 +2,36 @@ #include #include #include +#include +#include -// Funktion erzeugt zwei Matrizen A und B der Größe n x n mit Zufallswerten +// Zufallsmatrizen erzeugen void generateRandomMatrices(int n, std::vector>& A, - std::vector>& B, - double min_val = 0.0, - double max_val = 10.0) + std::vector>& B) { - // Zufallsgenerator initialisieren std::srand(static_cast(std::time(nullptr))); - A.resize(n, std::vector(n)); B.resize(n, std::vector(n)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { - double r1 = static_cast(std::rand()) / RAND_MAX; // 0..1 - double r2 = static_cast(std::rand()) / RAND_MAX; // 0..1 - A[i][j] = min_val + r1 * (max_val - min_val); - B[i][j] = min_val + r2 * (max_val - min_val); + A[i][j] = static_cast(rand()) / RAND_MAX * 10.0; + B[i][j] = static_cast(rand()) / RAND_MAX * 10.0; } } -// Matrizenmultiplikation -std::vector> matmul(const std::vector>& A, +// Serielle Matrizenmultiplikation +std::vector> matmul_serial( + const std::vector>& A, const std::vector>& B) { int n = A.size(); int m = B[0].size(); int p = B.size(); - - if (A[0].size() != B.size()) - throw std::runtime_error("Matrixgrößen passen nicht."); - std::vector> C(n, std::vector(m, 0.0)); -#pragma omp parallel for + for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) for (int k = 0; k < p; ++k) @@ -47,22 +40,55 @@ std::vector> matmul(const std::vector>& return C; } +// Parallele Zeilenzerlegung +std::vector> matmul_row_parallel( + const std::vector>& A, + const std::vector>& B) +{ + int n = A.size(); + int m = B[0].size(); + int p = B.size(); + std::vector> C(n, std::vector(m, 0.0)); + +#pragma omp parallel for + for (int i = 0; i < n; ++i) // Zeilen parallel + for (int j = 0; j < m; ++j) + for (int k = 0; k < p; ++k) + C[i][j] += A[i][k] * B[k][j]; + + return C; +} + +// ----------------------------- int main() { - int N = 500; // z.B. 500x500 + int N = 500; std::vector> A, B; - - auto start = std::chrono::steady_clock::now(); - generateRandomMatrices(N, A, B); - auto C_serial = matmul(A, B); + // Serielle Version + auto start = std::chrono::steady_clock::now(); + auto C_serial = matmul_serial(A, B); auto end = std::chrono::steady_clock::now(); - std::chrono::duration diff = end - start; - std::cout << "Took: " << diff.count() << "s\n"; + std::cout << "Serial multiplication took: " + << std::chrono::duration(end - start).count() << "s\n"; + // Zeilenparallel + start = std::chrono::steady_clock::now(); + auto C_row = matmul_row_parallel(A, B); + end = std::chrono::steady_clock::now(); + std::cout << "Row-parallel multiplication took: " + << std::chrono::duration(end - start).count() << "s\n"; + + // Vergleich + for (int i = 0; i < N; ++i) + for (int j = 0; j < N; ++j) + if (C_serial[i][j] != C_row[i][j]) + { + std::cout << "Mismatch at " << i << "," << j << "\n"; + return 1; + } + + std::cout << "All results match!\n"; return 0; } -// -// Created by Anke Bidlingmaier on 15.12.25. -// \ No newline at end of file