Homework
CS 300, Parallel and Distributed Computing (PDC)
- Homework 100 Due
-
-
OpenMP
-
[HC]
Consider the program
threadsafe.C, which does parallel operations on an array, including parallel calls to a functionfunc().The output from multiple sample runs of
threadsafe.Cshows thatfunc()is not a thread-safe function, because the value ofcountvaries during some calls offunc, and some of the multiplications of elements in the arrayarr[]are not performed correctly. Improve this code to make the code forfunc()thread-safe, using OpenMP constructs.Report on the corrections you made and any other observations you have on paper or in a
READMEfile.Notes:
-
After compiling
threadsafe.Cto produce an executablethreadsafe, invoke that executable as follows, in order to see the initial output that indicates howcountvaries:% ./threadsafe | lessTo see the next page of output from
less, enter a space; to see just one more line, press the Enter key. -
The program
threadsafeaccepts one positive integer command-line argument, representing the number of threads to use (default 8). Observe thatthreadsafeproduces correct results if a single thread is specified (sequential computation). -
Recall that OpenMP's
atomicconstruct can be specified before a simple assignment using operators such as+=,*=,<<=,++, etc., in order to cause that assignment to be indivisible. -
Also recall that OpenMP's
criticalconstruct can make a more general statement indivisible (e.g., a compound statement{ ... }). Sincecriticaluses locks to keep more than one thread from executing its statement, it has unacceptable performance for long program sequences. However, it can be useful for making local copies of a global variable, as follows: Suppose thatgis a globalintvariable. Then{ int copy; #pragma omp critical { g *= 2; copy = g; } ... }doubles the global variable
gand assigns the result to the local variablecopywithout any danger of another thread interfering (e.g., performing its own doubling operation before this thread can assign to copy, interfering with the doubling or assignment operations, etc.).
-
-
-
Threading Building Blocks
-
[C]
Carry out the steps of the TBB module on a
thingncomputer.Perform rudimentary performance tests of your resulting programs, and report your observations in
hw/README.Notes:
-
The module instructions call for executing an initialization script before using TBB. For example, this initialization script enables the compilation to find the header file
tbb/tbb.h.The TBB module document lists the appropriate initialization script for MTL, but the script is located in a different place on the
things. On thethingss, enter% source /opt/tbb/tbb.sh
to execute the initialization script.
-
The
things do not support a-ltbb-debugoption. Instead, use-ltbbwhen compiling a TBB program on those computers.
-
-
Not assigned in Interim 2015
From HW3 and HW4
-