This commit is contained in:
tilo 2025-12-15 15:10:24 +01:00
parent 9102188226
commit bb127c731d
2 changed files with 31 additions and 1 deletions

View File

@ -3,4 +3,5 @@ project(threads)
set(CMAKE_CXX_STANDARD 17)
add_executable(threads main.cpp)
add_executable(threads matrix_main.cpp
)

View File

@ -4,6 +4,8 @@
#include <stdexcept>
#include <chrono>
#include <iostream>
#include <omp.h>
// Funktion erzeugt zwei Matrizen A und B der Größe n x n mit Zufallswerten
void generateRandomMatrices(int n,
@ -49,6 +51,27 @@ std::vector<std::vector<double>> matmul(const std::vector<std::vector<double>>&
return C;
}
std::vector<std::vector<double>> matmul_parallel(const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& 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<std::vector<double>> C(n, std::vector<double>(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)
C[i][j] += A[i][k] * B[k][j];
return C;
}
int main()
{
int N = 500; // z.B. 500x500
@ -62,5 +85,11 @@ int main()
std::chrono::duration<double> diff = end - start;
std::cout << "Serial Took: " << diff.count() << "s\n";
start = std::chrono::steady_clock::now();
auto C_parallel= matmul_parallel(A, B);
end = std::chrono::steady_clock::now();
diff = end - start;
std::cout << "Serial Took: " << diff.count() << "s\n";
return 0;
}