import numpy as np import matplotlib.pyplot as plt from IPython.display import Image Image(filename="./parallel_architecture400.png") from multiprocessing import cpu_count print cpu_count() from IPython import parallel rc = parallel.Client(profile='hpc') rc.block = True rc.ids def power(a, b): return a**b dv = rc[0] dv dv.apply(power, 2, 10) X = [1, 2, 3, 4] X X[:] rc[:].apply_sync(power, 2, 10) map(power, [2]*10, range(10)) view = rc.load_balanced_view() view.map(power, [2]*10, range(10)) from IPython import parallel rc = parallel.Client(profile='hpc') rc.block = True dview = rc.direct_view() dview.block = False dview["a"] = 5 # shorthand for push dview["b"] = 7 dview.apply_sync(lambda x: a + b + x, 27) d = {} d["a"] = 5 d dview.push(dict(msg="Hi, there"), block=True) dview.block = True dview.execute("x = msg") dview["x"] # shorthand for pull #rc[::2].execute("c = a + b") # or dview.execute("c = a + b", targets=[0,2]) #rc[1::2].execute("c = a - b") # or dview.execute("c = a - b", targets=[1,3]) dview.pull("c") def wait(t): import time tic = time.time() time.sleep(t) return time.time() - tic ar = dview.apply_async(wait, 2) type(ar) ar.get() ar = dview.apply_async(wait, 15) print ar.ready() ar.get(5) result_list = [dview.apply_async(wait, 3) for i in range(5)] result_list dview.wait(result_list) result_list[4].get() dview.scatter('x', range(64)) %px y = [i**10 for i in x] y = dview.gather('y') print y[:10] rc = parallel.Client(profile='hpc') lview = rc.load_balanced_view() lview.block = True parallel_result = lview.map(lambda x:x**10, range(32)) print parallel_result[:10] y = np.array([4.284, 4.149, 3.877, .533, 2.211, 2.389, 2.145, 3.231, 1.998, 1.379, 2.106, 1.428, 1.011, 2.179, 2.858, 1.388, 1.651, 1.593, 1.046, 2.152]) x = np.array([.286, .645, .973, .585, .384, .310, .276, .058, .973, .455, .543, .779, .957, .259, .948, .202, .543, .028, .797, .099, .936, .142, .889, .296, .006, .175, .828, .180, .399, .842, .617, .039, .939, .103, .784, .620, .072, .158, .889, .704]).reshape(20,2) x = np.column_stack((np.ones(len(x)), x)) print y print x def func(params, y, x): import numpy as np theta = np.r_[params[0], params[1], params[1]**2] return y - np.dot(x,theta) theta1, theta2 = np.mgrid[-3:3:100j,-3:3:100j] Z = [np.sum(func([i,j], y, x)**2) for i,j in zip(theta1.flatten(), theta2.flatten())] Z = np.asarray(Z).reshape(100,100) fig, ax = plt.subplots(figsize=(6, 6)) V = [16.1, 18, 20, 20.5, 21, 22, 24, 25, 30, 40, 50, 100, 200, 300, 400, 500, 600, 700] c = ax.contour(theta1, theta2, Z, V) im = ax.imshow(Z, interpolation='bilinear', origin='lower', cmap=plt.cm.BrBG, extent=(-3,3,-3,3)) cb = plt.colorbar(c) ax.set_xlabel(r'$\theta_1$') ax.set_ylabel(r'$\theta_2$') #ax.scatter([.864737, 2.35447, 2.49860664], [1.235748, -.319186, -0.98261242], ax.scatter([.864737, 2.49860664], [1.235748, -0.98261242], marker="x", s=30, color='black', lw=2) ax.set_title('Loci of objective function') ax.set_xlim([-3,3]) ax.set_ylim([-3,3]) ax.grid(False) plt.show() x1 = [0,0] # good x2 = [2.354471, -.319186] # bad x3 = [1, 1] # good x4 = [-3.17604581, -0.680944] # bad # assume we got these in some sane way xs = np.random.normal(0, 4, size=(20, 2)) starts = np.row_stack((x1, x2, x3, x4, xs)) def optimize_func(start_params): return leastsq(func, start_params, args=(y, x))[0] dview = rc[:] with dview.sync_imports(): from scipy.optimize import leastsq import numpy as np dview.push(dict(func=func, y=y, x=x)); results = dview.map_sync(optimize_func, starts) opt_func = lambda params : np.sum(func(params, y, x)**2) i_best = np.argmin(map(opt_func, np.array([result for result in results]))) print results[i_best] from IPython import parallel rc = parallel.Client("./ipcontroller-client.json", sshserver="js2796a@zorro.american.edu", timeout=60) view = rc[:] view.block = True rc.ids view.execute("import socket; x = socket.gethostname()") view["x"] from pidigits import plot_one_digit_freqs, txt_file_to_digits, one_digit_freqs #view.execute("from pidigits import *") import sympy pi = sympy.pi.evalf(40) print pi pi = sympy.pi.evalf(10000) # make a sequence of strings digits = (d for d in str(pi)[2:]) freqs = one_digit_freqs(digits) ax = plot_one_digit_freqs(freqs) def compute_two_digit_freqs(filename): """ Read digits of pi from a file and compute the 2 digit frequencies. """ d = txt_file_to_digits(filename) freqs = two_digit_freqs(d) return freqs def reduce_freqs(freqlist): """ Add up a list of freq counts to get the total counts. """ allfreqs = np.zeros_like(freqlist[0]) for f in freqlist: allfreqs += f return allfreqs n = len(rc) print n filestring = 'pi200m.ascii.%(i)02dof20' files = [filestring % {'i':i} for i in range(1,n+1)] files view.map(fetch_pi_file, files) from timeit import default_timer as clock t1 = clock() id0 = rc.ids[0] freqs10m = rc[id0].apply_sync(compute_two_digit_freqs, files[0]) t2 = clock() digits_per_second1 = 10.0e6/(t2-t1) print "Digits per second (1 core, 10m digits): ", digits_per_second1 t1 = clock() # Compute the digits freqs_all = view.map(compute_two_digit_freqs, files[:n]) # Add up the frequencies from each engine. freqsn10m = reduce_freqs(freqs_all) t2 = clock() digits_per_secondn = n*10.0e6/(t2-t1) print "Digits per second (%i engines, %i0m digits): "%(n,n), digits_per_secondn print "Speedup: ", digits_per_secondn/digits_per_second1 plot_two_digit_freqs(freqsn10m, figsize=(10,10)) plt.title("2 digit sequences in %i0m digits of pi" % n);