@@ -61,12 +61,12 @@ Consider the following example which you should put in a script ``mpi_2.py``
61
61
from mpi4py import MPI
62
62
import numpy
63
63
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} " )
70
70
print (" I received this:" )
71
71
print (data)
72
72
@@ -90,18 +90,18 @@ fact, the above code will hang if ``[rank]*5`` is replaced by
90
90
from mpi4py import MPI
91
91
import numpy
92
92
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)
98
98
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 )
103
103
104
- print (" my rank is %d " % rank)
104
+ print (f " my rank is: { rank} " )
105
105
print (" I received this:" )
106
106
print (data)
107
107
@@ -119,15 +119,15 @@ computation should proceed. Consider the following code
119
119
120
120
from mpi4py import MPI
121
121
import numpy
122
- sendbuf= []
123
- root= 0
122
+ sendbuf = []
123
+ root = 0
124
124
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)
127
127
print (m)
128
128
sendbuf= m
129
129
130
- v= comm.scatter(sendbuf,root)
130
+ v = comm.scatter(sendbuf, root)
131
131
132
132
print (" I got this array:" )
133
133
print (v)
@@ -152,20 +152,20 @@ The root process then gathers the rows into a new matrix.
152
152
from mpi4py import MPI
153
153
import numpy
154
154
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)
160
160
print (m)
161
- sendbuf= m
161
+ sendbuf = m
162
162
163
- v= comm.scatter(sendbuf,root)
163
+ v = comm.scatter(sendbuf, root)
164
164
print (" I got this array:" )
165
165
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 :
169
169
print (numpy.array(recvbuf))
170
170
171
171
There is also a ``broadcast `` command that sends a single object to
@@ -175,17 +175,17 @@ the string "done", which is printed out.
175
175
176
176
.. CODE-BLOCK :: python
177
177
178
- v= MPI .COMM_WORLD .scatter(sendbuf,root)
178
+ v = MPI .COMM_WORLD .scatter(sendbuf, root)
179
179
print (" I got this array:" )
180
180
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 :
184
184
print (numpy.array(recvbuf))
185
185
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)
189
189
print (recvbuf)
190
190
191
191
MPI programming is difficult. It is "schizophrenic programming" in
0 commit comments