initial
This commit is contained in:
parent
9102188226
commit
bb127c731d
@ -3,4 +3,5 @@ project(threads)
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
add_executable(threads main.cpp)
|
add_executable(threads matrix_main.cpp
|
||||||
|
)
|
||||||
|
|||||||
@ -4,6 +4,8 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <omp.h>
|
||||||
|
|
||||||
|
|
||||||
// Funktion erzeugt zwei Matrizen A und B der Größe n x n mit Zufallswerten
|
// Funktion erzeugt zwei Matrizen A und B der Größe n x n mit Zufallswerten
|
||||||
void generateRandomMatrices(int n,
|
void generateRandomMatrices(int n,
|
||||||
@ -49,6 +51,27 @@ std::vector<std::vector<double>> matmul(const std::vector<std::vector<double>>&
|
|||||||
return C;
|
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 main()
|
||||||
{
|
{
|
||||||
int N = 500; // z.B. 500x500
|
int N = 500; // z.B. 500x500
|
||||||
@ -62,5 +85,11 @@ int main()
|
|||||||
std::chrono::duration<double> diff = end - start;
|
std::chrono::duration<double> diff = end - start;
|
||||||
std::cout << "Serial Took: " << diff.count() << "s\n";
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user