more stuff
This commit is contained in:
11
uebung_03/exercise_02/exercise_02a.py
Normal file
11
uebung_03/exercise_02/exercise_02a.py
Normal 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}")
|
23
uebung_03/exercise_02/exercise_02b.py
Normal file
23
uebung_03/exercise_02/exercise_02b.py
Normal 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}")
|
37
uebung_03/exercise_03/exercise_03a.py
Normal file
37
uebung_03/exercise_03/exercise_03a.py
Normal 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}.")
|
33
uebung_03/exercise_03/exercise_03b.py
Normal file
33
uebung_03/exercise_03/exercise_03b.py
Normal 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}.")
|
Reference in New Issue
Block a user