%
% This file was automatically produced at Feb 27 2003, 22:51:56 by
% c2latex -c mpithreads_serial.c
%
\documentclass[11pt,a4paper]{article}
\setlength{\textwidth}{15cm}
\setlength{\textheight}{22.5cm}
\setlength{\hoffset}{-2cm}
\setlength{\voffset}{-2cm}
\begin{document}
\expandafter\ifx\csname indentation\endcsname\relax%
\newlength{\indentation}\fi
\setlength{\indentation}{0.5em}
\begin{flushleft}
{$/\ast$\it{}$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$\mbox{}\\
$\ast$ FILE: mpithreads\_serial.c\mbox{}\\
$\ast$ DESCRIPTION:\mbox{}\\
$\ast$   This is a simple serial program that computes the dot product of two \mbox{}\\
$\ast$   vectors.  It is the first of four codes used to show the progression\mbox{}\\
$\ast$   from a serial program to a hybrid MPI/Pthreads program.  The other\mbox{}\\
$\ast$   relevant codes are:\mbox{}\\
$\ast$      $-$ mpithreads\_threads.c  $-$ A shared memory programming model using \mbox{}\\
$\ast$          Pthreads\mbox{}\\
$\ast$      $-$ mpithreads\_mpi.c $-$ A distributed memory programming model with MPI\mbox{}\\
$\ast$      $-$ mpithreads\_both.c $-$ A hybrid model that utilizes both MPI and \mbox{}\\
$\ast$          Pthreads to execute on systems that are comprised of clusters \mbox{}\\
$\ast$          of SMP's.\mbox{}\\
$\ast$\mbox{}\\
$\ast$ SOURCE: Vijay Sonnad, IBM\mbox{}\\
$\ast$ LAST REVISED:  10/8/99 Blaise Barney\mbox{}\\
$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast/$}\mbox{}\\
\mbox{}\\
{\tt \#include} $<${\tt{}stdio.h}$>$\mbox{}\\
{\tt \#include} $<${\tt{}malloc.h}$>$\mbox{}\\
\mbox{}\\
{$/\ast$\it{}   \mbox{}\\
The following structure contains the necessary information to allow the \mbox{}\\
function "dotprod" to access its input data and place its output so that \mbox{}\\
it can be accessed later. \mbox{}\\
$\ast/$}\mbox{}\\
\mbox{}\\
{\bf typedef} {\bf struct} \mbox{}\\
\hspace*{1\indentation}\{\mbox{}\\
\hspace*{3\indentation}{\bf double}      $\ast$a;\mbox{}\\
\hspace*{3\indentation}{\bf double}      $\ast$b;\mbox{}\\
\hspace*{3\indentation}{\bf double}     sum; \mbox{}\\
\hspace*{3\indentation}{\bf int}    veclen; \mbox{}\\
\hspace*{1\indentation}\} DOTDATA;\mbox{}\\
\mbox{}\\
{\tt \#define} VECLEN 100\mbox{}\\
DOTDATA dotstr; \mbox{}\\
\mbox{}\\
{$/\ast$\it{}\mbox{}\\
We will use a function (dotprod) to perform the scalar product. All input to \mbox{}\\
this routine is obtained through a structure of type DOTDATA and all output \mbox{}\\
from this function is written into this same structure.  While this is \mbox{}\\
unnecessarily restrictive for a sequential program, it will turn out to be \mbox{}\\
useful when we modify the program to compute in parallel.\mbox{}\\
$\ast/$}\mbox{}\\
\mbox{}\\
{\bf void}$\ast$ dotprod({\bf void})\mbox{}\\
\{\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{$/\ast$\it{} Define and use local variables for convenience $\ast/$}\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{\bf int} start, end, i; \mbox{}\\
\hspace*{3\indentation}{\bf double} mysum, $\ast$x, $\ast$y;\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}start=0;\mbox{}\\
\hspace*{3\indentation}end = dotstr.veclen;\mbox{}\\
\hspace*{3\indentation}x = dotstr.a;\mbox{}\\
\hspace*{3\indentation}y = dotstr.b;\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{$/\ast$\it{}\mbox{}\\
\hspace*{3\indentation}Perform the dot product and assign result to the appropriate variable in \mbox{}\\
\hspace*{3\indentation}the structure. \mbox{}\\
\hspace*{3\indentation}$\ast/$}\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}mysum = 0;\mbox{}\\
\hspace*{3\indentation}{\bf for} (i=start; i$<$end ; i++) \mbox{}\\
\hspace*{4\indentation}\{\mbox{}\\
\hspace*{6\indentation}mysum += (x[i] $\ast$ y[i]);\mbox{}\\
\hspace*{4\indentation}\}\mbox{}\\
\hspace*{3\indentation}dotstr.sum = mysum;\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{\bf return} ;\mbox{}\\
\}\mbox{}\\
\mbox{}\\
{$/\ast$\it{}\mbox{}\\
The main program initializes data and calls the dotprd() function.  Finally, \mbox{}\\
it prints the result.\mbox{}\\
$\ast/$}\mbox{}\\
\mbox{}\\
{\bf int} main ({\bf int} argc, {\bf char}$\ast$ argv[])\mbox{}\\
\{\mbox{}\\
\hspace*{3\indentation}{\bf int} i,len;\mbox{}\\
\hspace*{3\indentation}{\bf double} $\ast$a, $\ast$b;\mbox{}\\
\hspace*{3\indentation}\mbox{}\\
\hspace*{3\indentation}{$/\ast$\it{} Assign storage and initialize values $\ast/$}\mbox{}\\
\hspace*{3\indentation}\mbox{}\\
\hspace*{3\indentation}len = VECLEN;\mbox{}\\
\hspace*{3\indentation}a = ({\bf double}$\ast$) malloc (len$\ast${\bf sizeof}({\bf double}));\mbox{}\\
\hspace*{3\indentation}b = ({\bf double}$\ast$) malloc (len$\ast${\bf sizeof}({\bf double}));\mbox{}\\
\hspace*{2\indentation}\mbox{}\\
\hspace*{3\indentation}{\bf for} (i=0; i$<$len; i++)\mbox{}\\
\hspace*{4\indentation}\{\mbox{}\\
\hspace*{5\indentation}a[i]=1;\mbox{}\\
\hspace*{5\indentation}b[i]=a[i];\mbox{}\\
\hspace*{4\indentation}\}\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}dotstr.veclen = len; \mbox{}\\
\hspace*{3\indentation}dotstr.a = a; \mbox{}\\
\hspace*{3\indentation}dotstr.b = b; \mbox{}\\
\hspace*{3\indentation}dotstr.sum=0;\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{$/\ast$\it{} Perform the  dotproduct $\ast/$}\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}dotprod ();\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{$/\ast$\it{} Print result and release storage $\ast/$} \mbox{}\\
\mbox{}\\
\hspace*{3\indentation}printf ({\tt"Done. Serial version: sum $=$  \%f $\backslash$n"}, dotstr.sum);\mbox{}\\
\hspace*{3\indentation}free (a);\mbox{}\\
\hspace*{3\indentation}free (b);\mbox{}\\
\}\mbox{}\\
\end{flushleft}
\end{document}
