| 1 | /******************************************************************************
|
|---|
| 2 | * FILE: omp_bug5.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * Using SECTIONS, two threads initialize their own array and then add
|
|---|
| 5 | * it to the other's array, however a deadlock occurs.
|
|---|
| 6 | * AUTHOR: Blaise Barney 01/29/04
|
|---|
| 7 | * LAST REVISED: 04/06/05
|
|---|
| 8 | ******************************************************************************/
|
|---|
| 9 | /**
|
|---|
| 10 | * The first thread acquires locka and then tries to get lockb before releasing
|
|---|
| 11 | * locka. Meanwhile, the second thread has acquired lockb and then tries to get
|
|---|
| 12 | * locka before releasing lockb.
|
|---|
| 13 | * Online source:
|
|---|
| 14 | * https://computing.llnl.gov/tutorials/openMP/samples/C/omp_bug5.c
|
|---|
| 15 | **/
|
|---|
| 16 | #include <omp.h>
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 | #include <stdlib.h>
|
|---|
| 19 | #define N 1000000
|
|---|
| 20 | #define PI 3.1415926535
|
|---|
| 21 | #define DELTA .01415926535
|
|---|
| 22 |
|
|---|
| 23 | int main (int argc, char *argv[])
|
|---|
| 24 | {
|
|---|
| 25 | int nthreads, tid, i;
|
|---|
| 26 | float a[N], b[N];
|
|---|
| 27 | omp_lock_t locka, lockb;
|
|---|
| 28 |
|
|---|
| 29 | /* Initialize the locks */
|
|---|
| 30 | omp_init_lock(&locka);
|
|---|
| 31 | omp_init_lock(&lockb);
|
|---|
| 32 |
|
|---|
| 33 | /* Fork a team of threads giving them their own copies of variables */
|
|---|
| 34 | #pragma omp parallel shared(a, b, nthreads, locka, lockb) private(tid)
|
|---|
| 35 | {
|
|---|
| 36 |
|
|---|
| 37 | /* Obtain thread number and number of threads */
|
|---|
| 38 | tid = omp_get_thread_num();
|
|---|
| 39 | #pragma omp master
|
|---|
| 40 | {
|
|---|
| 41 | nthreads = omp_get_num_threads();
|
|---|
| 42 | printf("Number of threads = %d\n", nthreads);
|
|---|
| 43 | }
|
|---|
| 44 | printf("Thread %d starting...\n", tid);
|
|---|
| 45 | #pragma omp barrier
|
|---|
| 46 |
|
|---|
| 47 | #pragma omp sections nowait
|
|---|
| 48 | {
|
|---|
| 49 | #pragma omp section
|
|---|
| 50 | {
|
|---|
| 51 | printf("Thread %d initializing a[]\n",tid);
|
|---|
| 52 | omp_set_lock(&locka);
|
|---|
| 53 | for (i=0; i<N; i++)
|
|---|
| 54 | a[i] = i * DELTA;
|
|---|
| 55 | omp_set_lock(&lockb);
|
|---|
| 56 | printf("Thread %d adding a[] to b[]\n",tid);
|
|---|
| 57 | for (i=0; i<N; i++)
|
|---|
| 58 | b[i] += a[i];
|
|---|
| 59 | omp_unset_lock(&lockb);
|
|---|
| 60 | omp_unset_lock(&locka);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | #pragma omp section
|
|---|
| 64 | {
|
|---|
| 65 | printf("Thread %d initializing b[]\n",tid);
|
|---|
| 66 | omp_set_lock(&lockb);
|
|---|
| 67 | for (i=0; i<N; i++)
|
|---|
| 68 | b[i] = i * PI;
|
|---|
| 69 | omp_set_lock(&locka);
|
|---|
| 70 | printf("Thread %d adding b[] to a[]\n",tid);
|
|---|
| 71 | for (i=0; i<N; i++)
|
|---|
| 72 | a[i] += b[i];
|
|---|
| 73 | omp_unset_lock(&locka);
|
|---|
| 74 | omp_unset_lock(&lockb);
|
|---|
| 75 | }
|
|---|
| 76 | } /* end of sections */
|
|---|
| 77 | } /* end of parallel region */
|
|---|
| 78 |
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|