\begin{frame}[fragile]
\frametitle{Single program, multiple data -- spmd}

The \mcode{spmd} statement defines a block of code to be run simultaneously
on multiple workers that should be reserved using \mcode{parpool.}

The first couple of slides are a gentle adaptation of the text in:

\href{https://se.mathworks.com/help/distcomp/spmd.html}{https://se.mathworks.com/help/distcomp/spmd.html}

%\textbf{Assumption:} You have the \emph{Parallel Computing Toolbox} installed
The general form of an spmd (single program, multiple data) statement is:

\begin{lstlisting}
spmd
    statements
end
\end{lstlisting}

MATLAB® executes the spmd body denoted by statements on several MATLAB 
workers simultaneously. 
\end{frame}

\begin{frame}[fragile]
\frametitle{Open pool, \mcode{parpool,labindex,numlabs}}

First open a pool of MATLAB workers using \mcode{parpool} or have your 
parallel prefences allow the automatic start of a pool.

Inside the body of the spmd statement, each MATLAB worker has a unique value of \mcode{labindex}, while \mcode{numlabs} denotes the total number of workers 
executing the block in parallel. Within the body of the spmd statement, communication functions for communicating jobs (such as \mcode{labSend} and 
\mcode{labReceive}) can transfer data between the workers.
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
nlabs=2;   % laptop
% nlabs=16;  % Triton
parpool(nlabs)
spmd
  % build magic squares in parallel
  q = magic(labindex + 2);
end
\end{lstlisting}
The variable \mcode{q} is a \textcolor{blue}{Composite object}. 
The value \mcode{q\{k\}} is the value stored in the $k^{th}$ worker.

\begin{lstlisting}
 for ii=1:length(q);figure,imagesc(q{ii});end 
\end{lstlisting}

\textbf{Note:} The composite object \mcode{q} remains available
even outside the \mcode{spmd}-block as long as the pool is open.

\end{frame}
\begin{frame}[fragile]
\frametitle{Composite objects}
\begin{lstlisting}
>> q
q = 
   Lab 1: class = double, size = [3  3]
   Lab 2: class = double, size = [4  4]

>> q{1:nlabs}
ans =
     8     1     6
     3     5     7
     4     9     2
ans =
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1
\end{lstlisting}

\end{frame}
\begin{frame}[fragile]
\frametitle{Composite objects to cell arrays, results from workers to client}


The variable \mcode{q} ``behaves'' like a cell array. 
After closing the pool it isn't available (the workers are gone).
Here's how to bring q into the client:

\begin{lstlisting}
Q=cell(1,nlabs);        % Create a cell arrays Q.
Q(1:nlabs)=q(1:nlabs);  % Copy q to cell array Q.
% Q=q;                  % Not allowed.
delete(gcp)   % q no more available, Q remains.
cellplot(Q), figure
imagesc(Q{nlabs})  % Plot the last (largest) magic. 
\end{lstlisting}
\end{frame}
