IPython Parallel Push/Execute/Pull Demo¶
1. Allocate random matrices locally¶
InĀ [113]:
import numpy as np
InĀ [114]:
N = 2000
InĀ [115]:
a = np.random.randn(N, N)
InĀ [116]:
b = np.random.randn(N, N)
2. Compute their dot product locally¶
InĀ [117]:
c = np.dot(a,b)
3. Initialize the IPython Parallel Client¶
InĀ [118]:
from IPython.parallel import Client
InĀ [119]:
cl = Client()
InĀ [120]:
cl.ids
Out[120]:
[0, 1, 2]
4. Fetch a 'view' from the IPython parallel client¶
InĀ [121]:
v = cl[:]
5. Import NumPy on all IPEngines¶
InĀ [Ā ]:
v.execute('import numpy as np', block=True)
6. Push both random matrices to all IPEngines¶
InĀ [122]:
v.push(dict(a=a, b=b), block=True)
Out[122]:
[None, None, None]
7. Compute dot product on all IPEngines¶
InĀ [123]:
r = v.execute('c = np.dot(a,b); print c', block=True)
InĀ [124]:
r.display_outputs()
[stdout:0] [[ -4.67202217 104.39252912 -78.37783997 ..., -29.30709274 6.66403977 29.92526411] [ 39.22517891 73.37959579 -140.66938625 ..., -30.40772693 9.21601775 29.80644042] [ 31.60094895 -26.90207753 42.07903369 ..., 6.16564342 -40.9135798 -57.82278587] ..., [ -25.57059809 -63.64479316 22.76541772 ..., 1.68819694 -12.9543937 -61.07450388] [ 15.3646617 57.48457183 -34.15856279 ..., 7.34530517 -16.26106107 -17.58811896] [ -13.26202943 75.42065094 74.86911918 ..., -0.55862744 6.72326022 10.38620045]] [stdout:1] [[ -4.67202217 104.39252912 -78.37783997 ..., -29.30709274 6.66403977 29.92526411] [ 39.22517891 73.37959579 -140.66938625 ..., -30.40772693 9.21601775 29.80644042] [ 31.60094895 -26.90207753 42.07903369 ..., 6.16564342 -40.9135798 -57.82278587] ..., [ -25.57059809 -63.64479316 22.76541772 ..., 1.68819694 -12.9543937 -61.07450388] [ 15.3646617 57.48457183 -34.15856279 ..., 7.34530517 -16.26106107 -17.58811896] [ -13.26202943 75.42065094 74.86911918 ..., -0.55862744 6.72326022 10.38620045]] [stdout:2] [[ -4.67202217 104.39252912 -78.37783997 ..., -29.30709274 6.66403977 29.92526411] [ 39.22517891 73.37959579 -140.66938625 ..., -30.40772693 9.21601775 29.80644042] [ 31.60094895 -26.90207753 42.07903369 ..., 6.16564342 -40.9135798 -57.82278587] ..., [ -25.57059809 -63.64479316 22.76541772 ..., 1.68819694 -12.9543937 -61.07450388] [ 15.3646617 57.48457183 -34.15856279 ..., 7.34530517 -16.26106107 -17.58811896] [ -13.26202943 75.42065094 74.86911918 ..., -0.55862744 6.72326022 10.38620045]]
8. Pull the results from all IPEngines¶
InĀ [125]:
dot = v.pull('c', block=True)
InĀ [126]:
print dot
[array([[ -4.67202217, 104.39252912, -78.37783997, ..., -29.30709274, 6.66403977, 29.92526411], [ 39.22517891, 73.37959579, -140.66938625, ..., -30.40772693, 9.21601775, 29.80644042], [ 31.60094895, -26.90207753, 42.07903369, ..., 6.16564342, -40.9135798 , -57.82278587], ..., [ -25.57059809, -63.64479316, 22.76541772, ..., 1.68819694, -12.9543937 , -61.07450388], [ 15.3646617 , 57.48457183, -34.15856279, ..., 7.34530517, -16.26106107, -17.58811896], [ -13.26202943, 75.42065094, 74.86911918, ..., -0.55862744, 6.72326022, 10.38620045]]), array([[ -4.67202217, 104.39252912, -78.37783997, ..., -29.30709274, 6.66403977, 29.92526411], [ 39.22517891, 73.37959579, -140.66938625, ..., -30.40772693, 9.21601775, 29.80644042], [ 31.60094895, -26.90207753, 42.07903369, ..., 6.16564342, -40.9135798 , -57.82278587], ..., [ -25.57059809, -63.64479316, 22.76541772, ..., 1.68819694, -12.9543937 , -61.07450388], [ 15.3646617 , 57.48457183, -34.15856279, ..., 7.34530517, -16.26106107, -17.58811896], [ -13.26202943, 75.42065094, 74.86911918, ..., -0.55862744, 6.72326022, 10.38620045]]), array([[ -4.67202217, 104.39252912, -78.37783997, ..., -29.30709274, 6.66403977, 29.92526411], [ 39.22517891, 73.37959579, -140.66938625, ..., -30.40772693, 9.21601775, 29.80644042], [ 31.60094895, -26.90207753, 42.07903369, ..., 6.16564342, -40.9135798 , -57.82278587], ..., [ -25.57059809, -63.64479316, 22.76541772, ..., 1.68819694, -12.9543937 , -61.07450388], [ 15.3646617 , 57.48457183, -34.15856279, ..., 7.34530517, -16.26106107, -17.58811896], [ -13.26202943, 75.42065094, 74.86911918, ..., -0.55862744, 6.72326022, 10.38620045]])]
9. Check remote results against local results¶
InĀ [127]:
assert np.allclose(c, dot[0])
InĀ [128]:
assert np.allclose(dot[0], dot[1])
InĀ [129]:
assert np.allclose(dot[1], dot[2])