Differences between revisions 2 and 3
Revision 2 as of 2012-11-07 10:05:17
Size: 2287
Comment:
Revision 3 as of 2012-11-08 09:01:10
Size: 2272
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

Test code :

= Parallel Programming =

  • Some concepts and methods
  • Threading vs. Multiprocessing
  • ParallelPython

  • Processing package
  • Other approaches
  • clustering, multi nodes, ...
  • Loadbalancing, scheduling

As a simple application we use a program, which calculates the sum of prime numbers below a given integer in parallel

Here the code of the sequential implementation in python :

   1 #!/usr/bin/python
   2 # File: sum_primes_seq.py
   3 # Author: Heinrich Widmann (based on parallel version of Vitalii Vanovschi
   4 #  from Parallel Python Software: http://www.parallelpython.com )
   5 # Description: This program demonstrates sequential computation and
   6 #  calculates the sum of prime numbers below a given integer
   7 
   8 import math, sys, time
   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_seq.py [njobs]
  31   njobs are the number of jobs (calculate sum of primes up to N=10000+(njobs*100)) excecuted
  32 """
  33 
  34 if len(sys.argv) > 1:
  35     njobs = int(sys.argv[1])
  36 else:
  37     njobs = 8
  38 
  39 result = sum_primes(100)
  40 
  41 print "Sum of primes below 100 is", result
  42 
  43 start_time = time.time()
  44 
  45 # The following submits njobs jobs and then retrieves the results
  46 for i in xrange(njobs):
  47     input=10000+i*100
  48     job_start_time = time.time()
  49     print "Sum of primes below", input, "is", sum_primes(input), "and lasts", time.time() - job_start_time, "s"
  50 print "Time elapsed: ", time.time() - start_time, "s"

... Parllel code will follow ...

... May be I will find time to show a more realistic application with data processing ...

Precip data you need for the exercise: precip.tar.gz

LehreWiki: PythonCourse/PythonLES/Parallel_Programming (last edited 2012-11-09 20:22:45 by HeinrichWidmann)