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