more stuff

This commit is contained in:
Niklas Birk 2023-11-23 16:02:33 +01:00
parent 4b90be609e
commit 25d77e62e0
5 changed files with 108 additions and 3 deletions

View File

@ -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}")

View File

@ -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}")

View File

@ -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}.")

View File

@ -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}.")

View File

@ -9,7 +9,7 @@ fn main() {
let size = world.size(); let size = world.size();
let rank = world.rank(); let rank = world.rank();
let support_points = 20; let support_points = 100;
let a = 0.0; let a = 0.0;
let b = 1.0; let b = 1.0;
let N = (b - a) / size as f64; 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 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 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); 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(); world.barrier();
@ -26,7 +27,7 @@ fn main() {
if rank == 0 { if rank == 0 {
println!("The approximated integral is {}", integral); 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); assert!((expected - actual).abs() < tolerance, "Integral is {} but should be {}", actual, expected);
} }
} }