commit 9c3c4ad068c0322d5e7aff464ee732fd42af9c2a Author: niklas Date: Wed Nov 22 19:51:30 2023 +0100 Initial diff --git a/uebung_01/exercise_1.py b/uebung_01/exercise_1.py new file mode 100644 index 0000000..5ee8612 --- /dev/null +++ b/uebung_01/exercise_1.py @@ -0,0 +1,22 @@ +import numpy as np + +for n in [3,5,10,100,1000]: + A = np.zeros((n,n)) + x = np.zeros((n,1)) + y_c = np.zeros((n,1)) + + for i in range(0,n): + x[i] = i+1 + y_c[i] = (i+1) * n; + + for j in range(0,n): + A[i,j] =(i+1) / (j+1) + + y = A@x + + if n == 3 or n == 5: + print(x) + print(A) + print(y) + + print("Fuer n =",n,":",np.linalg.norm(y - y_c)) diff --git a/uebung_01/exercise_2a.py b/uebung_01/exercise_2a.py new file mode 100644 index 0000000..a9d3653 --- /dev/null +++ b/uebung_01/exercise_2a.py @@ -0,0 +1,24 @@ +import numpy as np +import matplotlib.pyplot as plt + +# x = np.linspace(0, 2 * np.pi, 100) +x_1 = np.arange(0, 2 * np.pi, 1) +x_0_5 = np.arange(0, 2 * np.pi, 0.5) +x_0_1 = np.arange(0, 2 * np.pi, 0.1) + +y_s_1 = np.sin(x_1) +y_s_0_5 = np.sin(x_0_5) +y_s_0_1 = np.sin(x_0_1) +y_c = np.cos(x_1) + +plt.plot(x_1,y_s_1,"purple", label="sin(x) with stepsize 1") +plt.plot(x_0_5,y_s_0_5,"pink", label="sin(x) with stepsize 0.5") +plt.plot(x_0_1,y_s_0_1,"red", label="sin(x) with stepsize 0.1") +plt.plot(x_1,y_c,"green", label="cos(x)") + +plt.title("A2.a) Sine and Cosine") +plt.xlabel("x") +plt.ylabel("y") +plt.legend() + +plt.show() diff --git a/uebung_01/exercise_2b.py b/uebung_01/exercise_2b.py new file mode 100644 index 0000000..ed7c67c --- /dev/null +++ b/uebung_01/exercise_2b.py @@ -0,0 +1,22 @@ +import numpy as np +import matplotlib.pyplot as plt + +plt.rcParams['text.usetex'] = True + +# x = np.linspace(0, 2 * np.pi, 100) +x = np.arange(-2, 2, 0.1) + +f = np.exp(-x**2) +g = np.sin(x**2) +h = np.sin(1 / (x**3 + 9)) + +plt.plot(x, f, "purple", label=r"$f(x) = \exp(-x^2)$") +plt.plot(x, g, "red", label=r"$g(x) = \sin(x^2)$") +plt.plot(x, h, "green", label=r"$h(x) = \frac{1}{x^3 + 9}$") + +plt.title("A2.b)") +plt.xlabel("x") +plt.ylabel("y") +plt.legend() + +plt.show() diff --git a/uebung_01/exercise_3.py b/uebung_01/exercise_3.py new file mode 100644 index 0000000..da1d328 --- /dev/null +++ b/uebung_01/exercise_3.py @@ -0,0 +1,68 @@ +import numpy as np +import matplotlib.pyplot as plt + +def runge(x): + return 1 / (1 + 25 * x**2) + +def divided_differences(x, y): + n = len(y) + a = np.zeros((n,n)) + + a[:,0] = y + + for i in range(1,n): + for j in range(1,i+1): + a[i,j] = (a[i,j-1] - a[i-1,j-1]) / (x[i] - x[i-j]) # (links daneben - links darĂ¼ber) / (x[zeile] - x[zeile - spalte]) + + return a + +def newton_interpolation(a, data, x): + n = len(a) + + p = np.zeros(len(x)) + + for i in range(1,n+1): + p = a[n-i] + (x - data[n-i]) * p # Horner Schema; '-' and '*' are overloaded for numpy arrays: so at least one argument 'data' or 'x' has to be numpy array + + return p + +#################################################################################################################################################################### +n = 12 + +x = np.linspace(-1, 1, 200) +x_e = np.linspace(-1, 1, n) # equidistant grid points + +# Chebyshev grid points +x_c = np.zeros(n) +for i in range(0,n): + x_c[i] = np.cos((2 * i + 1) * np.pi / (2 * n)) + +f = runge(x) +y_e = runge(x_e) # values for grid points for interpolation with equidistant grid points +y_c = runge(x_c) # values for grid points for interpolation with Chebyshev grid points + +# Interpolation with equidistant grid points and evaluation of interpolated values at x +a_e = np.diag(divided_differences(x_e, y_e)) +p_e = newton_interpolation(a_e, x_e, x) + +# Interpolation with Chebyshev grid points and evaluation of interpolated values at x +a_c = np.diag(divided_differences(x_c, y_c)) +p_c = newton_interpolation(a_c, x_c, x) + + +# Plotting of Runge and the two interpolated polynomials +plt.rcParams['text.usetex'] = True + +plt.plot(x, f, label=r"$f(x) = \frac{1}{1 + 25 x^2}$") +plt.plot(x, p_e, label=r"$p_n(x)$ equidistant") +plt.plot(x, p_c, label=r"$p_n(x)$ Chebyshev") +plt.plot([], [], ' ', label="$n = {}$".format(n)) + +plt.title("A3)") +plt.xlabel("x") +plt.ylabel("y") +plt.legend() + +plt.show() + +# braendel@math.tu-freiberg.de diff --git a/uebung_02/main_matrix.c b/uebung_02/main_matrix.c new file mode 100644 index 0000000..9380c05 --- /dev/null +++ b/uebung_02/main_matrix.c @@ -0,0 +1,64 @@ +#include +#include + +int compute(int N) { + + int i_0, i_1; + // claim space + double **A = (double **)malloc(sizeof(double *) * N); + for (i_0 = 0; i_0 < N; i_0++) { + A[i_0] = (double *)malloc(sizeof(double) * N); + } + double *x = (double *)malloc(sizeof(double) * N); + double *y = (double *)malloc(sizeof(double) * N); + double *y_sol = (double *)malloc(sizeof(double) * N); + + // fill matrix and vector + for (i_0 = 0; i_0 < N; i_0++) { + x[i_0] = i_0 + 1; + y_sol[i_0] = (i_0 + 1) * N; + for (i_1 = 0; i_1 < N; i_1++) { + A[i_0][i_1] = (i_0 + 1.0) / (i_1 + 1.0); + } + } + + // for (i_0 = 0; i_0 < N; i_0++){ + // printf("[ "); + // for(i_1 = 0; i_1 < N; i_1++){ + // printf("%.3f ",A[i_0][i_1]); + // } + // printf("]"); + // printf("[ %.1f ] = [ %.1f ][ %.1f ]\n",x[i_0],y[i_0],y_sol[i_0]); + // } + + // compute matrix-vector product + for (i_0 = 0; i_0 < N; i_0++) { + y[i_0] = 0; + for (i_1 = 0; i_1 < N; i_1++) { + y[i_0] += A[i_0][i_1] * x[i_1]; + } + } + + // compare result to solution + double sum = 0.0; + for (i_0 = 0; i_0 < N; i_0++) { + sum += (y[i_0] - y_sol[i_0]) * (y[i_0] - y_sol[i_0]); + } + + return sum; +} + +int main(int argc, char *argv[]) { + + int N; + if (argc != 2) { + N = 1000; + } else { + N = atoi(argv[1]); // Convert the argument to an integer + } + + double err = compute(N); + printf("Error for N = %d: %.8f\n", N, err); + + return 0; +} diff --git a/uebung_02/main_openmp.c b/uebung_02/main_openmp.c new file mode 100644 index 0000000..92be7c1 --- /dev/null +++ b/uebung_02/main_openmp.c @@ -0,0 +1,19 @@ +#include // load Open-MP library +#include +#include + +int main() { + int N = 5; + int num; + +#pragma omp parallel for private(num) // start Open-MP section + // here the program (1 thread) is split into multiple threads + // each thread has its own copy of i (loop) and num (private) + // all threads share N (by default, because defined outside) + for (int i = 0; i < N; i++) { + num = omp_get_thread_num(); + printf("Thread %d does iteration %d\n", num, i); + } + + return 0; +} diff --git a/uebung_02/matrix_mul b/uebung_02/matrix_mul new file mode 100755 index 0000000..f0a36d9 Binary files /dev/null and b/uebung_02/matrix_mul differ diff --git a/uebung_02/openmp_hello b/uebung_02/openmp_hello new file mode 100755 index 0000000..00099eb Binary files /dev/null and b/uebung_02/openmp_hello differ diff --git a/uebung_04/exercise_01/exercise_01.c b/uebung_04/exercise_01/exercise_01.c new file mode 100644 index 0000000..ca5fb6f --- /dev/null +++ b/uebung_04/exercise_01/exercise_01.c @@ -0,0 +1,17 @@ +#include "mpi.h" +#include + +int main(int argc, char **argv) { + int rank, size, err, n; + + MPI_Init(&argc, &argv); + + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + printf("rank=%d size=%d\n", rank, size); + + MPI_Finalize(); + + return 0; +} diff --git a/uebung_04/exercise_01/exercise_01.out b/uebung_04/exercise_01/exercise_01.out new file mode 100755 index 0000000..1421d5e Binary files /dev/null and b/uebung_04/exercise_01/exercise_01.out differ diff --git a/uebung_04/exercise_01/exercise_01.script b/uebung_04/exercise_01/exercise_01.script new file mode 100644 index 0000000..649503a --- /dev/null +++ b/uebung_04/exercise_01/exercise_01.script @@ -0,0 +1,13 @@ +#!/bin/bash + +## parameters +#PBS -N uebung_04__exercise_01 +#PBS -q entry_teachingq +#PBS -l select=1:ncpus=8:mpiprocs=8 +#PBS -l walltime=00:05:00 + +## environment +cd ~/pwr/uebung_04/exercise_01 + +## execute +mpiexec -n 8 exercise_01.out diff --git a/uebung_04/exercise_01/uebung_04__exercise_01.e662620 b/uebung_04/exercise_01/uebung_04__exercise_01.e662620 new file mode 100644 index 0000000..e69de29 diff --git a/uebung_04/exercise_01/uebung_04__exercise_01.o662620 b/uebung_04/exercise_01/uebung_04__exercise_01.o662620 new file mode 100644 index 0000000..89bfac5 --- /dev/null +++ b/uebung_04/exercise_01/uebung_04__exercise_01.o662620 @@ -0,0 +1,8 @@ +rank=2 size=8 +rank=4 size=8 +rank=5 size=8 +rank=6 size=8 +rank=7 size=8 +rank=0 size=8 +rank=1 size=8 +rank=3 size=8 diff --git a/uebung_04/exercise_03/exercise_03.c b/uebung_04/exercise_03/exercise_03.c new file mode 100644 index 0000000..2adcaf3 --- /dev/null +++ b/uebung_04/exercise_03/exercise_03.c @@ -0,0 +1,26 @@ +#include "mpi.h" +#include + +int main(int argc, char **argv) +{ + int rank, size, err, n; + + MPI_Init(&argc, &argv); + + MPI_Comm_size(MPI_COMM_WORLD,&size); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + + printf("rank=%d size=%d\n", rank, size); + + err = MPI_Barrier(MPI_COMM_WORLD); + + n = (rank + 1) * 4711; + + err = MPI_Bcast(&n, 1, MPI_INT, 1, MPI_COMM_WORLD); + + printf("Received =%d\n", n); + + MPI_Finalize(); + + return 0; +} diff --git a/uebung_04/exercise_03/exercise_03.out b/uebung_04/exercise_03/exercise_03.out new file mode 100755 index 0000000..dedb0d5 Binary files /dev/null and b/uebung_04/exercise_03/exercise_03.out differ diff --git a/uebung_04/exercise_03/exercise_03.script b/uebung_04/exercise_03/exercise_03.script new file mode 100644 index 0000000..34e10b3 --- /dev/null +++ b/uebung_04/exercise_03/exercise_03.script @@ -0,0 +1,13 @@ +#!/bin/bash + +## parameters +#PBS -N uebung_04__exercise_03 +#PBS -q entry_teachingq +#PBS -l select=1:ncpus=8:mpiprocs=8 +#PBS -l walltime=00:05:00 + +## environment +cd ~/pwr/uebung_04/exercise_03 + +## execute +mpiexec -n 8 exercise_03.out diff --git a/uebung_04/exercise_05/exercise_05.c b/uebung_04/exercise_05/exercise_05.c new file mode 100644 index 0000000..c236687 --- /dev/null +++ b/uebung_04/exercise_05/exercise_05.c @@ -0,0 +1,33 @@ +#include "mpi.h" +#include + +int main(int argc, char **argv) { + int rank, size, err, n, sum; + + MPI_Init(&argc, &argv); + + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + printf("rank=%d size=%d\n", rank, size); + + n = rank + 1; + + sum = 4710; + + MPI_Barrier(MPI_COMM_WORLD); + + MPI_Reduce(&n, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); + printf("Received in reduction=%d\n", sum); + + MPI_Barrier(MPI_COMM_WORLD); + + sum = 3710; + + MPI_Allreduce(&n, &sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); + printf("Received in allreduction =%d\n", sum); + + MPI_Finalize(); + + return 0; +} diff --git a/uebung_04/exercise_05/exercise_05.out b/uebung_04/exercise_05/exercise_05.out new file mode 100755 index 0000000..24df63b Binary files /dev/null and b/uebung_04/exercise_05/exercise_05.out differ