generated from freudenreichan/Programmieren_3b
Compare commits
8
Commits
3e4d49fe26
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf9b1bc1dc | ||
|
|
b70424b24c | ||
|
|
b829d1b699 | ||
|
|
8f35533e85 | ||
|
|
1396ad6883 | ||
|
|
eb47581c1d | ||
|
|
8c28df46c3 | ||
|
|
726f3acfc8 |
No files matched your search
@@ -16,6 +16,7 @@ set(SRC_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/gamecube.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/gamematrix.cpp
|
||||
includes/gamestate.h
|
||||
src/seriell.cpp
|
||||
)
|
||||
#automatisch hinzufügen
|
||||
file(GLOB SRC_FILES "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp")
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <omp.h>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
// Zufallsmatrizen erzeugen
|
||||
void generateRandomMatrices(int n,
|
||||
std::vector<std::vector<double>>& A,
|
||||
std::vector<std::vector<double>>& B)
|
||||
{
|
||||
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
||||
A.resize(n, std::vector<double>(n));
|
||||
B.resize(n, std::vector<double>(n));
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
for (int j = 0; j < n; ++j)
|
||||
{
|
||||
A[i][j] = static_cast<double>(rand()) / RAND_MAX * 10.0;
|
||||
B[i][j] = static_cast<double>(rand()) / RAND_MAX * 10.0;
|
||||
}
|
||||
}
|
||||
|
||||
// Serielle Matrizenmultiplikation
|
||||
std::vector<std::vector<double>> matmul_serial(
|
||||
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();
|
||||
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Spaltenparallel (Column-parallel)
|
||||
std::vector<std::vector<double>> matmul_column_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();
|
||||
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
|
||||
|
||||
#pragma omp parallel for
|
||||
for (int j = 0; j < m; ++j) // Spalten parallel
|
||||
for (int i = 0; i < n; ++i)
|
||||
for (int k = 0; k < p; ++k)
|
||||
C[i][j] += A[i][k] * B[k][j];
|
||||
|
||||
return C;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
int main()
|
||||
{
|
||||
int N = 500;
|
||||
std::vector<std::vector<double>> A, B;
|
||||
generateRandomMatrices(N, 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::cout << "Serial multiplication took: "
|
||||
<< std::chrono::duration<double>(end - start).count() << "s\n";
|
||||
|
||||
// Spaltenparallel
|
||||
start = std::chrono::steady_clock::now();
|
||||
auto C_col = matmul_column_parallel(A, B);
|
||||
end = std::chrono::steady_clock::now();
|
||||
std::cout << "Column-parallel multiplication took: "
|
||||
<< std::chrono::duration<double>(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_col[i][j])
|
||||
{
|
||||
std::cout << "Mismatch at " << i << "," << j << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "All results match!\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <omp.h>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
// Zufallsmatrizen erzeugen
|
||||
void generateRandomMatrices(int n,
|
||||
std::vector<std::vector<double>>& A,
|
||||
std::vector<std::vector<double>>& B)
|
||||
{
|
||||
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
||||
A.resize(n, std::vector<double>(n));
|
||||
B.resize(n, std::vector<double>(n));
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
for (int j = 0; j < n; ++j)
|
||||
{
|
||||
A[i][j] = static_cast<double>(rand()) / RAND_MAX * 10.0;
|
||||
B[i][j] = static_cast<double>(rand()) / RAND_MAX * 10.0;
|
||||
}
|
||||
}
|
||||
|
||||
// Serielle Matrizenmultiplikation
|
||||
std::vector<std::vector<double>> matmul_serial(
|
||||
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();
|
||||
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Parallele Zeilenzerlegung
|
||||
std::vector<std::vector<double>> matmul_row_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();
|
||||
std::vector<std::vector<double>> C(n, std::vector<double>(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;
|
||||
std::vector<std::vector<double>> A, B;
|
||||
generateRandomMatrices(N, 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::cout << "Serial multiplication took: "
|
||||
<< std::chrono::duration<double>(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<double>(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;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 560 KiB |
@@ -0,0 +1,67 @@
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
// Funktion erzeugt zwei Matrizen A und B der Größe n x n mit Zufallswerten
|
||||
void generateRandomMatrices(int n,
|
||||
std::vector<std::vector<double>>& A,
|
||||
std::vector<std::vector<double>>& B,
|
||||
double min_val = 0.0,
|
||||
double max_val = 10.0)
|
||||
{
|
||||
// Zufallsgenerator initialisieren
|
||||
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
||||
|
||||
A.resize(n, std::vector<double>(n));
|
||||
B.resize(n, std::vector<double>(n));
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
for (int j = 0; j < n; ++j)
|
||||
{
|
||||
double r1 = static_cast<double>(std::rand()) / RAND_MAX; // 0..1
|
||||
double r2 = static_cast<double>(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);
|
||||
}
|
||||
}
|
||||
|
||||
// Matrizenmultiplikation
|
||||
std::vector<std::vector<double>> matmul(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));
|
||||
|
||||
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
|
||||
std::vector<std::vector<double>> A, B;
|
||||
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
|
||||
generateRandomMatrices(N, A, B);
|
||||
auto C_serial = matmul(A, B);
|
||||
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
std::chrono::duration<double> diff = end - start;
|
||||
std::cout << "Took: " << diff.count() << "s\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
//
|
||||
// Created by Anke Bidlingmaier on 15.12.25.
|
||||
|
||||
Reference in new issue
Block a user