| 1 | # File: sum_primes.py
 | 
|---|
| 2 | # Author: VItalii Vanovschi
 | 
|---|
| 3 | # Desc: This program demonstrates parallel computations with pp module
 | 
|---|
| 4 | # It calculates the sum of prime numbers below a given integer in parallel
 | 
|---|
| 5 | # Parallel Python Software: http://www.parallelpython.com
 | 
|---|
| 6 | 
 | 
|---|
| 7 | import math, sys, time
 | 
|---|
| 8 | import pp
 | 
|---|
| 9 | 
 | 
|---|
| 10 | def isprime(n):
 | 
|---|
| 11 |     """Returns True if n is prime and False otherwise"""
 | 
|---|
| 12 |     if not isinstance(n, int):
 | 
|---|
| 13 |         raise TypeError("argument passed to is_prime is not of 'int' type")
 | 
|---|
| 14 |     if n < 2:
 | 
|---|
| 15 |         return False
 | 
|---|
| 16 |     if n == 2:
 | 
|---|
| 17 |         return True
 | 
|---|
| 18 |     max = int(math.ceil(math.sqrt(n)))
 | 
|---|
| 19 |     i = 2
 | 
|---|
| 20 |     while i <= max:
 | 
|---|
| 21 |         if n % i == 0:
 | 
|---|
| 22 |             return False
 | 
|---|
| 23 |         i += 1
 | 
|---|
| 24 |     return True
 | 
|---|
| 25 | 
 | 
|---|
| 26 | def sum_primes(n):
 | 
|---|
| 27 |     """Calculates sum of all primes below given integer n"""
 | 
|---|
| 28 |     return sum([x for x in xrange(2,n) if isprime(x)])
 | 
|---|
| 29 | 
 | 
|---|
| 30 | print """Usage: python sum_primes.py [ncpus]
 | 
|---|
| 31 |     [ncpus] - the number of workers to run in parallel, 
 | 
|---|
| 32 |     if omitted it will be set to the number of processors in the system
 | 
|---|
| 33 | """
 | 
|---|
| 34 | 
 | 
|---|
| 35 | # tuple of all parallel python servers to connect with
 | 
|---|
| 36 | ppservers = ()
 | 
|---|
| 37 | #ppservers = ("10.0.0.1",)
 | 
|---|
| 38 | 
 | 
|---|
| 39 | if len(sys.argv) > 1:
 | 
|---|
| 40 |     ncpus = int(sys.argv[1])
 | 
|---|
| 41 |     # Creates jobserver with ncpus workers
 | 
|---|
| 42 |     job_server = pp.Server(ncpus, ppservers=ppservers)
 | 
|---|
| 43 | else:
 | 
|---|
| 44 |     # Creates jobserver with automatically detected number of workers
 | 
|---|
| 45 |     job_server = pp.Server(ppservers=ppservers)
 | 
|---|
| 46 | 
 | 
|---|
| 47 | print "Starting pp with", job_server.get_ncpus(), "workers"
 | 
|---|
| 48 | 
 | 
|---|
| 49 | # Submit a job of calulating sum_primes(100) for execution. 
 | 
|---|
| 50 | # sum_primes - the function
 | 
|---|
| 51 | # (100,) - tuple with arguments for sum_primes
 | 
|---|
| 52 | # (isprime,) - tuple with functions on which function sum_primes depends
 | 
|---|
| 53 | # ("math",) - tuple with module names which must be imported before sum_primes execution
 | 
|---|
| 54 | # Execution starts as soon as one of the workers will become available
 | 
|---|
| 55 | job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",))
 | 
|---|
| 56 | 
 | 
|---|
| 57 | # Retrieves the result calculated by job1
 | 
|---|
| 58 | # The value of job1() is the same as sum_primes(100)
 | 
|---|
| 59 | # If the job has not been finished yet, execution will wait here until result is available
 | 
|---|
| 60 | result = job1()
 | 
|---|
| 61 | 
 | 
|---|
| 62 | print "Sum of primes below 100 is", result
 | 
|---|
| 63 | 
 | 
|---|
| 64 | start_time = time.time()
 | 
|---|
| 65 | 
 | 
|---|
| 66 | # The following submits 8 jobs and then retrieves the results
 | 
|---|
| 67 | inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)
 | 
|---|
| 68 | jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs]
 | 
|---|
| 69 | for input, job in jobs:
 | 
|---|
| 70 |     print "Sum of primes below", input, "is", job()
 | 
|---|
| 71 | 
 | 
|---|
| 72 | print "Time elapsed: ", time.time() - start_time, "s"
 | 
|---|
| 73 | job_server.print_stats()
 | 
|---|
| 74 | 
 | 
|---|
| 75 | # Parallel Python Software: http://www.parallelpython.com
 | 
|---|