source: CIVL/examples/omp/vecAdd_deadlock.c@ 7d77e64

main test-branch
Last change on this file since 7d77e64 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 2.2 KB
Line 
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 10 //Originally 1000000
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_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
Note: See TracBrowser for help on using the repository browser.