rmt.ipy
56 lines
| 1.5 KiB
| text/plain
|
TextLexer
MinRK
|
r3670 | #------------------------------------------------------------------------------- | ||
# Driver code that the client runs. | ||||
#------------------------------------------------------------------------------- | ||||
# To run this code start a controller and engines using: | ||||
# ipcluster -n 2 | ||||
# Then run the scripts by doing irunner rmt.ipy or by starting ipython and | ||||
# doing run rmt.ipy. | ||||
from rmtkernel import * | ||||
MinRK
|
r3675 | import numpy | ||
from IPython.parallel import Client | ||||
MinRK
|
r3670 | |||
def wignerDistribution(s): | ||||
"""Returns (s, rho(s)) for the Wigner GOE distribution.""" | ||||
return (numpy.pi*s/2.0) * numpy.exp(-numpy.pi*s**2/4.) | ||||
def generateWignerData(): | ||||
s = numpy.linspace(0.0,4.0,400) | ||||
rhos = wignerDistribution(s) | ||||
return s, rhos | ||||
def serialDiffs(num, N): | ||||
diffs = ensembleDiffs(num, N) | ||||
normalizedDiffs = normalizeDiffs(diffs) | ||||
return normalizedDiffs | ||||
def parallelDiffs(rc, num, N): | ||||
MinRK
|
r3675 | nengines = len(rc.targets) | ||
MinRK
|
r3670 | num_per_engine = num/nengines | ||
print "Running with", num_per_engine, "per engine." | ||||
MinRK
|
r3675 | ar = rc.apply_async(ensembleDiffs, num_per_engine, N) | ||
return numpy.array(ar.get()).flatten() | ||||
MinRK
|
r3670 | |||
# Main code | ||||
if __name__ == '__main__': | ||||
MinRK
|
r3675 | rc = Client() | ||
view = rc[:] | ||||
MinRK
|
r3670 | print "Distributing code to engines..." | ||
MinRK
|
r3675 | view.run('rmtkernel.py') | ||
view.block = False | ||||
MinRK
|
r3670 | |||
# Simulation parameters | ||||
nmats = 100 | ||||
matsize = 30 | ||||
MinRK
|
r3675 | # tic = time.time() | ||
%timeit -r1 -n1 serialDiffs(nmats,matsize) | ||||
%timeit -r1 -n1 parallelDiffs(view, nmats, matsize) | ||||
MinRK
|
r3670 | |||
# Uncomment these to plot the histogram | ||||
# import pylab | ||||
# pylab.hist(parallelDiffs(rc,matsize,matsize)) | ||||