##// END OF EJS Templates
update a few parallel examples...
MinRK -
Show More
@@ -6,6 +6,7 b' This only works for a local cluster, because the filenames are local paths.'
6 6
7 7
8 8 import os
9 import time
9 10 import urllib
10 11
11 12 from itertools import repeat
@@ -49,8 +50,11 b" if __name__ == '__main__':"
49 50 # Run the serial version
50 51 print "Serial word frequency count:"
51 52 text = open('davinci.txt').read()
53 tic = time.time()
52 54 freqs = wordfreq(text)
55 toc = time.time()
53 56 print_wordfreq(freqs, 10)
57 print "Took %.3f s to calcluate"%(toc-tic)
54 58
55 59
56 60 # The parallel version
@@ -67,7 +71,10 b" if __name__ == '__main__':"
67 71
68 72 cwd = os.path.abspath(os.getcwd())
69 73 fnames = [ os.path.join(cwd, 'davinci%i.txt'%i) for i in range(n)]
74 tic = time.time()
70 75 pfreqs = pwordfreq(view,fnames)
76 toc = time.time()
71 77 print_wordfreq(freqs)
78 print "Took %.3f s to calcluate on %i engines"%(toc-tic, len(view.targets))
72 79 # cleanup split files
73 80 map(os.remove, fnames)
@@ -1,7 +1,7 b''
1 1 from IPython.parallel import *
2 2
3 3 client = Client()
4 view = client[:]
4 view = client.load_balanced_view()
5 5
6 6 @view.remote(block=True)
7 7 def square(a):
@@ -21,8 +21,8 b' arlist = map(square, range(42))'
21 21 squares2 = [ r.get() for r in arlist ]
22 22
23 23 # now the more convenient @parallel decorator, which has a map method:
24
25 @view.parallel(block=False)
24 view2 = client[:]
25 @view2.parallel(block=False)
26 26 def psquare(a):
27 27 """return square of a number"""
28 28 return a*a
@@ -32,6 +32,5 b' ar = psquare.map(range(42))'
32 32
33 33 # wait for the results to be done:
34 34 squares3 = ar.get()
35
36 35 print squares == squares2, squares3==squares
37 36 # True No newline at end of file
@@ -15,7 +15,7 b" def echo(s=''):"
15 15
16 16 def time_throughput(nmessages, t=0, f=wait):
17 17 client = parallel.Client()
18 view = client[None]
18 view = client.load_balanced_view()
19 19 # do one ping before starting timing
20 20 if f is echo:
21 21 t = np.random.random(t/8)
@@ -25,32 +25,10 b' def time_throughput(nmessages, t=0, f=wait):'
25 25 for i in xrange(nmessages):
26 26 view.apply(f, t)
27 27 lap = time.time()
28 client.barrier()
28 client.wait()
29 29 toc = time.time()
30 30 return lap-tic, toc-tic
31 31
32 def time_twisted(nmessages, t=0, f=wait):
33 from IPython.kernel import client as kc
34 client = kc.TaskClient()
35 if f is wait:
36 s = "import time; time.sleep(%f)"%t
37 task = kc.StringTask(s)
38 elif f is echo:
39 t = np.random.random(t/8)
40 s = "s=t"
41 task = kc.StringTask(s, push=dict(t=t), pull=['s'])
42 else:
43 raise
44 # do one ping before starting timing
45 client.barrier(client.run(task))
46 tic = time.time()
47 tids = []
48 for i in xrange(nmessages):
49 tids.append(client.run(task))
50 lap = time.time()
51 client.barrier(tids)
52 toc = time.time()
53 return lap-tic, toc-tic
54 32
55 33 def do_runs(nlist,t=0,f=wait, trials=2, runner=time_throughput):
56 34 A = np.zeros((len(nlist),2))
@@ -13,5 +13,5 b' print "Using map_async: ", result'
13 13 @view.parallel(block=True)
14 14 def f(x): return 2*x
15 15
16 result = f(range(10))
16 result = f.map(range(10))
17 17 print "Using a parallel function: ", result No newline at end of file
@@ -1,6 +1,6 b''
1 1 """Parallel histogram function"""
2 2 import numpy
3 from IPython.utils.pickleutil import Reference
3 from IPython.parallel import Reference
4 4
5 5 def phistogram(view, a, bins=10, rng=None, normed=False):
6 6 """Compute the histogram of a remote array a.
@@ -24,8 +24,8 b' from IPython.parallel import Client'
24 24 def main():
25 25 parser = OptionParser()
26 26 parser.set_defaults(n=100)
27 parser.set_defaults(tmin=1)
28 parser.set_defaults(tmax=60)
27 parser.set_defaults(tmin=1e-3)
28 parser.set_defaults(tmax=1)
29 29 parser.set_defaults(profile='default')
30 30
31 31 parser.add_option("-n", type='int', dest='n',
@@ -45,7 +45,8 b' def main():'
45 45 print view
46 46 rc.block=True
47 47 nengines = len(rc.ids)
48 rc[:].execute('from IPython.utils.timing import time')
48 with rc[:].sync_imports():
49 from IPython.utils.timing import time
49 50
50 51 # the jobs should take a random time within a range
51 52 times = [random.random()*(opts.tmax-opts.tmin)+opts.tmin for i in range(opts.n)]
General Comments 0
You need to be logged in to leave comments. Login now