\documentclass[twocolumn]{article}

\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{7.0in}
\setlength{\textheight}{9.5in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}
\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\begin{document}

Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Directions: {\bf \Large Work only on this sheet} (on both sides, if
needed); do not turn in any supplementary sheets of paper. There is
actually plenty of room for your answers, as long as you organize
yourself BEFORE starting writing.

{\bf \Large Unless otherwise stated, give numerical answers as
expressions, e.g. $\frac{2}{3} \times 6 - 1.8$.  Do NOT use
calculators.}

{\bf 1.} (40) Suppose we were to write an OpenMP version of the dice
simulation example of the Python {\bf multiprocessing} module.  Say we
store our grand total in a global variable {\bf tot}, with {\bf count}
storing the thread's individual count.  Show how to efficiently write
the OpenMP version of

\begin{Verbatim}[fontsize=\relsize{-2}]
totlock.acquire()
tot.value += count  
totlock.release()
\end{Verbatim}

{\bf 2.} (30) Consider the Python {\bf multiprocessing} example of
Quicksort, using the {\bf Queue} class.  Suppose the original array to
be sorted was (12,5,13,6,8,10,2,21,20,15).  When the work item (i,j,2)
is placed into the queue, what will be the values of i and j?  Note:
The code in {\bf separate()} is not quite right, but assume it works
correctly, which is to rearrange the given range within {\bf xc} so that
all the elements smaller than {\bf xc[low]} are moved to the left of
that element, and all the ones larger than that element are moved to its
right, with the return value {\bf last} being the final resting place of
{\bf xc[low]}.

{\bf 3.} (30) The function {\bf allgt(x,y,n)} below, to run in an OpenMP
context, returns 1 (i.e. True) if all elements of {\bf x} are greater
than their counterparts in {\bf y}.  Each of the arrays {\bf x} and {\bf
y} is of length {\bf n}.  (In this implementation, no effort is made to
do no further checking after encountering a False case.)  The function
is to be called from within an OpenMP {\bf parallel} block.  Fill in the
blanks.

\begin{Verbatim}[fontsize=\relsize{-2}]
int allgt(x,y,n)
{  int all,i;
   #pragma omp _______________________________________
   for (i = 0; i < n; i++) _____________________________
   return all;
}
\end{Verbatim}

{\bf Solutions:}

{\bf 1.}

\begin{Verbatim}[fontsize=\relsize{-2}]
#pragma omp atomic
tot += count;
\end{Verbatim}

{\bf 2.} The first call to {\bf separate()} will rearrange the array to
(5,6,8,10,2,12,13,21,20,15) and return 5, and (6,9,1) will be put in the
queue.  The next thread will work on that, and put (7,9,2) in the queue.

{\bf 3.}

\begin{Verbatim}[fontsize=\relsize{-2}]
int allgt(x,y,n)
{  int all,i;
   #pragma omp for reduction(&&:all)
   for (i = 0; i < n; i++) all &&= (x[i] > y[i]);
   return all;
}
\end{Verbatim}

\end{document}

