| [38374b7] | 1 | /******************************************************************************
|
|---|
| 2 | * FILE: arrayloops.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * Example code demonstrating decomposition of array processing by
|
|---|
| 5 | * distributing loop iterations. A global sum is maintained by a mutex
|
|---|
| 6 | * variable.
|
|---|
| 7 | * AUTHOR: Blaise Barney
|
|---|
| 8 | * LAST REVISED: 01/29/09
|
|---|
| 9 | ******************************************************************************/
|
|---|
| 10 | #include <pthread.h>
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 |
|
|---|
| 14 | #define NTHREADS 4
|
|---|
| 15 | #define ARRAYSIZE 1000000
|
|---|
| 16 | #define ITERATIONS ARRAYSIZE / NTHREADS
|
|---|
| 17 |
|
|---|
| 18 | double sum=0.0, a[ARRAYSIZE];
|
|---|
| 19 | pthread_mutex_t sum_mutex;
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | void *do_work(void *tid)
|
|---|
| 23 | {
|
|---|
| 24 | int i, start, *mytid, end;
|
|---|
| 25 | double mysum=0.0;
|
|---|
| 26 |
|
|---|
| 27 | /* Initialize my part of the global array and keep local sum */
|
|---|
| 28 | mytid = (int *) tid;
|
|---|
| 29 | start = (*mytid * ITERATIONS);
|
|---|
| 30 | end = start + ITERATIONS;
|
|---|
| 31 | printf ("Thread %d doing iterations %d to %d\n",*mytid,start,end-1);
|
|---|
| 32 | for (i=start; i < end ; i++) {
|
|---|
| 33 | a[i] = i * 1.0;
|
|---|
| 34 | mysum = mysum + a[i];
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | /* Lock the mutex and update the global sum, then exit */
|
|---|
| 38 | pthread_mutex_lock (&sum_mutex);
|
|---|
| 39 | sum = sum + mysum;
|
|---|
| 40 | pthread_mutex_unlock (&sum_mutex);
|
|---|
| 41 | pthread_exit(NULL);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | int main(int argc, char *argv[])
|
|---|
| 46 | {
|
|---|
| 47 | int i, start, tids[NTHREADS];
|
|---|
| 48 | pthread_t threads[NTHREADS];
|
|---|
| 49 | pthread_attr_t attr;
|
|---|
| 50 |
|
|---|
| 51 | /* Pthreads setup: initialize mutex and explicitly create threads in a
|
|---|
| 52 | joinable state (for portability). Pass each thread its loop offset */
|
|---|
| 53 | pthread_mutex_init(&sum_mutex, NULL);
|
|---|
| 54 | pthread_attr_init(&attr);
|
|---|
| 55 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 56 | for (i=0; i<NTHREADS; i++) {
|
|---|
| 57 | tids[i] = i;
|
|---|
| 58 | pthread_create(&threads[i], &attr, do_work, (void *) &tids[i]);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /* Wait for all threads to complete then print global sum */
|
|---|
| 62 | for (i=0; i<NTHREADS; i++) {
|
|---|
| 63 | pthread_join(threads[i], NULL);
|
|---|
| 64 | }
|
|---|
| 65 | printf ("Done. Sum= %e \n", sum);
|
|---|
| 66 |
|
|---|
| 67 | sum=0.0;
|
|---|
| 68 | for (i=0;i<ARRAYSIZE;i++){
|
|---|
| 69 | a[i] = i*1.0;
|
|---|
| 70 | sum = sum + a[i]; }
|
|---|
| 71 | printf("Check Sum= %e\n",sum);
|
|---|
| 72 |
|
|---|
| 73 | /* Clean up and exit */
|
|---|
| 74 | pthread_attr_destroy(&attr);
|
|---|
| 75 | pthread_mutex_destroy(&sum_mutex);
|
|---|
| 76 | pthread_exit (NULL);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|