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

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.

Binary file not shown.

View file

@ -11,7 +11,7 @@ void vddiv(vector<double> &x, vector<double> const &y,
{
assert( x.size() == y.size() && y.size() == z.size() );
size_t n = x.size();
#pragma omp parallel for
for (size_t k = 0; k < n; ++k)
{
x[k] = y[k] / z[k];
@ -26,7 +26,7 @@ void vdaxpy(std::vector<double> &x, std::vector<double> const &y,
{
assert( x.size() == y.size() && y.size() == z.size() );
size_t n = x.size();
#pragma omp parallel for
for (size_t k = 0; k < n; ++k)
{
x[k] = y[k] + alpha * z[k];
@ -41,6 +41,7 @@ double dscapr(std::vector<double> const &x, std::vector<double> const &y)
size_t n = x.size();
double s = 0.0;
#pragma omp parallel for reduction(+:s)
for (size_t k = 0; k < n; ++k)
{
s += x[k] * y[k];

Binary file not shown.