This commit is contained in:
Georg Thomas Mandl 2025-12-10 17:13:48 +01:00
commit 71c9d9b858
4 changed files with 1434 additions and 0 deletions

30
Sheet_5/bsp_5_4/Makefile Normal file
View file

@ -0,0 +1,30 @@
#
# use GNU-Compiler tools
COMPILER=GCC_
# alternatively from the shell
# export COMPILER=GCC_
# or, alternatively from the shell
# make COMPILER=GCC_
# use Intel compilers
#COMPILER=ICC_
# use PGI compilers
# COMPILER=PGI_
SOURCES = main.cpp bsp_3_lib_bench_par.cpp
OBJECTS = $(SOURCES:.cpp=.o)
PROGRAM = main.${COMPILER}
# uncomment the next to lines for debugging and detailed performance analysis
CXXFLAGS += -g
LINKFLAGS += -g
# do not use -pg with PGI compilers
ifndef COMPILER
COMPILER=GCC_
endif
include ../${COMPILER}default.mk

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,288 @@
#pragma once
#include <omp.h>
#include <vector>
/** Inner product
@param[in] x vector
@param[in] y vector
@return resulting Euclidean inner product <x,y>
*/
double scalar(std::vector<double> const &x, std::vector<double> const &y);
/** Inner product - parallel
@param[in] x vector
@param[in] y vector
@return resulting Euclidean inner product <x,y>
*/
double scalar_par(std::vector<double> const &x, std::vector<double> const &y);
/** Inner product with cblas
@param[in] x vector
@param[in] y vector
@return resulting Euclidean inner product <x,y>
*/
double scalar_cblas(std::vector<double> const &x, std::vector<double> const &y);
/** Inner product with Kahan summation
@param[in] x vector
@param[in] y vector
@return resulting Euclidean inner product <x,y>
*/
double scalar_kahan(std::vector<double> const &x, std::vector<double> const &y);
/** euclidean norm
@param[in] x vector
@return resulting Euclidean norm
*/
double norm_eucl(std::vector<double> const &x);
/** sum of vector
@param[in] x vector
@return sum of the vector elements
*/
double sum(std::vector<double> const &x);
/** sum of vector - parallel
@param[in] x vector
@return sum of the vector elements
*/
double sum_par(std::vector<double> const &x);
/** \brief Matrix-Vektor-Multiplikation (row-wise access)
*
* \param[in] a Matrix with row wise access
* \param[in] x vector which gets multiplied
* \return resulting product a*x (vector)
*
*/
std::vector<double> MatVec(std::vector<double> const & a, std::vector<double> const & x);
/** \brief Matrix-Vektor-Multiplikation (row-wise access) - parallel
*
* \param[in] a Matrix with row wise access
* \param[in] x vector which gets multiplied
* \return resulting product a*x (vector)
*
*/
std::vector<double> MatVec_par(std::vector<double> const & a, std::vector<double> const & x);
/** \brief Matrix-Vektor-Multiplikation mit cblas (row-wise access)
*
* \param[in] a Matrix with row wise access
* \param[in] x vector which gets multiplied
* \return resulting product a*x (vector)
*
*/
std::vector<double> MatVec_cblas(std::vector<double> const & a, std::vector<double> const & x);
/** \brief Matrix-Vektor-Multiplikation (column-wise access)
*
* \param[in] a Matrix with row wise access
* \param[in] x vector which gets multiplied
* \return resulting product a*x (vector)
*
*/
std::vector<double> MatVec_column(std::vector<double> const & a, std::vector<double> const & x);
/** \brief Matrix-Matrix-Multiplikation (row-wise access)
*
* \param[in] a matrix with row wise access (M*L)
* \param[in] b matrix with row wise access (L*N)
* \param[in] L inner dimension of the matrix product
* \return resulting product a*b
*
*/
std::vector<double> MatMatProd(std::vector<double> const & a, std::vector<double> const & b, int const & L);
/** \brief Matrix-Matrix-Multiplikation (row-wise access) - parallel
*
* \param[in] a matrix with row wise access (M*L)
* \param[in] b matrix with row wise access (L*N)
* \param[in] L inner dimension of the matrix product
* \return resulting product a*b
*
*/
std::vector<double> MatMatProd_par(std::vector<double> const & a, std::vector<double> const & b, int const & L);
/** \brief Matrix-Matrix-Multiplikation mit cblas (row-wise access)
*
* \param[in] a matrix with row wise access (M*L)
* \param[in] b matrix with row wise access (L*N)
* \param[in] L inner dimension of the matrix product
* \return resulting product a*b
*
*/
std::vector<double> MatMatProd_cblas(std::vector<double> const & a, std::vector<double> const & b, int const & L);
/** \brief Polynomauswertung an Stelle x
*
* \param[in] a Vekor mit den Koeffizienten des Polynoms a=[a0,a1,a2,...]
* \param[in] x Vektor, für welchen das Polynom ausgewertet werden soll
* \return resulting vector p(x)
*
*/
std::vector<double> PolynomEval(std::vector<double> const & a, std::vector<double> const & x);
/** \brief Polynomauswertung an Stelle x - parallel
*
* \param[in] a Vekor mit den Koeffizienten des Polynoms a=[a0,a1,a2,...]
* \param[in] x Vektor, für welchen das Polynom ausgewertet werden soll
* \return resulting vector p(x)
*
*/
std::vector<double> PolynomEval_par(std::vector<double> const & a, std::vector<double> const & x);
/** \brief Benchmarking A - the scalar product
*
* \param N size of the vector
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_A(int const & N, int const & Nloops);
/** \brief Benchmarking A - the scalar product with cblas
*
* \param N size of the vector
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_A_cblas(int const & N, int const & Nloops);
/** \brief Benchmarking A - the scalar product with Kahan summation
*
* \param N size of the vector
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_A_kahan(int const & N, int const & Nloops);
/** \brief Benchmarking A - norm
*
* \param N size of the vector
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_A_norm(int const & N, int const & Nloops);
/** \brief Benchmarking B - matrix-vector product Ax=b (row wise access)
*
* \param N size of vector x
* \param M size of vector b (=> A: M*N)
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_B(int const & N, int const & M, int const & Nloops);
/** \brief Benchmarking B - matrix-vector product Ax=b (row wise access) [parallel]
*
* \param N size of vector x
* \param M size of vector b (=> A: M*N)
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_B_par(int const & N, int const & M, int const & Nloops);
/** \brief Benchmarking B - matrix-vector product Ax=b with cblas (row wise access)
*
* \param N size of vector x
* \param M size of vector b (=> A: M*N)
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_B_cblas(int const & N, int const & M, int const & Nloops);
/** \brief Benchmarking B - matrix-vector product Ax=b (column wise access)
*
* \param N size of vector x
* \param M size of vector b (=> A: M*N)
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_B_column(int const & N, int const & M, int const & Nloops);
/** \brief Benchmarking C - Matrix-Matrix product C=A*B A_M*L, B_L*N
*
* \param N
* \param M
* \param L
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_C(int const & N, int const & M, int const & L, int const & Nloops);
/** \brief Benchmarking C - Matrix-Matrix product C=A*B A_M*L, B_L*N [parallel]
*
* \param N
* \param M
* \param L
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_C_par(int const & N, int const & M, int const & L, int const & Nloops);
/** \brief Benchmarking C - Matrix-Matrix product with cblas; C=A*B A_M*L, B_L*N
*
* \param N
* \param M
* \param L
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_C_cblas(int const & N, int const & M, int const & L, int const & Nloops);
/** \brief Benchmarking D - polynomial evaluation
*
* \param p the degree of the polynomial
* \param N size of the input vector x where p(x)
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_D(int const & p, int const & N, int const & Nloops);
/** \brief Benchmarking D - polynomial evaluation [parallel]
*
* \param p the degree of the polynomial
* \param N size of the input vector x where p(x)
* \param Nloops number of iterations we want to do for the measuring
*
*/
void benchmark_D_par(int const & p, int const & N, int const & Nloops);
/** \brief solving system of linear equations with time measurement
*
* \param N size of the system Ax=b with A(nxn)
* \param nrhs number of right hand sides b(nxnrhs)
*
*/
void solver(int const & N, int const & nrhs);

47
Sheet_5/bsp_5_4/main.cpp Normal file
View file

@ -0,0 +1,47 @@
#include "bsp_3_lib_bench_par.h"
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
benchmark_B(8000,8000,400);
benchmark_B_par(8000,8000,400);
benchmark_C(4000,4000,4000,1);
benchmark_C_par(4000,4000,4000,1);
benchmark_D(1e4,1e5,15);
benchmark_D_par(1e4,1e5,15);
// comparing the time for sum and inner product with and without parallelization
cout << "\nComparing the runtime (in sec) for inner product and sum with and without parallelization\n\n";
for(int k=3; k<=8; ++k)
{
int N = pow(10,k);
cout << "k = " << k << " N = " << N << endl;
vector<double> v1(N,1.0/N);
vector<double> v2(N,N);
auto tstart = omp_get_wtime();
double s = scalar(v1, v2);
auto t_diff_scalar = omp_get_wtime() - tstart;
tstart = omp_get_wtime();
double sp = scalar_par(v1, v2);
auto t_diff_scalar_par = omp_get_wtime() - tstart;
tstart = omp_get_wtime();
double su = sum(v1);
auto t_diff_sum = omp_get_wtime() - tstart;
tstart = omp_get_wtime();
double sup = sum_par(v1);
auto t_diff_sum_par = omp_get_wtime() - tstart;
cout << "sum " << t_diff_sum << " inner_prod " << t_diff_scalar << endl;
cout << "sum_par " << t_diff_sum_par << " inner_prod_par " << t_diff_scalar_par << endl << endl;;
}
return 0;
}