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