Show More
@@ -96,9 +96,9 b' distributed array. Save the following text in a file called :file:`psum.py`:' | |||
|
96 | 96 | import numpy as np |
|
97 | 97 | |
|
98 | 98 | def psum(a): |
|
99 | s = np.sum(a) | |
|
99 | locsum = np.sum(a) | |
|
100 | 100 | rcvBuf = np.array(0.0,'d') |
|
101 | MPI.COMM_WORLD.Allreduce([s, MPI.DOUBLE], | |
|
101 | MPI.COMM_WORLD.Allreduce([locsum, MPI.DOUBLE], | |
|
102 | 102 | [rcvBuf, MPI.DOUBLE], |
|
103 | 103 | op=MPI.SUM) |
|
104 | 104 | return rcvBuf |
@@ -119,24 +119,29 b' using our :func:`psum` function:' | |||
|
119 | 119 | .. sourcecode:: ipython |
|
120 | 120 | |
|
121 | 121 | In [1]: from IPython.parallel import Client |
|
122 | ||
|
123 |
In [ |
|
|
124 | ||
|
125 |
In [ |
|
|
126 | ||
|
127 |
In [ |
|
|
128 | ||
|
122 | ||
|
123 | In [2]: c = Client(profile='mpi') | |
|
124 | ||
|
125 | In [3]: view = c[:] | |
|
126 | ||
|
127 | In [4]: view.activate() # enable magics | |
|
128 | ||
|
129 | 129 | # run the contents of the file on each engine: |
|
130 |
In [ |
|
|
131 | ||
|
132 | In [6]: %px a = np.random.rand(100) | |
|
133 | Parallel execution on engines: [0,1,2,3] | |
|
134 | ||
|
135 | In [8]: %px s = psum(a) | |
|
130 | In [5]: view.run('psum.py') | |
|
131 | ||
|
132 | In [6]: view.scatter('a',np.arange(16,dtype='float')) | |
|
133 | ||
|
134 | In [7]: view['a'] | |
|
135 | Out[7]: [array([ 0., 1., 2., 3.]), | |
|
136 | array([ 4., 5., 6., 7.]), | |
|
137 | array([ 8., 9., 10., 11.]), | |
|
138 | array([ 12., 13., 14., 15.])] | |
|
139 | ||
|
140 | In [7]: %px totalsum = psum(a) | |
|
136 | 141 | Parallel execution on engines: [0,1,2,3] |
|
137 | ||
|
138 |
In [ |
|
|
139 | Out[9]: [187.451545803,187.451545803,187.451545803,187.451545803] | |
|
142 | ||
|
143 | In [8]: view['totalsum'] | |
|
144 | Out[8]: [120.0, 120.0, 120.0, 120.0] | |
|
140 | 145 | |
|
141 | 146 | Any Python code that makes calls to MPI can be used in this manner, including |
|
142 | 147 | compiled C, C++ and Fortran libraries that have been exposed to Python. |
General Comments 0
You need to be logged in to leave comments.
Login now