Ex8 and minor improvements

This commit is contained in:
Markus Schmidt 2025-11-12 02:04:18 +01:00
commit 77bc8c6aa3
50 changed files with 214843 additions and 41 deletions

60
sheet3/1/output.txt Normal file
View file

@ -0,0 +1,60 @@
rm -f *.exe *.o
gcc -O3 -c -o mysecond.o mysecond.c
gcc -O3 -c mysecond.c
gfortran -O3 -DSTREAM_ARRAY_SIZE=80000000 -DNTIMES=20 -c stream.f
gfortran -O3 stream.o mysecond.o -o stream_f.exe
gcc -O3 -DSTREAM_ARRAY_SIZE=80000000 -DNTIMES=20 stream.c -o stream_c.exe
gcc -O3 -DUNIX flops.c -o flops.exe
./stream_c.exe
-------------------------------------------------------------
STREAM version $Revision: 5.10 $
-------------------------------------------------------------
This system uses 8 bytes per array element.
-------------------------------------------------------------
Array size = 80000000 (elements), Offset = 0 (elements)
Memory per array = 610.4 MiB (= 0.6 GiB).
Total memory required = 1831.1 MiB (= 1.8 GiB).
Each kernel will be executed 20 times.
The *best* time for each kernel (excluding the first iteration)
will be used to compute the reported bandwidth.
-------------------------------------------------------------
Your clock granularity/precision appears to be 1 microseconds.
Each test below will take on the order of 59858 microseconds.
(= 59858 clock ticks)
Increase the size of the arrays if this shows that
you are not getting at least 20 clock ticks per test.
-------------------------------------------------------------
WARNING -- The above is only a rough guideline.
For best results, please be sure you know the
precision of your system timer.
-------------------------------------------------------------
Function Best Rate MB/s Avg time Min time Max time
Copy: 23508.2 0.063228 0.054449 0.074427
Scale: 18323.7 0.089940 0.069855 0.116932
Add: 19762.6 0.276166 0.097153 3.054857
Triad: 19559.9 0.123390 0.098160 0.156530
-------------------------------------------------------------
Solution Validates: avg error less than 1.000000e-13 on all three arrays
-------------------------------------------------------------
./flops.exe
FLOPS C Program (Double Precision), V2.0 18 Dec 1992
Module Error RunTime MFLOPS
(usec)
1 4.0146e-13 0.0029 4831.3737
2 -1.4166e-13 0.0006 11258.2969
3 4.7184e-14 0.0031 5448.3769
4 -1.2557e-13 0.0030 5042.5895
5 -1.3800e-13 0.0060 4867.7339
6 3.2380e-13 0.0054 5322.4399
7 -8.4583e-11 0.0031 3907.7854
8 3.4867e-13 0.0056 5323.6214
Iterations = 512000000
NullTime (usec) = 0.0000
MFLOPS(1) = 8348.0311
MFLOPS(2) = 4650.8807
MFLOPS(3) = 5016.3434
MFLOPS(4) = 5297.2428

View file

@ -82,8 +82,12 @@ vector<double> benchmark_D(const vector<double>& coeff, const vector<double>& x)
double norm2(const vector<double>& x) double norm2(const vector<double>& x)
{ {
double s = 0.0; double s = 0.0;
for (unsigned int i = 0; i < x.size(); ++i) double xi;
s += x[i]*x[i]; for (unsigned int i = 0; i < x.size(); ++i){
xi = x[i];
s += xi*xi;
}
return sqrt(s); return sqrt(s);
} }
@ -116,7 +120,7 @@ vector<double> matrixMultColumnWise(const vector<double> &A, const vector<double
double sum = 0.0; double sum = 0.0;
for (unsigned int k = 0; k < L; k++) for (unsigned int k = 0; k < L; k++)
{ {
sum += A[k*L+i]*B[k*N+j]; sum += A[k*M+i]*B[k*N+j];
} }
C[i*N+j] = sum; C[i*N+j] = sum;
} }

View file

@ -131,6 +131,7 @@ int main(int argc, char **argv)
double bytesC = (MC * LC + LC * NC + MC * NC)* sizeof(double); double bytesC = (MC * LC + LC * NC + MC * NC)* sizeof(double);
cout << "\n===== Benchmark C =====\n"; cout << "\n===== Benchmark C =====\n";
cout << guardC << endl;
cout << "bytes: " << bytesC << endl; cout << "bytes: " << bytesC << endl;
cout << "Timing in sec. : " << tC << endl; cout << "Timing in sec. : " << tC << endl;
cout << "GFLOPS : " << flopsC / tC / 1024 / 1024 / 1024 << endl; cout << "GFLOPS : " << flopsC / tC / 1024 / 1024 / 1024 << endl;
@ -172,6 +173,7 @@ int main(int argc, char **argv)
double bytesD = (p + 2 * ND)*sizeof(double); double bytesD = (p + 2 * ND)*sizeof(double);
cout << "\n===== Benchmark D =====\n"; cout << "\n===== Benchmark D =====\n";
cout << guardD << endl;
cout << "bytes: " << bytesD << endl; cout << "bytes: " << bytesD << endl;
cout << "Timing in sec. : " << tD << endl; cout << "Timing in sec. : " << tD << endl;
cout << "GFLOPS : " << flopsD / tD / 1024 / 1024 / 1024 << endl; cout << "GFLOPS : " << flopsD / tD / 1024 / 1024 / 1024 << endl;
@ -208,6 +210,8 @@ int main(int argc, char **argv)
cout << "GFLOPS : " << 2.0 * NA / tA / 1024 / 1024 / 1024 << endl; cout << "GFLOPS : " << 2.0 * NA / tA / 1024 / 1024 / 1024 << endl;
cout << "GiByte/s : " cout << "GiByte/s : "
<< NA * sizeof(xA[0]) / tA / 1024 / 1024 / 1024 << endl; << NA * sizeof(xA[0]) / tA / 1024 / 1024 / 1024 << endl;
//a bit faster due to only accessing one vector
} }
@ -240,6 +244,8 @@ int main(int argc, char **argv)
cout << "GFLOPS : " << 5.0 * NA / tA / 1024 / 1024 / 1024 << endl; cout << "GFLOPS : " << 5.0 * NA / tA / 1024 / 1024 / 1024 << endl;
cout << "GiByte/s : " cout << "GiByte/s : "
<< 2.0 * NA * sizeof(xA[0]) / tA / 1024 / 1024 / 1024 << endl; << 2.0 * NA * sizeof(xA[0]) / tA / 1024 / 1024 / 1024 << endl;
//in comparison to benchmark A: a bit slower runtime but more than double the amount of FLOPS therefor also more GFLOPS
} }
@ -276,11 +282,16 @@ int main(int argc, char **argv)
double bytesC = (MC * LC + LC * NC + MC * NC)* sizeof(double); double bytesC = (MC * LC + LC * NC + MC * NC)* sizeof(double);
cout << "\n===== Benchmark 5C =====\n"; cout << "\n===== Benchmark 5C =====\n";
cout << guardC << endl;
cout << "bytes: " << bytesC << endl; cout << "bytes: " << bytesC << endl;
cout << "Timing in sec. : " << tC << endl; cout << "Timing in sec. : " << tC << endl;
cout << "GFLOPS : " << flopsC / tC / 1024 / 1024 / 1024 << endl; cout << "GFLOPS : " << flopsC / tC / 1024 / 1024 / 1024 << endl;
cout << "GiByte/s : " << bytesC / tC / 1024 / 1024 / 1024 << endl; cout << "GiByte/s : " << bytesC / tC / 1024 / 1024 / 1024 << endl;
//slower than rowwise access, due to incoherent acces in the vector memory of A
//Transpose matrix, the it is also row wise-access or reorder loops
} }
return 0; return 0;
} // memory for x and y will be deallocated by their destructors }

51
sheet3/345/output.txt Normal file
View file

@ -0,0 +1,51 @@
g++ -c -g -O0 -funroll-all-loops -std=c++17 -Wall -pedantic -Wextra -Weffc++ -Woverloaded-virtual -Wfloat-equal -Wshadow -Wredundant-decls -Winline -fmax-errors=1 -flto -o main.o main.cpp
g++ -c -g -O0 -funroll-all-loops -std=c++17 -Wall -pedantic -Wextra -Weffc++ -Woverloaded-virtual -Wfloat-equal -Wshadow -Wredundant-decls -Winline -fmax-errors=1 -flto -o mylib.o mylib.cpp
g++ -c -g -O0 -funroll-all-loops -std=c++17 -Wall -pedantic -Wextra -Weffc++ -Woverloaded-virtual -Wfloat-equal -Wshadow -Wredundant-decls -Winline -fmax-errors=1 -flto -o benchmark.o benchmark.cpp
g++ main.o mylib.o benchmark.o -g -O0 -llapack -lblas -flto -o main.GCC_
./main.GCC_
===== Benchmark A =====
<xA,yA> = 1.4e+06
Timing in sec. : 0.00893637
GFLOPS : 0.291808
GiByte/s : 2.33446
===== Benchmark B =====
340000
bytes: 2.31472e+07
Timing in sec. : 0.0133897
GFLOPS : 0.402029
GiByte/s : 1.61001
===== Benchmark C =====
7.37196e+07
bytes: 2.4e+07
Timing in sec. : 8.67235
GFLOPS : 0.21478
GiByte/s : 0.00257736
===== Benchmark D =====
10500
bytes: 3.20001e+07
Timing in sec. : 0.101087
GFLOPS : 0.515935
GiByte/s : 0.294821
===== Benchmark 5A =====
NORM = 150114
Timing in sec. : 0.00703533
GFLOPS : 0.370658
GiByte/s : 1.48263
===== Benchmark 5B =====
<xA,yA> = 1.4e+06
Timing in sec. : 0.0108377
GFLOPS : 0.601533
GiByte/s : 1.92491
===== Benchmark 5C =====
7.37196e+07
bytes: 2.4e+07
Timing in sec. : 15.2407
GFLOPS : 0.122215
GiByte/s : 0.00146658

View file

@ -139,4 +139,4 @@ int main(int argc, char **argv)
return 0; return 0;
} // memory for x and y will be deallocated by their destructors }

24
sheet3/6/output.txt Normal file
View file

@ -0,0 +1,24 @@
g++ -c -g -O0 -funroll-all-loops -std=c++17 -Wall -pedantic -Wextra -Weffc++ -Woverloaded-virtual -Wfloat-equal -Wshadow -Wredundant-decls -Winline -fmax-errors=1 -flto -o main.o main.cpp
g++ -c -g -O0 -funroll-all-loops -std=c++17 -Wall -pedantic -Wextra -Weffc++ -Woverloaded-virtual -Wfloat-equal -Wshadow -Wredundant-decls -Winline -fmax-errors=1 -flto -o mylib.o mylib.cpp
g++ -c -g -O0 -funroll-all-loops -std=c++17 -Wall -pedantic -Wextra -Weffc++ -Woverloaded-virtual -Wfloat-equal -Wshadow -Wredundant-decls -Winline -fmax-errors=1 -flto -o benchmark.o benchmark.cpp
g++ main.o mylib.o benchmark.o -g -O0 -llapack -lblas -flto -o main.GCC_
./main.GCC_
===== Benchmark A =====
<xA,yA> = 1.4e+06
Timing in sec. : 0.000900578
GFLOPS : 2.89559
GiByte/s : 23.1647
===== Benchmark B =====
1.7e+07
bytes: 2.31472e+07
Timing in sec. : 0.000687268
GFLOPS : 7.83252
GiByte/s : 31.3669
===== Benchmark C =====
bytes: 2.4e+07
Timing in sec. : 0.0151789
GFLOPS : 122.713
GiByte/s : 1.47255

View file

@ -13,8 +13,7 @@ COMPILER=GCC_
# COMPILER=PGI_ # COMPILER=PGI_
SOURCES = main.cpp benchmark.cpp
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o) OBJECTS = $(SOURCES:.cpp=.o)
PROGRAM = main.${COMPILER} PROGRAM = main.${COMPILER}

43
sheet3/7/benchmark.cpp Normal file
View file

@ -0,0 +1,43 @@
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#include <cblas.h>
// Inner product
double benchmark_A(const vector<double> &x, const vector<double> &y)
{
return cblas_ddot(x.size(),x.data(),1,y.data(),1);
}
//Matrix-vector product
vector<double> benchmark_B(const vector<double> &A, const vector<double> &x)
{
unsigned int N = x.size();
unsigned int M = A.size() / N;
vector<double> b(M, 0.0);
cblas_dgemv(CblasRowMajor,CblasNoTrans,M,N,1,A.data(),N,x.data(),1,0.0,b.data(),1);
return b;
}
//Matrix-Matrix product
vector<double> benchmark_C(const vector<double> &A, const vector<double> &B, unsigned int M)
{
unsigned int L = A.size()/M;
unsigned int N = B.size()/L;
vector<double> C(M*N,0.0);
cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,M,N,L,1.0,A.data(),L,B.data(),N,0.0,C.data(),N);
return C;
}

21
sheet3/7/benchmark.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef BENCHMARK_H
#define BENCHMARK_H
#include <vector>
using namespace std;
double benchmark_A(const vector<double> &x,
const vector<double> &y);
vector<double> benchmark_B(const vector<double> &A,
const vector<double> &x);
vector<double> benchmark_C(const vector<double> &A,
const vector<double> &B,
unsigned int M);
#endif

Binary file not shown.

View file

@ -8,17 +8,17 @@
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <lapacke.h> #include <lapacke.h>
#include "timing.h"
#include "benchmark.h"
using namespace std; using namespace std;
using namespace std::chrono; // timing using namespace std::chrono; // timing
int main() int main()
{ {
unsigned int n= 10; unsigned int n= 32;
unsigned int nhrs = 1;
vector<double> M(n*n,4.0); vector<double> M(n*n,4.0);
for(unsigned int i=0; i<n; i++) for(unsigned int i=0; i<n; i++)
{ {
for(unsigned int j=0; j<n; j++) for(unsigned int j=0; j<n; j++)
@ -32,30 +32,77 @@ int main()
} }
vector<double> M2 = M;
vector<int> ipiv(n); //pivots vector<int> ipiv(n); //pivots
LAPACKE_dgetrf(LAPACK_ROW_MAJOR,n,n, M.data(),n,ipiv.data()); //M=PLU LAPACKE_dgetrf(LAPACK_ROW_MAJOR,n,n, M.data(),n,ipiv.data()); //M=PLU
unsigned int runtimes[] = {1,2,4,8,16,32};
for(unsigned int i=0; i < 6;i++) double time;
unsigned int nhrsmax = 1000000;
for(unsigned int i=nhrsmax/10; i < nhrsmax;i+=nhrsmax/10)
{ {
nhrs = runtimes[i];
vector<double> b(n*nhrs,0.0); unsigned int nhrs = i;
for (unsigned int j=0; j<n; j++)
{
for (unsigned int k=0; k<nhrs; k++)
{
b[j*nhrs+k] = j*nhrs+k;
}
}
LAPACKE_dgetrs(LAPACK_ROW_MAJOR,'N',n,nhrs,M.data(),n,ipiv.data(),b.data(),nhrs);
}
//FOR CHECKING
vector<double> X(n*nhrs,1.0);
vector<double> b = benchmark_C(M2,X,n);
tic();
LAPACKE_dgetrs(LAPACK_ROW_MAJOR,'N',n,nhrs,M.data(),n,ipiv.data(),b.data(),nhrs);
time = toc();
cout << "Time for nhrs=" << nhrs << ": " << time << endl;
double max_err = 0.0;
for (unsigned int j = 0; j < n * nhrs; j++)
{
double err = b[j] - X[j];
err *= err;
if (err > max_err) max_err = err;
}
cout <<"max err^2:" << max_err <<endl;
cout <<endl;
}
/*
Time for nhrs=100000: 0.0605495
max err^2:4.93038e-32
Time for nhrs=200000: 0.127608
max err^2:4.93038e-32
Time for nhrs=300000: 0.182197
max err^2:4.93038e-32
Time for nhrs=400000: 0.202608
max err^2:4.93038e-32
Time for nhrs=500000: 0.24484
max err^2:4.93038e-32
Time for nhrs=600000: 0.298055
max err^2:4.93038e-32
Time for nhrs=700000: 0.362414
max err^2:4.93038e-32
Time for nhrs=800000: 0.410004
max err^2:4.93038e-32
Time for nhrs=900000: 0.492339
max err^2:4.93038e-32
Time grows slow (linearly)
*/

Binary file not shown.

51
sheet3/7/timing.h Normal file
View file

@ -0,0 +1,51 @@
//
// Gundolf Haase, Oct 18 2024
//
#pragma once
#include <chrono> // timing
#include <stack>
//using Clock = std::chrono::system_clock; //!< The wall clock timer chosen
using Clock = std::chrono::high_resolution_clock;
using TPoint= std::chrono::time_point<Clock>;
// [Galowicz, C++17 STL Cookbook, p. 29]
std::stack<TPoint> MyStopWatch; //!< starting time of stopwatch
/** Starts stopwatch timer.
* Use as @code tic(); myfunction(...) ; double tsec = toc(); @endcode
*
* The timining can be nested and the recent time point is stored on top of the stack.
*
* @return recent time point
* @see toc
*/
auto tic()
{
MyStopWatch.push(Clock::now());
return MyStopWatch.top();
}
/** Returns the elapsed time from stopwatch.
*
* The time point from top of the stack is used
* if time point @p t_b is not passed as input parameter.
* Use as @code tic(); myfunction(...) ; double tsec = toc(); @endcode
* or as @code auto t_b = tic(); myfunction(...) ; double tsec = toc(t_b); @endcode
* The last option is to be used in the case of
* non-nested but overlapping time measurements.
*
* @param[in] t_b start time of some stop watch
* @return elapsed time in seconds.
*
*/
double toc(TPoint const &t_b = MyStopWatch.top())
{
// https://en.cppreference.com/w/cpp/chrono/treat_as_floating_point
using Unit = std::chrono::seconds;
using FpSeconds = std::chrono::duration<double, Unit::period>;
auto t_e = Clock::now();
MyStopWatch.pop();
return FpSeconds(t_e-t_b).count();
}

2877
sheet3/8/Doxyfile Normal file

File diff suppressed because it is too large Load diff

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