

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A programming project for cs415/515 advanced parallel programming students to write a multi-threaded program using the pthread library to find all primes within a given range. The program should have a user interface that takes one or two command-line arguments, allowing users to specify the upper bound of the search range and the number of threads to use. An algorithm for dividing the search range into evenly-sized blocks for each thread and handling the general case where not all sieve primes are in the first block. Students are required to ensure proper synchronization, handle the general case, implement the program efficiently, and test it thoroughly.
Typology: Study Guides, Projects, Research
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Spring ’09: CS415/515 Advanced Parallel Programming 4/9/ Prof. Jingke Li(FAB 120-06, [email protected]); Classes: TTh noon-1:50pm @ FAB150; Office Hours: TTh 10-11am.
The goal of this project is to practice multi-threaded programming techniques with the use of the Pthread library.
Write a multi-threaded program prime.c with Pthread routines to find all primes within a given range. The program should have the following user interface,
prime
i.e. it takes one or two command-line arguments. The first argument is an integer representing the upper bound of the search range (the lower bound is always 1). The second (optional) argument is the number of threads to use. When it is not provided, the program uses a default value of 1. The program reports the total number of primes found, and gives the user an option to print them out (the default is not to print, since the list can be long for large cases). The following is a couple of examples:
% prime 100 Finding primes in range 1..100, using 1 threads Main: starting on range 1.. Total 25 primes found, print all (y/n)? y 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, % prime 1000 3 Finding primes in range 1..1000, using 3 threads Main: starting on range 1.. Worker 4: starting on range 335 .. 668 Worker 4: done Worker 5: starting on range 669 .. 1000 Worker 5: done Total 168 primes found, print all (y/n)? n
Algorithm to Use
Let 1..N be the search range and p be the number of threads. Divide 1..N into p “evenly-sized” blocks (i.e. the blocks’ sizes differ by at most one). Let each thread be responsible for finding primes in one block. The main program itself is considered one of the threads, and is responsible for the first block. In addition, it is also responsible for
N primes, and
A complication arises when all sieve primes are not in the first block. We call this the General Case. When this situation occurs, the task of finding the sieve primes needs to be done by more than one thread: After the master thread finished finding all sieve primes in its block, a message needs to be sent to the second thread, so that it can continue finding more sieve primes, in its block.
Requirements
(a) List the shared data objects used in your program; describe their sizes and purposes. (b) List the synchronization objects used in your program; describe their purposes. (c) List the main steps (or draw a flow graph) for the main program and the worker thread, indicating the synchronization interaction.
(a) Do not use more than
N primes as sieves. When the number of thread is one, the program should work just like a sequential prime-finding program. (b) When there are multiple threads, make sure that these threads are capable of interleaving. To do this, you need to set the threads’ SCOPE attribute to SYSTEM: pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create(&tid, &attr, (void*)worker, (void *)param); (c) Print out two messages from each thread, one at the beginning and one at the end. The first message should include the corresponding block range information. Both messages should include the thread’s id. Use pthread_t tid = pthread_self(); to get the tid. (d) Do not pre-declare arrays to be of fixed sizes; instead, derive array sizes from user parameters. (e) Pay attention to programming style. Include proper amount of comments.
(a) Test your program with various parameter combinations. Make some repeated runs for the same parameter setting. Save an execution script of your program, with N taking values 100, 1000, 10000, and 100000; and p ranging from 1 to 4. (b) Summarize your experiences. What major issues you have encountered in writing this program? How did you resolve them? Has your program had any synchronization-related bugs? Have you observed any interleaved execution among the threads? (You may need to insert more print statement in the worker routine to see this.) etc.