% % This file was automatically produced at Feb 27 2003, 22:51:56 by % c2latex -c dotprod_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$$\ast$\mbox{}\\ $\ast$ FILE: dotprod\_serial.c\mbox{}\\ $\ast$ DESCRIPTION:\mbox{}\\ $\ast$ This is a simple serial program which computes the dot product of two \mbox{}\\ $\ast$ vectors. The threaded version can is dotprod\_mutex.c.\mbox{}\\ $\ast$\mbox{}\\ $\ast$ SOURCE: Vijay Sonnad, IBM\mbox{}\\ $\ast$ LAST REVISED: 9/20/98 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 \mbox{}\\ to allow the function "dotprod" to access its input data and \mbox{}\\ place its output so that 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{}\\ \hspace*{3\indentation}DOTDATA dotstr; \mbox{}\\ \mbox{}\\ {$/\ast$\it{}\mbox{}\\ We will use a function (dotprod) to perform the scalar product. \mbox{}\\ All input to this routine is obtained through a structure of \mbox{}\\ type DOTDATA and all output from this function is written into\mbox{}\\ this same structure. While this is unnecessarily restrictive \mbox{}\\ for a sequential program, it will turn out to be useful when\mbox{}\\ we modify the program to compute in parallel.\mbox{}\\ $\ast/$}\mbox{}\\ \mbox{}\\ {\bf void} $\ast$dotprod({\bf void})\mbox{}\\ \{\mbox{}\\ \mbox{}\\ {$/\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{}\\ {$/\ast$\it{}\mbox{}\\ Perform the dot product and assign result\mbox{}\\ to the appropriate variable in the structure. \mbox{}\\ $\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.\mbox{}\\ Finally, 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{}\\ {$/\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{}\\ {$/\ast$\it{} Perform the dotproduct $\ast/$}\mbox{}\\ \mbox{}\\ \hspace*{3\indentation}dotprod ();\mbox{}\\ \mbox{}\\ {$/\ast$\it{} Print result and release storage $\ast/$} \mbox{}\\ \mbox{}\\ \hspace*{3\indentation}printf ({\tt"Sum $=$ \%f $\backslash$n"}, dotstr.sum);\mbox{}\\ \hspace*{3\indentation}free (a);\mbox{}\\ \hspace*{3\indentation}free (b);\mbox{}\\ \}\mbox{}\\ \mbox{}\\ \end{flushleft} \end{document}