1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
import time
import numpy
# import IPython.kernel.magic
from IPython.parallel import Client, Reference
from IPython.parallel import error
rc = Client()
mux = rc[:]
#-------------------------------------------------------------------------------
# Setup
#-------------------------------------------------------------------------------
%load_ext parallelmagic
mux.block = True
mux.clear()
mux.activate()
n = len(rc)
assert n >= 4, "Not Enough Engines: %i, 4 needed for this script"%n
values = [
10,
1.0,
range(100),
('asdf', 1000),
{'a': 10, 'b': 20}
]
keys = ['a','b','c','d','e']
sequences = [
range(100),
numpy.arange(100)
]
#-------------------------------------------------------------------------------
# Blocking execution
#-------------------------------------------------------------------------------
# Execute
with mux.sync_imports():
import math
import numpy
mux.execute('a = 2.0*math.pi')
print mux['a']
for id in mux.targets:
mux.execute('b=%d' % id, targets=id)
try:
mux.execute('b = 10',targets=max(mux.targets)+1)
except IndexError:
print "Caught invalid engine ID OK."
try:
mux.apply(lambda : 1/0)
except error.CompositeError:
print "Caught 1/0 correctly."
%px print a, b
try:
%px 1/0
except error.CompositeError:
print "Caught 1/0 correctly."
# %autopx
%px a = numpy.random.random((4,4))
%px a = a+a.transpose()
# %autopx
print mux.apply(numpy.linalg.eigvals, Reference('a'))
mux.targets = [0,2]
%px a = 5
mux.targets = [1,3]
%px a = 10
mux.targets = rc.ids
print mux['a']
# Push/Pull
mux.push(dict(a=10, b=30, c={'f':range(10)}))
mux.pull(('a', 'b'))
for id in mux.targets:
mux.push(dict(a=id), targets=id)
for id in mux.targets:
mux.pull('a', targets=id)
mux.pull('a')
mux['a'] = 100
mux['a']
# get_result/reset/keys
mux.get_result()
%result
mux.apply(lambda : globals().keys())
mux.clear()
mux.apply(lambda : globals().keys())
try:
%result
except error.CompositeError:
print "Caught IndexError ok."
%px a = 5
mux.get_result(-1)
mux.apply(lambda : globals().keys())
# Queue management methods
%px import time
ars = [mux.apply_async(time.sleep, 2.0) for x in range(5)]
mux.queue_status()
time.sleep(3.0)
mux.abort(ars, block=True)
mux.queue_status()
time.sleep(2.0)
mux.queue_status()
mux.wait(ars)
for ar in ars:
try:
ar.r
except error.CompositeError:
print "Caught QueueCleared OK."
# scatter/gather
mux.scatter('a', range(10))
mux.gather('a')
mux.scatter('b', numpy.arange(10))
mux.gather('b')
#-------------------------------------------------------------------------------
# Non-Blocking execution
#-------------------------------------------------------------------------------
mux.block = False
# execute
ar1 = mux.execute('a=5')
with mux.sync_imports():
import sets
ar1.wait()
ar1 = mux.execute('1/0')
ar2 = mux.execute('c = sets.Set()')
mux.wait((ar1, ar2))
try:
ar1.r
except error.CompositeError:
print "Caught ZeroDivisionError OK."
ar = mux.execute("arint 'hi'")
ar.r
ar = mux.apply(lambda : 1/0)
try:
ar.r
except error.CompositeError:
print "Caught ZeroDivisionError OK."
# Make sure we can reraise it!
try:
ar.r
except error.CompositeError:
print "Caught ZeroDivisionError OK."
# push/pull
ar1 = mux.push(dict(a=10))
ar1.get()
ar2 = mux.pull('a')
ar2.r
# This is a command to make sure the end of the file is happy.
print "The tests are done!"
|