source: CIVL/examples/omp/vecAdd_fix.c@ afc300c

1.23 2.0 main test-branch
Last change on this file since afc300c was 28208a9, checked in by Michael Rogers <mrogers08@…>, 11 years ago

Changed from for to while loop in the case that there are shared variables in the for loop initializer, condition, or incrementer.
Added OpenMP simplify tests to compare with tests that aren't simplified.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1871 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/******************************************************************************
2* FILE: omp_bug5fix.c
3* DESCRIPTION:
4* The problem in omp_bug5.c is that the first thread acquires locka and then
5* tries to get lockb before releasing locka. Meanwhile, the second thread
6* has acquired lockb and then tries to get locka before releasing lockb.
7* This solution overcomes the deadlock by using locks correctly.
8* AUTHOR: Blaise Barney 01/29/04
9* LAST REVISED: 04/06/05
10******************************************************************************/
11/**
12* Fixes the deadlock in vecAdd_deadlock.c.
13* Online source:
14* https://computing.llnl.gov/tutorials/openMP/samples/C/omp_bug5fix.c
15**/
16#include <omp.h>
17#include <stdio.h>
18#include <stdlib.h>
19#define N 100
20#define PI 3.1415926535
21#define DELTA .01415926535
22
23int main (int argc, char *argv[])
24{
25int nthreads, tid, i;
26float a[N], b[N];
27omp_lock_t locka, lockb;
28
29/* Initialize the locks */
30omp_init_lock(&locka);
31omp_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_unset_lock(&locka);
56 omp_set_lock(&lockb);
57 printf("Thread %d adding a[] to b[]\n",tid);
58 for (i=0; i<N; i++)
59 b[i] += a[i];
60 omp_unset_lock(&lockb);
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_unset_lock(&lockb);
70 omp_set_lock(&locka);
71 printf("Thread %d adding b[] to a[]\n",tid);
72 for (i=0; i<N; i++)
73 a[i] += b[i];
74 omp_unset_lock(&locka);
75 }
76 } /* end of sections */
77 } /* end of parallel region */
78
79}
80
Note: See TracBrowser for help on using the repository browser.