⇤ ← Revision 1 as of 2012-11-07 09:43:13
Size: 38
Comment:
|
Size: 2287
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
Parallel Programming | = Parallel Programming = |
Line 4: | Line 4: |
* 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 : {{{#!python #!/usr/bin/python # File: sum_primes_seq.py # Author: Heinrich Widmann (based on parallel version of Vitalii Vanovschi # from Parallel Python Software: http://www.parallelpython.com ) # Description: This program demonstrates sequential computation and # calculates the sum of prime numbers below a given integer import math, sys, 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 below given integer n""" return sum([x for x in xrange(2,n) if isprime(x)]) print """Usage: python sum_primes_seq.py [njobs] njobs are the number of jobs (calculate sum of primes up to N=10000+(njobs*100)) excecuted """ if len(sys.argv) > 1: njobs = int(sys.argv[1]) else: njobs = 8 result = sum_primes(100) print "Sum of primes below 100 is", result start_time = time.time() # The following submits njobs jobs and then retrieves the results for i in xrange(njobs): input=10000+i*100 job_start_time = time.time() print "Sum of primes below", input, "is", sum_primes(input), "and lasts", time.time() - job_start_time, "s" 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: [[https://wiki.zmaw.de/lehre/PythonCourse/PythonLES/Pandas?action=AttachFile&do=get&target=precip2.tar.gz | precip.tar.gz]] |
= Parallel Programming =
Test code :
- Some concepts and methods
- Threading vs. Multiprocessing
- 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