% Lecture notes - Lecture 9 - 10/06/2003
% Alexandru Caracas

\section{Execution Time Bounds For The {\em Cilk} Scheduler}
The {\em Cilk} work-stealing scheduling algorithm has provably good execution
time bounds. We introduce an accounting argument based on {\bf two buckets} to
help in proving the bound.

\begin{theorem}[Cilk Scheduler execution time bound]
  Consider the execution of any fully strict multithreaded computation with work
  $T_1$ and critical path $T_\infty$ by {\em Cilk}'s work-stealing algorithm on
  a parallel computer with $P$ processors. For any $\varepsilon>0$, with
  probability at least $1-\varepsilon$, the execution time on $P$ processors is
  \begin{equation}
    T_P \leq \frac{T_1}{P} + O\left( T_\infty + \lg \left( \frac{1}{\varepsilon}
      \right) \right) .
  \end{equation}
\end{theorem}

\begin{proof-idea}
  The proof is based on an {\bf accounting argument}. We have 2 buckets, one for
  {\bf work} and one for {\bf steal}. On each time-step, a processor puts one
  dollar into one of the two buckets, depending on the action that it performs
  in that step. The possible two actions and their respective results are:
  \begin{itemize}
  \item execute instruction -- one dollar into {\bf work} bucket,
  \item steal attempt -- one dollar into {\bf steal} bucket.
  \end{itemize}
  We assume that all threads are of equal size and that all processors have the
  same time-step. In the following we overload the notation of steal attempt
  with steal.
\begin{figure}
  \centering \includegraphics[scale=0.5]{9/lecture-9.2-fig0.eps}
  \caption{The {\bf work} and {\bf steal} buckets used in the accounting argument.}
  \label{fig:buckets}
\end{figure}
\end{proof-idea}

\begin{lemma}[Work Bucket]
  At the end of computation, the number of dollars in the {\bf work} bucket is
  $T_1$.
\end{lemma}
\begin{proof}
  The total work is exactly $T_1$ so at the end of the computation exactly $T_1$
  work has been executed. Thus the number of dollars in the {\bf work} bucket is
  precisely $T_1$.
\end{proof}

We know bound the steals with the following lemma.

\begin{lemma}[Steal Bucket]
  At the end of a computation the number of dollars in the {\bf steal} bucket is
  $O(P T_{\infty})$, with {\bf high probability}.
\end{lemma}

We give an exact definition of {\bf high probability} during the course of the
proof.  In the rest of the lecture, we prove the previous lemma.  We introduce
several concepts to prove the bound.

\begin{observation}[Dollars]
  Per time-step $P$ dollars enter the buckets.
\end{observation}

Thus, the running time on $P$ processors is the amount of time it takes to fill
out all the buckets.

\begin{claim}
  The running time on $P$ processors is less than the total number of dollars
  (actions) divided by the number of processors:
  \begin{eqnarray}
    T_P & \leq & \frac{\mbox{\rm \#dollars in work bucket} + 
      \mbox{\rm \#dollars in steal  bucket}}{P} \nonumber \\
    & \leq & \frac{T_1}{P} + O(T_\infty).
    \label{eqn:scheduler}
  \end{eqnarray}
\end{claim}

We know how many dollars will be in the work bucket. We need a bound for the
number of dollars in the steal bucket.

\section{Performance Analysis of Scheduling Algorithm Using Delay Sequences}
In this section, we analyze the performance of the {\em Cilk} work-stealing
scheduling algorithm. We define \defn{delay sequences} and use them to prove the
execution time bounds of the {\em Cilk } scheduler.

We prove equation (\ref{eqn:scheduler}) in the rest of the lecture.

\begin{observation}
  There is no constant in front of the term $T_1 / P$ in the previous bound. All
  scheduling overhead cost is amortized against the critical path $T_\infty$.
  Also, one important observation is that the term $T_1 / P$ is much bigger than
  the term $T_\infty$.
\end{observation}

\subsection{Rounds} 
As long as no processor is stealing, the computation is progressing efficiently.
What divide the computation into {\bf rounds}, which (more or less) contain the
same number of steals (see Figure~\ref{fig:rounds}).

\begin{figure}
  \centering \includegraphics[scale=0.5]{9/lecture-9.2-fig1.eps}
  \caption{Dividing computation into steal rounds.}
  \label{fig:rounds}
\end{figure}

\begin{definition}[Round]
  A \defn{round} of work stealing attempts is a set of at least $P$ and at most
  $2P-1$ steal attempts. We assume that steal attempts complete in one unit of
  time.
\end{definition}
Round 1 begins at time 0 and ends when there have been at least $P$ steals. In
general, round $i$ begins when round $i-1$ ends, and round $i$ ends when there
have been at least $P$ steals.

Recall by Definition~\ref{def:critthread} that a {\bf critical thread} is at the
top of some deque or is being executed.

\begin{proof-idea}
  The main idea is that, if a thread is on the top of a deque for $CP$ steal
  attempts, then the probability that it will not be executed for $C$ rounds is
  very small, namely:
  \[ \left( 1 - \frac{1}{P} \right)^{C P} \leq e^{-C}.\]
\end{proof-idea}


The idea is that a thread is unlikely to be critical for many rounds. Also, a
thread may be ready for many rounds but not critical.

\begin{theorem}
  The probability that a critical thread is un-stolen for $C$ rounds is:
  \begin{equation}
    \left( 1 - \frac{1}{P} \right)^{C P} \leq e^{-C}.
  \end{equation}
\end{theorem}
\begin{proof}
  For $C$ rounds, there are at least $CP$ steals. The steal process is done at
  random and there are $P$ places that a processor could steal from. Hence, the
  probability that a critical thread would be stolen during a round is $1/P$,
  and the probability that the critical thread will not be stolen is $1-1/P$.
  Hence, for $C$ rounds we obtain the probability that a critical thread is not
  stolen:
  \[ \left( 1 - \frac{1}{P} \right)^{C P}.\]
  For a simplified bound of the above equation we use Euler's inequality:
  \[  \left( 1 + \frac{1}{x} \right)^x \leq e.\]
  We obtain \[\left( 1 - \frac{1}{P} \right)^{C P} \leq e^{-C}.\]
\end{proof}

For a small number of rounds a thread might not be executed. However, for a
large number of rounds a thread will be executed with very high probability,
because the term $e^{-C}$ in the previous bound becomes exponentially small.  If
a thread is at the top of a deck then it has no un-executed predecessors in the
augmented DAG $G\prime$. Intuitively we would expect that in every time-step we
are able to peal some threads from the augmented DAG such that in time $O(P
T_\infty)$ there would be no augment DAG left (see Figure~\ref{fig:augdag}).
This idea is expressed in the following lemma and explanation.

\begin{figure}
  \centering \includegraphics[scale=0.4]{9/lecture-9.2-fig2.eps}
  \caption{A directed path $U=(u_1, u_2 , \ldots , u_L)$ in the augmented DAG $G\prime$.}
  \label{fig:augdag}
\end{figure}

\subsection{Delay Sequence Argument}

\begin{lemma}[Structural Lemma]
  There exists a directed path $U=(u_1, u_2 , \ldots , u_L)$ in $G'$ in which,
  during every time-step of the computation, some thread in $U$ is critical.
\end{lemma}

\begin{proof}
  The proof is by induction backwards in time.
  
  {\bf Base case}: During last time-step $t$, last thread in $G'$ is $u_L$ and
  it is critical and executed (see Figure~\ref{fig:augdag}).
  
  {\bf Induction step}: Suppose during time-step $t$, thread $u_j$ is critical.
  During time-step $t-1$, either $u_j$ is also critical, or if not it is because
  it has un-executed predecessors in $G\prime$. Some threads $u_i, {u_i}\prime,
  \ldots \prec_{G\prime} u_j$ are being executed during t-1. One of them is
  minimal and is critical.
  
  {\bf Main idea}: We prove that since the probability that any thread remains
  critical for a long period of time is small, there cannot be too many steal
  attempts.
\end{proof}

\begin{observation}
  An executing thread is not necessarily critical. The very last thread in the
  computation $u_L$ when it is being executed it is critical. The extra edges in
  the augmented DAG introduce additional ordering (see \figref{baddag}).
\end{observation}

In order to complete the proof for the execution time bound of the {\em Cilk}
scheduler, we introduce some definitions and lemmas.

\begin{definition}[Delay Sequence]
  A \defn{delay sequence} is an event described by the triple $(U,R,\Pi)$ where:
  \begin{itemize}
  \item $U=(u_1, u_2 , \ldots , u_L)$ is a directed path in $G'$ from {\bf
      source} of DAG to {\bf sink},
  \item $R>0$ is an integer,
  \item $\Pi=(\pi_1,\pi_2, \ldots , \pi_L)$ is a partition of R, namely
    $\sum_i{\pi_i}=R$
  \end{itemize}
  A \defn{delay sequence} occurs if $\forall i$ at least $\pi_i$ rounds occur
  while $u_i$ is critical and un-executed.
\end{definition}

A \defn{delay sequence} is a combinatorial object. A \defn{delay sequence}
occurs if there is a directed path in $G\prime$ in which one of the threads in
the path is critical at each time-step.

\begin{lemma}
  If at least $2P(2T_{\infty}+R)$ steal attempts occur, then some $(U,R,\Pi)$
  delay sequence occurs.
\end{lemma}

\begin{proof}
  Consider a path $U=(u_1, u_2 , \ldots , u_L)$ for which during each time-step
  $t$ some thread is critical. There are at most $2T_{\infty}$ rounds during
  which a thread in $U$ executes, since $L \leq 2T_{\infty}$. For all the other
  $R$ rounds, some thread is critical and not executed.
  
  \noindent {\bf Note}: The converse need not be true.
\end{proof}

The idea is to sum up over all possible events the probability that the event
occurs. Show that the sum is small. We would then be able to show that with {\em
  high probability} there at most $2P(2T_{\infty}+R)$ steal attempts.

A \defn{delay sequence} is an {\bf event} that explains why there are at least
$2P(2T_{\infty}+R)$ steal attempts. The probability that at least
$2P(2T_{\infty}+R)$ steal attempts occur is at most the probability that there
exists a delay sequence $(U,R,\Pi)$ that occurs.

\begin{observation}[Number of Steal Attempts]
  We make the following observation about the probability that at least
  $2P(2T_{\infty}+R)$ steal attempts occur.
  \begin{eqnarray*}
    \Pr {\column{\mbox{\rm at least}\ 2P(2T_{\infty}+R)}{\mbox{\rm steal
          attempts occur}}} 
    & \leq & 
      \Pr {\column{\mbox{\rm exists delay sequence}}{(U,R,\Pi)\, \mbox{\rm that occurs}}} \\
    & \leq & \sum_{\column{\mbox{\rm delay sequences}}{(U,R,\Pi)}}{\Pr{(U,R,\Pi)\,
      \mbox{\rm occurs}}} \\
    & \leq & \left(\column{\mbox{\rm Number of}}{\mbox{\rm Delay Sequences}}\right) 
    \left(\columnthree{\mbox{\rm Maximum Probability}}{\mbox{\rm that any
      delay}} {\mbox{\rm sequence occurs}}\right).
  \end{eqnarray*}
\end{observation}

\begin{lemma}[Probability of Delay Sequence]
  \label{lem:probdelay}
  The probability that a particular delay sequence $(U,R,\Pi)$ occurs is
  $e^{-R}$, which is exponentially small in the number of rounds, $R$.
\end{lemma}
\begin{proof}
  \begin{eqnarray*}
    \Pr {(U,R,\Pi)\, \mbox{\rm occurs}} & = & \prod_{1 \leq i \leq L} 
    {\Pr {\column{\pi_i\, \mbox{\rm rounds occur}}{\mbox{\rm while}\ u_i\,
    \mbox{\rm is critical}}}}\\
    & \leq & \prod_{1 \leq i \leq L}{e^{-\pi_i}}\\
    & = & e^{-R}.
  \end{eqnarray*}
\end{proof}

\begin{lemma}[Number of Delay Sequences]
  \label{lem:numdelay}
  The number of delay sequences is at most \[ 2^{2T_{\infty}}
  \left(\column{2T_{\infty}+R}{2T_{\infty}}\right) .\]
\end{lemma}
\begin{proof}
  We have
  \[\mbox{\rm \#paths} \leq 2^{2T_{\infty}},\]
  because no path is longer that $2T_\infty$ and the out degree of any thread
  (vertex) is at most two. Thus, there are two choices (either a continuation
  edge or a spawn edge) starting from the root node.  Also
  \[ \column{\mbox{\rm \#partitions of a path}}{\mbox{\rm in augmented DAG}\
    G\prime} = \left(\column{2T_{\infty}+R}{2T_{\infty}}\right).\] We are
  partitioning $R$ into $L$ pieces. However, for large $R$, this is smaller than
  partitioning $R$ into $2T_\infty$ pieces. We simply count up all the possible
  ways of partitioning the path. Thus, by multiplying the number of paths with
  the number of ways in which a path can be partitioned we obtain the number of
  delay sequences, which is at most
  \[ 2^{2T_{\infty}} \left(\column{2T_{\infty}+R}{2T_{\infty}}\right) .\]
\end{proof}  

Our goal is to show that the probability that any delay sequence occurs is very
small. Since we know the probability of a given delay sequence we use the {\em
  Boole's inequality} to compute the probability of any delay sequence. Recall
that:
  \[ \Pr {A \lor B \lor C \lor \cdots } \leq \Pr{A} + \Pr{B} + \Pr{C} + \cdots.\]
  Following from Lemma~\ref{lem:probdelay} and Lemma~\ref{lem:numdelay} we have
  that:
\begin{eqnarray*}
  \Pr {\mbox{\rm any}\ (U,R,\Pi)\, \mbox{\rm occurs}} 
  & \leq & \left(\column{\mbox{\rm Number of}}{\mbox{\rm Delay Sequences}}\right) 
  \left(\columnthree{\mbox{\rm Maximum Probability}}{\mbox{\rm that any delay}}
    {\mbox{\rm sequence occurs}}\right)\\
  & \leq & 2^{2T_{\infty}}  \left(\column{2T_{\infty}+R}{2T_{\infty}}\right)
   e^{-R}
\end{eqnarray*}
Next, we use the following ``death-bed'' formula to simplify the above
inequality.
\begin{equation*}
  \left(\frac{y}{x}\right)^x \leq \left(\column{y}{x}\right) 
  \leq \left(\frac{e y}{x}\right)^x.
\end{equation*}
We have:
\begin{eqnarray*}
  \Pr {\mbox{\rm any}\ (U,R,\Pi)\, \mbox{\rm occurs}} & \leq & 2^{2T_\infty} \left(\frac{e
    (2T_{\infty}+R)} {2T_{\infty}}\right)^{2T_\infty} e^{-R} \\
  & \leq & \left(\frac{2e (2T_{\infty}+R)}{2T_{\infty}}\right)^{2T_\infty} e^{-R}
\end{eqnarray*}
In the right part of the inequality, the first term is considerably large, while
the second term is exponentially small in $R$.  We want to choose a value for
$R$ such that the product of the two terms is small. We let $R=2 C T_{\infty}$
where $C$ is a constant. Replacing in the previous equation we have:
\begin{equation}
  \Pr {\mbox{\rm any}\ (U,R,\Pi)\, \mbox{\rm occurs}} \leq \left(
  \frac{[2e(C+1)]^{1/C}}{e} \right)^R.
\end{equation}

\begin{definition}[High Probability]
  An event $E$ occurs with \defn{high probability} if the probability that the
  event occurs that is $\Pr {E} \geq 1 - P(n)$, where $P(n)$ is a polynomial.
  That is the probability that the event does not occur is polynomially small.
\end{definition}

\begin{observation}
  When $C \geq 4$, probability decreases exponentially in $R$.
\end{observation}

\begin{example}
  If we take $C=4$ then the probability is less or equal to $0.84^R$.
\end{example}

Let: \[ \varepsilon = \left( \frac{[2e(1+C)]^{1/C}}{e} \right)^R .\]

Then $\Pr {\column{\geq 2P(2T_\infty + R)}{\mbox{\rm steal attempts occur}}}
\leq \varepsilon$, when $R=\Omega(\lg\left(\frac{1}{\varepsilon}\right)).$

\section{Wrap-Up}
We have shown that with {\bf high probability} the number of steal attempts is
small.  We have come up with an event which explains why there are steal
attempts. This event is a {\bf delay sequence}. Next we counted the number of
events. We computed the probability that such an event occurs. By summing up all
the possible events we have shown that with {\bf high probability} there are few
steal attempts. With probability at least $1-\varepsilon$ the number of steal
attempts is $O(P \left( T_\infty + \lg\left(\frac{1}{\varepsilon} \right)
\right))$.

We have started our analysis using an {\bf accounting argument}, based on a {\bf
  work} bucket and a {\bf steal} bucket.We have shown that at the end of the
computation there are $T_1$ dollars in the {\bf work} bucket and with
probability at least $1 - \varepsilon$ there are $O(P \left( T_\infty +
  \lg\left(\frac{1}{\varepsilon} \right) \right))$ dollars in the {\bf steal}
bucket. Thus, with probability at least $1 - \varepsilon$:
\[ T_P \leq \frac{T_1}{P} + O\left( T_\infty + \lg \left( \frac{1}{\varepsilon}
  \right) \right) .\] 

\subsection{Death-Bed Formulae}

Figure~\ref{fig:death-bed} shows a list of useful (``death-bed'') formulae that
were used in the analysis of the {\em Cilk} scheduler.

\begin{figure}[here!]
  Euler's inequality:
  \begin{equation}
    \left(1+\frac{1}{x}\right)^x \leq e \leq \left(1+\frac{1}{x}\right)^{x+1},
  \end{equation}
  \begin{equation}
    \left(1-\frac{1}{x}\right)^x \leq \frac{1}{e} \leq \left(1-\frac{1}{x}\right)^{x-1}.
  \end{equation}
  Boole's inequality (Union bound):
  \begin{equation}
    \Pr { A \vee B \vee C \vee \ldots } \leq \Pr{A} + \Pr{B} + \Pr{C} + \ldots.
  \end{equation}
  Combination of $y$ choose $x$ bound:
  \begin{equation}
    \left(\frac{y}{x}\right)^x \leq \choose{y}{x} \leq \left(\frac{ey}{x}\right)^x.
  \end{equation}
  \caption{Death-bed formulae.}
  \label{fig:death-bed}
\end{figure}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "lecture-9"
%%% End: 
