From 25d77e62e07dc30e37459888975584c0715bb7c0 Mon Sep 17 00:00:00 2001 From: niklas Date: Thu, 23 Nov 2023 16:02:33 +0100 Subject: [PATCH] more stuff --- uebung_03/exercise_02/exercise_02a.py | 11 ++++++++ uebung_03/exercise_02/exercise_02b.py | 23 +++++++++++++++++ uebung_03/exercise_03/exercise_03a.py | 37 +++++++++++++++++++++++++++ uebung_03/exercise_03/exercise_03b.py | 33 ++++++++++++++++++++++++ uebung_04/exercise_06/src/main.rs | 7 ++--- 5 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 uebung_03/exercise_02/exercise_02a.py create mode 100644 uebung_03/exercise_02/exercise_02b.py create mode 100644 uebung_03/exercise_03/exercise_03a.py create mode 100644 uebung_03/exercise_03/exercise_03b.py diff --git a/uebung_03/exercise_02/exercise_02a.py b/uebung_03/exercise_02/exercise_02a.py new file mode 100644 index 0000000..e81b8cd --- /dev/null +++ b/uebung_03/exercise_02/exercise_02a.py @@ -0,0 +1,11 @@ +from mpi4py import MPI + +comm = MPI.COMM_WORLD +rank = comm.Get_rank() + +if rank == 0: + data = "Hello, from process 0!" + comm.send(data, dest=1) +elif rank == 1: + received_data = comm.recv(source=0) + print(f"Process 1 received: {received_data}") diff --git a/uebung_03/exercise_02/exercise_02b.py b/uebung_03/exercise_02/exercise_02b.py new file mode 100644 index 0000000..9dc0ec9 --- /dev/null +++ b/uebung_03/exercise_02/exercise_02b.py @@ -0,0 +1,23 @@ +from mpi4py import MPI + +comm = MPI.COMM_WORLD +rank = comm.Get_rank() +size = comm.Get_size() + +send_data = f"Hello, from process {rank}!" +print(f"I am rank {rank}. I send to process {(rank+1)}") +print(f"I am rank {rank}. I receive from process {(rank-1)}") + +if rank == size-1: + comm.send(send_data, dest=(0)) +else: + comm.send(send_data, dest=(rank + 1)) + +recv_data = "" + +if rank == 0: + recv_data= comm.recv(source=(size - 1)) +else: + recv_data= comm.recv(source=(rank - 1)) + +print(f"I am rank {rank}. I have received: {recv_data}") diff --git a/uebung_03/exercise_03/exercise_03a.py b/uebung_03/exercise_03/exercise_03a.py new file mode 100644 index 0000000..600a288 --- /dev/null +++ b/uebung_03/exercise_03/exercise_03a.py @@ -0,0 +1,37 @@ +from mpi4py import MPI +import numpy as np +import math + +comm = MPI.COMM_WORLD +rank = comm.Get_rank() +size = comm.Get_size() + +chunk = 0 + +if rank == 0: + N = 100000 + x = np.random.uniform(0,1,(N,1)) + + chunksize = math.floor(N / size) + for i in range(1,size): + from_index = 0 + chunksize * i + to_index = (chunksize * (i + 1)) - 1 + chunk = x[from_index:to_index] + comm.send(chunk, dest = i) +else: + chunk = comm.recv(source = 0) + +chunk_sum = 0.0 +for i in range(len(chunk)): + chunk_sum += chunk[i] * chunk[i] + +if rank != 0: + comm.send(chunk_sum, dest = 0) + +if rank == 0: + sum = chunk_sum + + for i in range(1,size): + sum += comm.recv(source = i) + + print(f"The squared euclidian norm of x is {sum[0]:.2f}.") diff --git a/uebung_03/exercise_03/exercise_03b.py b/uebung_03/exercise_03/exercise_03b.py new file mode 100644 index 0000000..1079309 --- /dev/null +++ b/uebung_03/exercise_03/exercise_03b.py @@ -0,0 +1,33 @@ +from mpi4py import MPI +import numpy as np +import math + +comm = MPI.COMM_WORLD +rank = comm.Get_rank() +size = comm.Get_size() + +chunk = 0 + +if rank == 0: + N = 100000 + x = np.random.uniform(0,1,(N,1)) + + chunksize = math.floor(N / size) + for i in range(1,size): + from_index = 0 + chunksize * i + to_index = (chunksize * (i + 1)) - 1 + chunk = x[from_index:to_index] + comm.send(chunk, dest = i) +else: + chunk = comm.recv(source = 0) + +chunk_sum = 0.0 +for i in range(len(chunk)): + chunk_sum += chunk[i] * chunk[i] + +comm.Barrier() + +sum = comm.reduce(chunk_sum, op = MPI.SUM, root = 0) + +if rank == 0: + print(f"The squared euclidian norm of x is {sum[0]:.2f}.") diff --git a/uebung_04/exercise_06/src/main.rs b/uebung_04/exercise_06/src/main.rs index bd2a903..47ed6f7 100644 --- a/uebung_04/exercise_06/src/main.rs +++ b/uebung_04/exercise_06/src/main.rs @@ -9,7 +9,7 @@ fn main() { let size = world.size(); let rank = world.rank(); - let support_points = 20; + let support_points = 100; let a = 0.0; let b = 1.0; let N = (b - a) / size as f64; @@ -17,6 +17,7 @@ fn main() { let f = |x: f64| 1.0 / (1.0 + x.powi(2)) as f64; // function let n = (support_points / size) as u32; // fewer ranks should also use an appropriate amount of support points; the support points are distributed among the ranks let partial_integral = trapezoidal_rule(f, a + rank as f64 * N, a + (rank + 1) as f64 * N, n); + println!("Rank {}: Partial integral is {} on [{},{}]", rank, partial_integral, (a + rank as f64 * N), (a + (rank + 1) as f64 * N)); world.barrier(); @@ -26,7 +27,7 @@ fn main() { if rank == 0 { println!("The approximated integral is {}", integral); - println!("The error is {}", (integral - (PI / 4.0)).abs()); + println!("The error is |{:.7} - {:.7}| = {}", (PI / 4.0), integral, (integral - (PI / 4.0)).abs()); } } @@ -72,4 +73,4 @@ mod tests { assert!((expected - actual).abs() < tolerance, "Integral is {} but should be {}", actual, expected); } -} \ No newline at end of file +}