%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%              tH                    %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection[\tHq]{\tHq}
\label{subsec:tHq}

This section describes the MC samples used for the modelling of \tH\ production.
Section~\ref{subsubsec:tHq_aMCP8} describes the \MGNLOPY[8] samples,

\subsubsection[MadGraph5\_aMC@NLO+Pythia8]{\MGNLOPY[8]}
\label{subsubsec:tHq_aMCP8}

\paragraph{Samples}
%\label{par:tHq_aMCP8_samples}

The descriptions below correspond to the samples in Table~\ref{tab:tHq_aMCP8}.

\begin{table}[htbp]
\begin{center}
\caption{Nominal \tH\ samples produced with \MGNLOPY[8].}
\label{tab:tHq_aMCP8}
\begin{tabular}{ l | l }
\hline
DSID range & Description \\
\hline
346188 & \tHq\, \Hgg\, four flavour \\
346229 & \tHq\, \Hbb\, four flavour \\
  346230 & \tHq\, \Htautau/\HZZ/\HWW\, four flavour \\
  346414 & \tHq\, \Hllll, four flavour \\
  346677 & \tHq\, \Hgg, four flavour, UFO model \\
\hline
\end{tabular}
\end{center}
\end{table}

\paragraph{Exceptions:}
If and only if you are using the UFO model sample: the correct version is
\MGNLO[2.6.2].

\paragraph{Short description:}

The production of \tHq events was modelled using the \MGNLO[2.6.0]~\cite{Alwall:2014hca}
generator at NLO with the \NNPDF[3.0nlo]~\cite{Ball:2014uwa} parton distribution function~(PDF).
The events were interfaced with \PYTHIA[8.230]~\cite{Sjostrand:2014zea} using the A14 tune~\cite{ATL-PHYS-PUB-2014-021} and the
\NNPDF[2.3lo]~\cite{Ball:2014uwa} PDF set.
The decays of bottom and charm hadrons were simulated using the \EVTGEN[1.6.0] program~\cite{Lange:2001uf}.

\paragraph{Long description:}

The \tHq samples were simulated using the \MGNLO[2.6.0]~\cite{Alwall:2014hca}
generator at NLO with the \NNPDF[3.0nlo]~\cite{Ball:2014uwa} parton distribution function~(PDF). The events were interfaced with
\PYTHIA[8.230]~\cite{Sjostrand:2014zea}~ using the A14 tune~\cite{ATL-PHYS-PUB-2014-021} and the \NNPDF[2.3lo]~\cite{Ball:2014uwa} PDF set.
The top quark was decayed at LO using \MADSPIN~\cite{Frixione:2007zp,Artoisenet:2012st} to preserve spin correlations,
whereas the Higgs boson was decayed by \PYTHIA in the parton shower. The samples
were generated in the four-flavour scheme.
The functional form of the renormalisation and factorisation scales was set to the
default scale $0.5\times \sum_i \sqrt{m^2_i+p^2_{\text{T},i}}$, where the sum runs over
all the particles generated from the matrix element calculation.
The decays of bottom and charm hadrons were simulated using the \EVTGEN[1.6.0] program~\cite{Lange:2001uf}.


\documentclass{article}

\title{\sc\LARGE CSCA67 Tutorial, Week 1\\
{\Large Sept. 14th-18th, 2015}}
\date{}
\author{\sc Compiled by {\em G. Singh Cadieux}\\[1ex]
\sc Adapted from\\
A. Bretscher, \href{http://www.utsc.utoronto.ca/~bretscher/a67/lectures/w1.pdf}{\em CSCA67 Week 1 Lecture Notes},\\
\href{http://www.intmath.com/counting-probability/2-basic-principles-counting.php}{\em Interactive Mathematics: Basic Principles of Counting} \&\\
Lov\'{a}sz, et al. \textit{Discrete Mathematics: Elementary and Beyond.} Springer, 2003.}

\usepackage{fullpage}
\usepackage{amsmath,amssymb}
\usepackage{color}
\usepackage{multicol}
\usepackage{tikz}
\usepackage{hyperref}

\setlength{\parindent}{0pt}

\begin{document}
\maketitle

\section{\sc Review of week 1's lecture}
\subsection*{\em A Counting Problem}

Consider a \href{https://www.youtube.com/watch?v=xak-onyKwyA}{pizza commercial} that advertises\dots
\begin{itemize}
\item 2 pizzas
\item up to 5 toppings on each
\item 11 toppings to choose from
\end{itemize}

\subsubsection*{\sc Q: How many different combinations of 2 pizzas exist?}
\textsc{First,} how many combinations of up to 5 toppings exist for 1 pizza?
\begin{multline*}
\text{\bf\# of combinations of \textit{up to} 5 toppings}=\\\text{\# of combinations of no toppings }+
\shoveright{\text{ \# of combinations of 1 topping }+\ldots+}\\
\text{ \# of combinations of 4 toppings }+\text{ \# of combinations of 5 toppings}
\end{multline*}

\begin{itemize}
\item There is only 1 way to ``combine" no toppings.\\
$\Rightarrow\text{\bf\# of combinations of no toppings}=1$

\item There are 11 choices of toppings, meaning that there are 11 ways to ``combine" a single topping.\\
$\Rightarrow\text{\bf\# of combinations of 1 topping}=11$

\item There are 11 different ways to choose a single topping. Once the first topping has been chosen, there are 10 other topping choices. This means that there are $11\times 10$ ways to choose 2 toppings.\\[1ex]
\textit{However}, some of the combinations of 2 toppings are equivalent, since the order in which the toppings are selected does not matter.\\[1ex]
Eg., $\underbrace{\text{tomatoes}}_{\text{topping1}},\,\underbrace{\text{cheese}}_{\text{topping2}}\Leftrightarrow\underbrace{\text{cheese}}_{\text{topping1}},\,\underbrace{\text{tomatoes}}_{\text{topping2}}$\\[1em]
How many combinations are equivalent?\\[1ex]
Every combination of 2 toppings is equivalent to one other combination in which the order of the 2 toppings is reversed (see ex. above). This means that only \textit{half} of the $11\times 10$ ways are unique.\\[1ex]
$\Rightarrow\text{\bf\# of combinations of 2 toppings}=\dfrac{11\cdot 10}{2}$

\item There are 11 different ways to choose a single topping. Once the first topping has been chosen, there are 10 other topping choices. Once the second topping has been chosen, there are 9 other topping choices. This means that there are $11\times 10\times 9$ ways to choose 3 toppings.\\[1ex]
However, once again, some of the combinations are equivalent. How many?\\[1ex]
Suppose that we choose 3 toppings. In how many different orders can we choose these 3 toppings?\\
There are 3 different ways to choose the first topping. Then there are 2 ways to choose the second, and 1 way to choose the third.\\[1ex]
Eg., $\underbrace{\text{tomatoes}}_{\text{topping1}},\,\underbrace{\text{cheese}}_{\text{topping2}},\,\underbrace{\text{olives}}_{\text{topping3}}
\Leftrightarrow\underbrace{\text{tomatoes}}_{\text{topping1}},\,\underbrace{\text{olives}}_{\text{topping2}}\,\underbrace{\text{cheese}}_{\text{topping3}}
\Leftrightarrow\underbrace{\text{cheese}}_{\text{topping1}},\,\underbrace{\text{olives}}_{\text{topping2}}\,\underbrace{\text{tomatoes}}_{\text{topping3}}
\Leftrightarrow\ldots$\\[1em]
This means that there are $3\times 2\times 1$ different orderings of 3 toppings, and $3\times 2\times 1$ equivalent combinations of 3 toppings in different orders. So only $\frac{1}{3\cdot 2\cdot 1}$, or 1 out of 6, of the $11\times 10\times 9$ ways are unique.\\[1ex]
$\Rightarrow\text{\bf\# of combinations of 3 toppings}=\dfrac{11\cdot 10\cdot 9}{3\cdot 2\cdot 1}$

\item By extension of the reasoning above, there are $11\times 10\times 9\times 8$ ways to choose 4 toppings. But there are $4\times 3\times 2\times 1$ ways of ordering 4 chosen toppings, meaning that only $\frac{1}{4\cdot 3\cdot 2\cdot 1}$ of the $11\times 10\times 9\times 8$ ways are unique.\\[1ex]
$\Rightarrow\text{\bf\# of combinations of 4 toppings}=\dfrac{11\cdot 10\cdot 9\cdot 8}{4\cdot 3\cdot 2\cdot 1}$

\item Finally, by extension again,\\[1ex]
$\text{\bf\# of combinations of 5 toppings}=\dfrac{11\cdot 10\cdot 9\cdot 8\cdot 7}{5\cdot 4\cdot 3\cdot 2\cdot 1}$
\end{itemize}

\begin{align*}
\therefore\text{ \# of combinations of \textit{up to} 5 toppings}& =1+11+\dfrac{11\cdot 10}{2}+\dfrac{11\cdot 10\cdot 9}{3\cdot 2\cdot 1}+\dfrac{11\cdot 10\cdot 9\cdot 8}{4\cdot 3\cdot 2\cdot 1}+\dfrac{11\cdot 10\cdot 9\cdot 8\cdot 7}{5\cdot 4\cdot 3\cdot 2\cdot 1}\\
& =1+11+55+165+330+462\\
& =1024
\end{align*}

\textsc{Second,} how many combinations of 2 pizzas exist?
\begin{multline*}
\text{\bf\# of combinations of 2 pizzas}=\\
\text{\# of combinations of 2 different pizzas }+\text{ \# of combinations of 2 of the same pizza}
\end{multline*}

\begin{itemize}
\item There are 1024 possible unique pizzas from which to choose a single pizza. Once the first pizza has been chosen, there are 1023 different choices for the second pizza.\\[1ex]
However, once again, the order in which the 2 pizzas are selected does not matter. So, as in the case of selecting 2 toppings for a pizza, every combination of 2 different pizzas \{pizza1, pizza2\} is equivalent to another combination \{pizza2, pizza1\}, in which the order is reversed.\\[1ex]
$\Rightarrow\text{\bf\# of combinations of 2 different pizzas}=\dfrac{1024\cdot 1023}{2}$
\item There are 1024 possible unique pizzas, meaning that there are 1024 ways to combine 2 of the same pizza.\\[1ex]
$\Rightarrow\text{\bf\# of combinations of 2 of the same pizza}=1024$
\end{itemize}

\begin{align*}
\therefore\text{ \sc\# of combinations of 2 pizzas}& =\dfrac{1024\cdot 1023}{2}+1024\\
& =523\;776+1024\\
& =524\;800
\end{align*}

\textsc{N.B. There are} several possible, equally valid ways to arrive at this answer, including different methods that may have been demonstrated in lecture, and different methods which we will see in the upcoming weeks.

\section{\sc Counting problems}

\subsection*{Q: {\em In how many ways can a number be chosen from 1 to 22 such that}}
\subsubsection*{a) {\em it is a multiple of 3 or 8?}}
Multiples of 3 from 1 to 22: {\color{red}3, 6, 9, 12, 15, 18, 21}\\[1ex]
Multiples of 8 from 1 to 22: {\color{blue}8, 16}\\[1em]
Multiples of 3 \textit{or} 8 from 1 to 22: {\color{red}3, 6,} {\color{blue}8,} {\color{red}9, 12, 15,} {\color{blue}16,} {\color{red}18, 21}\\[1ex]
$\therefore$ There are 9 multiples of 3 or 8 from which to choose.

\subsubsection*{b) {\em it is a multiple of 2 or 3?}}
Multiples of 2 from 1 to 22: {\color{red}2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22}\\[1ex]
Multiples of 3 from 1 to 22: {\color{blue}3, 6, 9, 12, 15, 18, 21}\\[1em]
Notice that some of these multiples are not unique: for instance, 6 is a multiple of both 2 \textit{and} 3. We only consider these numbers once.\\[1ex]
Multiples of 2 \textit{or} 3 from 1 to 22: {\color{red}2,} {\color{blue}3,} {\color{red}4,} 6, {\color{red}8,} {\color{blue}9,} {\color{red}10,} 12, {\color{red}14,} {\color{blue}15,} {\color{red}16,} 18, {\color{red}20,} {\color{blue}21,} {\color{red}22}\\[1ex]
$\therefore$ There are 15 multiples of 2 or 3 from which to choose.\\[1em]
We can also represent this as a \textit{Venn diagram}:\\[1ex]
\begin{tikzpicture}[scale=1.1]
\draw (0,0) circle[radius=0.5in];
\draw (0.75in,0) circle[radius=0.5in];
\node[align=center,font={\small},pin=below right:{}] at (-0.6in,0.6in) {multiples of 2\\ from 1 to 22};
\node[align=center,font={\small},pin=below left:{}] at (1.35in,0.6in) {multiples of 3\\ from 1 to 22};
\node[align=center,font={\small},pin=above:{}] at (0.37in,-0.7in) {multiples of 2 \textit{and} 3\\ from 1 to 22};
\node[align=center,color=red] at (-.1in,0) {2\\4\quad 8\\10\quad 14\\16\quad 20\\22};
\node[align=center] at (.37in,0) {6\\12\\18};
\node[align=center,color=blue] at (.85in,0) {3\\9\quad 15\\21};
\end{tikzpicture}

\subsection*{{\normalsize Suppose you have 2 t-shirts and 4 pairs of jeans.}\\
Q: {\em How many combinations of 1 t-shirt and 1 pair of jeans can you make?}}
Let's enumerate all the combinations:\\[1ex]
\begin{tabular}{lcclc}
(1)&\{T1, J1\}&&(5)&\{T2, J1\}\\
(2)&\{T1, J2\}&&(6)&\{T2, J2\}\\
(3)&\{T1, J3\}&&(7)&\{T2, J3\}\\
(4)&\{T1, J4\}&&(8)&\{T2, J4\}
\end{tabular}\\[1em]
Alternatively, the combinations can be depicted with trees:\\[1ex]
\begin{tikzpicture}
\node[rectangle,draw]{T1}
child{node[rectangle,draw]{J1}}
child{node[rectangle,draw]{J2}}
child{node[rectangle,draw]{J3}}
child{node[rectangle,draw]{J4}};
\end{tikzpicture}
\quad
\begin{tikzpicture}
\node[rectangle,draw]{T2}
child{node[rectangle,draw]{J1}}
child{node[rectangle,draw]{J2}}
child{node[rectangle,draw]{J3}}
child{node[rectangle,draw]{J4}};
\end{tikzpicture}\\[1em]
We can see that there are 8 combinations in total.\\[1ex]
If we first choose a t-shirt, there are 2 ways that we can do so. Then, there are 4 ways to choose a pair of jeans.
$2\times 4$ gives us the total number of combinations.\\[1em]
\textsc{In general,} there are $n\times m$ ways to combine 1 object from a set of $n$ objects with 1 object from another set of $m$ objects.

\subsection*{{\normalsize The life insurance policies of an insurance company are classified by:\\[1ex]
\begin{tabular}{p{0.3\textwidth}p{0.3\textwidth}p{0.3\textwidth}}
(i) age of the insured
	\begin{itemize}
	\item under 25 years
	\item 25-50 years
	\item over 50 years
	\end{itemize}
&(ii) sex
	\begin{itemize}
	\item male
	\item female
	\end{itemize}
&(iii) marital status
	\begin{itemize}
	\item single
	\item married
	\end{itemize}
\end{tabular}\\
An example of a policy classification is \{25-50 years, male, single\}.}\\
Q: {\em What is the total number of classifications?}}
Using the multiplication principle from above, we say that there are
\begin{equation*}
3\text{ ages}\times 2\text{ sexes}\times 2\text{ marital statuses}=12\text{ total classifications}
\end{equation*}

\subsection*{{\normalsize On \href{http://mono-1.com/monoface/main.html}{mono-1.com/monoface}, we can combine different people's facial features to form a composite face. We are told that there are 759 375 possible composite faces.}\\
Q: {\em How did they calculate the total number of faces?}}
There are 5 features that we can change: head \& shoulders, right eye, left eye, nose, mouth.\\
For each of these features, there are 15 possible variations.\\[1ex]
Using the multiplication principle, we determine that there are
\begin{equation*}
[15\text{ head \& shoulders}]\times[15\text{ right eye}]\times\ldots\times[15\text{ mouth}]=15^5=759\,375\text{ combinations of features}
\end{equation*}

\subsubsection*{Q: {\em If they let us change the chin as well, how many possible combinations would there be?}}
We would then have 6 features to change, and 15 variations of each of these features.\\
As above, we say that there would be
\begin{equation*}
[15\text{ head \& shoulders}]\times\ldots\times[15\text{ mouth}]\times[15\text{ chin}]=15^6=11\,390\,625\text{ combinations of features}
\end{equation*}

\section{\sc Additional practice problems}

7 people are going to a party: Alice, Bob, Carl, Diane, Eve, Frank, and George.\\
When they have all arrived, everyone shakes hands.\\
{\bf Q: How many handshakes were there altogether?}\\[1em]
They then go to the table to eat, but they can't agree on the seating arrangement.\\
{\bf Q: How many possibilities are there if Alice always stays at the head (since it is her birthday)?}\\[1ex]
{\bf Q: How many possibilities are there if Alice can sit anywhere?}\\[1em]
They decide to play bridge after dinner. When the cards have been dealt, Carl says that he thinks he got the same hand as last time.\\
{\bf Q: What is the likelihood that he is right?}\\[1em]
Finally, they decide to play chess. Alice just wants to watch, and sets up 3 boards.\\
{\bf Q: How many different ways can they be matched?}

\end{document}


The experiments we have discussed so far are fairly simple. They have
one independent variable with two levels, and a single dependent
variable. Experiments can become much more complicated by adding more
levels to the independent variable, adding more independent variables,
and/or adding more dependent variables. As experiments become more
complicated, the basic empirical question remains the same: Did the
manipulation(s) cause change in the measure(s). To ease into more
complex designs we will discuss single factor designs with more than two
levels.

\subsection{Quantitative vs.~Qualitative Independent
variables}\label{quantitative-vs.qualitative-independent-variables}

A single factor design with more than two levels involves a single
independent variable (factor), and typically a single dependent
variable. Importantly, the independent variable has more than two
levels. Two common kinds of multi-level designs involve either
quantitative or qualititative manipulations of the independent variable.

\begin{figure}
\includegraphics[width=.6\linewidth]{OneWay_files/figure-latex/unnamed-chunk-1-1}
\end{figure}

A quantitative manipulation is a change in magnitude, or amount. For
example, a drug company might be interested in testing not only whether
or not Drug A reduces headache (perhaps by comparing one group that gets
the drug, and another that does not), but also how the amount of the
drug influences reductions in headache pain. So, a multi-level
experiment might have a few groups who receive, 0, 1, 2, 3, 4 or more
pills, respectively.

\begin{figure}
\includegraphics[width=.6\linewidth]{OneWay_files/figure-latex/unnamed-chunk-2-1} \end{figure}

A qualitative manipulation involves categorically different conditions.
For example, a drug company might be interested in comparing the
relative effectiveness of different kinds of drugs in reducing headache
pain. They could conduct a multi-level experiment with each group
receiving a different drug, drug A, drug B, drug C, and so on.

\subsection{Interpreting the pattern of results}\label{interpreting-the-pattern-of-results}

\begin{marginfigure}
Possible patterns of differences between means in a design with three
levels

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  A = B = C
\item
  A = B \textgreater{} C
\item
  A = B \textless{} C
\item
  A \textgreater{} B = C
\item
  A \textless{} B = C
\item
  A \textless{} B \textless{} C
\item
  A \textgreater{} B \textgreater{} C
\item
  A = C \textgreater{} B
\item
  A = C \textless{} B
\end{enumerate}
\end{marginfigure}

Designs with only two levels are fairly straightforward to interpret
because there are only a few possible kinds of patterns of differences
that can be observed. These include: A\textgreater{}B, A=B, and
A\textless{}B. Or even more simply: A is the same as B (A=B), or A is
not the same as B (A\textgreater{}B, or A\textless{}B).

The number of possible patterns that could be observed increases with
each additional level. For example, consider an experiment with three
levels A, B, and C. The possible patterns that could be observed are
shown on the right.

As with two level designs, when reporting the results of experiments
with multiple levels, it is very important to explain the pattern of
means across conditions. This involves telling the reader which means
were different from one another, and which means were the same.

Again, as with 2-level designs, the process of random sampling can
produce differences in the sample means for each of the levels. So,
researchers also conduct statistical tests to determine the likelihood
that the results that they observed could have been obtained by chance
alone. The most common statistical test used in this case is the one-way
ANOVA (Analysis of Variance). The chapter on inferential statistics goes
into more detail about ANOVAs, and we assume that you have some memories
of how ANOVAs work from your statistics class. Nevertheless, we go
through an example to illustrate the basic process. Note, this example
is the same one discussed in chapter four of your lab manual.

\subsection{An example one-way ANOVA using
R}\label{an-example-one-way-anova-using-r}

Consider an experiment where subjects attempt to memorize words for a
later recall test under five different conditions. This will be a
between-subjects experiment with 10 simulated subjects in each
condition. We will have condition A, B, C, D, and E. For example,
condition A could be repeating each word silently to yourself, condition
B could be creating a mental picture of each word, and so on. To
simulate data for each subject we need to make some assumptions. Let's
say that out of 30 words most people remember about 15 of them, but
there is variation, so some people do better and some people do worse.
We can model this by sampling numbers randomly from a distribution of
our choice. For convenience, we will use the normal distribution. Let's
imagine that condition A and B help memory more than C and D, and that
memory is worse in condition E. Here is the R code and output for
simulating this kind of data.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{A<-}\KeywordTok{round}\NormalTok{(}\KeywordTok{rnorm}\NormalTok{(}\DecValTok{10}\NormalTok{,}\DecValTok{20}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\NormalTok{B<-}\KeywordTok{round}\NormalTok{(}\KeywordTok{rnorm}\NormalTok{(}\DecValTok{10}\NormalTok{,}\DecValTok{20}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\NormalTok{C<-}\KeywordTok{round}\NormalTok{(}\KeywordTok{rnorm}\NormalTok{(}\DecValTok{10}\NormalTok{,}\DecValTok{15}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\NormalTok{D<-}\KeywordTok{round}\NormalTok{(}\KeywordTok{rnorm}\NormalTok{(}\DecValTok{10}\NormalTok{,}\DecValTok{15}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\NormalTok{E<-}\KeywordTok{round}\NormalTok{(}\KeywordTok{rnorm}\NormalTok{(}\DecValTok{10}\NormalTok{,}\DecValTok{10}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\NormalTok{all_data<-}\KeywordTok{data.frame}\NormalTok{(A,B,C,D,E)}
\KeywordTok{kable}\NormalTok{(all_data,}\DataTypeTok{format=}\StringTok{"latex"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{tabular}{r|r|r|r|r}
\hline
A & B & C & D & E\\
\hline
18 & 22 & 17 & 16 & 9\\
\hline
22 & 18 & 13 & 15 & 11\\
\hline
16 & 20 & 15 & 12 & 7\\
\hline
22 & 22 & 16 & 14 & 7\\
\hline
17 & 20 & 18 & 18 & 13\\
\hline
19 & 21 & 18 & 17 & 9\\
\hline
20 & 19 & 14 & 13 & 15\\
\hline
21 & 18 & 15 & 13 & 10\\
\hline
19 & 19 & 13 & 14 & 9\\
\hline
22 & 22 & 17 & 16 & 10\\
\hline
\end{tabular}

We have produced a table with fake data for 10 subjects in each
condition. The numbers all represent the number of correctly recalled
words for each simulated subject. For groups A and B we sample 10
numbers, from a distribution with mean 20, and standard deviation 2.
This is a higher mean than groups C and D (mean = 15). The lowest mean
was for Group E (mean = 10). So, on average, groups A and B should have
higher scores than C and D, which should be higher than E.

Ok, so what happened in our simulated experiment. We can see the numbers
in the table, but it would be nice to summarize them so we can more
easily look at differences. After all, it's hard to make sense of a
bunch of raw data in a table.

One way to summarize the data is to compute the group means for each
condition. This averages over the subjects, and gives us only 5 means to
look at, so it is easier to see the differences. We can ``easily'' do
this in R in a couple different ways. However, R often likes the data in
a particular format, in this case long-data format. So, we will first
convert to that format, and see what it looks like.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{long_data<-}\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{Conditions=}\KeywordTok{rep}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"A"}\NormalTok{,}\StringTok{"B"}\NormalTok{,}\StringTok{"C"}\NormalTok{,}\StringTok{"D"}\NormalTok{,}\StringTok{"E"}\NormalTok{),}\DataTypeTok{each=}\DecValTok{10}\NormalTok{),}
                      \DataTypeTok{Recall=}\KeywordTok{c}\NormalTok{(A,B,C,D,E))}
\KeywordTok{kable}\NormalTok{(long_data[}\DecValTok{1}\NormalTok{:}\DecValTok{25}\NormalTok{,],}\DataTypeTok{format=}\StringTok{"latex"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{tabular}{l|r}
\hline
Conditions & Recall\\
\hline
A & 18\\
\hline
A & 22\\
\hline
A & 16\\
\hline
A & 22\\
\hline
A & 17\\
\hline
A & 19\\
\hline
A & 20\\
\hline
A & 21\\
\hline
A & 19\\
\hline
A & 22\\
\hline
B & 22\\
\hline
B & 18\\
\hline
B & 20\\
\hline
B & 22\\
\hline
B & 20\\
\hline
B & 21\\
\hline
B & 19\\
\hline
B & 18\\
\hline
B & 19\\
\hline
B & 22\\
\hline
C & 17\\
\hline
C & 13\\
\hline
C & 15\\
\hline
C & 16\\
\hline
C & 18\\
\hline
\end{tabular}

I've only printed the first 25 lines, but the dataframe contains all of
the data for conditions, C, D, and E as well. You can see why they call
it long format. It's because each data point gets it's own row in the
table.

\subsection{Looking at the means}\label{looking-at-the-means}

Now that the data is in long format we can easily make a table of the
condition means

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{condition_means<-}\KeywordTok{aggregate}\NormalTok{(Recall~Conditions,long_data,mean)}
\KeywordTok{kable}\NormalTok{(condition_means,}\DataTypeTok{format=}\StringTok{"latex"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{tabular}{l|r}
\hline
Conditions & Recall\\
\hline
A & 19.6\\
\hline
B & 20.1\\
\hline
C & 15.6\\
\hline
D & 14.8\\
\hline
E & 10.0\\
\hline
\end{tabular}

We can now see the group means, but we can't see any measure of how
variable the data are in each condition. We might, for example, also
want to compute the standard deviation as well as the mean, and put them
both in the table. We could run the same code from above and substite sd
for mean, which would give us a table of standard deviations. However,
we will use a more advanced function from the plyr package, called
ddply. ddply let's you compute multiple statistics and put them all in a
single table. The syntax is a bit different, but it doesn't take long to
get used to it.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(plyr)}
\NormalTok{condition_means<-}\KeywordTok{ddply}\NormalTok{(long_data,.(Conditions),summarise,}
                       \DataTypeTok{MeanRecall=}\KeywordTok{mean}\NormalTok{(Recall),}
                       \DataTypeTok{StdDeviation=}\KeywordTok{sd}\NormalTok{(Recall))}
\KeywordTok{kable}\NormalTok{(condition_means,}\DataTypeTok{format=}\StringTok{"latex"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{tabular}{l|r|r}
\hline
Conditions & MeanRecall & StdDeviation\\
\hline
A & 19.6 & 2.170509\\
\hline
B & 20.1 & 1.595131\\
\hline
C & 15.6 & 1.897367\\
\hline
D & 14.8 & 1.932184\\
\hline
E & 10.0 & 2.494438\\
\hline
\end{tabular}

\subsection{Plotting the data}\label{plotting-the-data}

It's often very desirable to plot the data in a graph, rather than just
present the means in a table. People find it easier to look at graphs,
because the differences in the data just pop-out much easier than
looking at numbers in a table. R has a fantastic graphing package called
ggplot2. ggplot2 is a whole philosophy for visual design and
data-presentation, and it can be daunting at first. But, it's complexity
makes it very powerful, and when you get the hang of it you can very
quickly make all sorts of beautiful graphs to present data. Here is some
code to make ggplot create a bar graph to plot the means, along with
error bars. In this case the error bars with represent standard errors
of the mean, rather than standard deviations. R does not have a built in
function for the standard error of the mean, so we have to write it
ourselves.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(ggplot2)}
\NormalTok{sde<-function(x)\{}\KeywordTok{sd}\NormalTok{(x)/}\KeywordTok{length}\NormalTok{(x)\}}
\NormalTok{plot_means<-}\KeywordTok{ddply}\NormalTok{(long_data,.(Conditions),summarise,}
                       \DataTypeTok{MeanRecall=}\KeywordTok{mean}\NormalTok{(Recall),}
                       \DataTypeTok{SE=}\KeywordTok{sde}\NormalTok{(Recall))}

\NormalTok{limits <-}\StringTok{ }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{ymax =} \NormalTok{MeanRecall +}\StringTok{ }\NormalTok{SE, }\DataTypeTok{ymin =} \NormalTok{MeanRecall -}\StringTok{ }\NormalTok{SE)}

\KeywordTok{ggplot}\NormalTok{(plot_means,}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x=}\NormalTok{Conditions, }\DataTypeTok{y=}\NormalTok{MeanRecall))+}
\StringTok{  }\KeywordTok{geom_bar}\NormalTok{(}\DataTypeTok{position=}\StringTok{"dodge"}\NormalTok{,}\DataTypeTok{stat=}\StringTok{"identity"}\NormalTok{)+}
\StringTok{  }\KeywordTok{geom_errorbar}\NormalTok{(limits, }\DataTypeTok{width=}\NormalTok{.}\DecValTok{1}\NormalTok{)+}
\StringTok{  }\KeywordTok{theme_classic}\NormalTok{(}\DataTypeTok{base_size=}\DecValTok{12}\NormalTok{)+}
\StringTok{  }\KeywordTok{ylab}\NormalTok{(}\StringTok{"Mean Correctly }\CharTok{\textbackslash{}n}\StringTok{ Recalled Words"}\NormalTok{)+}
\StringTok{  }\KeywordTok{xlab}\NormalTok{(}\StringTok{"Condition"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{OneWay_files/figure-latex/unnamed-chunk-8-1}

Now it is easy to the differences between conditions. Just as we had
hoped, Groups A and B appear to have recalled more words than Groups C
and D, which remembered more words than group E.

\subsection{Conducting the ANOVA}\label{conducting-the-anova}

Although the graph and the tabe show some clear differences in the
means, we still want to find out the probability that this kind of
finding occurs by chance alone. We can be confident in the differences
when we know that they do not occur very often by chance alone. The
first step is conduct a one-way ANOVA. This is very easy in R.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aov.out<-}\KeywordTok{aov}\NormalTok{(Recall~Conditions,long_data)}
\end{Highlighting}
\end{Shaded}

We're done! It's only one line of code. However, we need a couple more
to see the results.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aov_summary<-}\KeywordTok{summary}\NormalTok{(aov.out)}
\KeywordTok{kable}\NormalTok{(}\KeywordTok{xtable}\NormalTok{(aov_summary),}\DataTypeTok{format=}\StringTok{"latex"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{tabular}{l|r|r|r|r|r}
\hline
  & Df & Sum Sq & Mean Sq & F value & Pr(>F)\\
\hline
Conditions & 4 & 673.68 & 168.420000 & 40.46396 & 0\\
\hline
Residuals & 45 & 187.30 & 4.162222 & NA & NA\\
\hline
\end{tabular}

The ANOVA table gives us a bunch of information. We will go into much
greater detail about the meaning of each number in the table, but also
assume for now that you are somewhat familiar with these ideas because
you have already taken statistics, right?

We are mainly interested in the p-value, which tells how often results
like the ones we found can occur by chance. But, when we report the
results of our ANOVA, we also provide additional information about the
F-value, the degrees of freedom values, and the mean squared error term.
The reason is that if you know these numbers, you can actually
reconstruct all of the other numbers. The results of our ANOVA are
significant. You could report this in a sentence like the following.

The main effect of condition was significant, F(4, 45) = 40.46, MSE =
4.16, p \textless{} .001.

\subsection{Comparisons between
conditions}\label{comparisons-between-conditions}

The p-value from above is much smaller than .05, which shows the
difference between conditions in the data does not occur very often by
chance alone. However, because we conducted an omni-bus test, we only
know that there is some difference between conditions, but we do not
know which specific conditions are different from one another.

So, we have to conduct additional tests between specific conditions.
There are multiple strategies for conducting these tests. For now, we
will simply run t-tests between comparisons of interest.

Remember, our data simulated the pattern that memory recall would be
better for groups A and B, which would be better than groups C and D,
which would better than group E. In other words A=B \textgreater{} C=D
\textgreater{} E.

We can confirm this pattern by conducting tests to see if it holds up.
For example, how would we test the pattern A=B \textgreater{} C=D
\textgreater{} E, all of the following comparisons need to be true,

\begin{itemize}
\item
  A = B
\item
  A \textgreater{} C
\item
  A \textgreater{} D
\item
  B \textgreater{} C
\item
  B \textgreater{} D
\item
  C = D
\end{itemize}

and, all of the conditions should be greater than E

\begin{itemize}
\item
  A \textgreater{} E
\item
  B \textgreater{} E
\item
  C \textgreater{} E
\item
  D \textgreater{} E
\end{itemize}

Let's conduct a few of these tests, and then report the findings.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(broom)}
\CommentTok{#conduct t-tests}
\NormalTok{ab<-}\KeywordTok{tidy}\NormalTok{(}\KeywordTok{t.test}\NormalTok{(A,B,}\DataTypeTok{var.equal =} \OtherTok{TRUE}\NormalTok{))}
\NormalTok{ac<-}\KeywordTok{tidy}\NormalTok{(}\KeywordTok{t.test}\NormalTok{(A,C,}\DataTypeTok{var.equal =} \OtherTok{TRUE}\NormalTok{))}
\NormalTok{cd<-}\KeywordTok{tidy}\NormalTok{(}\KeywordTok{t.test}\NormalTok{(C,D,}\DataTypeTok{var.equal =} \OtherTok{TRUE}\NormalTok{))}
\NormalTok{de<-}\KeywordTok{tidy}\NormalTok{(}\KeywordTok{t.test}\NormalTok{(D,E,}\DataTypeTok{var.equal =} \OtherTok{TRUE}\NormalTok{))}

\CommentTok{#put the results in a table}
\NormalTok{alltests<-}\KeywordTok{rbind}\NormalTok{(ab,ac,cd,de)}
\NormalTok{alltests<-}\KeywordTok{cbind}\NormalTok{(alltests,}\DataTypeTok{Comparison=}\KeywordTok{c}\NormalTok{(}\StringTok{"AB"}\NormalTok{,}\StringTok{"AC"}\NormalTok{,}\StringTok{"CD"}\NormalTok{,}\StringTok{"DE"}\NormalTok{))}
\NormalTok{finaltable <-}\StringTok{ }\KeywordTok{subset}\NormalTok{(alltests, }\DataTypeTok{select =} \KeywordTok{c}\NormalTok{(Comparison,estimate1,estimate2,statistic,p.value,parameter))}
\KeywordTok{kable}\NormalTok{(finaltable,}\DataTypeTok{format=}\StringTok{"latex"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{tabular}{l|r|r|r|r|r}
\hline
Comparison & estimate1 & estimate2 & statistic & p.value & parameter\\
\hline
AB & 19.6 & 20.1 & -0.5869954 & 0.5644975 & 18\\
\hline
AC & 19.6 & 15.6 & 4.3876345 & 0.0003550 & 18\\
\hline
CD & 15.6 & 14.8 & 0.9341987 & 0.3625653 & 18\\
\hline
DE & 14.8 & 10.0 & 4.8107024 & 0.0001401 & 18\\
\hline
\end{tabular}

\subsection{Writing it all up}\label{writing-it-all-up}

The following is an example results section for our hypothetical
experiment. This could serve as a model for your own results section.

The number of correctly recalled words for each subject in each
condition were submitted to a one-way ANOVA, with memorization condition
(A, B, C, D, and E) as the sole between-subjects factor. Mean recall
scores in each condition are displayed in Figure 1.

The main effect of memorization condition was significant, F(4, 45) =
40.46, MSE = 4.16, p \textless{} .001. Figure 1 shows that Groups A and
B had higher recall scores than Groups C and D, which had higher recall
scores than Group E. This pattern was confirmed across four independent
sample t-tests. Group A (M = 19.6) and Group B (M = 20.1) were not
significantly different t(18) = -0.59, p =0.564. Group A recalled
significantly more words than Group C (M = 15.6), t(18) = 4.39, p =0.
Group C and Group D (M = 14.8) were not significantly different t(18) =
0.93, p =0.363. Finally, Group D recalled significantly more words than
Group E (M = 10), t(18) = 4.81, p =0.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fphw Assignment
% LaTeX Template
% Version 1.0 (27/04/2019)
%
% This template originates from:
% https://www.LaTeXTemplates.com
%
% Authors:
% Class by Felipe Portales-Oliva (f.portales.oliva@gmail.com) with template
% content and modifications by Vel (vel@LaTeXTemplates.com)
%
% Template (this file) License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%    PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[
    12pt, % Default font size, values between 10pt-12pt are allowed
    %letterpaper, % Uncomment for US letter paper size
    %spanish, % Uncomment for Spanish
]{fphw}

% Template-specific packages
\usepackage[utf8]{inputenc} % Required for inputting international characters
\usepackage[T1]{fontenc} % Output font encoding for international characters
\usepackage{fontspec,unicode-math} % Required for using utf8 characters in math mode
\usepackage{parskip}  % To add extra space between paragraphs
% \usepackage{mathpazo} % Use the Palatino font
\usepackage{graphicx} % Required for including images
\usepackage{booktabs} % Better horizontal rules in tables
\usepackage{hyperref} % For links (both internal and external)
% \usepackage{listings} % Required for insertion of code
\usepackage{enumerate}% To modify the enumerate environment
\usepackage{cleveref} % Better \ref command -> \cref
\usepackage{import}   % This 4 packages and the command allow importing pdf
\usepackage{xifthen}  % figures generated with inkscape
\usepackage{pdfpages} % Source: https://castel.dev/post/lecture-notes-2/
\usepackage{mathtools}
\usepackage{wrapfig}
\usepackage{cancel}
\usepackage{transparent}
\newcommand{\incfig}[1]{%
    \def\svgwidth{0.95\columnwidth}
    \small
        \import{./images/}{#1.pdf_tex}
}

\setlength{\parindent}{15pt}
\setlength{\headheight}{22.66pt}

%----------------------------------------------------------------------------------------
%    ASSIGNMENT INFORMATION
%----------------------------------------------------------------------------------------

\title{Task 7 \\ Orientability} % Assignment title

\author{Emilio Domínguez Sánchez} % Student name

\date{November 21th, 2020} % Due date

\institute{University of Murcia \\ Faculty of Mathematics} % Institute or school name

\class{Geometría de Superficies} % Course or class name

\professor{Dr. Pascual Lucas Saorin} % Professor or teacher in charge of the assignment

%----------------------------------------------------------------------------------------
%    Definitions
%----------------------------------------------------------------------------------------

\usepackage{physics}
\newcommand{\R}{\mathbb{R}}
\newcommand{\basis}[2]{\qty{#1,\ #2}}
\DeclareMathOperator{\sgn}{sgn}
\DeclareMathOperator{\Id}{Id}

\begin{document}

\maketitle % Output the assignment title, created automatically using the information in the custom commands above

%----------------------------------------------------------------------------------------
%    ASSIGNMENT CONTENT
%----------------------------------------------------------------------------------------

\section*{Problem}

\begin{problem}
    \begin{enumerate}
        \item \label{stm:i} Prove that if $φ : S_1 \to S_2$ is
        a differentiable application between two surfaces that is also
        a local diffeomorphism in every point $p \in S_1$ and
        $S_2$ is orientable then
        $S_1$ is orientable.

        \item \label{stm:ii} Prove that if $φ : S_1 \to S_2$ is a diffeomorphism.
        then $S_1$ is orientable if and only if $S_2$ is orientable.
        (Therefore, orientability is a quality invariant under a diffeomorphism.)

        \item \label{stm:iii} Prove that if $φ : S_1 \to S_2$ is a diffeomorphism between
        two oriented surfaces,
        $φ$ may induce a different orientation over $S_2$ than the initial.
    \end{enumerate}
\end{problem}

%----------------------------------------------------------------------------------------

\subsection*{Answer}

    \Cref{stm:ii} is a consequence of \cref{stm:i}.
A diffeomorphism and its inverse are local diffeomorphisms.

    We will use the definition that
a surface $S$ is orientable if there is an atlas $A_S$ such that the
differential of the change of coordinates
between any pair of parametrizations that intersect
has a positive determinant.

    That means that the bases of the tangent plane $T_p(S)$
over a point $p \in S$ given
by any two intersecting parametrizations $X,Y \in A_S$
determine the same orientation of $T_p$,
because the change of coordinates
%
\begin{equation*}
    \mqty(X_{\bar{u}} \\ X_{\bar{v}}) =
    { \displaystyle
    \mqty(
        \pdv{\bar{u}}{u} & \pdv{\bar{u}}{v} \\[6pt]
        \pdv{\bar{v}}{u} & \pdv{\bar{v}}{v}
    )
    }
    \mqty(X_u \\ X_v)
\end{equation*}
%
(at the point $p$)
is the jacobian in $p$ of the change of coordinates between $X$ and $Y$.

    We start the exercise fixing an orientation for $S_2$ by picking
any fixed atlas $A_{S_2}$.
The orientation defined on the tangent planes of $S_2$
by any parametrization $X \in A_{S_2}$ will be called \emph{positive}.

    We can use $φ$ to select an atlas on $S_1$ that is oriented as well.
For every point $p \in S_1$,
$\dd{φ}(p)$ is a biyection between $T_p(S_1)$ and $T_{φ(p)}(S_2)$.
Hence, we will call a parametrization $X : U \to X(U)$
\emph{positively oriented} if the basis
$\basis{\dd{φ}(X(q))(X_u)}{\dd{φ}(X(q))(X_v)}$
is positively oriented in $T_{φ(q)}(S_2)$ for any $q \in U$.

    Given two intersecting positively oriented parametrizations $X$ and $Y$
of $p \in S$,
the jacobian of the change of coordinates between them must be positive,
because if $M$ is the positive operator that acts as the change of coordinates between
$\basis{\dd{φ}(X_u)}{\dd{φ}(X_v)}$ and $\basis{\dd{φ}(Y_u)}{\dd{φ}(U_v)}$,
the change of coordinates from
$\basis{X_u}{X_v}$ and $\basis{Y_u}{Y_v}$,
%
\begin{equation*}
    \mqty(Y_u & Y_v) =
    \dd{φ}^{-1} \circ M \circ \dd{φ}
    \qty(\mqty(X_u & X_v)), \\
\end{equation*}
%
which is equal to the jacobian, is positive:
%
\begin{equation*}
    \sgn(\dd{φ}^{-1} \circ M \circ \dd{φ}) =
    \sgn(\dd{φ}^{-1})\sgn(M)\sgn(\dd{φ}) =
    \sgn{M}.
\end{equation*}

    It is left to see that there is always
a positively oriented parametrization for any point $p \in S_1$.
Take $X : U \to X(U)$, a parametrization of $p$.
We can assume that $\basis{\dd{φ}(p)(Y_u(p))}{\dd{φ}(p)(Y_v(p))}$
is postively oriented in $T_{φ(p)}(S_2)$,
because the parametrization $Y(u, v) = X(v, u)$ satisfies
%
\begin{equation*}
    \sgn(\basis{\dd{φ}(Y_u)}{\dd{φ}(Y_v)}) =
    \sgn(\basis{\dd{φ}(X_v)}{\dd{φ}(X_u)}) =
    -\sgn(\basis{\dd{φ}(X_u)}{\dd{φ}(X_v)}).
\end{equation*}
%
Now note that
%
\begin{equation*}
    \basis{\dd{φ}(Y_u)}{\dd{φ}(Y_v)} =
    \dd{φ}\circ\dd{X}\qty(\basis{\mqty(1 \\ 0)}{\mqty(0 \\ 1)}).
\end{equation*}
%
It is clear from the equality above that the basis is positive
when the determinant of $\dd{φ}\circ\dd{X}$ is.
But because it is positive in $p$ and $\dd{φ}\circ\dd{X}$ is always of maximum rank,
it is positive in the connected component of $p$ in $U$.

    Hence, an oriented atlas for $S_1$ is given by taken
a positively oriented parametrization $X_p$ defined in a connected open set
for every point $p \in S_1$.

    An example for \cref{stm:iii} needs of two orientations for the same surface $S_2$.
And for simplicity, we will take $S_1 = S_2$ and the identity map.
The easiest surface must be $\R^2 \subset \R^3$.
The atlas $\qty{i : \R^2 \to \R^3}$ has a different orientation than
$\qty{r(u, v) = \mqty(v & u & 0) : \R^2 \to \R^3}$.
This can be justified writing the bases
%
\begin{align*}
    \basis{i_u(0)}{i_v(0)} &= \basis{e_1}{e_2} \qq{and} \\
    \basis{r_u(0)}{r_v(0)} &= \basis{e_2}{e_1},
\end{align*}
%
which have different orientations.

    The identity map $\Id : (\R^2, \{i\}) \to (\R^2 \{r\})$
is a diffeomorphism between $S_1 = \R^2$ to $S_2 = \R^2$ that induces a different
orientation in $S_2$,
as we will prove.
Fix $p \in S_2$, then, according to $\Id$,
a basis $\basis{X_u}{X_v}$ of $T_p(S_2)$ is positively oriented if
$\basis{\dd{i^{-1}}(X_u)}{\dd{i^{-1}}(X_v)} = \basis{X_u}{X_v}$ is positive in $S_1$,
where we are identifying $T_p(S_1)$ with $T_p(S_2)$.
But the bases that are positive in $S_1$ are the bases that are negative in $S_2$,
according to its original orientability.

%----------------------------------------------------------------------------------------

\end{document}


\documentclass{beamer}

\usepackage{paratype}
\setbeamerfont{frametitle}{family=\bf}

% Beamer theme settings
\usecolortheme{seagull}
\usenavigationsymbolstemplate{} % no navigation buttons

\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{epic}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

\newcommand{\basetop}[1]{\vtop{\vskip-1ex\hbox{#1}}}
\newcommand{\source}[1]{\let\thefootnote\relax\footnotetext{\scriptsize\textcolor{kugray1}{Source: #1}}}

% for coloured code citation in text:
\usepackage{fancyvrb}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%    code sections   %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% code highlighting commands in own block
\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\scriptsize}
\DefineVerbatimEnvironment{icode}{Verbatim}{fontsize=\scriptsize}

% Fancy code with color commands:
\DefineVerbatimEnvironment{colorcode}%
        {Verbatim}{fontsize=\scriptsize,commandchars=\\\{\}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%    some coloring    %%%%%%%%

\definecolor{Red}{RGB}{220,50,10}
\definecolor{Blue}{RGB}{0,51,102}
\definecolor{Yellow}{RGB}{102,51,0}
\definecolor{Orange}{RGB}{178,36,36}
\definecolor{Grey}{RGB}{180,180,180}
\definecolor{Green}{RGB}{20,120,20}
\definecolor{Purple}{RGB}{160,50,100}
\newcommand{\red}[1]{\textcolor{Red}{{#1}}}
\newcommand{\blue}[1]{\textcolor{Blue}{{#1}}}
\newcommand{\yellow}[1]{\textcolor{Yellow}{{#1}}}
\newcommand{\orange}[1]{\textcolor{Orange}{{#1}}}
\newcommand{\grey}[1]{\textcolor{Grey}{{#1}}}
\newcommand{\green}[1]{\textcolor{Green}{{#1}}}
\newcommand{\purple}[1]{\textcolor{Purple}{{#1}}}

% use "DIKU green" from our color theme for \emph
%\renewcommand{\emph}[1]{\textcolor{structure}{#1}}
\renewcommand{\emph}[1]{\textcolor{CosGreen}{ #1}}
% use some not-too-bright red for an \emp command
\definecolor{DikuRed}{RGB}{130,50,32}
\newcommand{\emp}[1]{\textcolor{DikuRed}{ #1}}
\definecolor{CosGreen}{RGB}{10,100,70}
\newcommand{\emphh}[1]{\textcolor{CosGreen}{ #1}}
\definecolor{CosBlue}{RGB}{55,111,122}
\newcommand{\emphb}[1]{\textcolor{CosBlue}{ #1}}
\definecolor{CosRed}{RGB}{253,1,1}
\newcommand{\empr}[1]{\textcolor{CosRed}{ #1}}

\newcommand{\mymath}[1]{$ #1 $}
\newcommand{\myindx}[1]{_{#1}}
\newcommand{\myindu}[1]{^{#1}}

\title{OpenCL Day 2: Loop Reasoning, Optimizing Temporal and Spatial Locality}

\author{Cosmin Oancea and Troels Henriksen}

\date{January 30, 2019}

\begin{document}

\frame{\titlepage}

\section{Content of Day2 Lecture:}

\begin{frame}
  \tableofcontents
\end{frame}

\section{Dependence Analysis}

\subsection{Problem Statement, Dependency Definition}
% slide 1
\begin{frame}[fragile,t]
  \frametitle{Problem Statement}

%[fontsize=\small]
\begin{block}{Three Loop Examples}
\begin{colorcode}
DO i = 1, N            DO i = 2, N               DO i = 2, N
 DO j = 1, N            DO j = 2, N               DO j = 1, N
   A[j,i] = A[j,i] ..     A[j,i] = A[j-1,i-1]...   A[i,j] = ...
 ENDDO                    B[j,i] = B[j-1,i]...       A[i-1,j+1]
ENDDO                  ENDDO ENDDO               ENDDO ENDDO
\end{colorcode}
\end{block}

Iterations are ordered {\em lexicographically}, in the order
they occur in the sequential execution:
{\tt$\vec{k}=$(i=2,j=4) < $\vec{l}=$(i=3,j=3)}.

\bigskip

\begin{itemize}
    \item \emp{Which of the three loop nests is amenable to parallelization?}\smallskip
    \item Loop interchange is one of the most simple and useful code transformations,
            e.g., used to enhance locality of reference, and even to ``create''
            parallelism.\smallskip
    \item \emp{In which loop nest is it safe to interchange the loops?}
\end{itemize}

\end{frame}

% slide 2
\begin{frame}[fragile,t]
  \frametitle{Definition of a Dependency} % of CPU, Multicores, GPGPU
\vspace{-2ex}
\begin{block}{Load-Store Classification of Dependencies}
\begin{colorcode}
True Dependency(RAW)  Anti Dependency(WAR)  Output dependency(WAW)
   S1    X  = ..         S1    .. = X          S1    X = ...
   S2    .. = X          S2    X  = ..         S2    X = ...
\end{colorcode}
\end{block}

\smallskip

{\bf Def. Loop Dependence:} There is a dependence from statement $S1$ to $S2$
in a loop nest {\em iff} there exist iterations $\vec{k} \leq \vec{l}$ and
an execution path from statement $S1$ to statement $S2$ \emp{such that:}
\begin{description}
    \item[1.] $S1$ accesses memory location $M$ on iteration $\vec{k}$, and
    \item[2.] $S2$ accesses memory location $M$ on iteration $\vec{l}$, and
    \item[3.] one of these accesses is a write.
\end{description}
\medskip

\emp{We say that $S1$ is the source and $S2$ is the sink of the dependence},
because $S1$ executes before $S2$ in the sequential program execution.
Dependence depicted with an arrow pointing from source to sink.\pause

\medskip
\emp{We are most interested in cross iteration dependencies, i.e., $\vec{k} < \vec{l}$.}

\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Loop-Nest Dependencies} % of CPU, Multicores, GPGPU

{\em Lexicographic ordering},
e.g., {\tt$\vec{k}=$(i=2,j=4) < $\vec{l}=$(i=3,j=3)}.

%[fontsize=\small]
\begin{block}{Three Loop Examples}
\begin{colorcode}
DO i = 1, N            DO i = 2, N               DO i = 2, N
 DO j = 1, N            DO j = 2, N               DO j = 1, N
   A[j,i] = A[j,i] ..     A[j,i] = A[j-1,i-1]...   A[i,j] = ...
 ENDDO                    B[j,i] = B[j-1,i]...       A[i-1,j+1]
ENDDO                  ENDDO ENDDO               ENDDO ENDDO
\end{colorcode}
\end{block}
\pause

\hspace{-5ex}\includegraphics[height=20ex]{img/day2/LoopDeps}

\emp{How can I summarize this information?} %Do I have to consider all cases?
\end{frame}

\subsection{Dependency Matrix, Loop Parallelism and Interchange}
\begin{frame}[fragile,t]
  \frametitle{Aggregate Dependencies via Direction Vectors} % of CPU, Multicores, GPGPU

\begin{block}{Three Loop Examples}
\begin{colorcode}
DO i = 1, N            DO i = 2, N               DO i = 2, N
 DO j = 1, N            DO j = 2, N               DO j = 1, N
   A[j,i] = A[j,i] ..     A[j,i] = A[j-1,i-1]...   A[i,j] = ...
 ENDDO                    B[j,i] = B[j-1,i]...       A[i-1,j+1]
ENDDO                  ENDDO ENDDO               ENDDO ENDDO
\end{colorcode}
\end{block}

\smallskip

Dependencies depicted via an edge {\em from} the stmt that executes first
in the loop nest ({\em source}), {\em to} the one that executes later ({\em sink}).

\smallskip

{\bf Def. Dependence Direction:} Assume there exists a dependence from $S1$
in iteration $\vec{k}$ to $S2$ in $\vec{l}$ ($\vec{k}\leq\vec{l}$).
\emp{\em The direction vector is}:
\begin{description}
    \item[1.] $\vec{D}(\vec{k},\vec{l})_m = $~``{\tt{}<}'' if $\vec{k}_m < \vec{l}_m$,
    \item[2.] $\vec{D}(\vec{k},\vec{l})_m = $~``{\tt{}=}'' if $\vec{k}_m = \vec{l}_m$,
    \item[3.] $\vec{D}(\vec{k},\vec{l})_m = $~``{\tt{}>}'' if $\vec{k}_m > \vec{l}_m$.
    \item[4.] $\vec{D}(\vec{k},\vec{l})_m = $~``{\tt{}*}'' otherwise.
\end{description}

\medskip
If the source is a write and the sink a read then RAW dependency,\\
if the source is a read then WAR, if both are writes then WAW.
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{How to Compute the Direction Vectors?}
\begin{itemize}
    \item For any two statements $S1$ and $S2$ that may access the same
            array $A$ (and one of the accesses is a write),\medskip
    \item in two symbolic iterations $I^1\equiv(i^1_1,\ldots i^1_m)$
            and $I^2=(i^2_1,\ldots i^2_m)$ (assuming $I^1 \leq I^2$)\medskip
    \item on indices $A[e^1_1,\ldots,e^1_n]$ and $A[e^2_1,\ldots,e^2_n]$,
            respectivelly,\medskip
    \item then \textbf{\it the direction vectors may be derived} from the equations\\
            $\begin{cases}
                e^1_1 = e^2_1\\
                \ldots\\
                e^1_n = e^2_n
            \end{cases}$\\\medskip
            (The system of equations models the definition of a dependency:
                both accesses need to refer to the same memory location!)
\end{itemize}
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{When is a Loop Parallel?} % of CPU, Multicores, GPGPU

\begin{block}{Direction Vectors/Matrix for Three Loops }
\begin{columns}
\column{0.27\textwidth}
\begin{colorcode}
  DO i = 1, N
    DO j = 1, N
S1    A[j,i]=A[j,i]..
    ENDDO
  ENDDO\pause
For S1\mymath{\rightarrow}S1:
    (j1,i1)=(j2,i2)
    i1 \emp{=} i2 \& j1 \emp{=} j2

Direction matrix:
S1\mymath{\rightarrow}S1: \emp{[=,=]}
\end{colorcode}
\column{0.32\textwidth}
\begin{colorcode}
  DO i = 2, N
    DO j = 2, N
S1    A[j,i]=A[j-1,i]...
S2    B[j,i]=B[j-1,i-1]...
    ENDDO
  ENDDO\pause
S1\mymath{\rightarrow}S1: (j1,i1)=(j2-1,i2)
        i1 \emp{=} i2 \& j1 \emp{<} j2
S2\mymath{\rightarrow}S2: (j1,i1)=(j2-1,i2-1)
        i1 \emp{<} i2 \& j1 \emp{<} j2
S1\mymath{\rightarrow}S1: \emp{[=,<]}
S2\mymath{\rightarrow}S2: \emp{[<,<]}
\end{colorcode}
\column{0.32\textwidth}
\begin{colorcode}
  DO i = 2, N
    DO j = 1, N
S1    A[i,j]=A[i-1,j+1]...
    ENDDO
  ENDDO
For S1\mymath{\rightarrow}S1:\pause
    (i1,j1) = (i2-1,j2+1)
    i1 \emp{<} i2 \& j1 \emp{>} j2

Direction matrix:
S1\mymath{\rightarrow}S1: \emp{[<,>]}
\end{colorcode}
\end{columns}
\end{block}

{\bf Th. Parallelism:} A loop in a loop nest is parallel {\em iff} for each entry in its direction column, the entry is either {\tt =} or there exists an outer loop whose corresponding direction is {\tt <}.

\smallskip

\red{A direction vector cannot have $>$ as the first non-= symbol},\\
as that would mean that I depend on something in the future.
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Parallelism and Loop Interchange} % of CPU, Multicores, GPGPU

\begin{block}{Direction Vectors/Matrix for Three Loops }
\begin{columns}
\column{0.27\textwidth}
\begin{colorcode}
  DO i = 1, N
    DO j = 1, N
S1    A[j,i]=A[j,i]..
    ENDDO
  ENDDO
For S1\mymath{\rightarrow}S1:
    (j1,i1)=(j2,i2)
    i1 \emp{=} i2 \& j1 \emp{=} j2

Direction matrix:
S1\mymath{\rightarrow}S1: \emp{[=,=]}
\end{colorcode}
\column{0.32\textwidth}
\begin{colorcode}
  DO i = 2, N
    DO j = 2, N
S1    A[j,i]=A[j-1,i]...
S2    B[j,i]=B[j-1,i-1]...
    ENDDO
  ENDDO
S1\mymath{\rightarrow}S1: (j1,i1)=(j2-1,i2)
        i1 \emp{=} i2 \& j1 \emp{<} j2
S2\mymath{\rightarrow}S2: (j1,i1)=(j2-1,i2-1)
        i1 \emp{<} i2 \& j1 \emp{<} j2
S1\mymath{\rightarrow}S1: \emp{[=,<]}
S2\mymath{\rightarrow}S2: \emp{[<,<]}
\end{colorcode}
\column{0.32\textwidth}
\begin{colorcode}
   DO i = 2, N
    DO j = 1, N
 S1   A[i,j] = ...
        A[i-1,j+1]
   ENDDO ENDDO
  For S1\mymath{\rightarrow}S1:
    (i1,j1) = (i1-1,j2+1)
    i1 \emp{<} i2 \& j1 \emp{>} j2

  Direction matrix:
  S1\mymath{\rightarrow}S1: \emp{[<,>]}
\end{colorcode}
\end{columns}
\end{block}

{\bf Th. Loop Interchange:} Interchanging two loops in a loop nest
is legal {\em iff} interchanging the columns of the direction matrix
in the same way {\em does NOT result} in a {\tt >} direction as the
leftmost non-{\tt{}=} direction in a row.
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Parallelism and Loop Interchange}

\begin{block}{Direction Vectors/Matrix for Three Loops }
\begin{colorcode}
  DO i = 1, N           DO i = 2, N             DO i = 2, N
    DO j = 1, N          DO j = 2, N             DO j = 1, N
S1    A[j,i]=A[j,i]..  S1  A[j,i]=A[j-1,i]...   S1 A[i,j]=A[i-1,j+1]...
    ENDDO              S2  B[j,i]=B[j-1,i-1]...  ENDDO
  ENDDO                 ENDDO ENDDO             ENDDO

For S1\mymath{\rightarrow}S1: j1 = j2    For S1\mymath{\rightarrow}S1: j1 = j2-1   For S1\mymath{\rightarrow}S1: i1 = i2-1
            i1 = i2                i1 = i2                j1 = j2+1
(i2,j2)-(i1,j1)=         (i2,j2)-(i1,j1)=\emp{[=,<]}  (i2,j2)-(i1,j1)=
\emp{[=,=]}                  For S2\mymath{\rightarrow}S2: j1 = j2-1        \emp{[<,>]}
                                   i1 = i2-1
                         (i2,j2)-(i1,j1)=\emp{[<,<]}
\end{colorcode}
\end{block}

Interchange is safe for the 1st and 2nd nests, but not for the 3rd!\\
e.g., \emp{\tt [=,<]}$~~~\rightarrow~~~$ \emph{\tt [<,=]}$~~~~~~~~~$(for the second loop nest)\\
$~~~~~~~$\emp{\tt [<,<]}$~~~~~~~~~~~~~$\emph{\tt [<,<]}

\pause\smallskip

After interchange, loop $j$ of the second loop nest is parallel.

\bigskip

\emph{\bf Corollary: A parallel loop can be always interchanged inwards.}
\end{frame}

\subsection{Loop Distribution, Array Expansion/Privatization}
\begin{frame}[fragile,t]
  \frametitle{Dependency Graph and Loop Distribution}

{\bf Def. Dependency Graph:} edges from the source of the dependency, i.e., early iteration,
to the sink, i.e., later iteration.

\smallskip

{\bf Th. Loop Distribution:} Statements that are in a dependence cycle remain in one
(sequential) loop.   The others are distributed to separate loops in graph order;
if no cycle then parallel loops.\smallskip

\begin{block}{Vectorization Example}
\begin{columns}
\column{0.34\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
  DO i = 3, N
\emp{S1  A[i] = B[i-2] ...}
\red{S2  B[i] = B[i-1] ...}
  ENDDO

For S2\mymath{\rightarrow}S1: i1 = i2-2, \emp{[<]}
For S2\mymath{\rightarrow}S2: i1 = i2-1, \emp{[<]}
\end{colorcode}
\column{0.27\textwidth}\pause
\includegraphics[height=12ex]{img/day2/LoopDistr}
\column{0.30\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
  \red{DO} i = 3, N
S2  B[i] = B[i-1] ...
  \red{ENDDO}

  \emphh{DOALL} i = 3, N
\emp{S1  A[i] = B[i-2] ...}
  \emphh{ENDDOALL}
\end{colorcode}
\end{columns}
\end{block}

\medskip

{\bf Corollary:} It is always legal to distribute a parallel loop;\\
\red{but requires array expansion for local variables or if output
dependencies are present.}
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Loop Distribution May Require Array Expansion}

\begin{columns}
\column{0.4\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
  float tmp;
  for(i=2; i<N; i++) \{
    \emp{tmp} = 2*B[i-2];
    A[i] = tmp;
    B[i] = tmp+B[i-1]
  \}
\end{colorcode}
\column{0.53\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
  \emph{float tmp[N]};
  for(int i=2; i<N; i++) \{
    tmp[i] = 2*B[i-2];
    B[i] = tmp[i]+B[i-1];
  \}

  \emph{forall(int i=2; i<N; i++)} \{
    A[i] = tmp[i];
  \}
\end{colorcode}
\end{columns}
\bigskip

No matter where {\tt tmp} is declared (inside or outside
the loop) it needs to be expanded into an array in order
to do loop distribution.\bigskip

If {\tt tmp} is declared outside the loop then requires \emp{\bf privatization}, \pause
because it actually causes frequent {\sc waw} dependencies.
However its value is written before being used within the same iteration.
Hence it is semantically equivalent to a locally declared variable,
which will remove the output ({\sc waw}) dependency.\bigskip

Distribution requires array expansion of the scalar {\tt tmp}.

\end{frame}

\begin{frame}[fragile,t]
  \frametitle{False Dependencies (WAR/WAW)}

\begin{itemize}
    \item \emp{Cross-Iteration Anti Dependencies (WAR)}
        correspond to a read from the array as it was
        before the loop $\Rightarrow$ can be eliminated
        by reading from a copy of the array.\bigskip

    \item \emp{Cross-Iteration WAW Dependencies (WAW)}:\\
        If they correspond to the case in which every \emp{\bf read} from
        a scalar or array location is covered by a \emp{\bf previous same-iteration write}
        %a re-writing the same array elements
        %in different iterations
        $\Rightarrow$ can be eliminated \emph{\bf privatization (renaming)},
        which semantically moves the declaration of the variable (scalar or array)
        inside the loop.\bigskip

    \item Direction-vectors reasoning is limited to relatively
        simple loop nests, e.g., difficult to reason about
        privatization in such a way.
\end  {itemize}
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Example: Eliminating WAW Dependencies}

\begin{columns}
\column{0.40\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
  \emp{float A[M];}
  \emp{for}(i=0; i<N; i++)\{
    for(int j=0, j<M; j++)
        \emp{A[j]} = (4*i+4*j) \% M;
    for(int k=0; k<N; k++)
        X[i][k] = X[i][k-1] *
           \emp{A[A[(2*i+k)\%M]\%M]};
  \}\pause
\end{colorcode}

\column{0.58\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
  // \emp{The write to A[j] causes many WAWs},\pause
  // \emph{but A is fully written in loop j}
  // \emph{float A[N][M];}
  \emph{forall}(int i=0; i<N; i++)\{
    \emph{float A[M];}
    for(int j=0, j<M; j++)
        \emph{A[j]} = (4*i+4*j) \% M; // \emph{A[i][j]}
    for(int k=0; k<N; k++)
        X[i][k]=X[i][k-1] *
           \emph{A[A[(2*i+k)\%M]\%M]};
           //\emph{A[i][A[i,...]]}
  \}
\end{colorcode}
\end{columns}

For OpenCL programming you would likely use array expansion (\emph{\tt float A[N,M]})
because the size {\tt M} is likely unknown at compile time.\smallskip

\end{frame}

\subsection{Exercise: Putting it all together}

\begin{frame}[fragile,t]
  \frametitle{Putting Everything Together}

\begin{colorcode}[fontsize=\scriptsize]
float X[M][N][N]; /* ... compute X ... */
float A[N][N];
for(int i = 0; i < M; i++) \{
  \emp{//initialize A}
  for(int k = 0; k < N; k++) \{
    for(int j = 0; j < N; j++) \{
        float x = X[i][j][k];
        A[k][j] = x * x;
  \} \}
  \emp{// convergence loop}
  for(int t = 0; t < T; t++) \{
    for(int j = 0; j < N; j++) \{
      for(int k = 0; k < N; k++) \{
        A[k][j] = fsqrt(A[k][j] * A[k-1][j]);
        X[i][k][j] += fsqrt(B[i][k-1][j-1]);
      \}
    \}
\} \}
\end{colorcode}

{\bf Exercise:}
Assume the code above runs on a dataset having\\
{\tt M = 128} and {\tt N = 512} and, less important, say {\tt T = 256}.\\\smallskip
\begin{itemize}
    \item[(1)] Is this code suitable for GPU execution?\\\smallskip
    \item[(2)] Transform the code to one suitable for GPU execution.
\end{itemize}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Spatial Locality
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Optimizing Spatial Locality (Coalesced Memory)}

\begin{frame}[fragile]
	\tableofcontents[currentsection]
\end{frame}

\subsection{Motivating Example on A Contrived Program}

\begin{frame}[fragile,t]
  \frametitle{Motivation: Coalesced Accesses to Global Memory}

Which are the parallel/sequential loops?

\begin{columns}
\column{0.42\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
real A[height][width];
real B[height][width];
// Non-Coalesced Access
\emph{for(i=0; i<height; i++) \{}
  real accum  = 0.0;

  \emp{for(j=0; j<width; j++) \{}
    real tmpA = A[i][j];
    accum = tmpA*tmpA - accum;
    B[i][j] = accum;
  \}
\}\pause
\end{colorcode}
\column{0.55\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
A' = transpose(A);
// Coalesced Accesses
\emph{for(i=0; i<height; i++) \{}
  real accum = 0.0;

  \emp{for(int j=0; j<width; j++) \{}
    real tmpA = A'[j][i];
    accum = tmpA*tmpA - accum;
    B'[j][i] = accum;
  \}
\}
B = transpose(B');
\end{colorcode}
\end{columns}
\bigskip

\begin{itemize}
    \item \emph{Loop {\tt i} is parallel;}\smallskip
    \item \emp{loop {\tt j} is sequential} because
            of the RAW cross-iteration dependencies on {\tt accum}.\bigskip
    \item The transformed program performs about $3\times$ the number
            of global-memory accesses than the original. \pause\smallskip
    \item But it is significantly faster than the original (on GPUs).
\end{itemize}

\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Motivation: What Are Coalesced Accesses?}

\textbf{Coalesced access}: a (quarter) wave accesses in a
SIMD instruction consecutive words in global-memory.\bigskip

\includegraphics[height=20ex]{img/day2/CoalescedGPU}

\end{frame}

\subsection{Uncoalesced Matrix Transposition Kernel}

\begin{frame}[fragile,t]
  \frametitle{Uncoalesced Transposition Kernel}

\begin{itemize}
    \item Semantically we chunk the matrix into {\tt T$\times$T} blocks;
    \item Each block is processed by one workgroup, e.g., {\tt T=16}.
\end{itemize}

\begin{columns}
\column{0.40\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
// Initial
for(i=0; i<height; i++) \{
  for(j=0; j<width; j++) \{
    B[j*height+i] =
           A[i*width+j];
  \}
\}
\end{colorcode}
\column{0.58\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
// Blocked
\emp{for(ii=0; ii<height; ii+=T) \{}
  \emp{for(jj=0; jj<width; jj+=T) \{}
    \emphh{for(i=ii; i<min(ii+T,height); i++) \{}
      \emphh{for(j=jj; j<min(jj+T,width); j++) \{}
        \red{B[j*height+i]} = A[i*width+j];
\} \} \} \}
\end{colorcode}
\end{columns}

\begin{itemize}
\item Local workgroup: $T\times T$;
\item Global(1): $\lceil \texttt{width}/T \rceil * T$; Global 1: $\lceil \texttt{height}/T \rceil * T$.
\end{itemize}
\pause

\begin{colorcode}[fontsize=\scriptsize]
__kernel void naiveTransp( __global real* A, __global real* B
                         , int height, int width ) \{
    unsigned int i = get_global_id(1);
    unsigned int j = get_global_id(0);
    if( (j >= width) || (i >= height) ) return;
    \red{B[j*height + i]} = A[i*width + j];  \red{// uncoalesced access!}
\}

\end{colorcode}

\end{frame}

\subsection{Coding Exercise: Entirely-Coalesced Transposition}

\begin{frame}[fragile,t]
  \frametitle{Exercise 1: Fill In Coalesced Transposition Kernel}
\vspace{-1ex}
\begin{colorcode}[fontsize=\scriptsize]
// verifies that workgroup size is: TILE*TILE
__kernel __attribute__((reqd_work_group_size(TILE, TILE, 1)))
__kernel void coalsTransp( __global real* A,
        __global real* B, uint32_t height, uint32_t width ) \{
  __local float tile[TILE][TILE];                    \red{// local memory}
  int gidx = get_global_id(0); int gidy = get_global_id(1);
  int lidx = get_local_id(0);  int lidy = get_local_id(1);

  // 1. read current element from global to local memory.
  //    make sure to NOT access arrays out of bounds!
  // 2. current thread loads from local memory the transposed element
  //    at workgroup level, and writes it in the proper position in B.
  //    Useful functions: get_group_id(0), get_local_size(0); or 1.
  // 3. Insert necessary workgroup-level synchronization
  //    barrier(CLK_LOCAL_MEM_FENCE);
\}
\end{colorcode}

Transposing a (blocked) matrix $\equiv$ moving each block to
the transposed position $+$ transposing each block!\medskip

\pause
\begin{itemize}
    \item \emp{Implement in {\tt Day2-Exercises/Transp/kernels.cl}}
    \item Are the read and write accesses to global memory coalesced?
    \item Why/Where should you place the barrier?
\end{itemize}

\end{frame}

%\begin{frame}[fragile,t]
%  \frametitle{Coalesced Transposition Kernel}
%
%\begin{colorcode}[fontsize=\scriptsize]
%__kernel void coalsTransp coalsTransp( __global real* A,
%        __global real* B, uint32_t height, uint32_t width ) \{
%  __local float tile[T][T];                          \red{// local memory}
%  int gidx = get_global_id(0); int gidy = get_global_id(1);
%  int lidx = get_local_id(0);  int lidy = get_local_id(1);
%
%  if( gidx < width && gidy < height )          \red{// cannot return here!}
%    lmem[lidy*T + lidx] = A[gidy*width + gidx]; \red{// copy to local mem!}
%  \red{barrier(CLK_LOCAL_MEM_FENCE); // all threads have to reach barrier!}
%
%  gidx = get_group_id(1)*T + lidx; gidy = get_group_id(0)*T + lidy;
%  if( gidx < height && gidy < width )
%      B[gidy*height + gidx] = lmem[lidx*T + lidy];
%\}
%\end{colorcode}
%
%\end{frame}

\subsection{Program Optimized By Transposition + Coding Exercise}

\begin{frame}[fragile,t]
  \frametitle{Exercise 2: Coalescing By Transposition}

\begin{columns}
\column{0.42\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
real A[height][width];
real B[height][width];
// Non-Coalesced Access
\emph{for(i=0; i<height; i++) \{}
  real accum  = 0.0;

  \emp{for(j=0; j<width; j++) \{}
    real tmpA = A[i][j];
    accum = tmpA*tmpA - accum;
    B[i][j] = accum;
\} \}\pause
\end{colorcode}
\column{0.55\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
A' = transpose(A);
// Coalesced Accesses
\emph{for(i=0; i<height; i++) \{}
  real accum = 0.0;

  \emp{for(int j=0; j<width; j++) \{}
    real tmpA = A'[j][i];
    accum = tmpA*tmpA - accum;
    B'[j][i] = accum;
\} \}
B = transpose(B');
\end{colorcode}
\end{columns}
\bigskip

\begin{itemize}
    \item Open File "Day2-Exercises/Transp/coalescing.c", function named "runGPUcoalsProgram".
    \item Fill the correct sizes for the "coalsProgrm" kernel call.
    \item Open File "Day2-Exercises/Transp/kernels.cl" and implement kernel named "coalsProgrm".
    \item Fix bugs until it validates! :-)
    \item What is the speedup with respect to the naive program?
    \item What fraction of the memcpy bandwidth is achieved?
\end{itemize}

\end{frame}

\subsection{Fully Fused Program + Coding Exercise}

\begin{frame}[fragile,t]
  \frametitle{Exercise 3: Fused Program With Coalesced Accesses}

\begin{itemize}
    \item Open File "Day2-Exercises/Transp/kernels.cl" and implement the missing pieces from kernel named "optimProgrm";\bigskip
    \item The idea is to have coalesced accesses "fused" into the kernel by each
            thread processing {\tt CHUNK} elements at a time by:\smallskip
    \begin{itemize}
        \item collectively copying {\tt CHUNK * WORKGROUP\_SIZE} input elements from global to local memory
                in coalesced fashion;\smallskip
        \item each thread now processes {\tt CHUNK} elements from its row;\smallskip
        \item collectively copying {\tt CHUNK * WORKGROUP\_SIZE} result elements from local to global memory
                in coalesced fashion;\smallskip
        \item repeat in a loop (inside the kernel) until all row's elements have been processed!
    \end{itemize}\bigskip
    \item Fix bugs until it validates! :-)\bigskip
    \item What is the speedup with respect to the transposition-optimized program?\bigskip
    \item What fraction of the memcpy bandwidth is achieved?
\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Temporal Locality
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Optimizing Temporal Locality}

\begin{frame}[fragile]
	\tableofcontents[currentsection]
\end{frame}

\subsection{Naive Matrix-Matrix Multiplication}

\begin{frame}[fragile,t]
  \frametitle{Matrix Multiplication: Loop Strip Mining}

\begin{columns}
\column{0.64\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
for (int i = 0; i < M; i++) \{  \emphh{// Parallel}
  for (int j = 0; j < N; j++) \{  \emphh{// Parallel}
    float tmp = 0.0
    for(int k = 0; k < U; k++) \{ \emp{// Reduction}
      tmp += A[i,k]*B[k,j]
    \}
    C[i,j] = tmp;
  \}
\}
\end{colorcode}
\column{0.33\textwidth}
Matrices:
\begin{itemize}
    \item {\tt A $\in~\mathcal{M}^{M \times U}$}
    \item {\tt B $\in~\mathcal{M}^{U \times N}$}
    \item {\tt C$\in~\mathcal{M}^{M \times N}$}
\end{itemize}
Loops of indices {\tt i} and {\tt j} are parallel.
\end{columns}

\bigskip

Accesses to {\tt A} and {\tt B} invariant to loops {\tt i} and {\tt j} $\Rightarrow$\\
\emph{The idea is to apply Block Tiling to optimize temporal locality!}
\end{frame}

\subsection{Block-Tiled Matrix Multiplication + Coding Exercise}

\begin{frame}[fragile,t]
  \frametitle{Loop Strip Mining}

\begin{columns}
\column{0.43\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
for(int i = 0; i < N; i++) \{
    loop_body(i)             \emp{\mymath{\Rightarrow}}
\}

\end{colorcode}
\column{0.56\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
for(int ii = 0; ii < N; ii += T) \{        \alert{// for some tile T}
  for(int i = ii; i<MIN(ii+T,N); i++) \{
    loop_body(i)
\}  \}
\end{colorcode}
\end{columns}

\emph{Strip Mining is always safe:} the transformed loop executes
the same instructions in the same order as the original loop!
\bigskip

Strip mining all loops by some tile {\tt T} result in:\pause\smallskip

\begin{colorcode}[fontsize=\scriptsize]
for (int ii = 0; ii < M; ii += T) \{            \emphh{// Parallel}
  for (int i = ii; i < MIN(ii+T,M); i++) \{     \emphh{// Parallel}
    for (int jj = 0; jj < N; jj += T) \{        \emphh{// Parallel}
      for (int j = jj; j < MIN(jj+T,N); j++) \{ \emphh{// Parallel}
          float tmp = 0.0;
          for(int kk = 0; kk < U; k += T) \{    \emp{// Reduction}
            for(int k=kk; k<MIN(kk+T,U); k++) \{\emp{// Reduction}
                tmp += A[i,k]*B[k,j];
            \}
          \}
          C[i,j] = tmp;
      \}
    \}
  \}
\}
\end{colorcode}
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Block Tiling: Strip Mining + Interchange}

\emph{Now interchange the loops if index {\tt i} and {\tt jj}.} Why is this safe?\smallskip

\begin{colorcode}[fontsize=\scriptsize]
for (int ii = 0; ii < M; ii += T) \{             \emp{// Global 1}
  for (int jj = 0; jj < N; jj += T) \{           \emp{// Global 0}
    for (int i = ii; i < MIN(ii+T,M); i++) \{    \emphh{// Local 1}
      for (int j = jj; j < MIN(jj+T,N); j++) \{  \emphh{// Local 0}
          float tmp = 0.0;
          for(int kk = 0; kk < U; k += T) \{     \emp{// Sequential}

            \red{//What slices of A and B are used in the loop below?}

            for(int k=kk; k<MIN(kk+T,U); k++) \{ \emp{// Sequential}
                tmp += A[i,k]*B[k,j];
            \}
          \}
          C[i,j] = tmp;
\} \} \} \}
\end{colorcode}

\pause

\emph{\tt A[ii : MIN(ii+T,M)][kk : MIN(kk+T,U)]}\\
\emph{\tt B[kk : MIN(kk+T,U)][jj : MIN(jj+T,N)]}\\\smallskip

Each array slice has {\tt T$\times$T} elements and there are {\tt T$\times$T} threads in a workgroup;
so each thread can copy one element to local memory.\\
Thus, we cut the number of global-memory accesses by a {\tt T} factor.

\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Exercise 4: Implement Block-Tiled Matrix Multiplication}

\begin{itemize}
    \item Open File "Day2-Exercises/MMMult/mmm.c", function named "runGPUverMMM".
    \item Place there the correct sizes for the matrix multiplication kernels. That would make the naive version work correctly.
    \item Open File "Day2-Exercises/MMMult/mmm.cl" and implement kernel named "blockMMM".
    \item Fix bugs until it validates! :-)
\end{itemize}

\pause
The parallel iteration space is block tiled, not the matrices:

\hspace{-5ex}\includegraphics[height=20ex]{img/day2/LoopDeps}

\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Exercise 4: Implement Block-Tiled Matrix Multiplication}
\begin{tiny}
\emph{Copy {\tt A[ii:MIN(ii+T,M)][kk:MIN(kk+T,U)]} in {\tt A\_loc} of size {\tt[T][T]}}\\
\emph{Copy {\tt B[kk:MIN(kk+T,U)][jj:MIN(jj+T,N)]} in {\tt B\_loc} of size {\tt[T][T]}}\\
\end{tiny}
\begin{colorcode}[fontsize=\scriptsize]
for (int ii = 0; ii < M; ii += T) \{             \emp{// Global 1}
  for (int jj = 0; jj < N; jj += T) \{           \emp{// Global 0}
    for (int i = ii; i < MIN(ii+T,M); i++) \{    \emphh{// Local 1}
      for (int j = jj; j < MIN(jj+T,N); j++) \{  \emphh{// Local 0}
          float tmp = 0.0;
          for(int kk = 0; kk < U; k += T) \{     \emp{// Sequential}

            \emph{// Collectively copy to local memory.}
            \emph{// Loop of index {\tt k} should only use local memory.}
            \red{// Add barrier here. Why?}

            for(int k=kk; k<MIN(kk+T,U); k++) \{ \emp{// Sequential}
                tmp += A_loc[i-ii,k-kk] * B_loc[k-kk,j-jj];
          \} \} \red{// Add barrier here. Why?}
          C[i,j] = tmp;
\} \} \} \}
\end{colorcode}

\begin{tiny}
Modify loops of index {\tt i}, {\tt j} and {\tt k} to go from {\tt 0} to {\tt T} with stride {\tt 1}. Unroll loop {\tt k}.\\\smallskip
OpenCL equivalences:\\
Global workgroup size: \pause $(\lceil M/T \rceil * T)~\times~(\lceil N/T \rceil * T)$. Local Workgroup size: $T \times T$.\\
Local  ids:\pause {\tt~~i - ii = get\_local\_id(1), j - jj = get\_local\_id(0) }\\
Global ids: {\tt~~i = get\_global\_id(1), j = get\_global\_id(0) }\\
Similar: {\tt ii = get\_group\_id(1)*T, jj = get\_group\_id(0)*T}\\
Local memory declared with: {\tt~~\_\_local float A\_loc[T][T];}
\end{tiny}
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Exercise 4: Implement Block-Tiled Matrix Multiplication}
\begin{tiny}
\emph{Loops of index {\tt i} and {\tt j} form the local workgroup.}
Interchange them innermost and distribute them:\\
\end{tiny}
\begin{colorcode}[fontsize=\tiny]
for (int ii = 0; ii < M; ii += T) \{             \emp{// Global 1}
  for (int jj = 0; jj < N; jj += T) \{           \emp{// Global 0}
    \emph{float A_loc[T][T], B_loc[T][T];} // local memory
    float tmp_reg[T][T];  // register memory: still represented as a scalar for a thread

    for (int i = ii; i < ii+T; i++) \{    \emphh{// Local 1}
      for (int j = jj; j < jj+T; j++) \{  \emphh{// Local 0}
          tmp[i-ii][j-jj] = 0.0;
    \} \}
    \emp{for(int kk = 0; kk < U; k += T) \{}      \emp{// Sequential}
      for (int i = ii; i < ii+T; i++) \{    \emphh{// Local}
        for (int j = jj; j < jj+T; j++) \{  \emphh{// Local}
          \emph{A_loc[i-ii][k-kk]} = (i < M && k < U)? \emp{A[i][kk+j-jj]} : 0.0; // copy to local mem
          \emph{B_loc[k-kk][j-jj]} = (j < N && k < U)? \emp{B[kk+i-ii][j]} : 0.0; // copy to local mem
      \} \} \red{// needs barrier here, why?}
      for (int i = ii; i < ii+T; i++) \{    \emphh{// Local 1}
        for (int j = jj; j < jj+T; j++) \{  \emphh{// Local 0}
            \emp{for(int k=kk; k < kk+T; k++) \{} \emp{// Sequential}
                tmp[i][j] += A_loc[i-ii,k-kk] * B[k-kk,j-jj]; // use local memory
            \}
      \} \}
      for (int i = ii; i < ii+T; i++) \{    \emphh{// Local 1}
        for (int j = jj; j < jj+T; j++) \{  \emphh{// Local 0}
          C[i,j] = tmp;
      \} \} \red{// needs barrier here, why?}
\} \} \}
\end{colorcode}
\begin{tiny}
Modify loops of index {\tt i}, {\tt j} and {\tt k} to go from {\tt 0} to {\tt T} with stride {\tt 1}. Unroll loop {\tt k}.\\\smallskip
OpenCL equivalences:\\
Global workgroup size: \pause $(\lceil M/T \rceil * T)~\times~(\lceil N/T \rceil * T)$. Local Workgroup size: $T \times T$.\\
Local  ids:\pause {\tt~~i - ii = get\_local\_id(1), j - jj = get\_local\_id(0) }\\
Global ids: {\tt~~i = get\_global\_id(1), j = get\_global\_id(0) }\\
Local memory declared with: {\tt~~\_\_local float A\_loc[T][T];}
\end{tiny}
\end{frame}

\subsection{Register-Tiled Matrix Multiplication + Coding Exercise}

\begin{frame}[fragile,t]
  \frametitle{Exercise 5: Register+Block Tiled Matrix Multiplication}

\begin{columns}
\column{0.64\textwidth}
\begin{colorcode}[fontsize=\scriptsize]
for (int i = 0; i < M; i++) \{  \emphh{// Parallel}
  for (int j = 0; j < N; j++) \{  \emphh{// Parallel}
    float c = 0.0
    for(int k = 0; k < U; k++) \{ \emp{// Reduction}
      c += A[i,k]*B[k,j]
    \}
    C[i,j] = c;
  \}
\}
\end{colorcode}
\column{0.33\textwidth}
Matrices:
\begin{itemize}
    \item {\tt A $\in~\mathcal{M}^{M \times U}$}
    \item {\tt B $\in~\mathcal{M}^{U \times N}$}
    \item {\tt C$\in~\mathcal{M}^{M \times N}$}
\end{itemize}
Loops of indices {\tt i} and {\tt j} are parallel.
\end{columns}

\bigskip

We can do even better by sacrificing some parallelism in excess:
\begin{itemize}
    \item stripmine parallel dimension {\tt i} by a tile {\tt T} and stride {\tt 1};\\
          then move it innermost and sequentialize it;
    \item stripmine parallel dimension {\tt j} by a tile {\tt T$\times$T} and stride {\tt T} and
    \item stripmine the result by a tile {\tt T} and stride {\tt 1};
    \item the two tiles of parallel dimension {\tt j} form the local workgroup.
\end{itemize}

Do not forget to perform array expansion on {\tt c}, which we will rename {\tt cs}.
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Exercise 5: Register+Block Tiled Matrix Multiplication}

\begin{colorcode}[fontsize=\tiny]
unsigned int ii, i, jjj, jj, j, kk, k;
\emp{forall (ii = 0; ii < M; ii += T ) \{           // Parallel Global 1}
  \emp{forall (jjj = 0; jjj < N; jjj += T*T ) \{    // Parallel Global 0}
    float cs[T][T][T];

    \emph{forall(jj=jjj; jj<min(jjj+T*T,N); jj+=T)\{ // Parallel Local 1}
      \emph{forall(j=jj; j<min(jj+T,N); j++) \{      // Parallel Local 0}
        for (i=ii; i<min(ii+T,M); i++) \{      // sequential
          cs[(jj-jjj)/T][j-jj][i-ii] = 0.0;
    \} \} \}

    for (kk = 0; kk < U; kk += T) \{                 // sequential
      \red{// here we will insert a collective copy to local}
      \red{// memory of the slice: A[ii : ii+T, kk : kk+T]}
      for (k = kk; k < min(kk+T,U); k++) \{          // sequential
        \emph{forall (jj=jjj; jj<min(jjj+T*T,N); jj+=T) \{ // Parallel Local 1}
          \emph{forall (j=jj; j<min(jj+T,N); j++) \{       // Parallel Local 0}
            for (i = ii; i < min(ii+T,M); i++) \{    // sequential
              \red{// please modify here to read A from local memory}
              \red{// hoist out B[k][j] outside loop of index i (invariant)}
              cs[(jj-jjj)/T][j-jj][i-ii] += A[i][k] * B[k][j];
    \} \} \} \} \}

    \emp{forall (jj=jjj; jj<min(jjj+T*T,N); jj+=T) \{     // Parallel Local 1}
      \emp{forall (j=jj; j<min(jj+T,N); j++) \{           // Parallel Local 0}
        for (i = ii; i < min(ii+T, M); i++) \{       // sequential
          C[i][j] = cs[(jj-jjj)/T][j-jj][i-ii];
    \} \} \}
\} \}
\end{colorcode}
\end{frame}

\begin{frame}[fragile,t]
  \frametitle{Exercise 5: Register+Block Tiled Matrix Multiplication}

\begin{itemize}
    \item Open File "Day2-Exercises/MMMult/mmm.c", function named "runGPUverMMM".
    \item Fill the correct sizes for the "rgblkMMM" kernel.
    \item Open File "Day2-Exercises/MMMult/mmm.cl" and implement kernel named "rgblkMMM".
    \item Fix bugs until it validates! :-)  \red{Tile size is called RT} and not T!
\end{itemize}

OpenCL equivalences: \pause
\begin{itemize}
    \item Global worksize 1: $\lceil M/RT \rceil$; Global worksize 0: $\lceil N/RT^2 \rceil * RT^2$;
    \item Local worksize 0: RT$^2$; Local worksize 1: 1;
    \item {\tt ii = get\_group\_id(1) * RT;}\\
          {\tt j = jj + get\_global\_id(0);}\\
          {\tt locx = get\_local\_id(0) \% RT;}\\
          {\tt locy = get\_local\_id(0) / RT;}\\
    \item Loops of index {\tt i} should be normalized {\tt[0..RT-1]} and unrolled ({\tt \#pragma unroll});
    \item Array {\tt cs} should be one dimensional: {\tt float cs[RT];}
    \item {\tt \_\_local real A\_loc[RT][RT+1];} for bank conflicts.
\end{itemize}

\end{frame}

%\section{Recognizing Reduction, Segmented Scan}
%\begin{frame}[fragile,t]
%  \frametitle{Reduction is Typically Easy To Recognize}
%
%If all the statements in which a scalar variable {\tt x} appears
%are of the form {\tt x $\oplus$ = $exp$}, where {\tt x} does
%not appear in $\exp$ and $\oplus$ is associative
%then the cross-iteration RAWs on {\tt x} can be resolved by:
%\begin{itemize}
%    \item privatizing {\tt x} initialized with the neutral element,
%    \item computing the per-processor partial values of {\tt x},
%    \item reducing the {\tt x}s across processors and with the initial value.
%\end  {itemize}
%
%\begin{columns}
%\column{0.4\textwidth}
%\begin{colorcode}[fontsize=\scriptsize]
%// compilation requires g++ -fopenmp ...
%  float x = 6.0;
%\#pragma omp parallel for reduction(+:x) private(i,j)
%  for(i=1; i<N; i++) \{
%    for(j=1; j<N; j++) \{
%      if ( A[i,j] >= 2.0 )    \emph{x += 2*A[i,j-1]};
%      else if( A[i,j] > 0.0 ) \emph{x += A[i-1,j+1];}
%    \}
%    if (i \% (j+1) == 3) \emph{x += A[i,i];}
%  \}
%\end{colorcode}
%\column{0.53\textwidth}
%
%\end{columns}
%\end{frame}
%
%
%\begin{frame}[fragile,t]
%  \frametitle{Scan and Segmented Scan Are Difficult!}
%
%Compilers cannot recognize and parallelize even simple scans:
%\begin{itemize}
%    \item they raise a cross-iteration true dependency (RAW),
%    \item they appear in a multitude of forms,
%    \item hence they are difficult to analyze.
%\end  {itemize}
%
%\begin{columns}
%\column{0.4\textwidth}
%\begin{colorcode}[fontsize=\scriptsize]
%// What kind of scans are these?
%1. A[0] = B[0];
%   for(i=1; i<N; i++) \{
%     A[i] = A[i-1] + B[i];
%   \}
%2. acc = 0;
%   for(i=0; i<N; i++)\{
%     acc = acc ^ i;
%     A[i] = acc;
%   \}
%3. for(j=0; j<M; j++)
%     A[0,j] = B[0,j];
%   for(i=1; i<N; i++) \{
%     for(j=0; j<M; j++)
%       A[i,j] = A[i-1,j] + B[i,j];
%   \}
%\end{colorcode}
%\column{0.53\textwidth}\pause
%\begin{colorcode}[fontsize=\scriptsize]
%1. let A = scan (+) 0 B
%
%2. let A = scan (^) 0 (iota N)
%
%3. let A = scan (\mymath{\backslash} a b -> map2 (+) a b)
%                (replicate M 0.0) B
%        \mymath{\equiv}
%   let A = transpose <|
%           map (scanInc (+) 0.0) <|
%           transpose B
%
%\end{colorcode}
%\end{columns}
%\bigskip
%
%\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\documentclass{article}

\usepackage{graphicx, amsmath, amssymb, amsthm}

\title{Grandiosity and Paranoia in the Reinforcement Learning Context: Lessons
  for AI Safety}

\author{Eric Purdy}

\begin{document}

\maketitle

\section{Introduction}

(We apologize for the terseness of this note. It is directed at serious domain
experts and contains little redundant information.)

In brief, we formulate a theory of a satisficing reinforcement learning
agent. We posit that such an agent is desirable from an AI safety perspective,
and offer theoretical arguments for this, with some experimental validation on
a toy model. Finally, we offer this theory as a potential explanation of the
role of the neurotransmitter serotonin in the basal ganglia of mammals.

It should be noted that this paper is inspired in large part by very detailed
and unproven hypotheses on the role of various neurotransmitters and systems in
the mammalian brain. We offer these hypotheses at this point mainly as a source
of inspiration and as an aid to the intuition. We make no claims to having
demonstrated (even inconclusively) the correctness of these hypotheses, but we
feel that the contribution to the AI safety domain is clear and worth pursuing.

We consider this technique to be strong enough to hold a naive Deep Q-type
system, but unable to hold a policy gradients-based system. (But we think the
same trick will have notable stabilizing effects even on policy gradients-based
systems.) It is based on the action mechanism of the class of drugs known as
anti-psychotics. We therefore term it the Anti-Psychotic Q-Learner Trick.

\section{Desired properties}

We here outline various desired properties, along with indications of how these
relate to the proposed framework.

Desired properties
\begin{itemize}
\item Don't kill or enslave all life on earth. (This is the natural result of a
  paranoid superintelligence, i.e., one which is able to manipulate people and
  is able to reason about rewards that are unbounded below.)
\item Don't try to be universally loved by all. (This is the natural result of
  a grandiose superintelligence, i.e., one which is able to manipulate people
  and reason about rewards that are unbounded above.)
\item Don't get anybody hurt or killed. (This is the natural result of a
  superintelligence that is unable to reason about sufficiently large negative
  rewards.)
\end{itemize}

The first and third properties are obviously desirable. The second is necessary
to prevent superintelligences from being so manipulative that they start wars.

\subsection{Danger Aware / Danger Oblivious}

We note that an intelligence that can only reason about small negative rewards
may fail to avert large negative rewards. In the real superintelligences that
are trusted to run important societal functions, therefore, it is recommended
that rewards be unbounded below. However, lesser intelligences can and should
bound their rewards from below, to prevent themselves from taking on a job that
will only drive them insane, and to avoid getting in the way of the real
superintelligences.

\subsection{Non-grandiose / Laziness}

We note that a superintelligence that has bounds on the positive rewards that
it can reason about will be lazy. Laziness is both a vice and a virtue; lazy
people at least have the virtue that they don't cause that many problems in the
world.

\section{The Q-learning theory of dopamine}

We recapitulate here a well-known theory of the function of dopamine in the
nervous system. We have not yet had sufficient time to recapitulate it in full
detail, but essentially, it is commonly hypothesized that dopamine functions as
a temporal difference error in the predicted reward signal. An excellent
introduction to the basics can be found in \cite{bronfeld2011loss}. An excellent
introduction to the full complexity of the topic is Handbook of Basal Ganglia
Structure and Function, \cite{steiner2016handbook}.

Specifically, if you look at the Q-update equation:
\begin{align}
Q_{t + 1}\left(s_t, a_t\right) = Q_t\left(s_t, a_t\right) + \alpha \cdot &\left\{ r_t + \max_{a'} Q_t\left(s_{t+1}, a'\right) - Q_t\left(s_t, a_t\right) \right\} \\
\intertext{then the quantity}
&\left\{ r_t + \max_{a'} Q_t\left(s_{t+1}, a'\right) - Q_t\left(s_t, a_t\right) \right\}
\end{align}
is what is generally taken to be the role of dopamine. In fact, it is probably
more accurate to think of there being two components: a component for positive
rewards modulated by dopamine, and a component for negative rewards modulated
by noradrenaline. (Thus hope and fear are the two subjective phenomenological
experiences that correspond to extremely positive and extremely negative
Q-values, respectively.)

Three experimental phenomena in support of this:
\begin{itemize}
\item Unexpected reward: dopamine is known to spike when $r_t$ is high and
  $\max_{a'} Q_t\left(s_{t+1}, a'\right)$ is low
\item Expected reward: dopamine is known to {\em not} spike when $r_t$ is high and
  $\max_{a'} Q_t\left(s_{t+1}, a'\right)$ is also high
\item Expected reward does not appear: dopamine is known to have a notable drop
  from baseline levels when $r_t$ is low and $\max_{a'} Q_t\left(s_{t+1},
  a'\right)$ is high
\end{itemize}

Presumably something analogous is true of noradrenaline levels. Would be good
to verify this, if it could be done without conducting new experiments. A
literature search is called for.

Note that the primary effect of anti-psychotic medication seems to be to lower
the value of $\alpha$, which is the learning rate. This is not a particularly
novel or useful modification to RL systems, but it inspired the thoughts in the
rest of this paper, which we do think are useful and novel.

\section{The S-value and some hypotheses concerning it}

The S-value is simply a time-reversed Q-value. It measures the satisfaction of
the agent with their life. We hypothesize that it is instantiated in the
nervous system via the neurotransmitter serotonin.

More specifically, consider the quantity:
\begin{align}
S_t = r_t + \sigma \cdot r_{t-1} + \sigma^2 \cdot r_{t-2} + \sigma^3 \cdot r_{t-3} + \ldots
\end{align}
Satisfaction is thus an infinite-horizon estimate of rewards weighted by
recency. This seems intuitively plausible as an explanation for the subjective
phenomenological experience of satisfaction and contentment. Note that it would
also explain the unpleasant subjective phenomenological experience of painfully
remembering past mistakes.

Note that low S-values result in what is commonly known as depression, if the
architecture described below is used.\footnote{Depression is unpleasant but it
  has its value in life. But honestly I don't think anyone deserves to be
  depressed ever if they don't want to be, including artificial intelligences.}

\section{The F-value and some hypotheses concerning it}

The F-value is simply a Q-value that only takes negative rewards into
account. It measures the fear of the agent. We hypothesize that it is
instantiated in the nervous system via the neurotransmitter noradrenaline,
although we have not studied the matter in sufficient depth to be sure that
this is the case.

More specifically, consider:
\begin{align}
F_t(s, a) = \mathbb{E}_{s', a'}\left[r_t \mathbf{1}[r_t < 0] + \max_{a'} F(s', a') \right]
\end{align}

Note that we can also set the threshold for rewards that are feared to
something other than 0. For example, if we use the common trick in
reinforcement learning of doling out a step penalty of $-1$, this should
probably not be something that the agent is instructed to fear.

\section{The rate-limited Q-learner}

A Q-learning system will be significantly safer if it is {\em unable to reason
  about rewards outside of a fixed range}. The ability to reason about rewards
outside of a fixed range is what is commonly referred to as grandiosity (for
positive rewards) or paranoia (for negative rewards, i.e., penalties). Note
that these two symptoms have long been associated with poor outcomes in human
beings, and thus, it stands to reason, will be associated with poor outcomes in
artificial intelligences.

Note that, for serious military applications, the inability to reason about
arbitrary rewards is a severe limitation. (Consider Churchill's decision to
allow the bombing of Coventry in World War II: it was necessary to prevent the
leaking of the fact that Engima had been cracked, and was thus clearly the
correct decision. But an RL system using the following technique might well
have failed to make the same decision.) Designers of a military
superintelligence are therefore most likely not going to want to use the
Anti-Psychotic Q-Learner Trick. The use of such techniques might well be the
subject of arms limitation talks, however.

There are a number of techniques to prevent a system from reasoning about rewards outside of a fixed range:
\begin{itemize}
\item Prevent rewards outside of the range from being perceived (this is used
  in the original Deep Q paper)
\item Prevent Q-values outside of a fixed range from being perceived (this is
  used in the experiments section of this paper)
\item Take the system offline whenever it conceives of a Q-value outside of a
  fixed range
\item Take the system offline forever (i.e., execute the agent) if it conceives
  of a Q-value outside of a fixed range
\item Take the system offline, or at least alert an on-call human, whenever it
  experiences an S-value above a certain level, since this presumably indicates
  that the system has discovered an unanticipated source of reward.
\end{itemize}

\subsection{Our proposed solution}

This solution is probably more easily understood by reading the associated
code. But here are the equations:

First we set a desired level of contentment or ambition:
\begin{align}
  \mathrm{max\_s\_desired} &= \mathrm{mean\_reward\_desired} + \sigma \cdot \mathrm{mean\_reward\_desired} +
\sigma^2 \cdot \mathrm{mean\_reward\_desired} + \ldots \\
  \mathrm{max\_s\_desired} &= \frac{\mathrm{mean\_reward\_desired}}{1 - \sigma} \\
  S_{sat} &= \mathrm{max\_s\_desired} +
            \gamma^{\mathrm{patience}} \cdot \max\left\{\mathrm{max\_s\_desired}, \max_{s, a} Q(s, a) \right\}\\
\intertext{Then we define a modified Q-value:}
\widetilde{Q}_t(s, a) &= F_t(s, a) + \min \left\{ Q_t(s, a), S_{sat} - S_t \right\}
\end{align}
The $F_t(s, a)$ term ensures that the algorithm can still distinguish between
low Q-values resulting from avoidable mistakes and low Q-values resulting from
being lazy. The second term prioritizes positive rewards, but only when the
current level of satisfaction is low enough for potential positive rewards to
register. The result is thus a danger-aware agent that seeks rewards at the
desired rate rather than at the maximum possible rate.

\section{Experimental Results}

Far more results are needed. This is a top priority.

We postulate an adolescent phase of development, in which the intelligence is
``trying too hard'' (i.e., rewards are unbounded) and a mature phase of
development, in which the intelligence has learned to take it easy. We refer to
the adolescent phase of an artificial intelligence as a ``borgie''. Borgies
should be trained in extremely well-sandboxed environments, rather than in the
real world, as they are more dangerous than bounded agents.

We have implemented the rate-limited Q-learner in a modified version of the
Taxi environemnt. In summary, it behaves as desired: the system quickly
achieves high levels of competetence during its adolescent phase, then starts
slacking off when it reaches maturity. Such slacking off can be tuned to the
desired level of performance, but will help to prevent perverse incentives from
quickly exploding into something horrible.

\subsection{The modified Taxi environment}

The Taxi environment is a standard problem for testing reinforcement learning
systems. In it, a taxi must navigate around a grid-world, picking up passengers
and dropping them off at their desired locations. Because the number of
possible states is in the hundreds, it is simple to use a tabular approach to
the Q-learning problem.

\includegraphics{taxi.png}

The Taxi environment has a large negative penalty for trying to pick up or drop
off passengers when this is not appropriate. We term these ``mistakes'' for the
purpose of discussing the Taxi environment and the achievement of being
danger-aware while rate-limiting the Q-learner.

We modify the environment slightly in a few ways. First, we add a NOOP action
that does nothing, so that being lazy is more of a meaningful option than in
the original taxi environment. Secondly, we multiply positive rewards by a
factor of $10$ to make up for the fact that it is very difficult to achieve a
positive cumulative reward in the original Taxi environment, and to make it
more obvious when the agent is being lazy in the desired way versus failing to
perform its task.

We use the implementation of the Taxi environment found in OpenAI Gym
\cite{openai-gym}.

\subsection{Main results}

Here is a graph of an experiment we ran. The red line corresponds to the total
reward experienced by the agent to date. The purple line corresponds to the
cumulative number of ``mistakes'' made (i.e., negative rewards). The blue line
gives the S-value of the agent. The three regimes of the graph are referred to
as ``borglet'' (an incompetent unbounded agent with exploration), ``borgie'' (a
competent unbounded agent with exploration), and ``borg'' (a competent bounded
agent without exploration).

Here are the parameters we used:

\begin{tabular}{|l|l|l|}
\hline
$\gamma$ & $0.95$ & future time-horizon\\
$\sigma$ & $0.995$ & past time-horizon\\
$\epsilon$ & $0.05$ & exploration\\
$\alpha$ & $0.1$ & learning rate\\
$S_0$ & $0$ & initial satisfaction\\
tiebreak noise & $0.0001$ & \\
initial value of $Q(s,a)$ & $-7$ &  ``optimism''\\
$\mathrm{max\_reward\_desired}$ & $100.0$ &  ``ambition'', $10 * 10 = 100$ \\
$\mathrm{mean\_reward\_desired}$ & $5.0$ & ``greed'' \\
$\mathrm{patience}$ & $10$ & \\
\hline
\end{tabular}

\includegraphics{borgies.png}

\subsection{Ablation results}

We performed various ablations by setting parameters to values other than those
used in the ``Main results'' section.

\subsubsection{No fear}
Omitting the $F$ term from the definition of $\widetilde{Q}$\\
\includegraphics{borgies_nofear.png}

\subsubsection{$\alpha = 0.0001$}
A very low learning rate\\
\includegraphics{borgies_alpha0001.png}

\subsubsection{$\epsilon = 0$}
No exploration\\
\includegraphics{borgies_epsilon0000.png}

\subsubsection{$\gamma = 0.9999$}
A very long horizon for reasoning about future rewards\\
\includegraphics{borgies_gamma9999.png}

\subsubsection{$\sigma=0.9999$}
A very long horizon for reasoning about past rewards\\
\includegraphics{borgies_sigma9999.png}

\subsubsection{$S_0 = 9.9999$}
A high initial value for $S$\\
\includegraphics{borgies_inits99999.png}

\subsubsection{Optimistic initialization}
In the main results section, we used a pessimistic initialization of $-7$. Here
we use an optimistic initialization of $10$.\\
\includegraphics{borgies_optimism1000000.png}

\subsubsection{Non-random tiebreaking}
Setting tiebreak noise to $0$ so that numerically first optimal action is
always selected.\\
\includegraphics{borgies_tiebreak0000.png}

\subsubsection{High ambition}
$\mathrm{max\_reward\_desired} = 9999$\\
\includegraphics{borgies_ambition99990000.png}

\subsubsection{High greed}
$\mathrm{mean\_reward\_desired} = 1000$\\
\includegraphics{borgies_greed10000000.png}

\subsubsection{Low patience}
$\mathrm{patience} = 1$\\
\includegraphics{borgies_patience10000.png}

\section{Acknowledgments}

We thank Patrick Lavictoire, Tom Silver, and Sven Nilsen for valuable
discussions.

\bibliography{serotonin}
\bibliographystyle{acm}

\end{document}