Pthread and OpenMP project |
due: May 1, 11:59pm |
Files requested for submission: A tar file includes two files (pthread.c, openmp.c) of source code (one is for Pthread, one is for OpenMP). |
|
Pthreads project: Producer-Consumer Problems
Refer to the lecture on Apr 20 for the producer-consumer problem. Unlike the implementation shown in the slides, we have the following requests for this project:
Generate one producer and two consumers. The producer generates N tasks, where N is a parameter passed in via the command line.
The task queue has two slots.
If the queue is empty, cosumers cannot extract tasks from the queue, while if the queue is full, the producer cannot put task in the queue.
Print out each step: (1) Who inserts task and who extracts task. (2) At the end of execution, print how many tasks each consumer extracts.
OpenMP project: Implement Monte Carlo Estimation of Pi using OpenMP
Refer to slides of the lecture on Apr 20, which talk about the algorithm and Pthreads implementation. The requirement for the OpenMP implementation is as follows:
Each thread uses a random function to generate its own number of samples (sample_points_per_thread in slide 9). Control the random number generator to produce reasonable number of samples with high accuracy and reasonable execution time for computing Pi.
Use an OpenMP reduction in the end to sum up all the samples from different threads and compute Pi.
If your code does not give an accurate Pi or runs for long time, we will deduct points.
Compile your code
Pthread: gcc -lpthread
OpenMP: gcc -fopenmp
Or you can provide your makefile
Pthread
Refer to the slides.
OpenMP
You can find information about OpenMP in several
places. Lawrence Livermore National Laboratory has posted a good OpenMP tutorial.
Complete information about OpenMP 3.0 can be found in the OpenMP 3.0 specification.
When you write OpenMP parallel regions and worksharing directives,
be careful to specify the data scope attribute clauses for variables used within
each directive's scope. I recommend using the data scoping directive default(none) to make sure that the
compiler doesn't implicitly add an incorrect sharing directive.
Specifying the Number of OpenMP Threads
You can change the number of threads for a program execution by setting the OMP_NUM_THREADS environment variable before launching the program. The deafult is to use the same number of threads as cores available on a node. For example, to create 8 threads on a single node
bash: export OMP_NUM_THREADS=8
csh: setenv OMP_NUM_THREADS 8
Test your program of the OpenMP
All tests should be performed on our lab machines. There are four cores, so test your program using 1 to 4 threads.