Skip to content

Commit

Permalink
backup for changing system
Browse files Browse the repository at this point in the history
  • Loading branch information
lrmor committed Dec 22, 2020
1 parent af6d948 commit acc72ee
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 45 deletions.
Binary file added many to many.pdf
Binary file not shown.
71 changes: 41 additions & 30 deletions mpi/collectives/skeleton.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mpi4py import MPI
import numpy
import numpy as np

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
Expand All @@ -13,19 +13,23 @@
# TODO: create data vector at task 0 and send it to everyone else
# using collective communication
if rank == 0:
data = ...
data = np.arange(8, dtype = int)
else:
data = ...
...
data = np.empty(8, dtype = int)
comm.Bcast(data, root = 0)
comm.barrier()
print(' Task {0}: {1}'.format(rank, data))


# Prepare data vectors ..
data = ... # TODO: create the data vectors
# .. and receive buffers
buff = numpy.full(8, -1, int)

# ... wait for every rank to finish ...
#
## Prepare data vectors ..
#data = ... # TODO: create the data vectors
if rank > 0:
data = data + rank*8
## .. and receive buffers
#print(data)
buff = np.full(8, -1, int)
#
## ... wait for every rank to finish ...
comm.barrier()
if rank == 0:
print('')
Expand All @@ -37,34 +41,41 @@
if rank == 0:
print('')
print('c)')

# TODO: how to get the desired receive buffer using a single collective
# communication routine?
...
#
## TODO: how to get the desired receive buffer using a single collective
## communication routine?
#...
comm.Scatter(data, buff[:2], root = 0)
print(' Task {0}: {1}'.format(rank, buff))

# ... wait for every rank to finish ...
buff[:] = -1
#
comm.barrier()
## ... wait for every rank to finish ...
buff[:] = -1
if rank == 0:
print('')
print('d)')

# TODO: how to get the desired receive buffer using a single collective
# communication routine?

...
## TODO: how to get the desired receive buffer using a single collective
## communication routine?
#
comm.Gather(data[:2], buff, root = 1)
#...
print(' Task {0}: {1}'.format(rank, buff))

# ... wait for every rank to finish ...
buff[:] = -1
#
## ... wait for every rank to finish ...
comm.barrier()
buff[:] = -1
#comm.barrier()
if rank == 0:
print('')
print('e)')

# TODO: how to get the desired receive buffer using a single collective
# communication routine?
...
#
## TODO: how to get the desired receive buffer using a single collective
## communication routine?
#o#...
##comm.Reduce(data, buff, op = MPI.SUM, root =1)
color = rank // 2
sub_comm = comm.Split(color)
sub_comm.Reduce(data, buff, op = MPI.SUM, root = 0)
print(' Task {0}: {1}'.format(rank, buff))

#
Binary file added mpi/heat-equation/heat_000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mpi/heat-equation/heat_200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 56 additions & 15 deletions mpi/heat-equation/skeleton.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import print_function
import numpy as np
import time
from mpi4py import MPI

# TODO: initialise MPI by importing it

import matplotlib
Expand All @@ -27,12 +29,20 @@

# MPI globals
# TODO: find out your rank and communicator size
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

# Up/down neighbouring MPI ranks
up = rank - 1
down = rank + 1
# TODO: if at the edge of the grid, use MPI.PROC_NULL

#--------------------------------------------------------------------------
if rank == 0:
up = MPI.PROC_NULL
if rank == size-1:
down = MPI.PROC_NULL
#--------------------------------------------------------------------------
def evolve(u, u_previous, a, dt, dx2, dy2):
"""Explicit time evolution.
u: new temperature field
Expand Down Expand Up @@ -65,50 +75,79 @@ def exchange(field):
# TODO: select border rows from the field and add MPI calls to
# exchange them with the neighbouring tasks
# send down, receive from up
sbuf = ... # last row of real data
rbuf = ... # ghost row
comm.Sendrecv(...)
#--------------------------------------------------------------------------
sbuf = field[-2,:] # last row of real data
rbuf = field[0,:] # ghost row
comm.Sendrecv(sbuf, dest=down, recvbuf = rbuf, source = up)
# send up, receive from down
sbuf = ... # first row of real data
rbuf = ... # ghost row
comm.Sendrecv(...)
sbuf = field[1,:] # first row of real data
rbuf = field[-1,:] # ghost row
comm.Sendrecv(sbuf, dest = up, recvbuf = rbuf, source = down)
#--------------------------------------------------------------------------

def iterate(field, local_field, local_field0, timesteps, image_interval):
for m in range(1, timesteps+1):
exchange(local_field0)
evolve(local_field, local_field0, a, dt, dx2, dy2)
if m % image_interval == 0:
#--------------------------------------------------------------------------
# TODO: gather partial fields to reconstruct the full field
comm.Gather(...)
#--------------------------------------------------------------------------
if rank == 0:
write_field(field, m)


def main():
# Read and scatter the initial temperature field
if rank == 0:
field, field0 = init_fields('bottle.dat')
field, field0 = init_fields('bottle_large.dat')
shape = field.shape
dtype = field.dtype
print("rank0",field.dtype)
#--------------------------------------------------------------------------
# TODO: send the shape and dtype to everyone else
sptp = {'shape':shape, 'dtype':dtype}
#--------------------------------------------------------------------------
else:
field = None
sptp = {}
#--------------------------------------------------------------------------
# TODO: receive the shape and dtype
bsptp = comm.bcast(sptp, root = 0)
shape = bsptp["shape"]
#--------------------------------------------------------------------------
if shape[0] % size:
raise ValueError('Number of rows in the temperature field (' \
+ str(shape[0]) + ') needs to be divisible by the number ' \
+ 'of MPI tasks (' + str(size) + ').')
n = shape[0] / size # number of rows for each MPI task
n = int(shape[0] / size) # number of rows for each MPI task
m = shape[1] # number of columns in the field

# TODO: scatter a portion of the field to each MPI task
buff = ... # receive buffer for (n,m) elements of dtype
comm.Scatter(...)

#--------------------------------------------------------------------------
xxtype = bsptp["dtype"]
print(xxtype)
print(type(xxtype))
# buff = np.zeros((n,m), dtype = bsptp["dtype"])
buff = np.zeros((n, m), xxtype)
# receive buffer for (n,m) elements of dtype
comm.Scatter(field, buff, root = 0)

#--------------------------------------------------------------------------
# TODO: construct local field based on the received data
# Note: remember to add two ghost rows (one on each side)
local_field = ...

#--------------------------------------------------------------------------
# if rank == 0:
# local_field = np.r_[np.zeros(m), buff, field[rank*n+n,:]]
# elif rank == size-1:
# local_field = np.r_[field[rank*n-1,:] , buff, np.zeros(m)]
# else:
# local_field = np.r_[field[rank*n-1,:] , buff, field[rank*n+n,:]]
#
local_field = np.zeros((n+2,m),xxtype)
local_field[1:-1,:] = buff
#--------------------------------------------------------------------------
local_field0 = np.zeros_like(local_field) # array for previous time step
# Fix outer boundary ghost layers to account for aperiodicity?
if True:
Expand All @@ -126,8 +165,10 @@ def main():
iterate(field, local_field, local_field0, timesteps, image_interval)
t1 = time.time()
# Plot/save final field
#--------------------------------------------------------------------------
# TODO: gather partial fields to reconstruct the full field
comm.Gather(...)
comm.Gather(local_field[1:-1,:], field, root = 0)
#--------------------------------------------------------------------------
if rank == 0:
write_field(field, timesteps)
print("Running time: {0}".format(t1-t0))
Expand Down
2 changes: 2 additions & 0 deletions mpi/heat-equation/solution/heat-p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def main():
+ 'of MPI tasks (' + str(size) + ').')
n = int(shape[0] / size) # number of rows for each MPI task
m = shape[1] # number of columns in the field
print(type(dtype))
print(dtype)
buff = np.zeros((n, m), dtype)
comm.Scatter(field, buff, 0) # scatter the data
local_field = np.zeros((n + 2, m), dtype) # need two ghost rows!
Expand Down
Binary file added mpi/heat-equation/solution/heat_000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mpi/heat-equation/solution/heat_200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions mpi/heat-equation/tstze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import numpy as np

m = 5
n = 6

buff = np.zeros((n,m),dtype=np.float64)

print(buff)
8 changes: 8 additions & 0 deletions mpi/hello-world/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from mpi4py import MPI

comm = MPI.COMM_WORLD

size = comm.Get_size()
rank = comm.Get_rank()

print("This is rank %d in group of %d process" %(rank,size))
18 changes: 18 additions & 0 deletions mpi/message-chain/mesg-chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
ntasks = comm.Get_size()
mypid = comm.Get_rank()

smsg = np.full(10, mypid, dtype = int)
print('process', mypid, 'of', ntasks)
if mypid < ntasks - 1:
comm.Send(smsg, dest = mypid +1)
print(smsg.shape)
if mypid >= 1:
bf = np.empty(10, dtype = int)
comm.Recv(bf, source = mypid - 1)
print(mypid, bf[0])


20 changes: 20 additions & 0 deletions mpi/message-exchange/mesg-ar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

a = np.empty(100000, dtype = int)
a[:] = rank
bf = np.empty(100000, dtype = int)

if rank ==0:
comm.Send(a, dest = 1)
comm.Recv(bf, source = 1)
print(rank, bf[0])
if rank ==1:
comm.Recv(bf, source = 0)
comm.Send(a, dest = 0)
print(rank, bf[0])


19 changes: 19 additions & 0 deletions mpi/message-exchange/mesg-com.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

msg = {'rank':rank}

if rank == 0:
comm.send(msg, dest = 1)
b = comm.recv(source = 1)
print(rank, b)

elif rank == 1:
comm.send(msg, dest = 0)
b = comm.recv(source = 0)
print(rank, b)


25 changes: 25 additions & 0 deletions mpi/non-blocking/mesg-chain-n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
ntasks = comm.Get_size()
mypid = comm.Get_rank()

smsg = np.full(10, mypid, dtype = int)
print('process', mypid, 'of', ntasks)
bf = np.empty(10, dtype = int)
destR = mypid + 1
srcR = mypid - 1
if mypid == ntasks - 1:
destR = MPI.PROC_NULL
elif mypid == 0:
scrR = MPI.PROC_NULL
req1 = comm.Isend(smsg, dest = destR)
req2 = comm.Irecv(bf, source = srcR)

req1.wait()
print(smsg.shape)
req2.wait()
print(mypid, bf[0])


Binary file removed numpy/heat-equation/.heat.py.swp
Binary file not shown.

0 comments on commit acc72ee

Please sign in to comment.