Show More
@@ -1,157 +1,157 b'' | |||
|
1 | 1 | .. _parallelmpi: |
|
2 | 2 | |
|
3 | 3 | ======================= |
|
4 | 4 | Using MPI with IPython |
|
5 | 5 | ======================= |
|
6 | 6 | |
|
7 | 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 | 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 | 11 | Additional installation requirements |
|
12 | 12 | ==================================== |
|
13 | 13 | |
|
14 | 14 | If you want to use MPI with IPython, you will need to install: |
|
15 | 15 | |
|
16 | 16 | * A standard MPI implementation such as OpenMPI [OpenMPI]_ or MPICH. |
|
17 | 17 | * The mpi4py [mpi4py]_ package. |
|
18 | 18 | |
|
19 | 19 | .. note:: |
|
20 | 20 | |
|
21 | 21 | The mpi4py package is not a strict requirement. However, you need to |
|
22 | 22 | have *some* way of calling MPI from Python. You also need some way of |
|
23 | 23 | making sure that :func:`MPI_Init` is called when the IPython engines start |
|
24 | 24 | up. There are a number of ways of doing this and a good number of |
|
25 | 25 | associated subtleties. We highly recommend just using mpi4py as it |
|
26 | 26 | takes care of most of these problems. If you want to do something |
|
27 | 27 | different, let us know and we can help you get started. |
|
28 | 28 | |
|
29 | 29 | Starting the engines with MPI enabled |
|
30 | 30 | ===================================== |
|
31 | 31 | |
|
32 | 32 | To use code that calls MPI, there are typically two things that MPI requires. |
|
33 | 33 | |
|
34 | 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 | 36 | 2. Once the process starts, it must call :func:`MPI_Init`. |
|
37 | 37 | |
|
38 | 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 | 47 | This approach is best as interrupting :command:`ipcluster` will automatically |
|
48 | 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 | 57 | This requires that you already have a controller running and that the FURL |
|
58 | 58 | files for the engines are in place. We also have built in support for |
|
59 | 59 | PyTrilinos [PyTrilinos]_, which can be used (assuming is installed) by |
|
60 | 60 | starting the engines with:: |
|
61 | 61 | |
|
62 |
mpi |
|
|
62 | mpiexec -n 4 ipengine --mpi=pytrilinos | |
|
63 | 63 | |
|
64 | 64 | Automatic starting using PBS and :command:`ipcluster` |
|
65 | 65 | ----------------------------------------------------- |
|
66 | 66 | |
|
67 | 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 | 69 | Actually using MPI |
|
70 | 70 | ================== |
|
71 | 71 | |
|
72 | 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 | 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 | 76 | .. sourcecode:: python |
|
77 | 77 | |
|
78 | 78 | from mpi4py import MPI |
|
79 | 79 | import numpy as np |
|
80 | 80 | |
|
81 | 81 | def psum(a): |
|
82 | 82 | s = np.sum(a) |
|
83 | 83 | return MPI.COMM_WORLD.Allreduce(s,MPI.SUM) |
|
84 | 84 | |
|
85 | 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 | 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 | 91 | .. sourcecode:: ipython |
|
92 | 92 | |
|
93 | 93 | In [1]: from IPython.kernel import client |
|
94 | 94 | |
|
95 | 95 | In [2]: mec = client.MultiEngineClient() |
|
96 | 96 | |
|
97 | 97 | In [3]: mec.activate() |
|
98 | 98 | |
|
99 | 99 | In [4]: px import numpy as np |
|
100 | 100 | Parallel execution on engines: all |
|
101 | 101 | Out[4]: |
|
102 | 102 | <Results List> |
|
103 | 103 | [0] In [13]: import numpy as np |
|
104 | 104 | [1] In [13]: import numpy as np |
|
105 | 105 | [2] In [13]: import numpy as np |
|
106 | 106 | [3] In [13]: import numpy as np |
|
107 | 107 | |
|
108 | 108 | In [6]: px a = np.random.rand(100) |
|
109 | 109 | Parallel execution on engines: all |
|
110 | 110 | Out[6]: |
|
111 | 111 | <Results List> |
|
112 | 112 | [0] In [15]: a = np.random.rand(100) |
|
113 | 113 | [1] In [15]: a = np.random.rand(100) |
|
114 | 114 | [2] In [15]: a = np.random.rand(100) |
|
115 | 115 | [3] In [15]: a = np.random.rand(100) |
|
116 | 116 | |
|
117 | 117 | In [7]: px from psum import psum |
|
118 | 118 | Parallel execution on engines: all |
|
119 | 119 | Out[7]: |
|
120 | 120 | <Results List> |
|
121 | 121 | [0] In [16]: from psum import psum |
|
122 | 122 | [1] In [16]: from psum import psum |
|
123 | 123 | [2] In [16]: from psum import psum |
|
124 | 124 | [3] In [16]: from psum import psum |
|
125 | 125 | |
|
126 | 126 | In [8]: px s = psum(a) |
|
127 | 127 | Parallel execution on engines: all |
|
128 | 128 | Out[8]: |
|
129 | 129 | <Results List> |
|
130 | 130 | [0] In [17]: s = psum(a) |
|
131 | 131 | [1] In [17]: s = psum(a) |
|
132 | 132 | [2] In [17]: s = psum(a) |
|
133 | 133 | [3] In [17]: s = psum(a) |
|
134 | 134 | |
|
135 | 135 | In [9]: px print s |
|
136 | 136 | Parallel execution on engines: all |
|
137 | 137 | Out[9]: |
|
138 | 138 | <Results List> |
|
139 | 139 | [0] In [18]: print s |
|
140 | 140 | [0] Out[18]: 187.451545803 |
|
141 | 141 | |
|
142 | 142 | [1] In [18]: print s |
|
143 | 143 | [1] Out[18]: 187.451545803 |
|
144 | 144 | |
|
145 | 145 | [2] In [18]: print s |
|
146 | 146 | [2] Out[18]: 187.451545803 |
|
147 | 147 | |
|
148 | 148 | [3] In [18]: print s |
|
149 | 149 | [3] Out[18]: 187.451545803 |
|
150 | 150 | |
|
151 | 151 | Any Python code that makes calls to MPI can be used in this manner, including |
|
152 | 152 | compiled C, C++ and Fortran libraries that have been exposed to Python. |
|
153 | 153 | |
|
154 | 154 | .. [MPI] Message Passing Interface. http://www-unix.mcs.anl.gov/mpi/ |
|
155 | 155 | .. [mpi4py] MPI for Python. mpi4py: http://mpi4py.scipy.org/ |
|
156 | 156 | .. [OpenMPI] Open MPI. http://www.open-mpi.org/ |
|
157 | 157 | .. [PyTrilinos] PyTrilinos. http://trilinos.sandia.gov/packages/pytrilinos/ No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now