Skip to content

Commit bee6e87

Browse files
author
Matthias Koeppe
committed
src/doc/en/thematic_tutorials/numerical_sage/mpi4py.rst: Update coding style in examples
1 parent 26719fb commit bee6e87

File tree

1 file changed

+38
-38
lines changed
  • src/doc/en/thematic_tutorials/numerical_sage

1 file changed

+38
-38
lines changed

src/doc/en/thematic_tutorials/numerical_sage/mpi4py.rst

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ Consider the following example which you should put in a script ``mpi_2.py``
6161
from mpi4py import MPI
6262
import numpy
6363
comm = MPI.COMM_WORLD
64-
rank=comm.rank
65-
size=comm.size
66-
v=numpy.array([rank]*5,dtype=float)
67-
comm.send(v,dest=(rank+1)%size)
68-
data=comm.recv(source=(rank-1)%size)
69-
print("my rank is %d"%rank)
64+
rank = comm.rank
65+
size = comm.size
66+
v = numpy.array([rank] * 5, dtype=float)
67+
comm.send(v, dest=(rank+1) % size)
68+
data = comm.recv(source=(rank-1) % size)
69+
print(f"my rank is: {rank}")
7070
print("I received this:")
7171
print(data)
7272
@@ -90,18 +90,18 @@ fact, the above code will hang if ``[rank]*5`` is replaced by
9090
from mpi4py import MPI
9191
import numpy
9292
comm = MPI.COMM_WORLD
93-
rank=comm.rank
94-
size=comm.size
95-
v=numpy.array([rank]*500,dtype=float)
96-
if comm.rank==0:
97-
comm.send(v,dest=(rank+1)%size)
93+
rank = comm.rank
94+
size = comm.size
95+
v = numpy.array([rank] * 500, dtype=float)
96+
if comm.rank == 0:
97+
comm.send(v, dest=(rank+1) % size)
9898
if comm.rank > 0:
99-
data=comm.recv(source=(rank-1)%size)
100-
comm.send(v,dest=(rank+1)%size)
101-
if comm.rank==0:
102-
data=comm.recv(source=size-1)
99+
data = comm.recv(source=(rank-1) % size)
100+
comm.send(v, dest=(rank+1) % size)
101+
if comm.rank == 0:
102+
data = comm.recv(source=size - 1)
103103
104-
print("my rank is %d"%rank)
104+
print(f"my rank is: {rank}")
105105
print("I received this:")
106106
print(data)
107107
@@ -119,15 +119,15 @@ computation should proceed. Consider the following code
119119
120120
from mpi4py import MPI
121121
import numpy
122-
sendbuf=[]
123-
root=0
122+
sendbuf = []
123+
root = 0
124124
comm = MPI.COMM_WORLD
125-
if comm.rank==0:
126-
m=numpy.random.randn(comm.size,comm.size)
125+
if comm.rank == 0:
126+
m = numpy.random.randn(comm.size, comm.size)
127127
print(m)
128128
sendbuf=m
129129
130-
v=comm.scatter(sendbuf,root)
130+
v = comm.scatter(sendbuf, root)
131131
132132
print("I got this array:")
133133
print(v)
@@ -152,20 +152,20 @@ The root process then gathers the rows into a new matrix.
152152
from mpi4py import MPI
153153
import numpy
154154
comm = MPI.COMM_WORLD
155-
sendbuf=[]
156-
root=0
157-
if comm.rank==0:
158-
m=numpy.array(range(comm.size*comm.size),dtype=float)
159-
m.shape=(comm.size,comm.size)
155+
sendbuf = []
156+
root = 0
157+
if comm.rank == 0:
158+
m = numpy.array(range(comm.size * comm.size), dtype=float)
159+
m.shape = (comm.size, comm.size)
160160
print(m)
161-
sendbuf=m
161+
sendbuf = m
162162
163-
v=comm.scatter(sendbuf,root)
163+
v = comm.scatter(sendbuf, root)
164164
print("I got this array:")
165165
print(v)
166-
v=v*v
167-
recvbuf=comm.gather(v,root)
168-
if comm.rank==0:
166+
v = v*v
167+
recvbuf = comm.gather(v, root)
168+
if comm.rank == 0:
169169
print(numpy.array(recvbuf))
170170
171171
There is also a ``broadcast`` command that sends a single object to
@@ -175,17 +175,17 @@ the string "done", which is printed out.
175175

176176
.. CODE-BLOCK:: python
177177
178-
v=MPI.COMM_WORLD.scatter(sendbuf,root)
178+
v = MPI.COMM_WORLD.scatter(sendbuf, root)
179179
print("I got this array:")
180180
print(v)
181-
v=v*v
182-
recvbuf=MPI.COMM_WORLD.gather(v,root)
183-
if MPI.COMM_WORLD.rank==0:
181+
v = v*v
182+
recvbuf = MPI.COMM_WORLD.gather(v, root)
183+
if MPI.COMM_WORLD.rank == 0:
184184
print(numpy.array(recvbuf))
185185
186-
if MPI.COMM_WORLD.rank==0:
187-
sendbuf="done"
188-
recvbuf=MPI.COMM_WORLD.bcast(sendbuf,root)
186+
if MPI.COMM_WORLD.rank == 0:
187+
sendbuf = "done"
188+
recvbuf = MPI.COMM_WORLD.bcast(sendbuf,root)
189189
print(recvbuf)
190190
191191
MPI programming is difficult. It is "schizophrenic programming" in

0 commit comments

Comments
 (0)