ORT BRAUDE COLLEGE OF ENGINEERING
Electronic and Electrical Engineering Department
Client Server Systems and prallel programming 31261
PROJECT 5: Multithreading - running multiple threads
* Finally we approach the second topic: parallel programming
* We will focus on the Python threading and multiprocessing modules
* We are going to solve the following problems in the laboratory
* As an example of a computational problem that can be solved with parallel techniques is:
Find the sum of all prime numbers less than 40000
* We will make an effort to complete the problem set at the lab, but whatever remains, you should
complete at home (and get help from me using office hours or email)
* Please copy and paste the following code to a file named: proj5.py
------------------------------------------------------------------------
import sys, os, time, subprocess
from threading import Thread
# Find all prime numbers from a to b (excluding b)
def find_primes(a,b):
primes = []
p=a
while p 17
What will happen if you forget the 'thread.join()' statement ??
Try to comment this statement and see what happens?
---------
PROBLEM 4
---------
Run the following two threads concurrently:
thread1 = PrimeSumThread(2, 200)
thread2 = PrimeSumThread(200, 400)
thread1. start()
thread2. start()
How can you prove that this two threads are really running concurrently ?
Hint:
In find_primes() function, uncomment the line:
#sys.stdout.write(str(i)+':')
and run the above code again
---------
PROBLEM 5
---------
(a) Use the PrimeSumThread class to write a function:
def sum_of_primes_parallel(n):
#code ...
which computes the sum of primes smaller than the integer n, in 4 parallel threads.
(b) Write a small test2() for computing: sum_of_primes_parallel(40000)
and measure the run time for this computation
Hint:
def sum_of_primes_parallel(n):
thread = {}
thread[0] = PrimeSumThread(2, n/4)
thread[1] = PrimeSumThread(n/4, 2*n/4)
thread[2] = PrimeSumThread(2*n/4, 3*n/4)
thread[3] = PrimeSumThread(3*n/4, n)
# continue from here ...
# ......................
def test2():
t1 = time.time()
#code
t2 = time.time()
time = t2-t1
---------
PROBLEM 6
---------
What was the surprise you found in problem 4 ???
To solve this surprise we will need to resolt to the multiprocessing module:
goto project 6 ...