Skip to content

Commit a88ddd2

Browse files
author
Matthias Koeppe
committed
src/doc/en/thematic_tutorials/numerical_sage/mpi4py.rst: Remove use of install_package; some formatting
1 parent 0c390a0 commit a88ddd2

File tree

1 file changed

+40
-56
lines changed
  • src/doc/en/thematic_tutorials/numerical_sage

1 file changed

+40
-56
lines changed

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

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
11
mpi4py
22
======
33

4-
MPI which stands for message passing interface is a common library
5-
for parallel programming. There is a package mpi4py that builds on
6-
the top of mpi, and lets arbitrary python objects be passed between
7-
different processes. These packages are not part of the default
8-
sage install. To install them do
4+
MPI, which stands for Message Passing Interface, is a common library
5+
for parallel programming. There is a package ``mpi4py`` that builds on
6+
the top of MPI, and lets arbitrary python objects be passed between
7+
different processes. These packages are not available from the
8+
Sage distribution. Install ``openmpi`` using your distribution's
9+
package manager. Then install ``mpi4py`` using
910

1011
.. skip
1112
1213
::
1314

14-
sage: optional_packages()
15+
sage: !pip install mpi4py
1516

16-
Find the package name openmpi-\* and mpi4py-\*and do
17-
18-
.. skip
19-
20-
::
21-
22-
sage: install_package('openmpi-*')
23-
sage: install_package('mpi4py-*')
24-
25-
Note that openmpi takes a while to compile (15-20 minutes or so).
26-
Openmpi can be run on a cluster, however this requires some set up
27-
so that processes on different machines can communicate (though if
28-
you are on a cluster this is probably already set up). The simplest
29-
case is if you are on a shared memory or multicore system where
30-
openmpi will just work with no configuration from you. To be
31-
honest, I have never tried to run mpi4py on a cluster, though there
32-
is much information about these topics online.
33-
34-
Now, the way that mpi works is you start a group of mpi processes,
17+
Now, the way that MPI works is you start a group of MPI processes,
3518
all of the processes run the same code. Each process has a rank,
3619
that is a number that identifies it. The following pseudocode
3720
indicates the general format of MPI programs.
@@ -48,9 +31,9 @@ indicates the general format of MPI programs.
4831
else if my rank is n+1:
4932
....
5033
51-
Each processes looks for what it's supposed to do (specified by its
52-
rank) and processes can send data and receive data. Lets give an
53-
example. Create a script with the following code in a file mpi_1.py
34+
Each process looks for what it's supposed to do (specified by its
35+
rank), and processes can send data and receive data. Let's give an
36+
example. Create a script with the following code in a file ``mpi_1.py``
5437

5538
.. CODE-BLOCK:: python
5639
@@ -59,18 +42,19 @@ example. Create a script with the following code in a file mpi_1.py
5942
print("hello world")
6043
print("my rank is: %d"%comm.rank)
6144
62-
To run it you can do (from the command line in your sage
45+
To run it you can do (from the command line in your Sage
6346
directory)
6447

6548
.. CODE-BLOCK:: shell-session
6649
67-
./local/bin/mpirun -np 5 ./sage -python mpi_1.py
50+
mpirun -np 5 ./sage -python mpi_1.py
51+
52+
The command ``mpirun -np 5`` starts 5 copies of a program under MPI. In
53+
this case we have 5 copies of Sage in pure Python mode run the
54+
script ``mpi_1.py``. The result should be 5 "hello worlds" plus 5 distinct ranks.
6855

69-
The command mpirun -np 5 starts 5 copies of a program under mpi. In
70-
this case we have 5 copies of sage in pure python mode run the
71-
script mpi_1.py. The result should be 5 "hello worlds" plus 5 distinct ranks.
72-
The two most important mpi operations are sending and receiving.
73-
Consider the following example which you should put in a script mpi_2.py
56+
The two most important MPI operations are sending and receiving.
57+
Consider the following example which you should put in a script ``mpi_2.py``
7458

7559
.. CODE-BLOCK:: python
7660
@@ -86,20 +70,20 @@ Consider the following example which you should put in a script mpi_2.py
8670
print("I received this:")
8771
print(data)
8872
89-
The same command as above with mpi_1.py replaced by mpi_2.py will
73+
The same command as above with ``mpi_1.py`` replaced by ``mpi_2.py`` will
9074
produce 5 outputs and you will see each process creates an array and
9175
then passes it to the next guy (where the last guy passes to the
92-
first.) Note that MPI.size is the total number of mpi
93-
processes. MPI.COMM WORLD is the communication world.
76+
first.) Note that ``MPI.size`` is the total number of MPI
77+
processes. ``MPI.COMM_WORLD`` is the communication world.
9478

9579
There are some subtleties regarding MPI to be aware of. Small sends
9680
are buffered. This means if a process sends a small object it will
9781
be stored by openmpi and that process will continue its execution
9882
and the object it sent will be received whenever the destination
99-
executes a receive. However, if an object is large a process will
83+
executes a receive. However, if an object is large, a process will
10084
hang until its destination executes a corresponding receive. In
101-
fact the above code will hang if [rank]\*5 is replaced by
102-
[rank]\*500. It would be better to do
85+
fact, the above code will hang if ``[rank]*5`` is replaced by
86+
``[rank]*500``. It would be better to do
10387

10488
.. CODE-BLOCK:: python
10589
@@ -126,8 +110,8 @@ ready to receive and then he will send and process 2 will be
126110
waiting to receive, etc. This will not lock regardless of how large
127111
of an array we pass.
128112

129-
A common idiom is to have one process, usually the one with rank 0
130-
act as a leader. That processes sends data out to the other
113+
A common idiom is to have one process, usually the one with rank 0,
114+
act as a leader. That process sends data out to the other
131115
processes and processes the results and decides how further
132116
computation should proceed. Consider the following code
133117

@@ -148,18 +132,18 @@ computation should proceed. Consider the following code
148132
print("I got this array:")
149133
print(v)
150134
151-
The scatter command takes a list and evenly divides it amongst all
135+
The ``scatter`` command takes a list and evenly divides it amongst all
152136
the processes. Here the root process creates a matrix (which is
153-
viewed as a list of rows) and then scatters it to everybody (roots
154-
sendbuf is divided equally amongst the processes). Each process
155-
prints the row it got. Note that the scatter command is executed by
156-
everyone, but when root executes it, it acts as a send and a
157-
receive (root gets one row from itself), while for everyone else it
158-
is just a receive.
159-
160-
There is a complementary gather command that collects results from
161-
all the processes into a list. The next example uses scatter and
162-
gather together. Now the root process scatters the rows of a
137+
viewed as a list of rows) and then scatters it to everybody (root's
138+
``sendbuf`` is divided equally amongst the processes). Each process
139+
prints the row it got. Note that the ``scatter`` command is executed by
140+
everyone, but when root executes it, it acts as a ``send`` and a
141+
``receive`` (root gets one row from itself), while for everyone else it
142+
is just a ``receive``.
143+
144+
There is a complementary ``gather`` command that collects results from
145+
all the processes into a list. The next example uses ``scatter`` and
146+
``gather`` together. Now the root process scatters the rows of a
163147
matrix, each process then squares the elements of the row it gets.
164148
Then the rows are all gathered up again by the root process who
165149
collects them into a new matrix.
@@ -185,9 +169,9 @@ collects them into a new matrix.
185169
if comm.rank==0:
186170
print(numpy.array(recvbuf))
187171
188-
There is also a broadcast command that sends a single object to
172+
There is also a ``broadcast`` command that sends a single object to
189173
every process. Consider the following small extension. This is the
190-
same as before, but now at the end the root process sends everyone
174+
same as before, but now at the end, the root process sends everyone
191175
the string "done", which is printed out.
192176

193177
.. CODE-BLOCK:: python

0 commit comments

Comments
 (0)