\documentclass[twocolumn]{article}
% \documentclass{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}

\setlength\columnsep{25pt}

\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\usepackage{listings}
\usepackage{amsmath}

\begin{document}

\lstset{xleftmargin=5mm,framexleftmargin=10mm,basicstyle=\footnotesize}

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

Directions: MAKE SURE TO COPY YOUR ANSWERS TO A SEPARATE SHEET FOR SENDING
ME AN ELECTRONIC COPY LATER.

{\bf 1.} Consider the code in Sec.\ 1.5.2, which finds the maximum
burst in a time series.

\begin{itemize}

\item [(a)] (10) What will be printed out when {\bf test()} is called?

\item [(b)] (10) What OpenMP construct is similar to the {\bf if}
statement prior to setting {\bf rslts}?

\end{itemize}

{\bf 2.} The R {\bf parallel} package includes a function {\bf
clusterApplyLB()}. It is similar to {\bf clusterApply()}, but, in the
docmentation's words,

\begin{quote}
...the first ‘n’ jobs are placed in order on the ‘n’ nodes.  When the 
first job completes, the next job is placed on the node that has become
free; this continues until all jobs are complete. 
\end{quote}

\begin{itemize}

\item [(a)] (10) What does the abbreviation ``LB'' likely stand for?

\item [(b)] (10) What OpenMP keyword indicates an approach similar to
this?

\end{itemize}

{\bf 3.} (30) The {\it (out)-degree} of a vertex i in a graph is the
number of outlinks for that vertex.  The OMP code below finds the
degrees for the adjacency matrix {\bf a}, returning them in {\bf dgs}
(preallocated by the caller).  The number of vertices in the graph is
{\bf n}.  Fill in the blanks.

\begin{lstlisting}
void finddgs(int *a, int n, int *dgs) {
   blank (a)
   {  int i,j,dg;
      blank (b)
      for (i = 0; i < n; i++) {
         dg = 0;
         for (j = 0; i < n; j++) 
            blank (c)
         dgs[i] = dg;
      }
   }
}
\end{lstlisting}

{\bf 4.} (30) In an interpreted language such as R or Python, the best
speed often depends on making good use of built-in functions, such as
matrix multiplication \lstinline{%*%} in R.  In this problem, we will
explore an approach to speedy computation of the mutual outlinks
application in Sec.\ 2.4.3.  (A vertex might link to itself, but this
will be irrelevant).  We won't parallelize the operation here, but we
could.

Fill in the blanks in the function {\bf meanmo()} below.
Note that blank (a) must be filled in with the name of a built-in R
function, and that you should find the following R code helpful

\begin{lstlisting}
> a <- rbind(1:2,3:4)
> a
     [,1] [,2]
[1,]    1    2
[2,]    3    4
> row(a)
     [,1] [,2]
[1,]    1    1
[2,]    2    2
> row(a) == 1
      [,1]  [,2]
[1,]  TRUE  TRUE
[2,] FALSE FALSE
> sum(row(a) == 1)
[1] 2
> sum(row(a) > col(a))
[1] 1
> a[row(a) == col(a)] <- 8
> a
     [,1] [,2]
[1,]    8    2
[2,]    3    8
\end{lstlisting}

Here is the function:

\begin{lstlisting}
meanmo <- function(a) {
   aap <- a %*% blank (a)(a)
   nummos <- blank (b)
   n <- nrow(a)
   nummos / blank (c)
}
\end{lstlisting}

\onecolumn

\newpage

{\bf Solutions:}

{\bf 1.a} 9, 16

{\bf 1.b} {\bf single}

{\bf 2.a} load balancing

{\bf 2.b} {\bf dynamic}

{\bf 3.}

\begin{lstlisting}
void finddgs(int *a, int n, int *dgs) {
   #pragma omp parallel
   {  int i,j,dg;
      #pragma omp for
      for (i = 0; i < n; i++) {
         dg = 0;
         for (j = 0; j < n; j++) 
            dg += a[i*n+j];
         dgs[i] = dg;
      }
   }
}
\end{lstlisting}

{\bf 4.}

\begin{lstlisting}
meanmo <- function(a) {
   aap <- a %*% t(a)
   nummos <- sum(aap[row(aap) < col(aap)])
   n <- nrow(a)
   nummos / (n*(n-1)/2)
}
\end{lstlisting}

\end{document}

         find degrees at each node; matrix output; OMP
     
         alt version of clusterApply() is clusterLBApply(); mgr doles
         out chunks one at a time; each time worker done with task, mgr
         gives new one  
         (a) "LB" likely stands for ________
         (b) this takes a(an) ___________ approach to task sched

