Lecturer: Álvaro Leitao2 - A.Leitao_at_cwi.nl
cd ./PP/
/home/jpsilva/Lisbon1214/notebooks/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
Starting pp with 4 workers. hello world
%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"
Sum of primes until 1000000 is 37550402023 Time elapsed: 7.96832108498 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
Starting pp with 4 workers Sum of primes between (1, 250000) is 2623031151 Sum of primes between (250001, 500000) is 7291205044 Sum of primes between (500001, 750000) is 11674022830 Sum of primes between (750001, 1000000) is 15962142998 Total sum of primes until 1000000 is 37550402023 Time elapsed: 4.41727805138 s Job execution statistics: job count | % of all jobs | job time sum | time per job | job server 4 | 100.00 | 14.4278 | 3.606950 | local Time elapsed since server creation 4.41744089127 0 active tasks, 4 cores
%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
Starting pp with 4 workers Sum of primes until 1000 is 76127 Sum of primes until 100100 is 454996777 Sum of primes until 100200 is 455898156 Sum of primes until 100300 is 456700218 Sum of primes until 100400 is 457603451 Sum of primes until 100500 is 458407033 Sum of primes until 100600 is 459412387 Sum of primes until 100700 is 460217613 Time elapsed: 1.30422210693 s Job execution statistics: job count | % of all jobs | job time sum | time per job | job server 8 | 100.00 | 4.4084 | 0.551054 | local Time elapsed since server creation 1.30437898636 0 active tasks, 4 cores
%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"
PI = 3.1392 Time elapsed: 0.598618030548 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
Starting pp with 4 workers PI = 3.143692 Time elapsed: 0.327872037888 s
%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"
Integral of f between (0, 1) is 3.14159265359 Time elapsed: 0.437613010406 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
Starting pp with 4 workers Integral of f between (0, 1) is 3.14159265359 Time elapsed: 0.223114967346 s Job execution statistics: job count | % of all jobs | job time sum | time per job | job server 4 | 100.00 | 0.8653 | 0.216336 | local Time elapsed since server creation 0.223268985748 0 active tasks, 4 cores
%load_ext version_information
%version_information pp
Software | Version |
---|---|
Python | 2.7.9 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] |
IPython | 2.3.1 |
OS | Linux 3.16.0 28 generic x86_64 with debian jessie sid |
pp | 1.6.4 |
Wed Dec 17 12:03:33 2014 CET |
from IPython.core.display import HTML
def css_styling():
styles = open("./styles/custom.css", "r").read()
return HTML(styles)
css_styling()