\documentclass[12pt]{article}
\author{David Alves}

\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{dirtytalk}
\usepackage[a4paper, total={6.5in, 8.75in}]{geometry}
\usepackage{forest}
\usepackage{skak}
\usepackage{tikz}
\usepackage{titling}
\usepackage{wrapfig}
\usepackage{xcolor}

\def\multichoose#1#2{\ensuremath{\left(\kern-.3em\left(\genfrac{}{}{0pt}{}{#1}{#2}\right)\kern-.3em\right)}}

\title{Math 142 Problem Set 6}
\author{David Alves}
\date{2016-10-06}

\begin{document}
\pagenumbering{gobble}

\begin{center}
\large \thetitle \\
\theauthor \\
\thedate
\end{center}

\subsection*{Sources}

    \begin{itemize}
    \item http://tex.stackexchange.com and https://www.sharelatex.com for help with \LaTeX
    \end{itemize}

\section{Some Chairs are Dry}

\subsection*{Problem Statement}
Find the exact problem with the following proof: \say{We prove that all sets of $n$ chairs are wet, $n \geq 0$ an integer, by induction. For base case, it is (vacuously) true that for all chairs in an empty set of 0 chairs, they are wet. Suppose our statement is true for $n$. Consider a set of $(n+1)$ chairs. Sit on a chair $C$. The chairs not counting $C$ form a set of $n$ chairs. By assumption, they are all wet. Now get off your chair $C$ and sit on a different chair. Now the other chairs, including $C$, form a set of $n$ chairs. By assumption, these chairs are all wet, so $C$ is wet. Since $C$ is wet and all other chairs are wet, all $(n+1)$ chairs are wet and the inductive step is complete.}
\subsection*{Solution}

The inductive step requires at least one wet chair in the set of $n$ wet chairs (the one which gets swapped with $C$) in order to prove $n+1$, but the base case is 0 chairs. Therefore the inductive step is not valid for $n=0$ and the proof fails. (Note that if the base case was $n=1$, \say{All sets of 1 chair are wet} then the proof would work.)

\section{Rooks on a Chessboard}

\subsection*{Problem Statement}
Find the number of ways to place three rooks on an $8\times8$ chessboard such that no rooks attack each other (meaning no two rooks are in the same row or column).
\subsection*{Solution}

There are 18816 ways to place three indistinguishable rooks onto an $8\times8$ chessboard so that no two rooks can attack each other.

\begin{proof}
In order for rooks not to attack each other, they need to all be on different ranks and all be different files. Label the rooks $R_1, R_2, R_3$. There are $8 \text{ ranks} \times 8 \text{ files} = 64$ ways to place $R_1$ on an empty board. There are 7 empty ranks and 7 empty files left for $R_2$, giving 49 choices. Finally there are 6 empty ranks and 6 empty files left for $R_3$. This gives $64\times49\times36=112896$ ways to place rooks if they were distinguishable. We then divide this result by $3!$ permutations of the labelled rooks due to the quotient principle (since rooks are indistinguishable) to get $\frac{112896}{3!} = 18816$ placements.
\end{proof}

\section{Digit Lists}

\subsection*{Problem Statement}
How many $n$-digit lists of digits 0 through 9 are there such that the digits 1,2,3, and 9 all appear at least once?
\subsection*{Solution}

\newcommand{\dlist}[1]{D_{#1}}
There are $10^n - 4(9^n) + 6(8^n) - 4(7^n) + 6^n$ $n$-digit lists which contain each of the digits 1,2,3 and 9 one or more times.

\begin{proof}
Let $D_k$ be the set of length-$n$ lists which do not contain the digit $k$. The set of length-$n$ lists which is missing at least one of the digits 1, 2, 3, and 9 is therefore $D_1 \cup D_2 \cup D_3 \cup D_9$. By the inclusion-exclusion principle, we have

\begin{multline}\label{eq:ps6_3_1}
    |\dlist{1} \cup \dlist{2} \cup \dlist{3} \cup \dlist{9}| = |\dlist{1}| + |\dlist{2}| + |\dlist{3}| + |\dlist{9}|\\
    -|\dlist{1} \cap \dlist{2}| - |\dlist{1} \cap \dlist{3}| - |\dlist{1} \cap \dlist{9}| - |\dlist{2} \cap \dlist{3}| - |\dlist{2} \cap \dlist{9}| - |\dlist{3} \cap \dlist{9}|\\
    +|\dlist{1} \cap \dlist{2} \cap \dlist{3}| + |\dlist{1} \cap \dlist{2} \cap \dlist{9}| + |\dlist{1} \cap \dlist{3} \cap \dlist{9}| + |\dlist{2} \cap \dlist{3} \cap \dlist{9}|\\
    -|\dlist{1} \cap \dlist{2} \cap \dlist{3} \cap \dlist{9}|
\end{multline}

The size of $D_k$ for any digit $k$ is $9^n$ since there are 9 possibilities for each digit (anything other than $k$). The size of $D_a \cap D_b : a \neq b$ is $8^n$ since each digit can be anything except $a$ and $b$. Similarly, the size of $D_a \cap D_b \cap D_c: (a,b,c \text{ all distinct})$ is $7^n$ and the size of $D_a \cap D_b \cap D_c \cap D_d: (a,b,c,d \text{ all distinct})$ is $6^n$. Substituting these values into equation \ref{eq:ps6_3_1} gives $
    |\dlist{1} \cup \dlist{2} \cup \dlist{3} \cup \dlist{9}| = 4(9^n) - 6(8^n) + 4(7^n) - 6^n$.

By the sum principle, the number of digit lists of size $n$ is equal to the number of size-$n$ lists where all of the digits $1,2,3,9$ appear plus the number of size-$n$ lists where one or more of the digits $1,2,3,9$ does not appear, which is $|\dlist{1} \cup \dlist{2} \cup \dlist{3} \cup \dlist{9}|$. The total number of lists of length $n$ is $10^n$ since there are 10 possibilities for each digit, therefore the number of lists of size $n$ where each digit $1,2,3,9$ appears one or more times is $10^n - 4(9^n) + 6(8^n) - 4(7^n) + 6^n$
\end{proof}

\section{Black \& White Graphs}
\subsection*{Problem Statement}
Consider a complete graph on 6 vertices. Color some of the edges white and some of the edges black. Prove that there must be a triangle with all white edges or all black edges somewhere in the graph. (A triangle with all white edges would be some 3 vertices $a,b,c$ such that the edges $(a,b), (a,c), (b,c)$ are all white)
\subsection*{Solution}
\begin{proof}
Assume that an edge coloring exists on $K_6$ such that each edge is either black or white and there are no all-black or all-white triangles. Consider a vertex $v$. $v$ is connected to the other five vertices by five edges. At least three of these edges must share some color $c$. (If more than three share a color, consider only three of them). Let the three vertices connected to $v$ by $c$-colored edges be $x$, $y$, and $z$. Since $(v,x)$ and $(v,y)$ are colored $c$, $(x,y)$ must be non-$c$-colored otherwise $(v,x), (v,y), (x,y)$ would form a $c$-colored triangle which would contradict our assumption. Similarly since $(v,x)$ and $(v,z)$ are $c$-colored, $(x,z)$ must be non-$c$-colored, and since $(v,y)$ and $(v,z)$ are $c$-colored, $(y,z)$ must be a non-$c$-colored. Since $(x,y)$, $(x,z)$, and $(y,z)$ are all non-$c$-colored, it is an all-black or all-white triangle, which contradicts our assumption. Therefore the assumption is incorrect, and there are no edge colorings on $K_6$ such that there are no all-black or all-white triangles.\\
\end{proof}

\colorlet{verylightgray}{black!45}
\begin{center}
\begin{tikzpicture}
\node[shape=circle,draw=black] (A) at (0,0) {$v$};
    \node[shape=circle,draw=black] (B) at (4,0) {$z$};
    \node[shape=circle,draw=black] (C) at (1.24,3.8) {$y$};
    \node[shape=circle,draw=black] (D) at (-3.24, 2.35) {$x$};
    \node[shape=circle,draw=black] (E) at (-3.24, -2.35) { };
    \node[shape=circle,draw=black] (F) at (1.24,-3.8) { } ;

    \path [-,line width=1pt] (A) edge node[left] {} (B);
    \path [-,line width=1pt] (A) edge node[left] {} (C);
    \path [-,line width=1pt] (A) edge node[left] {} (D);
    \path [-,color=verylightgray] (A) edge node[left] {} (E);
    \path [-,color=verylightgray] (A) edge node[left] {} (F);
    \path [-,line width=1pt,style=dashed] (B) edge node[left] {} (C);
    \path [-,line width=1pt,style=dashed] (B) edge node[left] {} (D);
    \path [-,color=verylightgray] (B) edge node[left] {} (E);
    \path [-,color=verylightgray] (B) edge node[left] {} (F);
    \path [-,line width=1pt,style=dashed] (C) edge node[left] {} (D);
    \path [-,color=verylightgray] (C) edge node[left] {} (E);
    \path [-,color=verylightgray] (C) edge node[left] {} (F);
    \path [-,color=verylightgray] (D) edge node[left] {} (E);
    \path [-,color=verylightgray] (D) edge node[left] {} (F);
    \path [-,color=verylightgray] (E) edge node[left] {} (F);
\end{tikzpicture}
\end{center}

\section{Friends at a Party}
\subsection*{Problem Statement}
Thirteen people meet at a party. Then none or some of the people decide to send Facebook friend requests to some of the other people at the party. How many outcomes are there? (Two outcomes are equivalent if the same set of friend requests were made, where a request only depends on the identity of the person asking and the person asked.)
\subsection*{Solution}

There are $2^{156}$ possible outcomes.

\begin{proof}
Let $S$ be the set of people at the party. A friend request can be represented as a two-element list $(a, b)$ where $a$ is the sender and $b$ is the recipient and $a \neq b$. Thus for each of the 13 senders, there are 12 possible recipients making a set of 156 possible friend requests by the product principle. An outcome is a subset of this set, thus there are $2^{156}$ possible outcomes.
\end{proof}

\section{Time Spent \& Thoughts}

This problem set was about average difficulty. I spent about six hours, mostly on the Black \& White problem. Typing up the Digit Lists problem also took a while, although the problem itself wasn't hard.

 The black and white graphs problem had me stuck for a while before I figured out the trick of starting with a single vertex and saying that there must be at least 3 outgoing edges with the same color.  I spent a while trying to prove that a set of 8 edges on $K_6$ guarantees a triangle (since you are partitioning 15 edges into two groups, one must have size 8 or more) but that was a more difficult approach. There doesn't seem to be any clear connection between the black and white problem and what we've been doing in this class, so I'm not sure what the idea behind including it was --- just more practice with proofs?
\end{document}


\begin{center}
{{\Large \sc High-Performance Computing}}
\end{center}
\rule{\textwidth}{1pt}
\begin{description}
\item[Student name and id:] Marie Mørk \{s112770\} Anja Liljedahl Christensen\{s162876\}
 Andreas Vedel Jantzen \{s162858\}
 Anders Launer Bæk \{s160159\}
\item[Hold name:] matmult\_MAAA
\item[Hand-in:] Matrix Multiplication
\end{description}
\rule{\textwidth}{1pt}

\section{Summary}
Different functions, performing matrix-matrix multiplication are implemented in C in order to compare performance. Cases considered are ordering of the nested loops, optimizers in the compiler and blocking. The implemented functions are compared with the CBLAS subroutine DGEMM. As memory in C is stored row-wise, it has been found that the most optimal ordering of nested loops is to have the column index in the most inner loop. The optimizers are found to enhance performance significantly. The blocked version is found to be less efficient than the optimized version of the most efficient ordering of the loops. This might be due to the implementation of the blocking in which as many columns as rows are considered at a time.

\section{Statement of the problem}

The goal of the assignment is to create a library of functions that perform matrix-matrix multiplications. Performance of the implementations are to be compared with the DGEMM matrix-matrix multiplication routine from the BLAS library. As input, each of the functions should take the dimensions of the matrices (M, N, K) as well as the matrices (A, B, C), according to figure \ref{fig:matmult}. The problem is divided into four sub-problems;
\begin{itemize}
    \item Wrap the call DGEMM into a function called \texttt{matmult\_lib()}, that takes the same inputs as
    specified above.
    \item Implement a function that performs double precision matrix-matrix multiplication. The function should be named \texttt{matmult\_nat()}.
    \item Functions for each of the permutations of the three nested loops needed to perform matrix multiplications. The performance of the permutations should be compared. Furthermore, the timings should be compared with and without different compiler optimizations. The functions should be named as \texttt{matmult\_per()}, where \texttt{per} is the ordering of the nested loops for the given implementation, e.g. \texttt{nmk,knm}, ect.
    \item A blocked version of the matrix-matrix multiplication function named \texttt{matmult\_blk()}. This function should take an extra input, \texttt{bs}, which specifies the blocking size.
\end{itemize}

\begin{figure}[h]
    \centering
    \includegraphics[]{contents/pics_1/matmult.PNG}
    \caption{Visualisation of the matrix-matrix multiplications to be performed. The picture is taken from the problem description on DTU Inside.}
    \label{fig:matmult}
\end{figure}

\section{Hardware and software}\label{sec:hard}

Specifications of the test environment are listed below:
\begin{itemize}
\item CPU information
\begin{itemize}
\item Vendor ID:             GenuineIntel
\item CPU family:            6
\item Model:                 79
\item Model name:            Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz
\item CPU MHz:               1297.656
\item L1 (d / i) cache:             32K
\item L2 cache:              256K
\item L3 cache:              30720K
\end{itemize}
\item Compiler and Matrix implementations
\begin{itemize}
\item The GCC compiler have been applied with and without compiler optimization using the compiler flag \texttt{-Ofast}.
\item The library function uses DGEMM from the CBLAS interface
\item Matrices are indexed in the following way: [m] [n]
\item The following line has been specified in the job script: "\texttt{\#BSUB -n 1}" to make sure the batch job only uses one core.
\end{itemize}
\end{itemize}

\section{Theory}
Matrix multiplications are central operations in many numerical algorithms. Due to this, it is essential to make the operation fast to keep the computation time low. In order to achieve an optimal matrix-matrix multiplication algorithm, it is therefore important to take the ordering of nested loops and memory storage in the computer into account.\\

In C matrices are stored row-wise. Hence, when an element is fetched from memory the next couple of elements in the same row are collected as well, since they are stored in the same cache line. This is useful if the next couple of elements from the same row are needed in the near future, since they have already been loaded into memory. If on the other hand the next element is extracted from another row, e.g. in the case of looping through a column, there will be a cache miss and a new cache line is loaded into memory. Thus, in the worst case there will be a cache miss each time a new element is needed. In the case of matrix multiplication it is necessary to loop through both rows and columns in multiple nested loops and hence there is a great chance of cache miss when the matrices are sufficiently large. If the matrices are small enough such that all three will fit in memory there should be no cache misses. For large scale matrices it is essential to optimize the implementation of the matrix-matrix multiplication in order to avoid too many cache misses.\\

Double precision matrix multiplication of square matrices requires space for $3\cdot d^3$ doubles in memory, if the dimensions of the matrices is given by $d$. As storage of a double takes up 8 bits, the maximum dimensions for the matrices to fit in the memory of a given cache is found theoretically by calculating
\begin{align*}
    d = \sqrt{\frac{L/8}{3}}
\end{align*}
where L is the memory in the given cache. The maximum dimensions of square matrices fitting into each of the caches used are listed in table \ref{tab:cache_max_dim}. In reality, the maximum allowed dimensions might be higher, as the program performs hardware prefetch.
\begin{table}[h]
    \centering
    \begin{tabular}{c|c|c}
         Cache & Size [bytes] & Maximum dimension of Matrix  \\ \hline
         $L_1$ & 32K & 36 \\
         $L_2$ & 256K & 103 \\
         $L_3$ & 30720K & 1131
    \end{tabular}
    \caption{Maximum dimensions of square matrices fitting into each of the caches.}
    \label{tab:cache_max_dim}
\end{table}

In the last sub problem of the report, blocking is implemented in order to optimize the memory usage in the algorithm. The idea behind blocking is to divide the iterations into smaller blocks, so that operations can be performed for parts of the matrices at a time, instead of considering the full size of the problem. This is especially useful when handling large problems, for which the memory needed to compute the full operation exceeds the bounds of the memory. When using an algorithm that relies on blocking it is important to use one of the respective compiler flags: \texttt{-O3} or \texttt{-Ofast}. By doing so the compiler analyses the code several lines ahead and is hereby able to optimize the code.
%Notet med optimizers i compileren

%Noget med blocking (evt. sammen med hukommelse)

% hardware prefetch

%Hit/miss og mflops/s - memory

\section{Algorithms}
\subsection{\texttt{matmult\_lib}} %Wrapping of GDEMM
The subroutine DGEMM from the library BLAS will be used to evaluate the performance of and compare with the different implementations of the matrix-matrix multiplication. For this purpose the CBLAS interface is used and wrapped into the function \texttt{matmult\_lib} such that the library implementation of the matrix-matrix multiplication takes the same arguments as the other functions.  The call to DGEMM using the CBLAS interface is \texttt{cblas\_dgemm} which takes 14 arguments\footnote{http://www.netlib.org/lapack/explore-html/dc/d18/cblas} as shown in algorithm \ref{alg:lib}. From the inputs, the matrix multiplication is calculated as
\begin{align*}
    C = \alpha \cdot A \cdot B + \beta \cdot C.
\end{align*}
Here, $\alpha$ and $\beta$ are arguments to be specified in the input. In this case $\alpha$ is set to 1 while $\beta$ is set to 0 in order to get the wanted output. The other inputs in DGEMM (aside from M, N, K, A, B, and C) are the layout (set to rowmajor), whether the A and B matrices should be transposed (TRANSA and TRANSB) and the leading dimensions of A, B and C. As seen in the algorithm \ref{alg:lib}, the A and B matrices are not transposed, and the dimensions are set to K, N and N for A, B and C.
\lstinputlisting[label={alg:lib},caption={\texttt{matmult\_lib}},style=CStyle]{code/matmult_lib.c}

\subsection{\texttt{matmult\_nat}} %nat
The implementation of the \texttt{matmult\_nat} function is listed in algorithm \ref{alg:nat}. For this function, it has been chosen to use the a MNK ordering of the nested loops. That is that the outer loop iterates over the dimension M, the second over N and the inner over K.
\lstinputlisting[label={alg:nat},caption={\texttt{matmult\_nat}},style=CStyle]{code/matmult_nat.c}

\subsection{\texttt{matmult\_per}} % permutations
The \texttt{matmult\_per} permutations are implemented as presented in algorithm \ref{alg:per}, where the ordering of the nested loops are interchanged for each permutation of M, N and K. There exist six different permutations of the \texttt{matmult\_nat} function, namely MNK, NKM, MKN, KMN, MNK, and NMK. The \texttt{matmult\_per} algorithm has been optimized in relation to the \texttt{matmult\_nat} function. This has been done using stripmining, and setting all entries of C to zero using 1 for loop instead of 2 enables the compiler to perform vectorization.
\lstinputlisting[label={alg:per},caption={\texttt{matmult\_per} for the \texttt{mnk} ordering of the loops.},style=CStyle]{code/permutationcodepiece.c}

\subsection{\texttt{matmult\_blk}} % Blocking
In order to implement blocking in the matrix-matrix multiplications, additional nested loops are needed. The algorithm used for blocking is based on one of the best performing versions of the \texttt{matmult\_per} functions – in this case \texttt{matmult\_mkn}. In algorithm \ref{alg:blk} the function is presented. As mentioned in the problem description \texttt{matmult\_blk} takes the additional input \texttt{bs}, which is the blocking size. In blocking, the matrices A, B and C are portioned into smaller matrices for which the multiplications are performed and then added to each other to get the full C matrix. In this implementation the blocks are chosen to be square matrices. This can be seen from the first three nested loops in which blocks of the matrices of size bs$\times$bs are chosen. In the inner three nested loops the extracted matrices are multiplied and added to C. When using \texttt{matmult\_blk} the compiler needs an optimizer flag, in order for the blocking to set in.
\lstinputlisting[label={alg:blk},caption={\texttt{matmult\_blk}},style=CStyle]{code/matmult_blk.c}

\newpage
\section{Results}
The implementation of the different functions were checked in a test-script to see if the C matrix was calculated as expected. Each of the functions \texttt{matmult\_nat}, \texttt{matmult\_per}, \texttt{matmult\_blk}, and \texttt{matmult\_lib} were tested using the square matrices:
\begin{align*}
    A_s =
    \begin{pmatrix}
        1 & 2\\
        2 & 4
    \end{pmatrix} \qquad
    B_s =
    \begin{pmatrix}
        2 & 2\\
        2 & 2
    \end{pmatrix}
\end{align*}
and the non-square matrices:
\begin{align*}
    A_{ns} =
    \begin{pmatrix}
        1 & 2\\
        2 & 4\\
        3 & 6
    \end{pmatrix} \qquad
    B_{ns} =
    \begin{pmatrix}
        2 & 2 & 2\\
        2 & 2 & 2
    \end{pmatrix}
\end{align*}
which all returned
\begin{align*}
    C_{s} =
    \begin{pmatrix}
        6 & 6\\
        12 & 12
    \end{pmatrix} \qquad
    C_{ns} =
    \begin{pmatrix}
        6 & 6 & 6\\
        12 & 12 & 12\\
        18 & 18 & 18
    \end{pmatrix}
\end{align*}
% The calculations are checked against the DGEMM subroutine to ensure that the matrix-matrix multiplication is computed correctly for each of the implemented functions.
The calculation check sum provided by the driver has also been studied during the performance evaluation of the implemented functions, and in each of the cases the return value was 0 as expected.

Only square matrices are considered, e.g. N = M = K when the performance of the functions is evaluated.

Furthermore the performances of the above functions have been evaluated with and without the compiler flag \texttt{-Ofast} which enable compiler optimization, indicated as "opt".
It has been decided to capture the hit and miss counters for the L1 cache and L2 cache in the first project.

\subsection{Performance of \texttt{matmult\_nat}}

Figure \ref{fig:nat_perf} illustrates two realizations of the naive implementation of the \texttt{matmult\_nat} function. The blue realization is the naive implementation without compiler optimization enabled and the black realization has compiler optimization enabled. By considering the differences between the realizations in figure \ref{fig:nat_perf} huge increases in performance can be achieved by using compiler optimization.\\

\begin{figure}[!th]
\centering
\begin{tikzpicture}[scale = 1]
\begin{axis}[
width=17cm, height=8cm,     % size of the image
grid = major,
grid style={dashed, gray!30},
xmode=log,log basis x=10,
xmin=0,    % start the diagram at this
ymin=50,    % start the diagram at this
ymax=2000, % end   the diagram at this
axis background/.style={fill=white},
ylabel=Mflops/s,
xlabel=Memory/kbytes]

\addplot[mark=*, blue] table [x=mem, y=mflops, col sep=comma]{data/matmult_nat.txt};
\addlegendentry{matmult\_nat}
\addplot[mark=*, black] table [x=mem, y=mflops, col sep=comma]{data/matmult_nat_opt.txt};
\addlegendentry{matmult\_nat\_opt}

\addplot[red] table [x=x, y=y, col sep=comma]{data/L1.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L2.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L3.txt};
\addlegendentry{L1, L2 and L3 cache}
\end{axis}
\end{tikzpicture}
\caption{Performance of the \texttt{matmult\_nat} function measured in Mflops/s as a function of kbytes.}
\label{fig:nat_perf}
\end{figure}

Table \ref{tab:L1L2hitmis_nat} reports the cache hits and cache miss for the \texttt{matmult\_nat} function. It is observed that there are fewer hits in the optimized version for the L1 cache, while the misses are of equal size. The hypothesis would be that the number of hits increases for the optimized version, but it might be that the optimizer reduces the memory to be fetched, why the number of hits drops. Even though the number of hits reduces, we can still conclude that the optimized function performs significantly better than the non-optimized.

\begin{table}[!th]
\centering
\begin{tabular}{l|rr|rr|rr|rr}
\multirow{3}{*}{} & \multicolumn{4}{|c}{Non-optimized}& \multicolumn{4}{|c}{Optimized}\\
& \multicolumn{2}{|c}{L1 in $\%$} & \multicolumn{2}{|c}{L2 in $\%$}& \multicolumn{2}{|c}{L1 in $\%$} & \multicolumn{2}{|c}{L2 in $\%$} \\
& Hit& Miss& Hit& Miss& Hit& Miss& Hit& Miss\\ \hline
\texttt{matmult\_nat(50x50)}& 10 & 0 & 0 & 0 &0 & 0 & 0 & 0\\
\texttt{matmult\_nat(850x850)} & 129129 & 6072 & 5368 & 704 & 11651 & 6069 & 5524 & 544\\
\texttt{matmult\_nat(1600x1600)}& 861503 & 34583 & 134 & 34445& 73553 & 31510 & 92 & 31416\\
\end{tabular}
\caption{Hits and miss of the L1 and L2 cache for nat, presented in millions}
\label{tab:L1L2hitmis_nat}
\end{table}
\newpage

The naive implementation of the matrix multiplication routine, \texttt{matmult\_nat} is compared to the library subroutine DGEMM, which has been wrapped in \texttt{matmult\lib}. In figure \ref{fig:nat_perf}, the performance of DGEMM is shown with and without optimization in the compiler. Please note, that the axes is different from the one in \ref{fig:nat_perf}.\\
It is seen from the figure that the performance of DGEMM is nearly the same whether or not an optimizer is used in the compiler. This is not surprising, as we expect the library function to be optimized in itself, and the wrapper funciton \texttt{matmult\_lib} only calls the DGEMM subroutine and assigns values to some of the inputs. The performance of the library sub routine is significantly better than that of \texttt{matmult\_nat}. In table \ref{tab:L1L2hitmis_lib}, the hits and miss of the function are shown. All hits and misses are seen to be zero. This might be a consequence of the way the analyzer function handles the wrapper function. We would have expected the library function to have a very high number of hits and only few misses.

\begin{figure}[!th]
\centering
\begin{tikzpicture}[scale = 1]
\begin{axis}[
width=17cm, height=8cm,     % size of the image
grid = major,
grid style={dashed, gray!30},
xmode=log,log basis x=10,
%xmin=0,    % start the diagram at this
%ymin=50,    % start the diagram at this
ymax=10000, % end   the diagram at this
axis background/.style={fill=white},
ylabel=Mflops/s,
xlabel=Memory/kbytes,
legend pos=south east]

%\addplot[mark=*, blue] table [x=mem, y=mflops, col sep=comma]{data/matmult_nat.txt};
%\addlegendentry{matmult\_nat}
\addplot[mark=*, black] table [x=mem, y=mflops, col sep=comma]{data/matmult_lib_opt.txt};
\addlegendentry{matmult\_lib\_opt}

\addplot[mark=*, blue] table [x=mem, y=mflops, col sep=comma]{data/matmult_lib.txt};
\addlegendentry{matmult\_lib}

\addplot[red] table [x=x, y=y, col sep=comma]{data/L1.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L2.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L3.txt};
\addlegendentry{L1, L2 and L3 cache}
\end{axis}
\end{tikzpicture}
\caption{Performance of the \texttt{matmult\_lib} function measured in Mflops/s as a function of kbytes.}
\label{fig:nat_perf}
\end{figure}

\begin{table}[!th]
\centering
\begin{tabular}{l|rr|rr|rr|rr}
\multirow{3}{*}{} & \multicolumn{4}{|c}{Non-optimized}& \multicolumn{4}{|c}{Optimized}\\
& \multicolumn{2}{|c}{L1 in $\%$} & \multicolumn{2}{|c}{L2 in $\%$}& \multicolumn{2}{|c}{L1 in $\%$} & \multicolumn{2}{|c}{L2 in $\%$} \\
& Hit& Miss& Hit& Miss& Hit& Miss& Hit& Miss\\ \hline
\texttt{matmult\_lib(50x50)}& 0.0 & 0.0 & 0.0 & 0.0&0.0 & 0.0 & 0.0 & 0.0\\
\texttt{matmult\_lib(850x850)}& 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\
\texttt{matmult\_lib(1600x1600)}& 0.0 & 0.0 & 0.0 & 0.0& 0.0 & 0.0 & 0.0 & 0.0\\
\end{tabular}
\caption{Hits and miss of the L1 and L2 cache for lib, presented in millions}
\label{tab:L1L2hitmis_lib}
\end{table}

\newpage
\subsection{Optimal permutations of \texttt{matmult\_nat}}

As mentioned earlier it is possible to create several permutations of the naive \texttt{matmult\_nat} function. The six possible (the naive is included) permutations are presented in figure \ref{fig:opt_nmk_1_non} and the compiler optimized permutations in figure \ref{fig:opt_nmk_1}.

\begin{figure}[!th]
\centering
\begin{tikzpicture}[scale = 1]
\begin{axis}[
width=17cm, height=8cm,     % size of the image
grid = major,
grid style={dashed, gray!30},
xmode=log,log basis x=10,
xmin=0,    % start the diagram at this
ymin=50,    % start the diagram at this
ymax=400, % end   the diagram at this
axis background/.style={fill=white},
ylabel=Mflops/s,
xlabel=Memory/kbytes,
legend pos=south west]

\addplot[mark=*, black] table [x=mem, y=mflops, col sep=comma]{data/matmult_nmk.txt};
\addlegendentry{nmk}

\addplot[mark=*, red] table [x=mem, y=mflops, col sep=comma]{data/matmult_nkm.txt};
\addlegendentry{nkm}

\addplot[mark=*, blue] table [x=mem, y=mflops, col sep=comma]{data/matmult_mnk.txt};
\addlegendentry{mnk}

\addplot[mark=*, orange] table [x=mem, y=mflops, col sep=comma]{data/matmult_mkn.txt};
\addlegendentry{mkn}

\addplot[mark=*, green] table [x=mem, y=mflops, col sep=comma]{data/matmult_knm.txt};
\addlegendentry{knm}

\addplot[mark=*, magenta] table [x=mem, y=mflops, col sep=comma]{data/matmult_kmn.txt};
\addlegendentry{kmn}

\addplot[red] table [x=x, y=y, col sep=comma]{data/L1.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L2.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L3.txt};
%\addlegendentry{L1, L2 and L3 cache}
\end{axis}
\end{tikzpicture}
\caption{Performance of all permutations of the \texttt{matmult\_per} functions measured in Mflops/s as a function of kbytes.}
\label{fig:opt_nmk_1_non}
\end{figure}

\begin{figure}[!th]
\centering
\begin{tikzpicture}[scale = 1]
\begin{axis}[
width=17cm, height=8cm,     % size of the image
grid = major,
grid style={dashed, gray!30},
xmode=log,log basis x=10,
xmin=0,    % start the diagram at this
ymin=0,    % start the diagram at this
ymax=5000, % end   the diagram at this
axis background/.style={fill=white},
ylabel=Mflops/s,
xlabel=Memory/kbytes,
legend pos=north west]

\addplot[mark=*, black] table [x=mem, y=mflops, col sep=comma]{data/matmult_nmk_opt.txt};
\addlegendentry{nmk\_opt}

\addplot[mark=*, red] table [x=mem, y=mflops, col sep=comma]{data/matmult_nkm_opt.txt};
\addlegendentry{nkm\_opt}

\addplot[mark=*, blue] table [x=mem, y=mflops, col sep=comma]{data/matmult_mnk_opt.txt};
\addlegendentry{mnk\_opt}

\addplot[mark=*, orange] table [x=mem, y=mflops, col sep=comma]{data/matmult_mkn_opt.txt};
\addlegendentry{mkn\_opt}

\addplot[mark=*, green] table [x=mem, y=mflops, col sep=comma]{data/matmult_knm_opt.txt};
\addlegendentry{knm\_opt}

\addplot[mark=*, magenta] table [x=mem, y=mflops, col sep=comma]{data/matmult_kmn_opt.txt};
\addlegendentry{kmn\_opt}

\addplot[red] table [x=x, y=y, col sep=comma]{data/L1.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L2.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L3.txt};
%\addlegendentry{L1, L2 and L3 cache}
\end{axis}
\end{tikzpicture}
\caption{Performance of all permutations of the the compiler optimized \texttt{matmult\_***} functions measured in Mflops/s as a function of kbytes.}
\label{fig:opt_nmk_1}
\end{figure}

% \begin{figure}[!th]
% \centering
% \begin{tikzpicture}[scale = 1]
% \begin{axis}[
% width=17cm, height=8cm,     % size of the image
% grid = major,
% grid style={dashed, gray!30},
% xmode=log,log basis x=10,
% xmin=0,    % start the diagram at this
% ymin=0,    % start the diagram at this
% ymax=5000, % end   the diagram at this
% axis background/.style={fill=white},
% ylabel=Mflops/s,
% xlabel=Memory/kbytes,
% legend pos=north west]

% \addplot[mark=*, black] table [x=mem, y=mflops, col sep=comma]{data/matmult_mkn_opt.txt};
% \addlegendentry{mkn\_opt}

% \addplot[mark=*, red] table [x=mem, y=mflops, col sep=comma]{data/matmult_knm_opt.txt};
% \addlegendentry{knm\_opt}

% \addplot[mark=*, blue] table [x=mem, y=mflops, col sep=comma]{data/matmult_kmn_opt.txt};
% \addlegendentry{kmn\_opt}

% \addplot[red] table [x=x, y=y, col sep=comma]{data/L1.txt};
% \addplot[red] table [x=x, y=y, col sep=comma]{data/L2.txt};
% \addplot[red] table [x=x, y=y, col sep=comma]{data/L3.txt};
% %\addlegendentry{L1, L2 and L3 cache}
% \end{axis}
% \end{tikzpicture}
% \caption{Performance of the compiler optimized \texttt{matmult\_mkn}, \texttt{matmult\_knm} and \texttt{matmult\_kmn} functions measured in Mflops/s as a function of kbytes.}
% \label{fig:opt_nmk_2}
% \end{figure}

Figure \ref{fig:opt_nmk_1_non} tells that \texttt{matmult\_mkn} and \texttt{matmult\_kmn} are the best performing permutations in the L3 cache. Hence having N as the last index, makes the matrix-matrix multiplication the fastest. This was also expected, because M and K are the row indexes of the input matrices and fits to the way memory is stored the most efficient way. \\
The permutations has calculated using an optimizer. This result is presented in \ref{fig:opt_nmk_1}. This confirms that \texttt{matmult\_mkn} and \texttt{matmult\_kmn} are the fastest permutations, having \texttt{matmult\_mkn} a bit faster. When the optimizer is used the performance difference already occours in L1 and definately in L2. Hence, the combination of optimizer and having the right indexing makes \texttt{matmult\_mkn} the fastest permutation. \\
Table \ref{tab:L1L2hitmis_1600}, \ref{tab:L1L2hitmis_850} and \ref{tab:L1L2hitmis_50} presents the cache hits and misses for L1 and L2.
The functions have been evaluated for 50, 850 and 1600 and the global variable \texttt{} has been set to perform 10 iterations in each loop in order to make comparable tests. Table \ref{tab:L1L2hitmis_850} and \ref{tab:L1L2hitmis_1600} does again confirm that \texttt{matmult\_mkn} and \texttt{matmult\_kmn} are the most optimal matrix-matrix multiplication indexing. This can be concluded because the ratio of their respective hits and misses are big, both for L1 and L2.

\begin{table}[!th]
\centering
\begin{tabular}{l|rr|rr|rr|rr}
\multirow{3}{*}{50x50} & \multicolumn{4}{|c}{Non-optimized}& \multicolumn{4}{|c}{Optimized}\\
& \multicolumn{2}{|c}{L1} & \multicolumn{2}{|c}{L2}& \multicolumn{2}{|c}{L1} & \multicolumn{2}{|c}{L2} \\
& Hit& Miss& Hit& Miss& Hit& Miss& Hit& Miss\\ \hline
\texttt{matmult\_nmk}& 10 & 0.0 & 0.0 & 0.0 &0 & 0 & 0 & 0\\
\texttt{matmult\_nkm}& 10 & 0.0 & 0.0 & 0.0&0 & 0 & 0 & 0\\
\texttt{matmult\_mnk}& 10 & 0.0 & 0.0 & 0.0 &0 & 0 & 0 & 0 \\
\texttt{matmult\_mkn}& 10 & 0.0 & 0.0 & 0.0&0 & 0 & 0 & 0\\
\texttt{matmult\_knm}& 10 & 0.0 & 0.0 & 0.0&0 & 0 & 0 & 0\\
\texttt{matmult\_kmn}& 10 & 0.0 & 0.0 & 0.0 & 0 & 0 & 0 & 0
\end{tabular}
\caption{Hits and miss of the L1 and L2 cache for 50x50}
\label{tab:L1L2hitmis_50}
\end{table}

\begin{table}[!th]
\centering
\begin{tabular}{l|rr|rr|rr|rr}
\multirow{3}{*}{850x850} & \multicolumn{4}{|c}{Non-optimized}& \multicolumn{4}{|c}{Optimized}\\
& \multicolumn{2}{|c}{L1} & \multicolumn{2}{|c}{L2}& \multicolumn{2}{|c}{L1} & \multicolumn{2}{|c}{L2} \\
& Hit& Miss& Hit& Miss& Hit& Miss& Hit& Miss\\ \hline
\texttt{matmult\_nmk}& 129209 & 6094 & 6081 & 10& 11291 & 6350 & 6338 & 7 \\
\texttt{matmult\_nkm}& 123043 & 12138 & 11430 & 707 &13814 & 13310 & 12714 & 593\\
\texttt{matmult\_mnk}& 129169 & 6072 & 5364 & 703 &11851 & 6024 & 5448 & 573 \\
\texttt{matmult\_mkn}& 135215 & 192 & 16 & 2& 356 & 531 & 528 & 2\\
\texttt{matmult\_knm} & 123043 & 12144 & 11446 & 696& 13693 & 13341 & 12765 & 576 \\
\texttt{matmult\_kmn}& 135215 & 35 & 25 & 90& & 3524 & 525 & 509 & 18
\end{tabular}
\caption{Hits and miss of the L1 and L2 cache for 850x850}
\label{tab:L1L2hitmis_850}
\end{table}

\begin{table}[!th]
\centering
\begin{tabular}{l|rr|rr|rr|rr}
\multirow{3}{*}{1600x1600} & \multicolumn{4}{|c}{Non-optimized}& \multicolumn{4}{|c}{Optimized}\\
& \multicolumn{2}{|c}{L1} & \multicolumn{2}{|c}{L2}& \multicolumn{2}{|c}{L1} & \multicolumn{2}{|c}{L2} \\
& Hit& Miss& Hit& Miss& Hit& Miss& Hit& Miss\\ \hline
\texttt{matmult\_nmk}& 861423 & 34484 & 86 & 34397 & 51572 & 33418 & 26 & 33390 \\
\texttt{matmult\_nkm}& 819981 & 75809 & 121 & 75685 &11379 & 64094 & 134 & 63958\\
\texttt{matmult\_mnk}& 861623 & 34644 & 124 & 34518 &73593 & 31507 & 92 & 31412\\
\texttt{matmult\_mkn}& 901463 & 262 & 2464 & 16 & 22983 & 3790 & 3774 & 17\\
\texttt{matmult\_knm}& 820021 & 75806 & 89 & 75715 & 114034 & 64055 & 118 & 63937\\
\texttt{matmult\_kmn}& 901383 & 211 & 172 & 39 & 22943 & 3627 & 3521 & 105
\end{tabular}
\caption{Hits and miss of the L1 and L2 cache for 1600x1600}
\label{tab:L1L2hitmis_1600}
\end{table}

\newpage

% \todo[inline]{Hvad viser disse nedenstående plots??}
% From the Figure \ref{fig:permatrix}, \ref{fig:perhits} and \ref{fig:permisses} we can tell that the permutations that has N as the last index, is the ones that run the fastest. This makes sense because index K and M is the rows index's of the input matrices. The memory is build the way that it is faster to read rows than columns, which one can make a benefit of when indexing loops the right way.

% \begin{figure}[!th]
% \centering
% \begin{tikzpicture}[scale = 1]
% \begin{axis}[
% width=17cm, height=8cm,     % size of the image
% grid = major,
% grid style={dashed, gray!30},
% %xmode=log,log basis x=10,
% ymode=log,log basis y=10,
% %xmin=0,    % start the diagram at this x-coordinate
% %xmax=10, % end   the diagram at this x-coordinate
% %ymin=0,    % start the diagram at this y-coordinate
% %ymax=11000, % end   the diagram at this y-coordinate
% %/pgfplots/xtick={0,1,...,10}, % make steps of length 5
% %/pgfplots/ytick={0,1,...,10}, % make steps of length 5
% axis background/.style={fill=white},
% xlabel=CPU time [s],
% ylabel=Matrix size,
% legend style={at={(0.03,0.5)},anchor=center}]

% \addplot[mark=*,red] table [x=CPUsec, y=L2Misses, col sep=comma]{data/plotmnk.txt};
% \addlegendentry{matmult\_mnk()}

% \addplot[mark=*,blue] table [x=CPUsec, y=L2Misses, col sep=comma]{data/plotmkn.txt};
% \addlegendentry{matmult\_mkn()}

% \addplot[mark=*,green] table [x=CPUsec, y=L2Misses, col sep=comma]{data/plotnmk.txt};
% \addlegendentry{matmult\_nmk()}

% \addplot[mark=*,black] table [x=CPUsec, y=L2Misses, col sep=comma]{data/plotnkm.txt};
% \addlegendentry{matmult\_nkm()}

% \addplot[mark=*,magenta] table [x=CPUsec, y=L2Misses, col sep=comma]{data/plotkmn.txt};
% \addlegendentry{matmult\_kmn()}

% \addplot[mark=*,yellow] table [x=CPUsec, y=L2Misses, col sep=comma]{data/plotknm.txt};
% \addlegendentry{matmult\_knm()}

% \end{axis}
% \end{tikzpicture}
% \caption{Misses on permutations}
% \label{fig:permisses}
% \end{figure}

\newpage
\subsection{Blocking version}
As mentioned in the section on algorithms, the blocked version is based on \texttt{matmult\_mkn} because it is the best performing permutation of the \texttt{matmult\_nat} function, see figure \ref{fig:opt_nmk_1_non}.

Figure \ref{fig:blk_opti} illustrates a realization of the performance of \texttt{matmult\_blk} as a function of \texttt{bs} for $M=N=K=1500$. This problem size has been chosen for the experiment because three matrices of this size only fits in the L3 cache. Seeing as the blocking version performs operations on smaller blocks of the problem at a time, even though the full problem does not fit in the first two caches, the subproblems might fit and hence improve computation time. The red vertical lines in the figure illustrates the theoretical maximum blocking size for the matrices to fit in the memory of cache L1, L2, and L3 respectively. The maximum sizes are calculated in the theory section and are equal to those presented in table \ref{tab:cache_max_dim}.

Close to the boarder of L1, Mflops/s fluctuates a lot which can be caused by the hardware prefetching. Even though it is not visible on the logarithmic scale used for the blocking size, other experiments have shown that the drop in Mflops/s occurs just after the limit of L1. This is due to hardware prefetching and optimization of the compiler. The same behaviour is seen for L2. It is surprising that Mflops/s drops again well before the theoretical L3 limit, as we would have expected the drop to occur closer to the limit. The premature drop might be due to the implementation of blocking.

\begin{figure}[!th]
\centering
\begin{tikzpicture}[scale = 1]
\begin{axis}[
width=17cm, height=8cm,     % size of the image
grid = major,
grid style={dashed, gray!30},
xmode=log,log basis x=10,
%xmin=0,    % start the diagram at this
ymin=1300,    % start the diagram at this
ymax=1550, % end   the diagram at this
axis background/.style={fill=white},
ylabel=Mflops/s,
xlabel=block size,
legend pos=south east]

\addplot[mark=*, blue] table [x=mnk, y=mflops, col sep=comma]{data/matmult_blk_1500_bs.txt};
\addlegendentry{bs}

\addplot[red] table [x=x, y=y, col sep=comma]{data/L1_blk.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L2_blk.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L3_blk.txt};
\addlegendentry{L1, L2 and L3 cache}
\end{axis}
\end{tikzpicture}
\caption{Performance of the \texttt{matmult\_blk} function measured in Mflops/s as a function of block size.}
\label{fig:blk_opti}
\end{figure}

\begin{figure}[!th]
\centering
\begin{tikzpicture}[scale = 1]
\begin{axis}[
width=17cm, height=8cm,     % size of the image
grid = major,
grid style={dashed, gray!30},
xmode=log,log basis x=10,
xmin=0,    % start the diagram at this
ymin=800,    % start the diagram at this
ymax=1700, % end   the diagram at this
axis background/.style={fill=white},
ylabel=Mflops/s,
xlabel=Memory/kbytes,
legend pos=south east]

\addplot[mark=*, black] table [x=mem, y=mflops, col sep=comma]{data/matmult_blk_36_opt.txt};
\addlegendentry{blk\_36\_opt}
\addplot[mark=*, red] table [x=mem, y=mflops, col sep=comma]{data/matmult_blk_103_opt.txt};
\addlegendentry{blk\_103\_opt}
\addplot[mark=*, blue] table [x=mem, y=mflops, col sep=comma]{data/matmult_blk_1103_opt.txt};
\addlegendentry{blk\_1103\_opt}

\addplot[red] table [x=x, y=y, col sep=comma]{data/L1.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L2.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L3.txt};
%\addlegendentry{L1, L2 and L3 cache}
\end{axis}
\end{tikzpicture}
\caption{Performance of the \texttt{matmult\_blk\_opt} for different block sizes with compiler optimization.}
\label{fig:opt_blk_1}
\end{figure}

Figure \ref{fig:opt_blk_1} illustrates the three realizations of the  \texttt{matmult\_blk\_opt}. The three realizations illustrates that the choice of block size is depends on the problem size, e.g. the size of the matrix.

\begin{table}[!th]
\centering
\begin{tabular}{l|rr|rr|rr|rr|rr|rr}
\multirow{3}{*}{} & \multicolumn{4}{|c}{bs = 36}& \multicolumn{4}{|c}{bs = 103} & \multicolumn{4}{|c}{bs = 1103}\\
& \multicolumn{2}{|c}{L1 in $\%$} & \multicolumn{2}{|c}{L2 in $\%$}& \multicolumn{2}{|c}{L1 in $\%$} & \multicolumn{2}{|c}{L2 in $\%$}& \multicolumn{2}{|c}{L1 in $\%$} & \multicolumn{2}{|c}{L2 in $\%$} \\
& Hit& Miss& Hit& Miss& Hit& Miss& Hit& Miss & Hit & Miss & Hit & Miss\\ \hline
(50x50)&0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
(850x850)& 18458 & 26 & 26 & 1 & 17978 & 131 & 128 & 3 & 18418 & 10 & 10 & 0\\
(1600x1600)& 123003 & 176 & 170 & 6 & 119800 & 931 & 912 & 20 & 121882 & 125 & 42 & 81\\
\end{tabular}
\caption{Hits and misses of the L1 and L2 cache for blk, presented in millions}
\label{tab:L1L2hitmis_lib}
\end{table}

\newpage
\subsection{Comparison}
As can be seen in figure \ref{fig:compareperfor} below non of the implemented functions beat the CBLAS library function \texttt{cblas\_dgemm}. The optimized permutations MKN and KMN of \texttt{matmult\_nat} are the ones who come closest to \texttt{matmult\_lib}. The blocked version of MKN performs worse than expected since using a blocking size of $bs = 103$ it should be almost similar to \texttt{matmult\_mkn} for small problems, though of course 3 additional unnecessary outer loops are used (since the step size is the entire width of the matrices). It is possible that a different implementation of \texttt{matmult\_blk} would result in better performance, e.g. if the extracted blocks were not quadratic but rectangular instead – I.e. if the number of columns were less than the number of rows. The library function increases in performance as the problem size increases as it should, since the library function is optimized for large scale problems. As discussed earlier the non-library functions experience drops in Mflops/s later than expected as a result of prefetching which is most apparent for the optimized versions of \texttt{matmult\_mkn} and \texttt{matmult\_kmn}.

\begin{figure}[!th]
\centering
\begin{tikzpicture}[scale = 1]
\begin{axis}[
width=17cm, height=8cm,     % size of the image
grid = major,
grid style={dashed, gray!30},
xmode=log,log basis x=10,
%ymode=log,log basis y=10,
xmin=0,    % start the diagram at this x-coordinate
%xmax=10, % end   the diagram at this x-coordinate
ymin=0,    % start the diagram at this y-coordinate
ymax=10000, % end   the diagram at this y-coordinate
%/pgfplots/xtick={0,1,...,10}, % make steps of length 5
%/pgfplots/ytick={0,1,...,10}, % make steps of length 5
axis background/.style={fill=white},
ylabel=Mflops/s,
xlabel=Memory/kbytes,
legend pos=north west]

\addplot[mark=*, blue] table [x=mem, y=mflops, col sep=comma]{data/matmult_lib_opt.txt};
\addlegendentry{lib\_opt}

\addplot[mark=*, red] table [x=mem, y=mflops, col sep=comma]{data/matmult_nat_opt.txt};
\addlegendentry{nat\_opt}

\addplot[mark=*, black] table [x=mem, y=mflops, col sep=comma]{data/matmult_blk_103_opt.txt};
\addlegendentry{blk\_103\_opt}

\addplot[mark=*, orange] table [x=mem, y=mflops, col sep=comma]{data/matmult_mkn_opt.txt};
\addlegendentry{mkn\_opt}

\addplot[mark=*, magenta] table [x=mem, y=mflops, col sep=comma]{data/matmult_kmn_opt.txt};
\addlegendentry{kmn\_opt}

\addplot[red] table [x=x, y=y, col sep=comma]{data/L1.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L2.txt};
\addplot[red] table [x=x, y=y, col sep=comma]{data/L3.txt};
%\addlegendentry{L1, L2 and L3 cache}
\end{axis}
\end{tikzpicture}
\caption{Performance of the compiler optimized \texttt{matmult\_lib}, \texttt{matmult\_nat}, \texttt{matmult\_blk\_1103} and \texttt{matmult\_mkn} functions measured in Mflops/s as a function of kbytes.}
\label{fig:compareperfor}
\end{figure}

Though the advantage of the blocked version was not visible in figure \ref{fig:compareperfor} it seems to pay off when considering the number of cache hits in table \ref{tab:L1L2hitmis_compare}. I.e. the blocked version is the implementation with the highest number of cache hits in the L1 cache, while the naive implementation has the overall lowest number of hits and highest number of misses. The \texttt{analyzer} tool never managed to collect the number of cache misses and cache hits for any of the caches optimized or not for the library function, which is most likely caused by the implementation.

\begin{table}[!th]
\centering
\begin{tabular}{l|rr|rr|rr|rr}
\multirow{3}{*}{1600x1600} & \multicolumn{4}{|c}{Non-optimized}& \multicolumn{4}{|c}{Optimized}\\
& \multicolumn{2}{|c}{L1} & \multicolumn{2}{|c}{L2}& \multicolumn{2}{|c}{L1} & \multicolumn{2}{|c}{L2} \\
& Hit& Miss& Hit& Miss& Hit& Miss& Hit& Miss\\ \hline
\texttt{matmult\_lib\_opt}&0& 0& 0& 0 &0& 0& 0& 0\\
\texttt{matmult\_nat\_opt}&861503 & 34584 & 134 & 34445 & 73554 & 31511 & 93 & 3142\\
\texttt{matmult\_blk\_103\_opt}&1062545 & 1012 & 970 & 40 & 119800 & 931 & 912 & 20\\
\texttt{matmult\_mkn\_opt}&901464 & 262 & 246 & 16 & 22983 & 3790 & 3774 & 17\\
\texttt{matmult\_kmn\_opt}&901383 & 211 & 173 & 39 & 22943 & 3627 & 3521 & 105\\
\end{tabular}
\caption{Hits and miss of the L1 and L2 cache presented in millions}
\label{tab:L1L2hitmis_compare}
\end{table}

\newpage
\section{Conclusion}

The naive implementation of the \texttt{matmult\_nat} has successfully been implemented. It is possible to create six permutations of the naive structure. The permutations \texttt{matmult\_mkn} and \texttt{matmult\_kmn} has the best performance, compared to the four other permutations.

The performances of the implementation are lower than the performance of the \texttt{matmult\_lib} function which uses the DGEMM subroutine from the CBLAS library.

It is possible to achieve a huge increases in performance of the functions when using compiler optimization particularly for the \texttt{matmult\_mkn} function and the \texttt{matmult\_kmn} function.


\documentclass[12pt]{article}

\usepackage[margin=1.1in]{geometry}
\input{../../syllabi/preamble}

\newcommand{\coursedept}{Math}
\newcommand{\coursenumber}{342W}
\newcommand{\coursenumbercrosslisted}{/ 650.03~}
\newcommand{\semester}{Spring}
\newcommand{\numcredits}{6}
\newcommand{\lectimeandloc}{Mon and Wed 5-6:50PM / on zoom}
\newcommand{\requiredlabtimeandloc}{Required Lab Time / Loc 		& Thurs 9-10:50AM / on zoom \\}
\newcommand{\tataofficehourtimeandloc}{TA 	/ TA Office Hours / Loc 			& Tzipora Horowitz / Wed 6:55-7:55PM / on zoom}
\newcommand{\coursewebpageurl}{https://github.com/kapelner/QC_\coursedept_\coursenumber_\semester_\the\year}
\newcommand{\coursewebpagelink}{\href{\coursewebpageurl}{course homepage}}
\newcommand{\slackurl}{https://QC\coursedept\coursenumber\semester\the\year.slack.com/}
\newcommand{\slacklink}{\href{\slackurl}{slack}}
\newcommand{\numtheoryhws}{4--7}
\newcommand{\extrahwzero}{\item provide a link to your public repository on github (this means you need to sign up for github first)}
\newcommand{\hwzerodue}{Wednesday, Feb 3 11:59PM}
\newcommand{\lastdatetimetohandinhomeworks}{May 18 at noon}

\input{../../syllabi/_header}

\section*{Course Overview}

MATH 342W. Data Science via Machine Learning and Statistical Modeling. 6 hr.; 4 cr. Prereq.: MATH 241 (intro to probablity and statistics), MATH 231 (intro to linear algebra), CSCI 111 (intro to programming) or equivalents. Recommended: ECON 382 (intro to economentrics) or equivalent. Philosophy of modeling and learning using data. Prediction via the ordinary linear model including orthogonal projections, sum of squares identity, $R^2$ and RMSE. Polynomial and interaction regressions. Prediction with machine learning including neural nets (the perceptron), support vector machines and the tree methods CART, bagged trees and Random Forests. Probability estimation using logistic regression, asymmetric cost classifiers and the ROC / DET performance curves. Underfitting vs. overfitting and the bias-variance decomposition / tradeoff. Model validation including out of sample techniques such as cross validation and bootstrap validation. Correlation vs. causation, causal models, lurking variables and interpretations of linear model coefficients. Extrapolation. The \texttt{R} language will be taught formally from the ground and up (its use will be a substantial part of the homework) as well as visualization using the \texttt{ggplot} library and manipulation using the \texttt{dplyr} and \texttt{data.table} libraries. \pagebreak

You should be familiar with the following before entering the class:

\begin{itemize}
\itemsep -0.0em
\item Basic Probability Theory: conditional probability, in/dependence, identical distributedness
\item Modeling with discrete random variables: Bernoulli, Binomial%, Poisson, Geometric, Negative Binomial, Uniform Discrete and others
\item Expectation and variance
%\item Modeling with continuous random variables: Exponential, Uniform and Normal
%\item Frequentist confidence intervals and hypothesis testing for one-sample proportions
%\item Basic visualization of data: plots, histograms, bar charts
\item Linear algebra: Vectors, matrices, rank, transpose
\item Programming: basic data types, vectors, arrays, control flow (for, while, if, else), functions
\end{itemize}

\noindent We will review the above \textit{throughout the semester} when needed and we will do so rapidly. \\

\textbf{This is not your typical mathematics course.} This course will do lots of modeling of real-world situations using data via the \texttt{R} statistical language.

\section*{Course Materials}

We will be using many reference texts and three popular books which you will read portions from. However the main materials are the course notes. You should always supplement concepts from class by reading up on them online; \href{https://en.wikipedia.org}{wikipedia} I find the best for this.

\paragraph{Theory Reference:} It is not necessary to have these two books, but it is recommended. The first is \qu{Learning from Data: A Short Course} by Abu-Mostafa, Magdon-Ismael and Lin which can be purchased used on \href{https://www.amazon.com/Learning-Data-Yaser-S-Abu-Mostafa/dp/1600490069}{Amazon}. We will also be using portions from \qu{Deep Learning} by Goodfellow, Bengio and Courville that can be purchased on \href{https://www.amazon.com/Deep-Learning-Adaptive-Computation-Machine/dp/0262035618}{Amazon} and read for free at \url{http://www.deeplearningbook.org/}.

\paragraph{Popular Books:} We will also be reading the non-fiction novel \qu{The Signal and the Noise} by Nate Silver which can also be purchased on \href{https://www.amazon.com/Signal-Noise-Many-Predictions-Fail-but/dp/0143125087}{Amazon}. This is \textit{required} --- you will have homework questions directly from this book. We will also be reading \qu{Preditive Analytics, Data Mining and Big Data} by Steven Finlay that can be purchased on \href{https://www.amazon.com/Predictive-Analytics-Data-Mining-Misconceptions/dp/1349478687}{Amazon} and it is also available online from the \href{https://link-springer-com.queens.ezproxy.cuny.edu/book/10.1057%2F9781137379283}{Queens College library system}.

\paragraph{Computer Software:} You need your own personal computer, laptop preferred. We will be using \texttt{R} which is a free, open source statistical programming language and console available for all operating systems. Please download the latest version from: \url{http://cran.mirrors.hoobly.com/}. You will be expected to do programming. I recommend the IDE \texttt{RStudio} available for free at \url{https://www.rstudio.com/products/rstudio/download/}.

\paragraph{Source Control:} You will be expected to use \texttt{git} and have a \url{github.com} account with a repository named \texttt{QC\_MATH\_342}. You will use this repository to submit coding homework assignments (and theory assignments if you use \LaTeX).

\paragraph{Book on \texttt{R}:} We will be making some use of \qu{R for Data Science} by Wickham and Grolemund which can be purchased on \href{https://www.amazon.com/R-Data-Science-Hadley-Wickham/dp/1491910399}{Amazon} or read online at \url{http://r4ds.had.co.nz/}.

\input{../../syllabi/_the650section}

\input{../../syllabi/_announcements_on_slack}

\input{../../syllabi/_use_of_slack}

\section*{Class Meetings}

There are 42 scheduled meetings. Of these, 26 will be lectures, 10 will be labs, 2 will be midterm exams (see the schedule on page~\pageref{subsec:exam_schedule}) which are in class and 2 will be review periods before the exams, 1 will be a review lab and 1 will be help with the final paper. I am \inred{canceling} Monday, May 17 (the last meeting) due to a Jewish holiday. This meeting would have been for help on the final paper. We will decide when to have this session during finals week that fits the majority's schedule.

\subsection*{Lectures}

Lectures will be on zoom and will be split usually into two periods: theory and practice. The first is a standard \qu{chalkboard} lecture where we learn concepts and the second will be using the \qu{computer/projector} to see the concepts in action in the \texttt{R} language. %I have a no computer / tablet / phone policy during the theory component of the lectures (only pen / pencil and paper) but you are highly recommended to have the laptop during the second part.

\input{../../syllabi/_zoom_policies}

\input{_lecture_schedule}

\input{../../syllabi/_lecture_upload}

\subsection*{Labs}

Labs  will be on zoom during the Thursday morning meetings. Sometimes we will spend some of the two hours doing a practice lecture but the majority of this time will be your time. You will take turns \qu{driving} the coding in front of the class, working on exercises that you will finish for homework. Thus we will spend most a lot of time talking through problem solving skills in data science.

\section*{Homework}

Homework will be split into \textit{theory} and \textit{practice} (called \qu{labs}). This course will be the \qu{writing in the major course} next year. Thus, a portion of each theory and practice homework will involve writing \textit{English} and being graded on \textit{English}.

\subsection*{Theory Homework}

\input{../../syllabi/_theory_hws_text}
\input{../../syllabi/_theory_hws_submission_text}

\subsection*{Practice Homework (Labs)}

These will almost exclusively consist of short and medium coding exercises in \texttt{R}. Most of the assignment will be done for you and your peers during the Friday lab session.

\input{../../syllabi/_philosophy_hws}

\input{../../syllabi/_time_spent_hws}

\input{../../syllabi/_late_hw_policy}

\input{../../syllabi/_latex_hw_bonus_policy}

\input{../../syllabi/_hw_ec_policy}

\input{../../syllabi/_hw_0}

\section*{Writing Assignments}

There will be two writing assingments. (1) A \qu{philosophy of modeling} essay. Here you will coalesce the non-mathematical material that is crucial to this class. The purpose is to make you truly understand the modeling process and its limitations from start to finish. (2) A final project. Here you will use build a predictive model using a dataset. This is the capstone project for the entire data science and statistics major and it is where you will tie everything together.

This class will soon be the writing in the major course. Thus, writing is a major part of the curriculum herein.

\section*{Examinations}

\input{../../syllabi/_examination_text}

Since the is the capstone course, there is no final exam, but a large final project. There will be two midterm exams and the schedule is below.

\subsection*{Exam and Major Assignment Schedule}\label{subsec:exam_schedule}

\begin{itemize}
\itemsep -0.0em
\item Midterm examination I will be Thurs, March 24 in class with the first review session on the Wednesday prior
\item The philosophy of modeling paper's first draft is due Sunday, Mar 21 at 11:59PM
\item Midterm examination II will be Thurs, May 13 in class with a review on the Wednesday prior
\item The final project is due Sunday, May 23 11:59PM
\end{itemize}

\subsection*{Exam Policies and Materials}

\input{../../syllabi/_examination_policies}

I also allow \qu{cheat sheets} on examinations. For both midterms, you are allowed to bring \ingreen{two} 8.5'' $\times$ 11'' sheet of paper (front and back). \inred{Four sheets single sided are not allowed.} On this paper you can write anything you would like which you believe will help you on the exam. %For the final, you are allowed to bring three 8.5'' $\times$ 11'' sheet of paper (front and back). \inred{Six sheets single sided are not allowed.} I will be handing back the cheat sheets so you can reuse your midterm cheat sheets for the final if you wish.

\input{../../syllabi/_cheating_on_exams_and_missing_exams}
\input{../../syllabi/_special_services}

\input{../../syllabi/_class_participation}

\input{../../syllabi/_zoom_attendance}

\section*{Grading and Grading Policy}\label{sec:grading}

Your course grade will be calculated based on the percentages as follows:

\begin{table}[h]
\centering
\begin{tabular}{l|l}
Theory Homework & 9\% \\
Labs & 14\% \\
Midterm Examination I & 18\%\\
Midterm Examination II* & 18\%\\
Philosophy of Modeling Paper & 9\% \\
Final Project with Writeup & 22\% \\
Class participation & 5\% \\
Attendance & 5\%
\end{tabular}
\end{table}
\FloatBarrier

\noindent *The second midterm is not cumulative. It only covers material \textit{after} midterm I.

\input{../../syllabi/_advanced_course_grade_distribution}

\input{../../syllabi/_grade_checking_on_gradesly}

\input{../../syllabi/_auditing_policy}

\end{document}


%%%%%%%%%%%%%%%%%%%%
%%%%% Preamble %%%%%
%%%%%%%%%%%%%%%%%%%%

%This is a simple template for the LaTeX 'article' class document
\documentclass[]{article}%

%These are general packages that we will use quite a bit
%Once you begin using Latex more and more, you will learn what packages suit your needs the best and you can just copy and paste them into any new documents your are creating
\usepackage{amsmath}					%Standard math package built by the American Mathematical Society
\usepackage{amsfonts}					%Standard font package
\usepackage{amssymb}					%Extended symbol collection
\usepackage{amsthm}						%To use the AMS theorem collection
\usepackage{graphicx}					%For when you want to include images or figures into your document
\usepackage{url} 						%For when you want to include a url into your document
\usepackage[hidelinks]{hyperref} 		%For hyperlinking references (NOTE: can remove the [hidelinks] to show link boxes around hyperlinks
\usepackage[usenames,dvipsnames]{color} %This allows me to change the color of my font
\usepackage{comment}					%For block comments under figures and tables
\usepackage{fancyhdr}					%Allows for a fancy header and footer on your document
\usepackage{lastpage}					%Keeps count of what page number is the last page to be used in the footer
\usepackage{titling}					%Allows for image on title page
\usepackage{hologo}						%Allows for typsetting of the bibtex logo

%Set the margins of the paper using the geometry package
\usepackage[margin = 2cm]{geometry} %This creates a 2 cm margin around the page (NOTE: this does not include the header or footer in the 2 cm cutoff.  If you wanted to you could 										say \usepackage[margin = 2cm, includefoot, includehead]{geometry} which would make it so the header and footer start 2 cm down the page and 										the contents would follow appropriately)

%The following allows for the necessary documents to build the pdf to be embedded into the final pdf file.
%This allows for ease of sharing and automatically generates a back up in a sense in case you lose your .tex file.
%This isn't critical to include but it is nice to know about.
\usepackage{embedfile} 					%Allows embedding of files into the pdf document
\embedfile{MasterGuide.tex} 			%Embed the .tex file into the pdf
\embedfile{MyBib.bib}					%Embed the .bib file into the pdf
\embedfile{./Figs/cat.jpg}				%Embed the .jpg file into the pdf

%This is the standard setup for creating new commands
%The following commands are two that have been regularly throughout this document
%If you want to rename an already defined command, you must use \renewcommand instead
\newcommand{\del}{\partial}
\newcommand{\bs}{\textbackslash}
\newcommand{\TT}[1]{\texttt{#1}}
\newcommand{\tpc}{\textperiodcentered}

\definecolor{darkgrey}{gray}{0.3}
\newcommand{\greytext}[1]{{\textcolor{darkgrey}{#1}}}

\theoremstyle{definition}
\newtheorem{dfn}{Definition}

%Create the header for the paper
\pagestyle{fancy}				%Makes header in the style of 'fancy', look up LaTeX's fancyhdr to see how you can customize
\lhead{\today}			%Left head
\rhead{MEGC \LaTeX\ Workshop}	%Right head
\cfoot{Page \thepage\ of \pageref{LastPage}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Preamble Ends, Content Begins %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Includes figure on title page but needs the titling package to work
\pretitle{
	\begin{center}
		\LARGE
		\includegraphics[width=0.5\textwidth]{./Figs/MEGC_logo.png}\\[\bigskipamount]
	}
	\posttitle{\end{center}}

\begin{document}

	%%%%%%%%%%%%%%%%%
	%%%%% Title %%%%%
	%%%%%%%%%%%%%%%%%

	%The commands below define and make the titlepage
	\title{Learning \LaTeX\ with MEGC}
	\author{\vspace{1 in} \\University of Michigan
		\\Ann Arbor, Michigan \vspace{.25 in} \\by: \\Jeff Koller \vspace{1 in}
	}
	\date{October 1\textsuperscript{rd}, 2015}
	\maketitle				%Create title
	\thispagestyle{empty} 	%Remove header and footer.
	\clearpage				%Equivalent to a page break

	%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Introduction %%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%

	\section{Introduction}

	\subsection{Editors}
	There are many different \LaTeX\ editors out there.
	This website gives a good overview of the different editors, their benefits and disadvantages, and what operating systems they work on:
	\begin{center}
		\url{http://en.wikipedia.org/wiki/Comparison_of_TeX_editors}.
	\end{center}
	It is important to note that you can do all of your \LaTeX\ using online editors like ShareLaTeX, but you need an internet connection to do so.
	If you wish to work on your local machine (which is typically faster to compile than going through a cloud service) you may want to consider downloading an editor.
	When you download, you will receive a default editor called TeXworks.
	This is a bare bones editor and you could greatly benefit from an alternative.
	Some editors that MEGC members have used or have considered are the following:
	\begin{itemize}
		\item \textbf{Windows:} \url{https://www.youtube.com/watch?v=g6ez7sbaiWc}
		\begin{itemize}
			\item \href{http://www.texstudio.org/}{TeXstudio} $\leftarrow$ \emph{This is T.J.'s favorite editor}
			\item \href{http://www.xm1math.net/texmaker/}{Texmaker} $\leftarrow$ \emph{This is Jeff's favorite editor}
			\item \href{http://www.texniccenter.org/}{TeXnicCenter} $\leftarrow$ \emph{This editor is not our favorite, but is the most widely used on Windows OS}
		\end{itemize}
		\item \textbf{Mac OS:} \url{https://www.youtube.com/watch?v=5CNmIaRxS20}
		\begin{itemize}
			\item \href{http://pages.uoregon.edu/koch/texshop/}{TeXShop} $\leftarrow$ \emph{Sara's go to editor on Mac}
			\item \href{http://tacosw.com/latexian/download.php}{Latexian} $\leftarrow$ \emph{Does live previewing but somewhat out of date}
		\end{itemize}
		\item \textbf{Ubuntu (Linux):} \url{https://www.youtube.com/watch?v=g6ez7sbaiWc}
		\begin{itemize}
			\item \href{https://apps.ubuntu.com/cat/applications/texmaker/}{Texmaker}
			\item \href{https://apps.ubuntu.com/cat/applications/precise/gummi/}{Gummi} $\leftarrow$ \emph{Brandon's go to editor in Ubuntu and has live previewing}
			\item \href{https://apps.ubuntu.com/cat/applications/kile/}{Kile} $\leftarrow$ \emph{Sara's go to editor in Ubuntu}
			\item \href{https://www.gnu.org/software/auctex/}{AUCTeX} $\leftarrow$ \emph{Works with Emacs}
		\end{itemize}
	\end{itemize}

	\subsection{Syntax}
	\begin{itemize}
		\item Command Syntax
		\begin{itemize}
			\item For the most part, every command in \LaTeX\ can be described as \TT{\bs command[options]\{argument\}}.
		\end{itemize}
		%
		\item Environments
		\begin{itemize}
			\item Writing in \LaTeX\ can be thought of as writing in multiple different environments.
			You can have text environments, math environments, table environments, figure environments, and many many more.
			These environments can be nested and are typically marked by \TT{\bs begin\{\tpc\}} and \TT{\bs end\{\tpc\}} where what is inside of \TT{\{\tpc\}} denotes the environment.
			By default you are usually in the text environment when writing a document.
			%
			\item One exception to the \TT{\bs begin\{\tpc\}} and \TT{\bs end\{\tpc\}} markings is when you switch between text and math environments while keeping the math inline with the text.
			This switch is marked by \TT{\$\tpc\$}, but we will get to more of this in Section \ref{sec:math}.
		\end{itemize}
	\end{itemize}

	\subsection{Components to a \LaTeX\ Document}
	\begin{itemize}
		\item Preamble
		\begin{itemize}
			\item The preamble sets up the document by telling \LaTeX\ what document type you are working on, what the page layout should look like, what packages you plan to use, and allows you to define new commands.
			The preamble is everything before the \TT{\bs begin\{document\}} command.
		\end{itemize}
		\item Content
		\begin{itemize}
			\item The content of the \LaTeX\ document is everything between the \TT{\bs begin\{document\}} command and the \TT{\bs end\{document\}} command.
			This includes everything that you see in the finalized PDF document.
			By default the \TT{document} environment is a text environment.
			Everything you write between \TT{\bs begin\{document\}} and \TT{\bs end\{document\}} will be plain text unless an alternative environment is indicated.
		\end{itemize}
	\end{itemize}

	\subsection{Document Class}
	There are many different classes of documents you can write in \LaTeX.
	The document class can be thought of the style that you are writing in.
	The document class is defined at the top of the preamble (line 6 in this .tex file) and is generally denoted by \TT{\bs documentclass[options]\{class\}}.
	\begin{itemize}
		\item \TT{article} - for short documents such as a publication (this document)
		\item \TT{report} - for longer technical documents; like articles, but has chapters
		\item \TT{book} - for large documents, such as books
		\item \TT{letter} - for writing letters (has other special properties which have to be set)
	\end{itemize}
	A full list of document classes and class options can be found here: \url{https://en.wikibooks.org/wiki/LaTeX/Document_Structure#Document_classes}.

	\subsection{Packages}
	Packages are essentially libraries of commands that you are telling \LaTeX\ you will be using throughout the document.
	Packages may be needed for specific mathematical symbols, fonts, colors, the ability to insert figures, the ability to change document formatting, and much much more.
	An example of a package used in this document is the following:\\
	\begin{center}
		{\TT{\bs usepackage[margin = 2cm]\{geometry\}}}
	\end{center}
	The package \TT{geometry} allows for an easy way to customize the document margins.
	The option \TT{[margin = 2cm]} allows for a 2 centimeter margin all the way around the page without including the header or footer in that cutoff (ie the header and footer are within 2 centimeters of the paper's edge).
	We could change the option to \TT{[margin = 2cm, includefoot, includehead]} to make sure that the header and footer are outside of this margin cutoff as well.
	The option \TT{margin} can also be changed to \TT{top}, \TT{bottom}, \TT{left}, and \TT{right}.
	You can always look up a package name to figure out the specific options for that given package.
	Take a minute to try changing the options on the \TT{geometry} package in the preamble to see how it effects this document.
	%
	\clearpage

	%%%%%%%%%%%%%%%%%%%
	%%%%% Writing %%%%%
	%%%%%%%%%%%%%%%%%%%

	\section{General Writing with \LaTeX}
	In this section, we are mainly going to talk about general writing in \LaTeX.
	To write in \LaTeX, you can just start typing regularly like you would do in a normal text editor (e.g. Notepad in Windows; TextEdit in Mac).
	However, your document will be void of any formatting.
	Therefore, to make it easier to read, we must add in different types of formatting. \vspace{0.25cm}

	\noindent \textbf{\underline{Note:}} We suggest that you keep every sentence on its own separate line when writing.
	This way if your code fails to compile, you should get an error message saying which line caused the crash and you can easily debug the problem.

	\subsection{General Formatting}
	\begin{itemize}
		\item Basic text formatting
		\begin{itemize}
			\item For basic text formatting, we can include \textit{italics} with \TT{\bs textit\{\tpc\}}, \underline{underline} with \TT{\bs underline\{\tpc\}}, or \textbf{bold face} with \TT{\bs textit\{\tpc\}}.
			You can format text with \textit{\underline{\textbf{all three}}} by nesting the commands!
			\item The percent symbol (\%) is used to comment any lines out
		\end{itemize}
		\item Text color
		\begin{itemize}
			\item You can change the color of your text.
			For example, we can make our text \textcolor{red}{red}, \textcolor{blue}{blue}, \textcolor{yellow}{maize}, \textcolor{darkgrey}{dark grey}, etc.
			To change the colors of text though you must make sure you have the \TT{color} package included in your preamble.
			\item Information about the different colors, the packages needed, and how to create your own color can be found here: \url{https://en.wikibooks.org/wiki/LaTeX/Colors}.
		\end{itemize}
		\item Change font type
		\begin{itemize}
			\item You can easily change the font size in \LaTeX\ using commands.
			\begin{itemize}
				\item \tiny For example this text is tiny
				\item \LARGE For example this text is Large
			\end{itemize}
			We are not going to show you how to do much font changes in this presentation, as it is not the most vital.
			You can easily search online for how to do this (for a start, look here: \url{http://en.wikibooks.org/wiki/LaTeX/Fonts}.
			%		\item One font system that is already built into the packages we have above (i.e. \TT{amssymb}) is the font system for when we want to refer to the real number system.
			%		To do this, we use \TT{\$\bs mathbb\{R\}\$} to get $\mathbb{R}$.
			%		So for example we could say the following:
			%		\begin{center}
			%			$\pi \in \mathbb{R}$ and $\pi \notin \mathbb{Z}$
			%		\end{center}
			%		Note: We will talk more about what these \TT{\$\tpc\$} mean when we get to the math environment in Section \ref{sec:math}.
		\end{itemize}
		\item Starting new paragraph
		\begin{itemize}
			\item Leave a blank line between sentences $\leftarrow$ \emph{Would recommend most}
			\item Use the command \TT{\bs newline} followed by \TT{\bs indent} $\leftarrow$ \emph{Would avoid to keep .tex readable}
			\item Use the command \TT{\bs \bs} $\leftarrow$ \emph{Would avoid to keep .tex readable}
		\end{itemize}
	\end{itemize}

	\subsection{Creating List Environments}
	\LaTeX\ has different kinds of list environments, and you can even create your own (e.g. see the \TT{Resume\_Template} that we will give you at the end of the workshop).
	You can read more about the lists here: \url{http://en.wikibooks.org/wiki/LaTeX/List_Structures}.
	In general, the three standard list environments are the following:
	\begin{enumerate}
		\item \textbf{Enumerate} -- What was used for this list.
		\item \textbf{Itemize} -- Standard bulleted item list; can choose your own bullets and place them in brackets right after item.
		For example \TT{\bs item[\$\bs circ\$]} will give you this symbol $\circ$ as your bullet.
		\item \textbf{Description} -- More freedom to specify the item label.
	\end{enumerate}
	For a list, you must start with for example \TT{\bs begin\{enumerate\}} and end with \TT{\bs end\{enumerate\}} (or  \TT{\bs begin\{itemize\}}; \TT{\bs end\{itemize\}}, etc.).
	In between those two commands, you can have as many items as you would like.
	You must signify each item using the command \TT{\bs item}.

	\subsection{Labels and References}
	\label{sec:labelsreferences}
	One of the best parts of \LaTeX\ is the ease at which you can reference the different elements in your document, and \LaTeX\ will automatically number everything accordingly (if you have different preferences for numbering, you can make your own styling--but that is a little more advanced).
	Labels and references are commonly used for these elements:
	\begin{itemize}
		\item Sections in the document (e.g. a numbered subsection, a chapter, an appendix)
		\item Figure
		\item Equations
		\item Table
		\item Citation
	\end{itemize}
	Now look at the \LaTeX\ code and see what was typed just below \TT{\bs subsection\{Labels and References\}}. You should see a \TT{\bs label\{sec:labelsreferences\}}.
	This command assigned the label ``sec:labelsreference'' to this very subsection.
	Now if you want to refer to this section anywhere throughout the document, all you have to do is type the command \TT{\bs ref\{sec:labelsreferences\}} and \LaTeX\ will reference Section \ref{sec:labelsreferences}.
	There is a special reference command you can use for equations that will include parenthesis around the citation.
	The command is \TT{\bs eqref\{\tpc\}}.

	The same labeling technique can be used for equations, figures, tables, appendices, chapters etc.
	It is usually considered good practice to put a signifier in your label as to what you are actually labeling.
	For example, if you were to label an equation, such as the first equation in Section \ref{sec:equations}, you could call it ``eq:\dots''.
	This labeling convention just makes things easy to interpret when writing.
	So for the first equation in Section \ref{sec:equations}, we have labeled the equation \TT{\bs label\{eq:example\}}.
	Now we can refer to Eqn. \eqref{eq:example} by using the command \TT{\bs eqref\{eq:example\}}.

	The signifier for a table is usually \TT{tab:}, the signifier for a figure is usually \TT{fig:}, and the signifier for an equation is usually \texttt{eq:}.
	You can then create your own signifiers for everything else.
	We will discuss how to make references to citations in Section \ref{sec:bibliography}, but just as a heads up, instead of using the command \TT{\bs ref\{\tpc\}} we will use \TT{\bs cite\{\tpc\}} for bibliography citations.

	\paragraph{Practice your text skills by trying to replicate Exercise1.pdf}
	\clearpage

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Math Environment %%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	\section{Math environment}
	\label{sec:math}
	Another wonderful aspect of \LaTeX\ is its ability to seamlessly move from mathematical environments to text environments.
	When we are going into math environment, we are telling \LaTeX\ that we are now using math symbols.
	There are two ways to enter the math environment:
	\begin{enumerate}
		\item Inline with your other text.
		You enter in and out of the math environment using a set of dollar signs: \TT{\$\textit{math stuff}\$}.
		\item In a display style.
		For the display style you entering into a different environment (ie. equation, align, matrix, etc.) which assumes everything you are typing is a math symbol unless you specify otherwise.
		You will see more of this in Section \ref{sec:equations}.
	\end{enumerate}
	%
	Some other notes for the math environment:
	\begin{itemize}
		\item Spacing is done automatically unless forced with dedicated commands (\texttt{\bs quad}, \texttt{\bs vspace}, \texttt{\bs hspace}, etc...these are described in detail in Section \ref{sec:spacemanagement})
		\item Empty lines are not allowed so use comments (\%...) to clean things up.
		\item After entering a math environment, math fonts will be used unless the \texttt{\bs text\{\tpc\}} environment is invoked.
		\item As was stated above, to use the math environment in prose writing, you must separate whatever you are writing that is ``math'' by dollar signs (i.e. \TT{\$\textit{math stuff}\$}).
		Thus, if you want to write \emph{sigma} in line with your text, you should type \TT{\$\bs sigma\$} to get $\sigma$.
	\end{itemize}
	%
	\subsection{Basic Math Commands}
	Some basic math environment commands:
	\begin{itemize}
		\item \TT{\bs\bs} is used to end a line and start a new one
		\item The \textbf{ampersand} (\&) is usually used for alignment in the math environment.
		You will see this when we create arrays and equations with more than one line.
		\item $\_$ gives you a subscript.
		For example to get $x_2$, you type \TT{\$x\_2\$}.
		\item $ ^\wedge $ gives you a superscript.
		For example to get $x^2$, you type \TT{\$x$^\wedge$2\$}.
		\item \textbf{\TT{\{\tpc\}}} are used to include extra but necessary information for a command.
		For example, if you want to type $x_{i+1}$, you must put \TT{i+1} inside of \TT{\{\tpc\}}, otherwise the subscript only reads the \TT{i}.
		So to get $x_{i+1}$ you would type \TT{\$x\_\{i+1\}\$}.
		Without the \textbf{\TT{\{\tpc\}}} around the \TT{i+1}, it looks like $x_i+1$.
		\item \textbf{Greek symbols} are widely used in the math mode.
		Greek symbols are usually the name of the symbol with a \TT{\bs} at the beginning.
		For example, $\delta$ is typed as \TT{\$\bs delta\$}.
		For the upper-case version of the greek symbols, you usually just capitalize the first letter of the greek symbol command.
		Thus, for $\Delta$, we type \TT{\$\bs Delta\$}.
		For more on Greek symbols, look here: \url{http://en.wikibooks.org/wiki/LaTeX/Mathematics#List_of_Mathematical_Symbols}.
		\item A \textbf{fraction} command is another widely used math command.
		For a fraction, you will use the command \TT{\bs frac\{\}\{\}\$} where you must input two arguments.
		The first argument is the \emph{numerator} and the second argument is the \emph{denominator}.
		So for example, if you want $\frac{1}{2}$, you would type it as \TT{\$\bs frac\{1\}\{2\}\$}.
	\end{itemize}
	%
	\subsection{Equations}
	\label{sec:equations}
	%******************************* Equations ******************************************************
	One of the most attractive parts of \LaTeX\ is the ease and cleanliness when typing complicated equations.
	%
	Here is our basic equation presented using the align environment:
	\begin{align}
		\label{eq:example}
		\frac{\partial u}{\partial y} + C_1^2
	\end{align}
	%
	The align environment is the same as the equation environment shown below however we recommend always using align because it allows for added features:
	\begin{equation}
		\frac{\partial u}{\partial y} + C_1^2
	\end{equation}
	%
	Adding a star inside of the brackets of \TT{equation} (or any numbered environment for that matter) will take away the numbering associated with it.
	For example, if we begin our \TT{equation} environment with \TT{\bs begin\{environment*\}} we get the following:
	\begin{equation*}
		\frac{\partial u}{\partial y} + C_1^2
	\end{equation*}
	%
	Using the \TT{align} command, we can easily align parts of a multi-lined equation using \TT{\&} as the alignment object for each line (where you end a line of the equation using the standard \TT{\bs \bs}):
	\begin{align*}
		5 x^2 - 20x &= 20 \\
		5 x^2 - 20x -20 &= 0 \\
		x^2 - 4x -4 &= 0 \\
		(x-2)^2 &= 0 \\
		\Rightarrow x - 2 &= 0 \\
		x &= 2
	\end{align*}
	%
	Let's show another example of using alignment with our equations. Let $y=3$ then we can solve for $x$:
	\begin{align*}
		x &= y^3 + e^{y^2} - y + \log y \\
		& = (3)^3 + e^{3^2} - 3 + \log 3 \\
		& = 27 + e^9 - 3 + \log 3 \\
		& = 30 + e^9 - \log 3 \\
		& \approx 30 + 8103.082  - 1.099 \\
		& \approx \mathbf{8131.983}
	\end{align*}
	%
	We can also implement more complicated math symbols such as summation and integration.
	To demonstrate this, we will write out the definition to a definite integral:
	\begin{dfn}
		Let $f(x)$ be a function which is continuous on the closed interval $[a,b]$. The definite integral of $f(x)$ from $a$ to $b$ is
		\begin{align*}
			\int_a^b f(x)\,dx = \lim_{n\rightarrow \infty} \sum_{i=1}^n f(x_i) \Delta x
		\end{align*}
		where
		\begin{align*}
			\sum_{i=1}^n \Delta x_i
		\end{align*}
		is a Riemann sum of $f(x)$ on $[a,b]$.
	\end{dfn}
	\noindent It should be noted that using \TT{\bs,} right after \TT{f(x)} in the integral, gives the correct spacing between $f(x)$ and $dx$.

	You may also need to utilize the \TT{\bs displaystyle} command when your integrals or summations are inline. So for example:
	\begin{align}
		f(x) = \frac{\int_0^1g(x)\,dx}{\int_0^1h(x)\,dx}
	\end{align}
	Here is the same function but written using \TT{\bs displaystyle} before \TT{\bs int}:
	\begin{align}
		f(x) = \frac{\displaystyle\int_0^1g(x)\,dx}{\displaystyle\int_0^1h(x)\,dx}
	\end{align}
	If you ever need to write an integral, a fraction, or a summation inline, you can use \TT{\bs displaystyle} command.

	\subsection{Matrices}
	\label{sec:matrices}
	We can make a matrix by entering the \TT{matrix} environment.
	Here, we use \TT{\&} to separate items in the row of a matrix and we use \TT{\bs\bs} to create new rows.
	Here is the basic $3 \times 3$ identity matrix using the matrix environment \TT{\bs begin\{matrix\}} and \TT{\bs end\{matrix\}}:
	\begin{align*}
		\begin{matrix}
			1 & 0 & 0 \\
			0 & 1 & 0 \\
			0 & 0 & 1 \\
		\end{matrix}
	\end{align*}
	%%
	You can add brackets around it by typing \TT{\bs left[} and \TT{\bs right]} before and after you enter the \TT{matrix} environment, respectively:
	\begin{align*}
		\left[
		\begin{matrix}
			1 & 0 & 0 \\
			0 & 1 & 0 \\
			0 & 0 & 1 \\
		\end{matrix}
		\right]
	\end{align*}
	%
	You can also do the same thing as above but using the \TT{array} environment.
	This environment is a little more complicated than the matrix environment, but does give you more control when you get the hang of it.
	You can learn more about it here: \url{http://en.wikibooks.org/wiki/LaTeX/Mathematics#Matrices_and_arrays}.
	Here is an example of the same thing, but using the \TT{array} environment:
	\begin{align*}
		\begin{array}{ccc}
			1 & 0 & 0 \\
			0 & 1 & 0 \\
			0 & 0 & 1 \\
		\end{array}
	\end{align*}
	%
	We can also use the \TT{array} environment for certain types of equations:
	\begin{align}
		F(x) = \left\{
		\begin{array}{ll}
			0, & x \leq 0 \\
			\frac{\partial u}{\partial y} + C_1^2  & x > 0\\
		\end{array}
		\right.
	\end{align}
	Note here that using the command \TT{\bs left\{} puts a curly bracket to encompass the left side of the array, but when using \TT{\bs left}, you must also have a \TT{\bs right} command at some point. However, since we do not want a curly bracket on the right, we use \TT{\bs right.} (\TT{\bs right} with a period).
	%
	Using the \TT{\bs dfrac\{numerator\}\{denominator\}} command will make the fractions larger and easier to read
	\begin{align}
		\label{eq:exampledel}
		F(x) = \left\{
		\begin{array}{ll}
			0, & x \leq 0 \\
			\dfrac{\partial u}{\partial y} + C_1^2  & x > 0\\
		\end{array}
		\right.
	\end{align}

	\paragraph{Practice your math skills by attempting to replicate Exercise2.pdf}
	\clearpage

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Figures and Tables %%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	\section{Tables and Figures}
	\LaTeX\ has the ability to include nicely formatted tables and figures.
	These can sometimes be a pain to get in the right place because they are known in \LaTeX\ as floats.
	\LaTeX\ typically places floats where it deems best for them to go, but this typically isn't where you want them placed.
	You can constrain a float to be in the location it is typed in the code using the \texttt{[h]} command directly after the begin environment command (see code for the following table and figure for an example) but there are many other ways to place floats where you want them.
	A good description on how to control floats is listed here: \url{http://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat}.

	\label{sec:tablesfigures}
	\subsection{Tables}
	To create tables, we use the \TT{tabular} environment nested within a \TT{table} environment.
	It is important to note that most tables will begin with \TT{\bs begin\{tabular\}\{\tpc\}} and what is inside of the \TT{\{\tpc\}} helps preallocate the table itself.
	Take a look at the following table.

	\begin{table}[h]
		\begin{center}
			\begin{tabular}{l | l | l} \hline\hline
				\textbf{Greek symbol} & \textbf{Upper-case} & \textbf{Lower-case} 	\\ \hline
				Omega 	& \TT{\bs Omega} ($\Omega$) & \TT{\bs omega} ($\omega$) 	\\
				Beta 	& \text{N/A} 				& \TT{\bs beta} ($\beta$) 		\\
				Delta 	& \TT{\bs Delta} ($\Delta$) & \TT{\bs delta} ($\delta$) 	\\
				Sigma 	& \TT{\bs Sigma} ($\Sigma$) & \TT{\bs sigma} ($\sigma$) 	\\
				Alpha 	& \text{N/A} 				& \TT{\bs alpha} ($\alpha$) 	\\ \hline
			\end{tabular}
			\caption{Some Greek symbols and their corresponding commands.}
			\label{tab:greeksymbols}
		\end{center}
	\end{table}

	This table has three columns that are all left aligned.
	If you take a look at the \LaTeX\ code that generated the table you will see that it begins with \TT{\bs begin\{tabular\}\{l | l | l\}}.
	The \TT{l}'s in this preallocation define the alignment of each column.
	Preallocation can be defined with \TT{l}'s, \TT{c}'s, or \TT{r}'s for alignment.
	The \TT{|}'s that separates each \TT{l} tell \LaTeX\ to put a vertical line between each column.
	You should also notice that each row uses ampersands (\TT{\&}) to signify which content belongs in each column and \TT{\bs\bs} are used to signify when to start a new row (just like when creating matrices as explained in Section~\ref{sec:matrices}).
	Additionally, the command \TT{\bs hline} can be used to put in horizontal lines where needed.
	A really good overview of \LaTeX\ tables can be seen here: \url{http://en.wikibooks.org/wiki/LaTeX/Tables}.
	\clearpage

	\subsection{Figures}
	In order to insert figures into your \LaTeX\ documents you must include the \TT{graphicx} package.
	To begin a figure you will enter the \TT{figure} environment.
	Once in the \TT{figure} environment you can then include a figure using the \TT{\bs includegraphics\{\tpc\}} command where the figure name (or figure path relative to your \TT{.tex} file location and filename) are in the \TT{\{\tpc\}}.
	A common method of sizing an image is to size it relative to the paper's text width.
	An example of this is below.

	\begin{figure}[h]
		\begin{center}
			\includegraphics[width=0.5\textwidth]{./Figs/cat}
			\caption{Picture of an awesome cat.}
			\label{fig:awesomecat}
		\end{center}
	\end{figure}

	If you look at the \LaTeX\ code that generated this figure you will see that the figure width was set to $\frac{1}{2}$ the documents text width.
	Additionally a caption can be added to a figure using the \TT{\bs caption\{\tpc\}} command where the caption text is between the \TT{\{\tpc\}}.
	More information on figures can be found here: \url{https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions}.

	\LaTeX\ also has the ability for you to draw your own pictures.
	We are not going to go into that for this presentation.
	But if you want to learn more about it, you can find out more here: \url{http://en.wikibooks.org/wiki/LaTeX/PGF/TikZ}

	\paragraph{Practice your figure and table skills by trying to replicate Exercise3.pdf}
	\clearpage

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Space Management %%%%%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	\section{Space Management}
	\label{sec:spacemanagement}
	A great freedom (and curse) of \LaTeX\ is that you have free range to change mostly any spacing on the page.
	Sometimes you need to nudge something up, down, left, or right.
	The following section will discuss how to do this.

	\subsection{Units}
	\noindent Common units used in \LaTeX:
	\begin{itemize}
		\item pt - point (1 in = 72.27 pt)
		\item in - inch (1 in = 25.4 mm)
		\item cm - centimeter (1 cm = 10 mm)
		\item mm - millimeter
		\item em - roughly the width of an 'M' (uppercase) in the current font
		\item ex - roughly the height of an 'x' in the current font
	\end{itemize}

	\noindent Useful \LaTeX\ measurements to reference:
	\begin{itemize}
		\item \TT{\bs textwidth} - The width of the text on the page (margin to margin)
		\item \TT{\bs textheight} - The height of the text on the page (margin to margin)
	\end{itemize}

	\subsection{Creating Whitespace}
	The following are commonly used commands for creating whitespace.

	\begin{itemize}
		\item \TT{\bs noindent} - Removes indent at the beginning of a new paragraph
		\item \TT{\bs} - About a single spacebar of spacing
		\item \TT{\bs quad} - About the spacing of `M'
		\item \TT{\bs qquad} - Add the spacing of `MM'
		\item \TT{\bs vspace\{\tpc\}} and \TT{vspace*\{\tpc\}} - Add vertical space of \TT{\{\tpc\}} units(*indicates manual override)
		\item \TT{\bs hspace\{\tpc\}} and \TT{hspace*\{\tpc\}} - Add horizontal space of \TT{\{\tpc\}} units (*indicates manual override)
		\item \TT{\bs vfill} - Adds whitespace to fill the page vertically
		\item \TT{\bs hfill} - Adds whitespace to fill the page horizontally
	\end{itemize}

	\subsection{Minipages}
	Sometimes you want to section out a page into subsections of space making it easier to manipulate sets of objects or place objects relative to one another.
	This can be difficult to do traditionally in \LaTeX\ but minipages can be used to make it easier.
	Minipages is somewhat analogous to axes in MATLAB.
	The general syntax for minpages is as follows:
	\begin{center}
		\TT{\bs begin\{minipage\}[pos][height][contentpos]\{width\}}\\
	\end{center}
	Minipages are a somewhat advanced feature and we will not go into too much detail on the topic here yet they are used quite a bit in the \TT{Slides\_Template.tex} file that will be shared with you after the workshop.

	\paragraph{Practice your whitespace management by trying to replicate Exercise4.pdf}
	\clearpage

	\section{Custom commands}
	\label{sec:customcommands}
	When using commands that can be tedious to type (e.g. \TT{\bs partial}), we can create new command handles that are easier to type out.
	For this one, we created a command such that \TT{\bs del} also gives us the same symbol as \TT{\bs partial}.
	At the beginning of the document, you can see where we typed this:
	\begin{center}
		\TT{\bs newcommand\{\bs del\}\{\bs partial\}} \\
	\end{center}
	So now we can retype Eqn. \eqref{eq:exampledel} using \TT{\bs del} instead of \TT{\bs partial} and we get the same result:
	\begin{align*}
		F(x) = \left\{
		\begin{array}{ll}
			0, & x \leq 0 \\
			\dfrac{\del u}{\del y} + C_1^2  & x > 0\\
		\end{array}
		\right.
	\end{align*}
	\vspace{1cm}

	\begin{center}
		\textbf{Can you find the other custom commands that we made in this document?}
	\end{center}

	\clearpage
	\section{Bibliographies \& BibTeX}
	\label{sec:bibliography}
	As we have seen in the previous sections, \LaTeX\ is very very good at keeping track of the numbering of labels and compiling them in the proper order.
	This becomes EXTREMELY useful when it comes to creating a bibliography.
	In short, with the use of \hologo{BibTeX} a handle can be assigned to each reference.
	When you would like to cite the reference in writing all you need to do is use the command \TT{\bs cite\{\tpc\}}.
	\LaTeX\ will take care of the rest!

	\subsection{Using \hologo{BibTeX}}
	Creating bibliographies in \LaTeX\ is easy using \hologo{BibTeX}.
	\hologo{BibTeX} is awesome and a simple extension of \LaTeX\ \cite{MyRef0}.

	\begin{itemize}
		\item Information for references is stored in a separate \TT{.bib} file.
		\item For each reference the \TT{.bib} file will include an entry with your label for that reference and other relevant citation information such as author, title, journal, year, publisher, etc...
		\item These \TT{.bib} entries can usually be downloaded directly from the documents online source (eg., Google Scholar, Web of Knowledge, Elsevier, etc...) or generated with a third party literature management software such as Mendeley.
		\item Once the \TT{.bib} file is populated with reference entries and compiled, a reference can be cited in the text using \TT{\bs cite\{ReferenceLabel\}}.
		This will automatically add the reference to the bibliography and keep things in the order defined by the bibliography style being used.
		\item At the end of your document you will define the bibliography style using the command \TT{\bs bibliographystyle\{\tpc\}} and then point \LaTeX\ to your BibTex file name using the command \TT{\bs bibliography\{Filename.bib\}}
		\begin{itemize}
			\item There are many different bibliography styles and many journals will have their own style defined which you can just insert in your manuscript writing.
			Here we are using IEEE Transactions which is a standard built in style in \LaTeX.
		\end{itemize}
	\end{itemize}
	Take a look at the code that generated the citation at the beginning of this subsection and the corresponding \hologo{BibTeX} file to see how it was done.

	\subsection{Creating a \hologo{BibTeX} File and Entry Content}
	Creating a \TT{.bib} file is super easy.
	All you need to do is open a new file in \LaTeX, include the references, and save it as a \TT{.bib}.
	There is no limit to how many references you put in your \TT{.bib} file and only the ones that are cited will show up in the reference section.
	There is no need for a document class definition at the top of your \TT{.bib} file or any \TT{\bs begin\{\tpc\}} or \TT{\bs end\{\tpc\}} statements necessary.
	Like stated above, you can easily pull \hologo{BibTeX} citations from Google Scholar, but it is important to understand the content of a \hologo{BibTeX} entry.
	A sample reference entry for the \TT{.bib} file is shown below.
	\begin{verbatim}
	@article{hawking1982,
	title={The development of irregularities in a single bubble inflationary universe},
	author={Hawking, Stephen W},
	journal={Physics Letters B},
	volume={115},
	number={4},
	pages={295--297},
	year={1982},
	publisher={Elsevier}
	}
	\end{verbatim}
	More information on different types of entries can be found here: \url{https://en.wikipedia.org/wiki/BibTeX}

	\paragraph{Practice your \hologo{BibTeX} skills by trying to replicate Exercise5.pdf}

	\clearpage
	\section{Other Random Resources}
	\begin{itemize}
		\item Templates - Can pull pre-generated style and class files for a specific document type (\url{http://www.latextemplates.com/})
		\item Mathematica - Can copy inputs and outputs as \LaTeX\, great for large tables and matrices
		\item Detexify - Can draw symbols and it will output the \LaTeX\ command (\url{http://detexify.kirelabs.org/classify.html})
		\item Codecogs  - Real time typing of \LaTeX\ math equations with a helpful user interface (\url{https://www.codecogs.com/latex/eqneditor.php})
	\end{itemize}

	\clearpage
	\bibliographystyle{ieeetr}
	\bibliography{MyBib}
\end{document}


% !TeX root = ../bd-screen.tex

\chapter{Preface}

This is an introductory textbook on modal logic. I use it as the main
text when I teach Philosophy~579.2 (Modal Logic) at the University of
Calgary. It is based on material from the
\href{https://openlogicproject.org}{Open Logic Project}.

The main text assumes familiarity with some elementary set theory and
the basics of (propositional) logic. This material is part of a
prerequisite for my course, Logic~II. The textbook for that course,
\href{https://slc.openlogicproject.org}{\emph{Sets, Logic,
Computation}}, is also based on the OLP, and so is available for free.
The required material is included as appendices in this book,
however. I assign these appendices for background reading whenever I
teach the material.

\Cref{nml:::part} is originally based in part on Aldo Antonelli's
lecture notes on ``Classical Correspondence Theory for Basic Modal
Logic,'' which he contributed to the OLP before his untimely death in
2015. I heavily revised and expanded these notes, e.g., the material
on frame definability and tableaux is new.