Skip to content

Commit 5af098a

Browse files
committed
2 parents 166f25e + 8b281af commit 5af098a

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

answers/ex29.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mpi4py import MPI
22
import numpy
3+
from sys import stdout
34

45
comm = MPI.COMM_WORLD
56
rank = comm.Get_rank()
@@ -31,6 +32,7 @@
3132
(rank, len(data), tgt))
3233

3334
# ... wait for every rank to finish ...
35+
stdout.flush()
3436
comm.barrier()
3537
if rank == 0:
3638
print("")
@@ -53,6 +55,7 @@
5355
(rank, len(data), tgt))
5456

5557
# ... wait for every rank to finish ...
58+
stdout.flush()
5659
comm.barrier()
5760
if rank == 0:
5861
print("")
@@ -78,6 +81,7 @@
7881
(rank, len(data), tgt))
7982

8083
# ... wait for every rank to finish ...
84+
stdout.flush()
8185
comm.barrier()
8286
if rank == 0:
8387
print("")

answers/ex31a.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mpi4py import MPI
22
import numpy
3+
from sys import stdout
34

45
comm = MPI.COMM_WORLD
56
rank = comm.Get_rank()
@@ -30,6 +31,7 @@
3031
print(" Rank %d: received an array filled with %ds." % (rank, buff[0]))
3132

3233
# ... wait for every rank to finish ...
34+
stdout.flush()
3335
comm.barrier()
3436
if rank == 0:
3537
print("")
@@ -51,6 +53,7 @@
5153
print(" Rank %d: received an array filled with %ds." % (rank, buff[0]))
5254

5355
# ... wait for every rank to finish ...
56+
stdout.flush()
5457
comm.barrier()
5558
if rank == 0:
5659
print("")

answers/ex32.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mpi4py import MPI
22
import numpy
3+
from sys import stdout
34

45
comm = MPI.COMM_WORLD
56
rank = comm.Get_rank()
@@ -26,13 +27,15 @@
2627
buff = numpy.full(8, -1, int)
2728

2829
# ... wait for every rank to finish ...
30+
stdout.flush()
2931
comm.barrier()
3032
if rank == 0:
3133
print('')
3234
print('-' * 32)
3335
print('')
3436
print('Data vectors:')
3537
print(' Task {0}: {1}'.format(rank, data))
38+
stdout.flush()
3639
comm.barrier()
3740
if rank == 0:
3841
print('')
@@ -44,6 +47,7 @@
4447

4548
# ... wait for every rank to finish ...
4649
buff[:] = -1
50+
stdout.flush()
4751
comm.barrier()
4852
if rank == 0:
4953
print('')
@@ -58,6 +62,7 @@
5862

5963
# ... wait for every rank to finish ...
6064
buff[:] = -1
65+
stdout.flush()
6166
comm.barrier()
6267
if rank == 0:
6368
print('')

demos/mpi-example.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from mpi4py import MPI
2+
import numpy
3+
4+
comm = MPI.COMM_WORLD
5+
rank = comm.Get_rank()
6+
size = comm.Get_size()
7+
8+
# Send and receive buffers
9+
n = 100000
10+
data = numpy.full(n, rank, int)
11+
buff = numpy.full(n, -1, int)
12+
13+
# Destination and source for messages (using PROC_NULL for out-of-bounds)
14+
tgt = rank + 1
15+
src = rank - 1
16+
if tgt >= size:
17+
tgt = MPI.PROC_NULL
18+
if src < 0:
19+
src = MPI.PROC_NULL
20+
21+
# Use a single MPI call to do communication
22+
comm.Sendrecv(data, dest=tgt, recvbuf=buff, source=src)
23+
print(" Rank %d: receive buffer is filled with %ds." % (rank, buff[0]))
24+

0 commit comments

Comments
 (0)