% % This file was automatically produced at Feb 27 2003, 22:51:56 by % c2latex -c arrayloops.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: arrayloops.c\mbox{}\\ $\ast$ DESCRIPTION:\mbox{}\\ $\ast$ Example code demonstrating decomposition of array processing by\mbox{}\\ $\ast$ distributing loop iterations. A global sum is maintained by a mutex\mbox{}\\ $\ast$ variable. \mbox{}\\ $\ast$ LAST REVISED: 02/11/2002 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{}pthread.h}$>$\mbox{}\\ {\tt \#include} $<${\tt{}stdio.h}$>$\mbox{}\\ \mbox{}\\ {\tt \#define} NTHREADS 4\mbox{}\\ {\tt \#define} ARRAYSIZE 1000000\mbox{}\\ {\tt \#define} ITERATIONS ARRAYSIZE $/$ NTHREADS\mbox{}\\ \mbox{}\\ {\bf double} sum=0.0, a[ARRAYSIZE];\mbox{}\\ pthread\_mutex\_t sum\_mutex;\mbox{}\\ \mbox{}\\ \mbox{}\\ {\bf void} $\ast$do\_work({\bf void} $\ast$tid) \mbox{}\\ \{\mbox{}\\ \hspace*{2\indentation}{\bf int} i, start, $\ast$mytid, end;\mbox{}\\ \hspace*{2\indentation}{\bf double} mysum=0.0;\mbox{}\\ \mbox{}\\ \hspace*{2\indentation}{$/\ast$\it{} Initialize my part of the global array and keep local sum $\ast/$}\mbox{}\\ \hspace*{2\indentation}mytid = ({\bf int} $\ast$) tid;\mbox{}\\ \hspace*{2\indentation}start = ($\ast$mytid $\ast$ ITERATIONS);\mbox{}\\ \hspace*{2\indentation}end = start + ITERATIONS;\mbox{}\\ \hspace*{2\indentation}printf ({\tt"Thread \%d doing iterations \%d to \%d$\backslash$n"},$\ast$mytid,start,end$-$1); \mbox{}\\ \hspace*{2\indentation}{\bf for} (i=start; i $<$ end ; i++) \{\mbox{}\\ \hspace*{4\indentation}a[i] = i $\ast$ 1.0;\mbox{}\\ \hspace*{4\indentation}mysum = mysum + a[i];\mbox{}\\ \hspace*{4\indentation}\}\mbox{}\\ \mbox{}\\ \hspace*{2\indentation}{$/\ast$\it{} Lock the mutex and update the global sum, then exit $\ast/$}\mbox{}\\ \hspace*{2\indentation}pthread\_mutex\_lock (\&sum\_mutex);\mbox{}\\ \hspace*{2\indentation}sum = sum + mysum;\mbox{}\\ \hspace*{2\indentation}pthread\_mutex\_unlock (\&sum\_mutex);\mbox{}\\ \hspace*{2\indentation}pthread\_exit(NULL);\mbox{}\\ \}\mbox{}\\ \mbox{}\\ \mbox{}\\ {\bf int} main({\bf int} argc, {\bf char} $\ast$argv[])\mbox{}\\ \{\mbox{}\\ \hspace*{2\indentation}{\bf int} i, start, tids[NTHREADS];\mbox{}\\ \hspace*{2\indentation}pthread\_t threads[NTHREADS];\mbox{}\\ \hspace*{2\indentation}pthread\_attr\_t attr;\mbox{}\\ \mbox{}\\ \hspace*{2\indentation}{$/\ast$\it{} Pthreads setup: initialize mutex and explicitly create threads in a\mbox{}\\ \hspace*{5\indentation}joinable state (for portability). Pass each thread its loop offset $\ast/$}\mbox{}\\ \hspace*{2\indentation}pthread\_mutex\_init(\&sum\_mutex, NULL);\mbox{}\\ \hspace*{2\indentation}pthread\_attr\_init(\&attr);\mbox{}\\ \hspace*{2\indentation}pthread\_attr\_setdetachstate(\&attr, PTHREAD\_CREATE\_JOINABLE);\mbox{}\\ \hspace*{2\indentation}{\bf for} (i=0; i$<$NTHREADS; i++) \{\mbox{}\\ \hspace*{4\indentation}tids[i] = i;\mbox{}\\ \hspace*{4\indentation}pthread\_create(\&threads[i], \&attr, do\_work, ({\bf void} $\ast$) \&tids[i]);\mbox{}\\ \hspace*{4\indentation}\}\mbox{}\\ \mbox{}\\ \hspace*{2\indentation}{$/\ast$\it{} Wait for all threads to complete then print global sum $\ast/$} \mbox{}\\ \hspace*{2\indentation}{\bf for} (i=0; i$<$NTHREADS; i++) \{\mbox{}\\ \hspace*{4\indentation}pthread\_join(threads[i], NULL);\mbox{}\\ \hspace*{2\indentation}\}\mbox{}\\ \hspace*{2\indentation}printf ({\tt"Done. Sum$=$ \%e $\backslash$n"}, sum);\mbox{}\\ \mbox{}\\ \hspace*{2\indentation}sum=0.0;\mbox{}\\ \hspace*{2\indentation}{\bf for} (i=0;i$<$ARRAYSIZE;i++)\{ \mbox{}\\ \hspace*{2\indentation}a[i] = i$\ast$1.0;\mbox{}\\ \hspace*{2\indentation}sum = sum + a[i]; \}\mbox{}\\ \hspace*{2\indentation}printf({\tt"Check Sum$=$ \%e$\backslash$n"},sum);\mbox{}\\ \mbox{}\\ \hspace*{2\indentation}{$/\ast$\it{} Clean up and exit $\ast/$}\mbox{}\\ \hspace*{2\indentation}pthread\_attr\_destroy(\&attr);\mbox{}\\ \hspace*{2\indentation}pthread\_mutex\_destroy(\&sum\_mutex);\mbox{}\\ \hspace*{2\indentation}pthread\_exit (NULL);\mbox{}\\ \}\mbox{}\\ \mbox{}\\ \end{flushleft} \end{document}