cd ./PP/ %load pp_hello_world.py import pp def build_hello(): return "h"+"e"+"l"+"l"+"o" def build_world(): return "w"+"o"+"r"+"l"+"d" job_server = pp.Server() print "Starting pp with", job_server.get_ncpus(), "workers." job1 = job_server.submit(build_hello) job2 = job_server.submit(build_world) result1 = job1() result2 = job2() print result1 + " " + result2 %load sum_primes.py import math import time def isprime(n): #Returns True if n is prime and False otherwise if not isinstance(n, int): raise TypeError("argument passed to is_prime is not of 'int' type") if n < 2: return False if n == 2: return True max = int(math.ceil(math.sqrt(n))) i = 2 while i <= max: if n % i == 0: return False i += 1 return True def sum_primes(n): #Calculates sum of all primes until given integer n return sum([x for x in xrange(2,n+1) if isprime(x)]) n = 1000000 start_time = time.time() result = sum_primes(n) print "Sum of primes until", n, "is", result print "Time elapsed: ", time.time() - start_time, "s" import math import time def isprime(n): #Returns True if n is prime and False otherwise if not isinstance(n, int): raise TypeError("argument passed to is_prime is not of 'int' type") if n < 2: return False if n == 2: return True max = int(math.ceil(math.sqrt(n))) i = 2 while i <= max: if n % i == 0: return False i += 1 return True def sum_primes(n): #Calculates sum of all primes until given integer n return sum([x for x in xrange(2,n+1) if isprime(x)]) n = 1000000 start_time = time.time() result = sum_primes(n) print "Sum of primes until", n, "is", result print "Time elapsed: ", time.time() - start_time, "s" %load pp_sum_primes.py import sys import math import time import pp def isprime(n): #Returns True if n is prime and False otherwise if not isinstance(n, int): raise TypeError("argument passed to is_prime is not of 'int' type") if n < 2: return False if n == 2: return True max = int(math.ceil(math.sqrt(n))) i = 2 while i <= max: if n % i == 0: return False i += 1 return True def sum_primes(n1, n2): # Calculates sum of all primes between given integers n1 and n2 if n1 >= n2: print "Second argument has to be greater than the first one." return 0 return sum([x for x in xrange(n1, n2+1) if isprime(x)]) if len(sys.argv) > 1: ncpus = int(sys.argv[1]) # Creates jobserver with ncpus workers job_server = pp.Server(ncpus) else: # Creates jobserver with automatically detected number of workers job_server = pp.Server() ncpus = job_server.get_ncpus() print "Starting pp with", ncpus, "workers" n = 1000000 inputs = [( i*n/ncpus + 1, (i+1)*n/ncpus ) for i in range(0,ncpus)] start_time = time.time() jobs = [(input, job_server.submit(sum_primes, input, (isprime,), ("math",))) for input in inputs] total_sum = 0 for input, job in jobs: print "Sum of primes between", input, "is", job() total_sum += job() print "Total sum of primes until", n, "is", total_sum print "Time elapsed: ", time.time() - start_time, "s" job_server.print_stats() %run pp_sum_primes.py 4 %load pp_sum_primes_ntimes.py import sys import math import time import pp def isprime(n): # Returns True if n is prime and False otherwise if not isinstance(n, int): raise TypeError("argument passed to is_prime is not of 'int' type") if n < 2: return False if n == 2: return True max = int(math.ceil(math.sqrt(n))) i = 2 while i <= max: if n % i == 0: return False i += 1 return True def sum_primes(n): # Calculates sum of all primes until given integer n return sum([x for x in xrange(2,n+1) if isprime(x)]) if len(sys.argv) > 1: ncpus = int(sys.argv[1]) # Creates jobserver with ncpus workers job_server = pp.Server(ncpus) else: # Creates jobserver with automatically detected number of workers job_server = pp.Server() print "Starting pp with", job_server.get_ncpus(), "workers" # The following submits 8 jobs and then retrieves the results inputs = (1000, 100100, 100200, 100300, 100400, 100500, 100600, 100700) start_time = time.time() jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs] for input, job in jobs: print "Sum of primes until", input, "is", job() print "Time elapsed: ", time.time() - start_time, "s" job_server.print_stats() %run pp_sum_primes_ntimes.py 4 %load montecarlo_pi.py import math import random import time def MC_Pi(n): counter = 0 for i in range(n): if math.pow(random.random(), 2.0) + math.pow(random.random(), 2.0) <= 1.0: counter += 1 return counter n = 1000000 start_time = time.time() ctr = MC_Pi(n) print "PI = ", 4.0*ctr/n print "Time elapsed: ", time.time() - start_time, "s" import math import random import time def MC_Pi(n): counter = 0 for i in range(n): if math.pow(random.random(), 2.0) + math.pow(random.random(), 2.0) <= 1.0: counter += 1 return counter n = 1000000 start_time = time.time() ctr = MC_Pi(n) print "PI = ", 4.0*ctr/n print "Time elapsed: ", time.time() - start_time, "s" %load pp_montecarlo_pi.py import sys import math import random import time import pp def MC_Pi(n): counter = 0 for i in range(n): if math.pow(random.random(), 2.0) + math.pow(random.random(), 2.0) <= 1.0: counter += 1 return counter if len(sys.argv) > 1: ncpus = int(sys.argv[1]) # Creates jobserver with ncpus workers job_server = pp.Server(ncpus) else: # Creates jobserver with automatically detected number of workers job_server = pp.Server() ncpus = job_server.get_ncpus() print "Starting pp with", ncpus, "workers" n = 1000000 start_time = time.time() ln = n/ncpus jobs = [job_server.submit(MC_Pi, (ln,), modules = ("math",'random')) for i in range(ncpus)] ctr = 0 for job in jobs: ctr += job() print "PI = ", 4.0*ctr/n print "Time elapsed: ", time.time() - start_time, "s" %run pp_montecarlo_pi.py 4 %load midpoint_integration.py import time def func_val(x): # return pow(sin(x)+cos(x),1/3.0) # return sin(x) # return pow(x,3)+pow(x,2)+3*x+4; # return 2*pow(x,2)/ (3*pow(x,5)+4); return 4/(1+x*x) # should return Pi for int from 0 .. 1 = (4 * atan(1) ) def simpleint(a, m, h): result = 0 # This is simple midpoint integration for x in range(0,m): result += func_val(a + x*h + h/2) # average function value between a and a+h return result * h # multiply with width=h to compute surface a = 0 b = 1 m = 1000000 i = float(b - a) h = i/m start_time = time.time() print "Integral of f between", (a,b), "is", simpleint(a, m, h) print "Time elapsed: ", time.time() - start_time, "s" import time def func_val(x): # return pow(sin(x)+cos(x),1/3.0) # return sin(x) # return pow(x,3)+pow(x,2)+3*x+4; # return 2*pow(x,2)/ (3*pow(x,5)+4); return 4/(1+x*x) # should return Pi for int from 0 .. 1 = (4 * atan(1) ) def simpleint(a, m, h): result = 0 # This is simple midpoint integration for x in range(0,m): result += func_val(a + x*h + h/2) # average function value between a and a+h return result * h # multiply with width=h to compute surface a = 0 b = 1 m = 1000000 i = float(b - a) h = i/m start_time = time.time() print "Integral of f between", (a,b), "is", simpleint(a, m, h) print "Time elapsed: ", time.time() - start_time, "s" %load pp_midpoint_integration.py import sys import time import pp def func_val(x): # return pow(sin(x)+cos(x),1/3.0) # return sin(x) # return pow(x,3)+pow(x,2)+3*x+4; # return 2*pow(x,2)/ (3*pow(x,5)+4); return 4/(1+x*x) # should return Pi for int from 0 .. 1 = (4 * atan(1) ) def simpleint(a, m, h): result = 0 # This is simple midpoint integration for x in range(0,m): result += func_val(a + x*h + h/2) # average function value between a and a+h return result * h # multiply with width=h to compute surface if len(sys.argv) > 1: ncpus = int(sys.argv[1]) # Creates jobserver with ncpus workers job_server = pp.Server(ncpus) else: # Creates jobserver with automatically detected number of workers job_server = pp.Server() ncpus = job_server.get_ncpus() print "Starting pp with", ncpus, "workers" a = 0 b = 1 m = 1000000 i = float(b - a) h = i/m start_time = time.time() lm = m/ncpus if(lm == 0): lm = 1 local_a = [( a + j*i/ncpus) for j in range(0,ncpus)] jobs = [job_server.submit(simpleint, (la,lm,h), (func_val,)) for la in local_a] total_sum = 0 for job in jobs: total_sum += job() print "Integral of f between", (a,b), "is", total_sum print "Time elapsed: ", time.time() - start_time, "s" job_server.print_stats() %run pp_midpoint_integration.py 4 %load_ext version_information %version_information pp from IPython.core.display import HTML def css_styling(): styles = open("./styles/custom.css", "r").read() return HTML(styles) css_styling()