Initial
This commit is contained in:
commit
9c3c4ad068
22
uebung_01/exercise_1.py
Normal file
22
uebung_01/exercise_1.py
Normal file
@ -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))
|
24
uebung_01/exercise_2a.py
Normal file
24
uebung_01/exercise_2a.py
Normal file
@ -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()
|
22
uebung_01/exercise_2b.py
Normal file
22
uebung_01/exercise_2b.py
Normal file
@ -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()
|
68
uebung_01/exercise_3.py
Normal file
68
uebung_01/exercise_3.py
Normal file
@ -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
|
64
uebung_02/main_matrix.c
Normal file
64
uebung_02/main_matrix.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
19
uebung_02/main_openmp.c
Normal file
19
uebung_02/main_openmp.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <omp.h> // load Open-MP library
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
BIN
uebung_02/matrix_mul
Executable file
BIN
uebung_02/matrix_mul
Executable file
Binary file not shown.
BIN
uebung_02/openmp_hello
Executable file
BIN
uebung_02/openmp_hello
Executable file
Binary file not shown.
17
uebung_04/exercise_01/exercise_01.c
Normal file
17
uebung_04/exercise_01/exercise_01.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "mpi.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
BIN
uebung_04/exercise_01/exercise_01.out
Executable file
BIN
uebung_04/exercise_01/exercise_01.out
Executable file
Binary file not shown.
13
uebung_04/exercise_01/exercise_01.script
Normal file
13
uebung_04/exercise_01/exercise_01.script
Normal file
@ -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
|
8
uebung_04/exercise_01/uebung_04__exercise_01.o662620
Normal file
8
uebung_04/exercise_01/uebung_04__exercise_01.o662620
Normal file
@ -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
|
26
uebung_04/exercise_03/exercise_03.c
Normal file
26
uebung_04/exercise_03/exercise_03.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include "mpi.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
BIN
uebung_04/exercise_03/exercise_03.out
Executable file
BIN
uebung_04/exercise_03/exercise_03.out
Executable file
Binary file not shown.
13
uebung_04/exercise_03/exercise_03.script
Normal file
13
uebung_04/exercise_03/exercise_03.script
Normal file
@ -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
|
33
uebung_04/exercise_05/exercise_05.c
Normal file
33
uebung_04/exercise_05/exercise_05.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "mpi.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
BIN
uebung_04/exercise_05/exercise_05.out
Executable file
BIN
uebung_04/exercise_05/exercise_05.out
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user