Show More
@@ -1,157 +1,157 b'' | |||||
1 | .. _parallelmpi: |
|
1 | .. _parallelmpi: | |
2 |
|
2 | |||
3 | ======================= |
|
3 | ======================= | |
4 | Using MPI with IPython |
|
4 | Using MPI with IPython | |
5 | ======================= |
|
5 | ======================= | |
6 |
|
6 | |||
7 | Often, a parallel algorithm will require moving data between the engines. One way of accomplishing this is by doing a pull and then a push using the multiengine client. However, this will be slow as all the data has to go through the controller to the client and then back through the controller, to its final destination. |
|
7 | Often, a parallel algorithm will require moving data between the engines. One way of accomplishing this is by doing a pull and then a push using the multiengine client. However, this will be slow as all the data has to go through the controller to the client and then back through the controller, to its final destination. | |
8 |
|
8 | |||
9 | A much better way of moving data between engines is to use a message passing library, such as the Message Passing Interface (MPI) [MPI]_. IPython's parallel computing architecture has been designed from the ground up to integrate with MPI. This document describes how to use MPI with IPython. |
|
9 | A much better way of moving data between engines is to use a message passing library, such as the Message Passing Interface (MPI) [MPI]_. IPython's parallel computing architecture has been designed from the ground up to integrate with MPI. This document describes how to use MPI with IPython. | |
10 |
|
10 | |||
11 | Additional installation requirements |
|
11 | Additional installation requirements | |
12 | ==================================== |
|
12 | ==================================== | |
13 |
|
13 | |||
14 | If you want to use MPI with IPython, you will need to install: |
|
14 | If you want to use MPI with IPython, you will need to install: | |
15 |
|
15 | |||
16 | * A standard MPI implementation such as OpenMPI [OpenMPI]_ or MPICH. |
|
16 | * A standard MPI implementation such as OpenMPI [OpenMPI]_ or MPICH. | |
17 | * The mpi4py [mpi4py]_ package. |
|
17 | * The mpi4py [mpi4py]_ package. | |
18 |
|
18 | |||
19 | .. note:: |
|
19 | .. note:: | |
20 |
|
20 | |||
21 | The mpi4py package is not a strict requirement. However, you need to |
|
21 | The mpi4py package is not a strict requirement. However, you need to | |
22 | have *some* way of calling MPI from Python. You also need some way of |
|
22 | have *some* way of calling MPI from Python. You also need some way of | |
23 | making sure that :func:`MPI_Init` is called when the IPython engines start |
|
23 | making sure that :func:`MPI_Init` is called when the IPython engines start | |
24 | up. There are a number of ways of doing this and a good number of |
|
24 | up. There are a number of ways of doing this and a good number of | |
25 | associated subtleties. We highly recommend just using mpi4py as it |
|
25 | associated subtleties. We highly recommend just using mpi4py as it | |
26 | takes care of most of these problems. If you want to do something |
|
26 | takes care of most of these problems. If you want to do something | |
27 | different, let us know and we can help you get started. |
|
27 | different, let us know and we can help you get started. | |
28 |
|
28 | |||
29 | Starting the engines with MPI enabled |
|
29 | Starting the engines with MPI enabled | |
30 | ===================================== |
|
30 | ===================================== | |
31 |
|
31 | |||
32 | To use code that calls MPI, there are typically two things that MPI requires. |
|
32 | To use code that calls MPI, there are typically two things that MPI requires. | |
33 |
|
33 | |||
34 | 1. The process that wants to call MPI must be started using |
|
34 | 1. The process that wants to call MPI must be started using | |
35 |
:command:`mpi |
|
35 | :command:`mpiexec` or a batch system (like PBS) that has MPI support. | |
36 | 2. Once the process starts, it must call :func:`MPI_Init`. |
|
36 | 2. Once the process starts, it must call :func:`MPI_Init`. | |
37 |
|
37 | |||
38 | There are a couple of ways that you can start the IPython engines and get these things to happen. |
|
38 | There are a couple of ways that you can start the IPython engines and get these things to happen. | |
39 |
|
39 | |||
40 |
Automatic starting using :command:`mpi |
|
40 | Automatic starting using :command:`mpiexec` and :command:`ipcluster` | |
41 | ------------------------------------------------------------------- |
|
41 | ------------------------------------------------------------------- | |
42 |
|
42 | |||
43 |
The easiest approach is to use the `mpi |
|
43 | The easiest approach is to use the `mpiexec` mode of :command:`ipcluster`, which will first start a controller and then a set of engines using :command:`mpiexec`:: | |
44 |
|
44 | |||
45 |
$ ipcluster mpi |
|
45 | $ ipcluster mpiexec -n 4 | |
46 |
|
46 | |||
47 | This approach is best as interrupting :command:`ipcluster` will automatically |
|
47 | This approach is best as interrupting :command:`ipcluster` will automatically | |
48 | stop and clean up the controller and engines. |
|
48 | stop and clean up the controller and engines. | |
49 |
|
49 | |||
50 |
Manual starting using :command:`mpi |
|
50 | Manual starting using :command:`mpiexec` | |
51 | --------------------------------------- |
|
51 | --------------------------------------- | |
52 |
|
52 | |||
53 |
If you want to start the IPython engines using the :command:`mpi |
|
53 | If you want to start the IPython engines using the :command:`mpiexec`, just do:: | |
54 |
|
54 | |||
55 |
$ mpi |
|
55 | $ mpiexec -n 4 ipengine --mpi=mpi4py | |
56 |
|
56 | |||
57 | This requires that you already have a controller running and that the FURL |
|
57 | This requires that you already have a controller running and that the FURL | |
58 | files for the engines are in place. We also have built in support for |
|
58 | files for the engines are in place. We also have built in support for | |
59 | PyTrilinos [PyTrilinos]_, which can be used (assuming is installed) by |
|
59 | PyTrilinos [PyTrilinos]_, which can be used (assuming is installed) by | |
60 | starting the engines with:: |
|
60 | starting the engines with:: | |
61 |
|
61 | |||
62 |
mpi |
|
62 | mpiexec -n 4 ipengine --mpi=pytrilinos | |
63 |
|
63 | |||
64 | Automatic starting using PBS and :command:`ipcluster` |
|
64 | Automatic starting using PBS and :command:`ipcluster` | |
65 | ----------------------------------------------------- |
|
65 | ----------------------------------------------------- | |
66 |
|
66 | |||
67 | The :command:`ipcluster` command also has built-in integration with PBS. For more information on this approach, see our documentation on :ref:`ipcluster <parallel_process>`. |
|
67 | The :command:`ipcluster` command also has built-in integration with PBS. For more information on this approach, see our documentation on :ref:`ipcluster <parallel_process>`. | |
68 |
|
68 | |||
69 | Actually using MPI |
|
69 | Actually using MPI | |
70 | ================== |
|
70 | ================== | |
71 |
|
71 | |||
72 | Once the engines are running with MPI enabled, you are ready to go. You can now call any code that uses MPI in the IPython engines. And, all of this can be done interactively. Here we show a simple example that uses mpi4py [mpi4py]_. |
|
72 | Once the engines are running with MPI enabled, you are ready to go. You can now call any code that uses MPI in the IPython engines. And, all of this can be done interactively. Here we show a simple example that uses mpi4py [mpi4py]_. | |
73 |
|
73 | |||
74 | First, lets define a simply function that uses MPI to calculate the sum of a distributed array. Save the following text in a file called :file:`psum.py`: |
|
74 | First, lets define a simply function that uses MPI to calculate the sum of a distributed array. Save the following text in a file called :file:`psum.py`: | |
75 |
|
75 | |||
76 | .. sourcecode:: python |
|
76 | .. sourcecode:: python | |
77 |
|
77 | |||
78 | from mpi4py import MPI |
|
78 | from mpi4py import MPI | |
79 | import numpy as np |
|
79 | import numpy as np | |
80 |
|
80 | |||
81 | def psum(a): |
|
81 | def psum(a): | |
82 | s = np.sum(a) |
|
82 | s = np.sum(a) | |
83 | return MPI.COMM_WORLD.Allreduce(s,MPI.SUM) |
|
83 | return MPI.COMM_WORLD.Allreduce(s,MPI.SUM) | |
84 |
|
84 | |||
85 | Now, start an IPython cluster in the same directory as :file:`psum.py`:: |
|
85 | Now, start an IPython cluster in the same directory as :file:`psum.py`:: | |
86 |
|
86 | |||
87 |
$ ipcluster mpi |
|
87 | $ ipcluster mpiexec -n 4 | |
88 |
|
88 | |||
89 | Finally, connect to the cluster and use this function interactively. In this case, we create a random array on each engine and sum up all the random arrays using our :func:`psum` function: |
|
89 | Finally, connect to the cluster and use this function interactively. In this case, we create a random array on each engine and sum up all the random arrays using our :func:`psum` function: | |
90 |
|
90 | |||
91 | .. sourcecode:: ipython |
|
91 | .. sourcecode:: ipython | |
92 |
|
92 | |||
93 | In [1]: from IPython.kernel import client |
|
93 | In [1]: from IPython.kernel import client | |
94 |
|
94 | |||
95 | In [2]: mec = client.MultiEngineClient() |
|
95 | In [2]: mec = client.MultiEngineClient() | |
96 |
|
96 | |||
97 | In [3]: mec.activate() |
|
97 | In [3]: mec.activate() | |
98 |
|
98 | |||
99 | In [4]: px import numpy as np |
|
99 | In [4]: px import numpy as np | |
100 | Parallel execution on engines: all |
|
100 | Parallel execution on engines: all | |
101 | Out[4]: |
|
101 | Out[4]: | |
102 | <Results List> |
|
102 | <Results List> | |
103 | [0] In [13]: import numpy as np |
|
103 | [0] In [13]: import numpy as np | |
104 | [1] In [13]: import numpy as np |
|
104 | [1] In [13]: import numpy as np | |
105 | [2] In [13]: import numpy as np |
|
105 | [2] In [13]: import numpy as np | |
106 | [3] In [13]: import numpy as np |
|
106 | [3] In [13]: import numpy as np | |
107 |
|
107 | |||
108 | In [6]: px a = np.random.rand(100) |
|
108 | In [6]: px a = np.random.rand(100) | |
109 | Parallel execution on engines: all |
|
109 | Parallel execution on engines: all | |
110 | Out[6]: |
|
110 | Out[6]: | |
111 | <Results List> |
|
111 | <Results List> | |
112 | [0] In [15]: a = np.random.rand(100) |
|
112 | [0] In [15]: a = np.random.rand(100) | |
113 | [1] In [15]: a = np.random.rand(100) |
|
113 | [1] In [15]: a = np.random.rand(100) | |
114 | [2] In [15]: a = np.random.rand(100) |
|
114 | [2] In [15]: a = np.random.rand(100) | |
115 | [3] In [15]: a = np.random.rand(100) |
|
115 | [3] In [15]: a = np.random.rand(100) | |
116 |
|
116 | |||
117 | In [7]: px from psum import psum |
|
117 | In [7]: px from psum import psum | |
118 | Parallel execution on engines: all |
|
118 | Parallel execution on engines: all | |
119 | Out[7]: |
|
119 | Out[7]: | |
120 | <Results List> |
|
120 | <Results List> | |
121 | [0] In [16]: from psum import psum |
|
121 | [0] In [16]: from psum import psum | |
122 | [1] In [16]: from psum import psum |
|
122 | [1] In [16]: from psum import psum | |
123 | [2] In [16]: from psum import psum |
|
123 | [2] In [16]: from psum import psum | |
124 | [3] In [16]: from psum import psum |
|
124 | [3] In [16]: from psum import psum | |
125 |
|
125 | |||
126 | In [8]: px s = psum(a) |
|
126 | In [8]: px s = psum(a) | |
127 | Parallel execution on engines: all |
|
127 | Parallel execution on engines: all | |
128 | Out[8]: |
|
128 | Out[8]: | |
129 | <Results List> |
|
129 | <Results List> | |
130 | [0] In [17]: s = psum(a) |
|
130 | [0] In [17]: s = psum(a) | |
131 | [1] In [17]: s = psum(a) |
|
131 | [1] In [17]: s = psum(a) | |
132 | [2] In [17]: s = psum(a) |
|
132 | [2] In [17]: s = psum(a) | |
133 | [3] In [17]: s = psum(a) |
|
133 | [3] In [17]: s = psum(a) | |
134 |
|
134 | |||
135 | In [9]: px print s |
|
135 | In [9]: px print s | |
136 | Parallel execution on engines: all |
|
136 | Parallel execution on engines: all | |
137 | Out[9]: |
|
137 | Out[9]: | |
138 | <Results List> |
|
138 | <Results List> | |
139 | [0] In [18]: print s |
|
139 | [0] In [18]: print s | |
140 | [0] Out[18]: 187.451545803 |
|
140 | [0] Out[18]: 187.451545803 | |
141 |
|
141 | |||
142 | [1] In [18]: print s |
|
142 | [1] In [18]: print s | |
143 | [1] Out[18]: 187.451545803 |
|
143 | [1] Out[18]: 187.451545803 | |
144 |
|
144 | |||
145 | [2] In [18]: print s |
|
145 | [2] In [18]: print s | |
146 | [2] Out[18]: 187.451545803 |
|
146 | [2] Out[18]: 187.451545803 | |
147 |
|
147 | |||
148 | [3] In [18]: print s |
|
148 | [3] In [18]: print s | |
149 | [3] Out[18]: 187.451545803 |
|
149 | [3] Out[18]: 187.451545803 | |
150 |
|
150 | |||
151 | Any Python code that makes calls to MPI can be used in this manner, including |
|
151 | Any Python code that makes calls to MPI can be used in this manner, including | |
152 | compiled C, C++ and Fortran libraries that have been exposed to Python. |
|
152 | compiled C, C++ and Fortran libraries that have been exposed to Python. | |
153 |
|
153 | |||
154 | .. [MPI] Message Passing Interface. http://www-unix.mcs.anl.gov/mpi/ |
|
154 | .. [MPI] Message Passing Interface. http://www-unix.mcs.anl.gov/mpi/ | |
155 | .. [mpi4py] MPI for Python. mpi4py: http://mpi4py.scipy.org/ |
|
155 | .. [mpi4py] MPI for Python. mpi4py: http://mpi4py.scipy.org/ | |
156 | .. [OpenMPI] Open MPI. http://www.open-mpi.org/ |
|
156 | .. [OpenMPI] Open MPI. http://www.open-mpi.org/ | |
157 | .. [PyTrilinos] PyTrilinos. http://trilinos.sandia.gov/packages/pytrilinos/ No newline at end of file |
|
157 | .. [PyTrilinos] PyTrilinos. http://trilinos.sandia.gov/packages/pytrilinos/ |
General Comments 0
You need to be logged in to leave comments.
Login now