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.C
shows thatfunc()
is not a thread-safe function, because the value ofcount
varies 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
README
file.Notes:
-
After compiling
threadsafe.C
to produce an executablethreadsafe
, invoke that executable as follows, in order to see the initial output that indicates howcount
varies:% ./threadsafe | less
To see the next page of output from
less
, enter a space; to see just one more line, press the Enter key. -
The program
threadsafe
accepts one positive integer command-line argument, representing the number of threads to use (default 8). Observe thatthreadsafe
produces correct results if a single thread is specified (sequential computation). -
Recall that OpenMP's
atomic
construct 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
critical
construct can make a more general statement indivisible (e.g., a compound statement{ ... }
). Sincecritical
uses 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 thatg
is a globalint
variable. Then{ int copy; #pragma omp critical { g *= 2; copy = g; } ... }
doubles the global variable
g
and assigns the result to the local variablecopy
without 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
thingn
computer.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
thing
s. On thethings
s, enter% source /opt/tbb/tbb.sh
to execute the initialization script.
-
The
thing
s do not support a-ltbb-debug
option. Instead, use-ltbb
when compiling a TBB program on those computers.
-
-
Not assigned in Interim 2015
From HW3 and HW4
-