Task 5, 5* some fixes and cleanup

This commit is contained in:
Markus Schmidt 2025-12-09 23:33:08 +01:00
commit c8bf307391
154 changed files with 214851 additions and 93 deletions

42
clean_all.sh Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -e
echo "Searching for Makefiles..."
# Find all directories containing a Makefile
find . -type f \( -iname "makefile" -o -iname "Makefile" \) | while read -r mk; do
dir=$(dirname "$mk")
# Check if Makefile contains a clean target
if grep -qE '^[[:space:]]*clean[: ]' "$mk"; then
echo "→ Found clean target in: $dir"
echo " Running make clean..."
(cd "$dir" && make clean)
echo " Done."
else
echo "→ No clean target in: $dir"
fi
done
echo ""
echo "─────────────────────────────"
echo "Deleting extra files"
echo "─────────────────────────────"
echo "→ Deleting .Identifier files"
find . -type f -name ".Identifier" -print -delete
echo "→ Deleting *:Zone.Identifier files"
find . -type f -name "*:Zone.Identifier" -print -delete
echo "→ Deleting all .o object files"
find . -type f -name "*.o" -print -delete
echo "→ Deleting all .GCC_ files"
find . -type f -name "*.GCC_" -print -delete
echo ""
echo "Cleanup complete."

Binary file not shown.

Binary file not shown.

View file

@ -1,3 +0,0 @@
[ZoneTransfer]
ZoneId=3
HostUrl=https://imsc.uni-graz.at/haasegu/Lectures/Math2CPP/Examples/goldbach/mayer_primes.h

Binary file not shown.

View file

@ -10,7 +10,7 @@
#include <cmath>
using namespace std;
void benchmark(vector<double> &x, vector<double> &y, unsigned int N, unsigned int NLOOPS)
void benchmark(vector<double> &x, vector<double> &y, unsigned int NLOOPS)
{
double sk = 0.0;
for (int i = 0; i < NLOOPS; ++i)
@ -92,8 +92,7 @@ int main(int argc, char const *argv[])
for (int i = 0; i < NLOOPS; ++i)
{
sk = scalar(x, y);
sk = scalar_trans(x, y);
//sk = norm(x);
sk = norm(x);
}
double t1 = omp_get_wtime() - tstart; // OpenMP
@ -124,7 +123,7 @@ int main(int argc, char const *argv[])
cout << "done\n";
cout << vr << endl;
N=2;
N=200;
//Data (re-)inizialiion
for (unsigned int i = 0; i < N; ++i)
{
@ -147,9 +146,9 @@ int main(int argc, char const *argv[])
omp_set_schedule(omp_sched_static, 0);
tstart = omp_get_wtime();
benchmark(x, y, N, NLOOPS);
t1 = omp_get_wtime()/NLOOPS;
cout << "static (chunk 0) "<< (t1-tstart) << endl;
benchmark(x, y, NLOOPS);
t1 = (omp_get_wtime()-tstart)/NLOOPS;
cout << "static (chunk 0) "<< t1 << endl;
for(int i=0; i<= 5; i++)
{
@ -159,30 +158,30 @@ int main(int argc, char const *argv[])
// STATIC
omp_set_schedule(omp_sched_static, chunk);
tstart = omp_get_wtime();
benchmark(x, y, N, NLOOPS);
t1 = omp_get_wtime()/NLOOPS;
std::cout << "static: " << (t1 - tstart) << " s\n";
benchmark(x, y, NLOOPS);
t1 = (omp_get_wtime()-tstart)/NLOOPS;
std::cout << "static: "<< t1 << " s\n";
// DYNAMIC
omp_set_schedule(omp_sched_dynamic, chunk);
tstart = omp_get_wtime();
benchmark(x, y, N, NLOOPS);
t1 = omp_get_wtime()/NLOOPS;
std::cout << "dynamic: " << (t1 - tstart) << " s\n";
benchmark(x, y, NLOOPS);
t1 = (omp_get_wtime()-tstart)/NLOOPS;
std::cout << "dynamic: "<< t1 << " s\n";
// GUIDED
omp_set_schedule(omp_sched_guided, chunk);
tstart = omp_get_wtime();
benchmark(x, y, N, NLOOPS);
t1 = omp_get_wtime()/NLOOPS;
std::cout << "guided: " << (t1 - tstart) << " s\n";
benchmark(x, y, NLOOPS);
t1 = (omp_get_wtime()-tstart)/NLOOPS;
std::cout << "guided: "<< t1 << " s\n";
// AUTO
omp_set_schedule(omp_sched_auto, chunk);
tstart = omp_get_wtime();
benchmark(x, y, N, NLOOPS);
t1 = omp_get_wtime()/NLOOPS;
std::cout << "auto: " << (t1 - tstart) << " s\n";
benchmark(x, y, NLOOPS);
t1 = (omp_get_wtime()-tstart)/NLOOPS;
std::cout << "auto: "<< t1 << " s\n";
cout << endl;
}
cout << endl;
@ -194,7 +193,9 @@ int main(int argc, char const *argv[])
vector<int> vec = reduction_vec_append(N);
for(int i=0; i< N; i++)
unsigned int n = vec.size();
cout << "vec.size() = " << n << "\n";
for(int i=0; i< n; i++)
{
cout << vec[i] << ", ";
}

Binary file not shown.

View file

@ -91,7 +91,7 @@ vector<int> reduction_vec(int n)
vector<int> reduction_vec_append(int n)
{
vector<int> vec(n);
vector<int> vec;
#pragma omp parallel default(none) shared(cout,n) reduction(VecAppend:vec)
{
int tid = omp_get_thread_num();
@ -99,10 +99,7 @@ vector<int> reduction_vec_append(int n)
vector<int> local(n);
iota(local.begin(), local.end(), tid);
#pragma omp critical
cout << tid << " : " << local.size() << endl;
vec = local;
vec = local;
}
return vec;

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1,3 +0,0 @@
[ZoneTransfer]
ZoneId=3
HostUrl=https://imsc.uni-graz.at/haasegu/Lectures/Math2CPP/Examples/goldbach/mayer_primes.h

Binary file not shown.

Binary file not shown.

View file

@ -183,50 +183,63 @@ int main(int argc, char **argv)
const int NLOOPS = 20;
for (int k = 3; k <= 8; ++k)
{
unsigned int n = pow(10.0, k);
vector<double> x(n), y(n);
for (unsigned int i = 0; i < n; ++i)
{
size_t n = (size_t)pow(10, k);
vector<double> x(n), y(n);
for (unsigned int i = 0; i < n; ++i)
{
double xi= (i % 219) + 1;
x[i] = xi;
y[i] = 1.0 / xi;
}
// ---- SUM benchmark (sequential) ----
double t0 = omp_get_wtime();
double s1 = benchmark_A_sum_old(x);
double t_sum_seq = omp_get_wtime() - t0;
// ---- SUM benchmark (parallel) ----
t0 = omp_get_wtime();
double s2 = benchmark_A_sum(x);
double t_sum_omp = omp_get_wtime() - t0;
double sum_speedup = t_sum_seq / t_sum_omp;
// ---- INNER PRODUCT benchmark (sequential) ----
t0 = omp_get_wtime();
double ip1 = benchmark_A_old(x, y);
double t_inner_seq = omp_get_wtime() - t0;
// ---- INNER PRODUCT benchmark (parallel) ----
t0 = omp_get_wtime();
double ip2 = benchmark_A(x, y);
double t_inner_omp = omp_get_wtime() - t0;
double inner_speedup = t_inner_seq / t_inner_omp;
// ---- Print results ----
cout << k << endl;
cout << t_sum_seq << ", " << t_sum_omp << ", " << sum_speedup << endl;
cout << t_inner_seq << ", " << t_inner_omp << ", " << inner_speedup << endl;
cout << endl;
double xi = (i % 219) + 1;
x[i] = xi;
y[i] = 1.0 / xi;
}
double s1_guard = 0.0, s2_guard = 0.0;
double ip1_guard = 0.0, ip2_guard = 0.0;
// ---- SUM benchmark (sequential) ----
double t0 = omp_get_wtime();
for (int r = 0; r < NLOOPS; ++r)
s1_guard += benchmark_A_sum_old(x);
double t_sum_seq = (omp_get_wtime() - t0) / NLOOPS;
// ---- SUM benchmark (parallel) ----
t0 = omp_get_wtime();
for (int r = 0; r < NLOOPS; ++r)
s2_guard += benchmark_A_sum(x);
double t_sum_omp = (omp_get_wtime() - t0) / NLOOPS;
double sum_speedup = t_sum_seq / t_sum_omp;
// ---- INNER PRODUCT benchmark (sequential) ----
t0 = omp_get_wtime();
for (int r = 0; r < NLOOPS; ++r)
ip1_guard += benchmark_A_old(x, y);
double t_inner_seq = (omp_get_wtime() - t0) / NLOOPS;
// ---- INNER PRODUCT benchmark (parallel) ----
t0 = omp_get_wtime();
for (int r = 0; r < NLOOPS; ++r)
ip2_guard += benchmark_A(x, y);
double t_inner_omp = (omp_get_wtime() - t0) / NLOOPS;
double inner_speedup = t_inner_seq / t_inner_omp;
// ---- Print results ----
std::cout << "k = " << k << " (n = 10^" << k << " = " << n << ")\n";
std::cout << "SUM seq: " << t_sum_seq << " s, omp: " << t_sum_omp
<< " s, speedup = " << sum_speedup << '\n';
std::cout << "INNER seq: " << t_inner_seq << " s, omp: " << t_inner_omp
<< " s, speedup = " << inner_speedup << '\n';
std::cout << "guards: "
<< s1_guard << ", " << s2_guard << ", "
<< ip1_guard << ", " << ip2_guard << "\n\n";
}
return 0;

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -8,6 +8,7 @@
#include <iostream>
#include <list>
#include <vector>
#include <omp.h>
using namespace std;
@ -168,33 +169,64 @@ void CRS_Matrix::Debug() const
void CRS_Matrix::CalculateLaplace(vector<double> &f)
{
double t_start = omp_get_wtime();
double t0 ,t1,t2;
assert(_mesh.NdofsElement() == 3); // only for triangular, linear elements
//cout << _nnz << " vs. " << _id[_nrows] << " " << _nrows<< endl;
assert(_nnz == _id[_nrows]);
for (int k = 0; k < _nrows; ++k)
#pragma omp parallel
{
_sk[k] = 0.0;
}
for (int k = 0; k < _nrows; ++k)
{
f[k] = 0.0;
}
#pragma omp for
for (int k = 0; k < _nrows; ++k)
{
_sk[k] = 0.0;
}
double ske[3][3], fe[3];
// Loop over all elements
auto const nelem = _mesh.Nelems();
auto const &ia = _mesh.GetConnectivity();
auto const &xc = _mesh.GetCoords();
#pragma omp barrier
#pragma omp single
t0 = omp_get_wtime();
#pragma omp for
for (int k = 0; k < _nrows; ++k)
{
f[k] = 0.0;
}
#pragma omp barrier
#pragma omp single
t1 = omp_get_wtime();
double ske[3][3], fe[3];
// Loop over all elements
auto const nelem = _mesh.Nelems();
auto const &ia = _mesh.GetConnectivity();
auto const &xc = _mesh.GetCoords();
#pragma omp barrier
#pragma omp single
t2 = omp_get_wtime();
#pragma omp for
for (int i = 0; i < nelem; ++i)
{
CalcElem(ia.data() + 3 * i, xc.data(), ske, fe);
AddElem_3(ia.data() + 3 * i, ske, fe, f);
}
//Debug();
for (int i = 0; i < nelem; ++i)
{
CalcElem(ia.data() + 3 * i, xc.data(), ske, fe);
AddElem_3(ia.data() + 3 * i, ske, fe, f);
}
//Debug();
double t3 = omp_get_wtime();
cout << "Zero matrix: " << (t0 - t_start) << " sec\n";
cout << "Zero RHS: " << (t1 - t0) << " sec\n";
cout << "Element loop: " << (t3 - t2) << " sec\n";
cout << "Total assembly:" << (t3 - t_start) << " sec\n";
return;
}
void CRS_Matrix::ApplyDirichletBC(std::vector<double> const &u, std::vector<double> &f)
@ -286,7 +318,8 @@ void CRS_Matrix::Defect(vector<double> &w,
{
assert( _nrows == static_cast<int>(w.size()) );
assert( w.size() == u.size() && u.size() == f.size() );
#pragma omp parallel for
for (int row = 0; row < _nrows; ++row)
{
double wi = f[row];
@ -340,8 +373,10 @@ void CRS_Matrix::AddElem_3(int const ial[3], double const ske[3][3], double cons
assert(ip >= 0);
}
#endif
#pragma omp atomic
_sk[ip] += ske[i][j];
}
#pragma omp atomic
f[ii] += fe[i];
}
}

Binary file not shown.

View file

@ -23,6 +23,7 @@ void JacobiSolve(CRS_Matrix const &SK, vector<double> const &f, vector<double> &
cout << endl << " Start Jacobi solver for " << nrows << " d.o.f.s" << endl;
// Choose initial guess
#pragma omp parallel for
for (int k = 0; k < nrows; ++k)
{
u[k] = 0.0; // u := 0
@ -44,6 +45,7 @@ void JacobiSolve(CRS_Matrix const &SK, vector<double> const &f, vector<double> &
// Iteration sweeps
int iter = 0;
double sigma = sigma0;
// i dont parallelize this cause its iteration dependent
while ( sigma > tol2 * sigma0 && maxiter > iter)
{
++iter;

Binary file not shown.

Binary file not shown.

View file

@ -11,6 +11,7 @@
#include <chrono> // timing
#include <cmath>
#include <iostream>
#include <omp.h>
using namespace std;
using namespace std::chrono; // timing
@ -19,7 +20,6 @@ int main(int, char ** )
{
const int numprocs = 1;
const int myrank = 0;
if (myrank == 0)
{
cout << "\n There are " << numprocs << " processes running.\n \n";
@ -121,6 +121,7 @@ int main(int, char ** )
//mesh.Write_ascii_matlab("uv.txt", uv);
//mesh.Visualize(uv);
}
return 0;
}

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show more