From b74d704b179ee6f3fd48b4c3b8c9888cba754b24 2011-07-05 04:56:28 From: MinRK Date: 2011-07-05 04:56:28 Subject: [PATCH] update a few parallel examples a few files had gone out of date with method names, etc. --- diff --git a/docs/examples/newparallel/davinci/pwordfreq.py b/docs/examples/newparallel/davinci/pwordfreq.py index 8b9b02b..439cc16 100644 --- a/docs/examples/newparallel/davinci/pwordfreq.py +++ b/docs/examples/newparallel/davinci/pwordfreq.py @@ -6,6 +6,7 @@ This only works for a local cluster, because the filenames are local paths. import os +import time import urllib from itertools import repeat @@ -49,8 +50,11 @@ if __name__ == '__main__': # Run the serial version print "Serial word frequency count:" text = open('davinci.txt').read() + tic = time.time() freqs = wordfreq(text) + toc = time.time() print_wordfreq(freqs, 10) + print "Took %.3f s to calcluate"%(toc-tic) # The parallel version @@ -67,7 +71,10 @@ if __name__ == '__main__': cwd = os.path.abspath(os.getcwd()) fnames = [ os.path.join(cwd, 'davinci%i.txt'%i) for i in range(n)] + tic = time.time() pfreqs = pwordfreq(view,fnames) + toc = time.time() print_wordfreq(freqs) + print "Took %.3f s to calcluate on %i engines"%(toc-tic, len(view.targets)) # cleanup split files map(os.remove, fnames) diff --git a/docs/examples/newparallel/demo/map.py b/docs/examples/newparallel/demo/map.py index d0316d1..f9f4f5e 100644 --- a/docs/examples/newparallel/demo/map.py +++ b/docs/examples/newparallel/demo/map.py @@ -1,7 +1,7 @@ from IPython.parallel import * client = Client() -view = client[:] +view = client.load_balanced_view() @view.remote(block=True) def square(a): @@ -21,8 +21,8 @@ arlist = map(square, range(42)) squares2 = [ r.get() for r in arlist ] # now the more convenient @parallel decorator, which has a map method: - -@view.parallel(block=False) +view2 = client[:] +@view2.parallel(block=False) def psquare(a): """return square of a number""" return a*a @@ -32,6 +32,5 @@ ar = psquare.map(range(42)) # wait for the results to be done: squares3 = ar.get() - print squares == squares2, squares3==squares # True \ No newline at end of file diff --git a/docs/examples/newparallel/demo/throughput.py b/docs/examples/newparallel/demo/throughput.py index b9eaa08..416a316 100644 --- a/docs/examples/newparallel/demo/throughput.py +++ b/docs/examples/newparallel/demo/throughput.py @@ -15,7 +15,7 @@ def echo(s=''): def time_throughput(nmessages, t=0, f=wait): client = parallel.Client() - view = client[None] + view = client.load_balanced_view() # do one ping before starting timing if f is echo: t = np.random.random(t/8) @@ -25,32 +25,10 @@ def time_throughput(nmessages, t=0, f=wait): for i in xrange(nmessages): view.apply(f, t) lap = time.time() - client.barrier() + client.wait() toc = time.time() return lap-tic, toc-tic -def time_twisted(nmessages, t=0, f=wait): - from IPython.kernel import client as kc - client = kc.TaskClient() - if f is wait: - s = "import time; time.sleep(%f)"%t - task = kc.StringTask(s) - elif f is echo: - t = np.random.random(t/8) - s = "s=t" - task = kc.StringTask(s, push=dict(t=t), pull=['s']) - else: - raise - # do one ping before starting timing - client.barrier(client.run(task)) - tic = time.time() - tids = [] - for i in xrange(nmessages): - tids.append(client.run(task)) - lap = time.time() - client.barrier(tids) - toc = time.time() - return lap-tic, toc-tic def do_runs(nlist,t=0,f=wait, trials=2, runner=time_throughput): A = np.zeros((len(nlist),2)) diff --git a/docs/examples/newparallel/multienginemap.py b/docs/examples/newparallel/multienginemap.py index 2fa8849..8b87fad 100644 --- a/docs/examples/newparallel/multienginemap.py +++ b/docs/examples/newparallel/multienginemap.py @@ -13,5 +13,5 @@ print "Using map_async: ", result @view.parallel(block=True) def f(x): return 2*x -result = f(range(10)) +result = f.map(range(10)) print "Using a parallel function: ", result \ No newline at end of file diff --git a/docs/examples/newparallel/phistogram.py b/docs/examples/newparallel/phistogram.py index c2605bb..4393e74 100644 --- a/docs/examples/newparallel/phistogram.py +++ b/docs/examples/newparallel/phistogram.py @@ -1,6 +1,6 @@ """Parallel histogram function""" import numpy -from IPython.utils.pickleutil import Reference +from IPython.parallel import Reference def phistogram(view, a, bins=10, rng=None, normed=False): """Compute the histogram of a remote array a. diff --git a/docs/examples/newparallel/task_profiler.py b/docs/examples/newparallel/task_profiler.py index b4f05ba..c88c4ef 100644 --- a/docs/examples/newparallel/task_profiler.py +++ b/docs/examples/newparallel/task_profiler.py @@ -24,8 +24,8 @@ from IPython.parallel import Client def main(): parser = OptionParser() parser.set_defaults(n=100) - parser.set_defaults(tmin=1) - parser.set_defaults(tmax=60) + parser.set_defaults(tmin=1e-3) + parser.set_defaults(tmax=1) parser.set_defaults(profile='default') parser.add_option("-n", type='int', dest='n', @@ -45,7 +45,8 @@ def main(): print view rc.block=True nengines = len(rc.ids) - rc[:].execute('from IPython.utils.timing import time') + with rc[:].sync_imports(): + from IPython.utils.timing import time # the jobs should take a random time within a range times = [random.random()*(opts.tmax-opts.tmin)+opts.tmin for i in range(opts.n)]