Advanced Parallel Programming Project: Finding Primes with Multi-Threaded Techniques - Pro, Study Guides, Projects, Research of Computer Science

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

Pre 2010

Uploaded on 08/18/2009

koofers-user-d54
koofers-user-d54 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Spring ’09: CS415/515 Advanced Parallel Programming 4/9/09
Prof. Ji ngke Li(FAB 120-06, [email protected]); Classes: TTh noon-1:50pm @ FAB150; Office Hours: TTh 10-11am.
Programming Project #1
(Due Thursday, 4/23/09 @ noon)
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 <bound> [<num_threads>]
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..100
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..334
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 pbe 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
creating the other p1 threads;
finding all the sieve primes, i.e. the first Nprimes, and
summarizing the result at the end of the execution.
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.
1
pf3

Partial preview of the text

Download Advanced Parallel Programming Project: Finding Primes with Multi-Threaded Techniques - Pro and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

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.

Programming Project

(Due Thursday, 4/23/09 @ noon)

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 [<num_threads>]

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

  • creating the other p − 1 threads;
  • finding all the sieve primes, i.e. the first

N primes, and

  • summarizing the result at the end of the execution.

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

  1. Synchronization Design (20%): Your program must allow the task of finding the sieve primes and the task of crossing out primes’ multiples to run in parallel. As a counter example, a program that lets a master thread find all sieve primes first, then lets the slave threads to cross out the composite numbers is not acceptable. Before starting programming, think carefully what types of synchronization you need to use, when and where.

(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.

  1. Handling the General Case (10%): If you are an undergraduate student, you don’t have to handle this case. However, your program needs to detect such cases from the input parameters; and it should print out a message (e.g. “This is beyond this program’s scope.”) and quit. You will receive extra 10% credit if you handle this issue correctly (see below). However, if you are a graduate student, you must handle this issue. For graduate students, you need to describe your approach. Include one or two illustrating examples.
  2. Implementation (50%):

(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.

  1. Testing and Summary (20%):

(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.