\documentclass[12pt]{amsart}
\input{defalgo.tex}
\usepackage{program}
\usepackage{epsf}


\title{Parallel Programming: 1\\
\emph{(draft notes)}}

\author{Raul H.C. Lopes}


\begin{document}
\nocite{dijkstra:fdpp}
\maketitle
\section{P+1-ary search}
This section presents a (p+1)-ary search algorithm, for searching an ordered 
sequence for a given item, using $p$ processors.
Dijkstra presents in \cite{dijkstra:fillers} a derivation of a binary search 
algorithm that can be easily generalized to a (p+1)-ary search with $p$ 
processors.

\subsection{Generalizing Dijkstra' binary search}
Given an \[X\in\Int\] and
\[S.0..M-1 \in \Int\text{ where }\forall i,j:\lelt{0}{i}{j}\lt M:S.i\lt S.j\]
one is required to calculate $present$ such that
\[present\leq \exists i:0\le i\lt M: X = S.i  \]

The existential quantifier in the formula above is better understood from
a constructive point of view: it demands that an $i$ is produced that will 
validate $X=S.i$,
or else $present$ will be false. The message that quantifier to be sending is
is: \emph{find and $i$ that validates $x=S.i$ or prove that any legal 
$i$ makes $X=S.i$ a contradiction.}\footnote{Dijkstra\cite{dijkstra:fillers}
says in his argument that it ``is hard to visualize an algorithm correctly 
establishing present ... without establishing  for some integer'' $i$ such that
$S.i=X\land 0\le i\lt M$.}

Determining $present$ is then relaxed to finding an $i$
\[R\leq\lelt{0}{i}{M}\land \lelt{S.i}{X}{S.i+1} \]
assuming that $S.M$ is defined to be greater than $X$. 

Dijkstra's program has the form

\begin{program}
\hspace*{0.5cm}\tab
  \bb\tab\Var i\in \Int
     ; establish\ R
     ; present:= (S.i=X)
  \untab\eb
\end{program}
 
Generalizing $R$ by replacing $(i+1)$ by $j$ gives an  invariant for 
the case when $X$ occurs in $S$:

\[\lelt{0}{i}{j}\le M\land \lelt{S.i}{X}{S.j} \]

That together with the fact $X$ may be less than $S.0$ gives the invariant
\[P\leq \lelt{0}{i}{j}\le M\land (\lelt{S.i}{X}{S.j} \lor X\lt S.0) \]
  
That invariant establishes at any transition that $S.i..j-1$ must be searched
for a pair of indexes $(i,j)$ delimiting the area where $X$ can be found in
$S$.
A progress condition should state that the area to be scanned is decreasing
\[(j-i)\gt(j-i)'\]
and a fixed point should bound progress: it doesn't make much sense to search
a singleton subsequence:
\[(j-i)=1\] 

``establish R'' is then refined to

\begin{program}
\hspace*{0.5cm}\tab
  \bb\tab\Var j\in \Int
     ;i,j:= 0,M
     ; \{P\}
     \bloop  
        j-i\neq 1\athen shrink(S,j,i) \{P\}
     \eloop \{R\}
   \untab\eb \{R\}
\end{program}

Shrinking $(j-i)$ can be done by either increasing $i$ to some $h$,
if $S.h\le X$, or decreasing $j$ to $h$, if $S.h\lt X$, or both
simultaneously, why not? 

\begin{program}
\hspace*{0.5cm}\tab
  sh\tab rink(S,X,i,j)\ \progis
  \bb\tab \Var h\in \Int
     ; h:=(i+j)/2
     ;\tab \ifbb S.h\le X\athen i:=h
       \ifassync X\lt S.h \athen j:=h
      \ifeb
  \untab\eb
\end{program}


\begin{exercise} \ 
  \begin{enumerate}
    \item Prove the invariant $P$ holds for the program above.
    \item Prove the program terminates.
    \item Prove that it correctly determines $q$.
  \end{enumerate}
\end{exercise}

\subsection{Generalizing to (p+1)-ary search}
Determining $present$ is now relaxed to finding an interval with size up to $p$
that can be tested in parallel for the presence of $X$.
\[R\leq\lelt{0}{i}{M}\land \lelt{S.i}{X}{S.min(i+p,M)} \]
assuming that $S.M$ is defined to be greater than $X$. 

The program becomes

\begin{program}
\hspace*{0.5cm}\tab
  \bb\tab\Var i\in \Int
     ; establish\ R
     ; \tab\parbb{i}{\lelt{i}{k}{min(i+p,M)}}
        q:=(X=S.k)
	\pareb\untab
  \untab\eb
\end{program}

The loop invariant  is the same. The fixed point now is reached when $(j-i)$
is less than or equal to $p$. 
 
``establish R'' is then refined to

\begin{program}
\hspace*{0.5cm}\tab
  \bb\tab\Var j\in \Int
     ;i,j:= 0,M
     ; \{P\}
     \bloop  
        j-i\gt p\athen pshrink(S,j,i,p) \{P\}
     \eloop \{R\}
   \untab\eb \{R\}
\end{program}

Shrinking $(j-i)$ can be performed by either 
\begin{itemize}
\item increasing $i$ to some $h$,
if $S.h\le X$;
\item or decreasing $j$ to $h$, if $S.h\lt X$;
\item or both simultaneously. 
\end{itemize}
This is performed by a parallel test.

\begin{program}
\hspace*{0.5cm}\tab
  psh\tab rink(S,X,i,j)\ \progis
  \bb\tab \Var h\in \Int
     ; nel:=(i+j)/(p+1)
     ; \tab\parbb{k}{\lele{1}{k}{p}}
            \Var h,hb\in \Int
            ; h,hb := i+(nel*k),i+(nel*(k-1))
            ;\tab\ifbb k=p \land X\ge S.h\athen i:=h
	    \ifassync S.hb\le X \land X\lt S.h\athen i,j:=hb,h
            \ifeb\untab
            \pareb\untab
     
  \untab\eb
\end{program}


\begin{exercise}\ 
\item Prove $P$ is still an invariant for the new program.
\item Evaluate its complexity in terms of: time (i.e, number of parallel
transitions or supersteps), and work.
  
\end{exercise}

\section{Quicksort}

\begin{exercise}
  Derive a quicksort algorithm that splits its input in $p+1$ intervals, when
assigned $p$ processors. Use a parallel scan operation for splitting around
$p$ pivots.
\end{exercise}

\section{Sequences}

\begin{exercise} \ 
\begin{enumerate} 
\item Given sequences $a.0..n-1\in\Int,$ and $b.0..n-1\in\Int,$
and $s.0=0$, give a program to compute, for $\lelt{1}{i}{n}$:
\begin{enumerate}
  \item $s.i = (a.i * s.i-1)+b.i$
  \item $s.i = (a.i * s.i-1)+(b.i*s.i-2)$
\end{enumerate}

\item Given a nondecreasing sequence $a.0..n-1\in\Int,$ and a number 
$b\in\Int,$ derive a program to find the rank of $b$ in $a$: the 
index of the largest element of $a$ that is smaller
than $b$.

\item Derive a program to find 
the rank of all elements of a sequence $b.0..m-1\in\Int,$ in
a nondecreasing sequence $b.0..n-1\in\Int.$

\item Derive a program to merge $2$ nondecreasing sequences.
\end{enumerate}

\end{exercise}

\bibliography{abbrev,analysis,formal,algorithms,program}

\bibliographystyle{amsplain}


\end{document}

