% Begin your document as any latex file
%\documentclass{article}
% Use package 6895
%\usepackage{6895}
%\usepackage{epsfig}
%\bibliographystyle{plain}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Begin the lecture with a line like the following:
% This replaces the usual \begin{document} that a latex
% file begins with.
\begin{guestlecture}{15}{Snoopy caching and the Spin-Block Problem}{Sharad Ganesh and Neelkamal}
{Bradley C. Kuszmaul and Michael Bender}

% Now summarize the lecture.  This should be like an abstract
% for the lecture, broken down into a few bullet points.
  \begin{summary}
  % The first item of each \sumitem is the title of the part of the
  % lecture, and the second part is a description (i.e. abstract) of
  % that part.
    \sumitem{Competitive snoopy cache proof} {This section discusses the proof by induction of snoopy bus-based caching algorithm.} \sumitem{Directory-based caching systems}{This section gives a brief
    overview of the algorithm and its scalability as compared to the snoopy cache algorithm.}
    \sumitem{Randomized algorithm for the Ski-rental Problem} {This section introduces the Spin-Block problem, which is a continuous version of the Ski-buy problem.}
  \end{summary}

% Now organize your lecture into sections.
  \section{Strategy for the proof (Snoopy caching)}
  A competitive algorithm that is within a factor of 2. The idea
  is to keep these buckets such that:\\ \\
  \textbf{The economics of the algorithm:}\\ \\
  $W_i(B) = p$  ;          whenever we have to fetch block B from
  memory(M).\\ \\
  $W_j(B) --$   ;          when i writes and j has money in its
  cookie-jar. Whenever I do a write, I decrement somebody else's
  cookie jar. At some point of time they are going to put money
  into the cookie-jar, so I am actually going to charge them
  something. If nobody has any money left, then they all have to
  invalidate their copies in their caches and then I will not
  have to do a broadcast in future.\\ \\
  $W_i(B) = 0$  ;           when I drop a block. A reason I could
  drop a block is the possiblility of a conflict with another
  block or maybe somebody else decremented my cookie-jar count to
  0.\\ \\
  $W_i(B) ++$   ; We increment the count upto a point(only upto p),
  when we read a block.\\ \\
  \textbf{Some facts about the algorithm} \\ \\
  It does tell us when there is going to be a switch from a
  write-through to a write-back. Basically, you do a switch from pack-rat, where everybody tries to hang onto everything, to the exclusive write
  where I can do writes to my local cache and not tell anybody. Basically, I have to broadcast(keep updating everybody) as long as
  everyone is reading. \\ \\
  We are not too far off from the optimal, because everytime I did one of these
  operations that add money, there was already doing an operation that
  cost me that much (anyway $W_i(B) = p$ cost p cycles). So i'm
  putting money into the cookie-jar(i.e. p). \\So, anybody who is
  going to do work and steal money from my cookie-jar, that work
  just charge it to me. Otherwise, anytime I increment it, because I did a
  read, its saying if it weren't in the cache, I'd be happy to pay
  something to have it gotten into the cache, because the read
  would really have cost me a lot more.\\ \\

\subsection{Strategy}
\seclabel{strategy} Define a potential function, which function
that tells us how far off our algorithm is from the offline
algorithm A. \\
This is a function of the cache states of algorithm A(which is the
one we are trying to beat - to come up within a factor of 2 of A).
Those cache states after processing i steps in sequence of
operations(reads and writes). The sequence we are talking about
is:\\ \\
 - $\tau$: which is the ops for A for the first read/write (tagged "A") \\
 - ops for dsc for the first read / write (tagged "dsc")\\
 - supply / update at the end of the first read / write. (tagged "both" - because both have to do it) \\ \\
 Basically both the algorithms are constrained in the sense that,
 when they see the first read, they can do whatever they want to,
 but you have to end with the supply which gives the value to the
 processor.\\ \\
 We will be taking all the operations for A and tag them as
 specified in the bracket as tagged "A". It is important to know
 the order in which the ops happened.\\
\subsection{What is this potential function ?}
\seclabel{potential}

\begin{eqnarray}
\phi(t) = \sum_{(i,B) \in S_A}(w_i(B) - 2p) + \sum_{(i,B) \notin
S_A} - w_i(B).
\end{eqnarray}
where: $(i,B) \in S_A$ => A has B in cache i at step t. Basically,
the set of things that A caches.\\ \\
Recall $w_i(B) = 0$ iff it is not in cache i. So, no matter how
big the memory is, there is only a certain amount of non-zero
values in the sum in equation (1).\\
\subsection{Proof by induction of the following statement}
\seclabel{proof}
\begin{eqnarray}
COST_{dsc}(t) - 2 COST_A(t) \le \phi(t) - \phi(0).
\end{eqnarray}
where: \\$COST_{dsc}(t)$: cost of doing dsc upto time t\\
       $COST_{dsc}(t)$: cost of doing A upto time t\\
       RHS          : Amount the potential function has increased since we
       started.\\ \\
\begin{theorem}
\[
COST_{dsc}(t) \le 2 COST_A(t) + k
\]\\
where: k is essentially the difference of the potential functions.
\end{theorem}
If cache started out with 0, i.e. completely empty, then k = 0.
But, if cache started out with some value, that may favor A, k
covers for that.\\ \\
By construction:\\
\begin{eqnarray}
\phi_t \le 0.
\end{eqnarray}
Basically, the $W_{i's}(B)$ are between 0 and p. ($0 \le w_i(B)\le
p$). \\Then we take some point of those numbers, subtract 2p and
then $ -w_{i}(B)$ makes it more negative.\\So, we are taking a
negative number + something having
to do with the initial state.\\
Just let,\\ \\
\[
k = - \phi(0)
\]
\\ \\
If the caches are completely empty at the beginning of the
algorithm, $\phi(0)$ will turn out be be 0. So, we will later see
that this will actually prove $k = 0$, if the cache starts out
empty.\\ \\
\begin{proof}
\\
Base case for induction (t=0) \\ \\
LHS:Neither algorithm has done
anything yet, so their costs are
both 0.\\
RHS: $\phi(0) - \phi(0)$ = 0. \\ \\
Both sides are 0.
\\ \\
Inductive case: Basically, we want to show something about the
changes in the costs and how they are related to the change in
potential function. We would want to show:
\[
\Delta COST_{dsc} - 2 \Delta COST_{A} \le \phi
\]
\\
where:\\ \\
$\Delta COST_{dsc}$ denotes the change in cost of dsc. \\
$\Delta
COST_{A}$ denotes the change in cost of A.\\
\\ \\
\textbf{We present a case analysis for the above:} \\
Idea of the analysis: \\ \\
We have the sequences, that we'd discussed earlier, and for
everything in the sequence the property holds. \\ i.e. We have to
show that, if one of them drops a block or does a fetch or an
update, the changes should have this property. \\ \\
We will go through some of the cases, but for the rest you can
refer to the Goodman's paper \cite{goodman}.\\ \\
{\it Case 1}: \\
Step: A does a fetchblock(i, B), which is fetching over the bus to
get a block into cache i of block B.\\ \\
\[
\Delta COST_{A} = p
\]
\\
\[
\Delta COST_{dsc} = 0
\]
This is because, dsc did'nt do anything \\ \\
 Now we have to show:
\[
\Delta \phi \ge 2p
\]
\\ \\
Before the action: $(i, B) \notin S_A$ \\
This is because, none of these snoopy caches do a fetch unless
they do a read / write. Moreover, they don't a fetch if they
already got it cached. So, before the action it was'nt in
the cache.\\ \\
After the action: $(i, B) \in S_A$ \\
After the action, it is in the cache.\\ \\
So,
\[
\Delta \phi = w_{i}(B) - 2p - w{i}(B)
\]
\\ Therefore, $\Delta \phi = - 2p$
\\Therefore, whatever A does if it does a fetch, potential function does the right thing.\\ \\
{\it Case 2}: \\
A drops (i, B) \\ \\
At this point 'A' has more freedom to do something different than
we did. 'A' might drop something that you didn't drop because we
were still paying something and so could'nt drop it. A might
decide to drop something and let the slot be occupied by something
else. In that case, I won't be incurring any cost for update
later. \\
\[
\Delta COST_{A} = 0
\]
\\
\[
\Delta COST_{dsc} = 0
\]
\\ Now we have to show:
\[
\Delta \phi \ge 0
\]
\\
So we have,\\
$\-w_{i}(B) + 2p - w_{i}(B)$ \\
The second  $-w_{i}(B)$, because it
is no longer in the cache.
\[
 \Delta \phi = -2w_{i}(B) + 2p
\]
\\ The above term is always greater than or equal to 0, because
$0 \le W_{i}(B) \le p$.
\\ \\
{\it Case 3}: \\
dsc does a fetchblock(i, B)
\[
\Delta COST_{dsc} = p
\]
\\
\[
\Delta COST_{A} = 0
\]
\\ Now we have to show:
\[
\Delta \phi \ge p
\]
\\
Therefore here, $w{i}(B)$ changes from 0 to p.\\
Before it was'nt in the cache($w{i}(B) = 0$), and then when we did
a fetch we set($w{i}(B)$ = p).\\
Since we did A's operations first, we must have (i, B) in A's
cache at the end of A's ops. Both algorithms can be supplied from
the cache. The key point is since we did A's ops first, we know it
is in the cache. \\ \\ Therefore,
\[
\Delta \phi = p
\]
\\ \\
For the other cases, you can read the Goodman's[1] paper.
\end{proof}
\section{Directory based caches}
The problem with MESI is the bus.

\begin{figure}[thb]
\begin{center}
\includegraphics[scale=.3]{15/dbc.eps}
\caption{MESI network setup} \figlabel{dbc}
\end{center}
\end{figure}


\begin{figure}[thb]
\begin{center}
\includegraphics[scale=.3]{15/direntry.eps}
\caption{A typical directory entry}  \figlabel{direntry}
\end{center}
\end{figure}

Some processor wants to obtain E(exclusive) access, So send a
message to M saying you want E access.\\ \\
Case 1: Nobody has it. Memory says you have got it and changes the
state.
\\ \\
Case 2: Some other proc has it in E state. M sends message to the
other processor. The other processor sends it to M. If it has been
modified, it has to send it back to M. In any case M sends it to
P. Real machines do a shortcut sending a message directly to the
processor.
\\ \\
\textbf{Directory based cache analysis \\} None.


%\begin{document} Start of neel's portion

\section{Spin-Block Problem}
There are basically two ways in which a process waits for a lock
\begin{enumerate}
\item Spin: keep on waiting in a nop loop till get the lock
        Cost = spin time
\item Block: You ask the lock manager to give u a lock and while
he manages for the lock u go on and do your work and return after
some time.
        Cost: fixed cost 'c'

\end{enumerate}

\section{Continuous ski rental problem}
A simple algorithm based on randomized algorithm strategy.

\subsection {Spin}
\begin{verbatim}
1. until time c/2
2. With probability P, block
3. With probability 1-P, keep spinning until 'c' spins.
4. Then block
\end{verbatim}

\begin{definition}
f(t) = expected cost of waiting t units of time.

f(t)= $\left \{
\begin{array}{ll}
    t, & \hbox{t $<$ c/2;} \\
    p(c + c/2)+(1-p)t, & \hbox{c/2 $\le$ t $<$ c;} \\
    p(c+c/2)+(1-p)*2c, & \hbox{t $\ge$ c.} \\
\end{array}
\right.$
\end{definition}

Goal: Choose p to minimize competitive ratio (1+ $ \alpha$)

%missing a few lines here

%f(t) $\le (1+ $ \alpha $)t

%$\left\{
%\begin{array}{ll}
    %f(t) $ \le $ (1+$\alpha$)t, & \hbox{c/2 $\le$ t $\le$ c;} \\
    %f(t) $\le$ (1+$\alpha$)c, & \hbox{t $\ge$ c.} \\
%\end{array}
%\right.$

Setting the inequalities to equalities and solving:
\begin{equation}
    f(t) = c/2 + pc = (1 + \alpha)c/2
\end{equation}
\begin{equation}
    f(t) = p(3/2c) + (1-p)*2c = (1+\alpha)c
\end{equation}
\begin{equation}
    p = 2/5
\end{equation}
\begin{equation}
    1+\alpha = 9/5
\end{equation}

\bf Good News: (1+ $ \alpha $) is less than 2.

\begin{definition}
Function $\Pi(t)$ = density function of time before a processor
should block.
\end{definition}

Expected cost of waiting Q steps.
\begin{equation}
    f(q) =  \int_{t=0}^{q}dt \prod(t)(t+c) + q \int_{t=q}
    ^{\inf}(dt \Pi(t))
\end{equation}

Solving the above equation:

\bf Idea: choose $\Pi(t) $ to minimum competitive ratio:
\begin{equation}
f1(q) = (q+c) \Pi(q) + \int _{t=q}^{\inf}(dt \Pi(t)) - q\Pi(q)
\end{equation}

\begin{equation}
f1(q) = c \Pi(q) + \int _{t=q}^{\inf}(dt \Pi(t)) - q\Pi(q)
\end{equation}

\begin{equation}
f2(q) = c \Pi1(q) - c \Pi(q) = 0
\end{equation}

\begin{equation}
\Pi(q) = A\exp{q/c}
\end{equation}


\textbf{Calculating A}
\begin{equation}
\int_{t=0}^{c} dt \Pi(t) = 1
\end{equation}

\begin{equation}
A \int_{t=0}^{c} \exp(t/c)dt \Rightarrow AC(e-1) = 1
\end{equation}
\begin{equation}
\Rightarrow A = 1/(C(e-1))
\end{equation}

\textbf{Calculating 1+$ \alpha $}
\begin{equation}
f(C)/C = A/C \times \int _{t=0}^{c}dt(t+C) \exp(t/c)
\end{equation}

\begin{equation}
= A/C \times C^{2}
\end{equation}

\begin{equation}
= e/(e-1)\simeq 1.59
\end{equation}

\bf \textit{Note: If you make this discrete space solution, then
also u will get the same value}

%end of neel's part
%\bibliography{lec-15}
\end{guestlecture}
\theend
