Automatic date update in version.in
[binutils-gdb/blckswan.git] / gdb / testsuite / gdb.threads / omp-par-scope.c
blob2d66676a6a51d4188ce8b95e42d0b9834074e778
1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2017-2022 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include <stdio.h>
19 #include <omp.h>
21 omp_lock_t lock;
22 omp_lock_t lock2;
24 /* Enforce execution order between two threads using a lock. */
26 static void
27 omp_set_lock_in_order (int num, omp_lock_t *lock)
29 /* Ensure that thread num 0 first sets the lock. */
30 if (num == 0)
31 omp_set_lock (lock);
32 #pragma omp barrier
34 /* Block thread num 1 until it can set the lock. */
35 if (num == 1)
36 omp_set_lock (lock);
38 /* This bit here is guaranteed to be executed first by thread num 0, and
39 once thread num 0 unsets the lock, to be executed by thread num 1. */
43 /* Testcase for checking access to variables in a single / outer scope.
44 Make sure that variables not referred to in the parallel section are
45 accessible from the debugger. */
47 void
48 single_scope (void)
50 static int s1 = -41, s2 = -42, s3 = -43;
51 int i1 = 11, i2 = 12, i3 = 13;
53 #pragma omp parallel num_threads (2) shared (s1, i1) private (s2, i2)
55 int thread_num = omp_get_thread_num ();
56 omp_set_lock_in_order (thread_num, &lock);
58 s2 = 100 * (thread_num + 1) + 2;
59 i2 = s2 + 10;
61 #pragma omp critical
62 printf ("single_scope: thread_num=%d, s1=%d, i1=%d, s2=%d, i2=%d\n",
63 thread_num, s1, i1, s2, i2);
65 omp_unset_lock (&lock);
68 printf ("single_scope: s1=%d, s2=%d, s3=%d, i1=%d, i2=%d, i3=%d\n",
69 s1, s2, s3, i1, i2, i3);
72 static int file_scope_var = 9876;
74 /* Testcase for checking access to variables from parallel region
75 nested within more than one lexical scope. Of particular interest
76 are variables which are not referenced in the parallel section. */
78 void
79 multi_scope (void)
81 int i01 = 1, i02 = 2;
84 int i11 = 11, i12 = 12;
87 int i21 = -21, i22 = 22;
89 #pragma omp parallel num_threads (2) \
90 firstprivate (i01) \
91 shared (i11) \
92 private (i21)
94 int thread_num = omp_get_thread_num ();
95 omp_set_lock_in_order (thread_num, &lock);
97 i21 = 100 * (thread_num + 1) + 21;
99 #pragma omp critical
100 printf ("multi_scope: thread_num=%d, i01=%d, i11=%d, i21=%d\n",
101 thread_num, i01, i11, i21);
103 omp_unset_lock (&lock);
106 printf ("multi_scope: i01=%d, i02=%d, i11=%d, "
107 "i12=%d, i21=%d, i22=%d\n",
108 i01, i02, i11, i12, i21, i22);
113 /* Nested functions in C is a GNU extension. Some non-GNU compilers
114 define __GNUC__, but they don't support nested functions. So,
115 unfortunately, we can't use that for our test. */
116 #if HAVE_NESTED_FUNCTION_SUPPORT
118 /* Testcase for checking access of variables from within parallel
119 region in a lexically nested function. */
121 void
122 nested_func (void)
124 static int s1 = -42;
125 int i = 1, j = 2, k = 3;
127 void
128 foo (int p, int q, int r)
130 int x = 4;
133 int y = 5, z = 6;
134 #pragma omp parallel num_threads (2) shared (i, p, x) private (j, q, y)
136 int tn = omp_get_thread_num ();
137 omp_set_lock_in_order (tn, &lock);
139 j = 1000 * (tn + 1);
140 q = j + 1;
141 y = q + 1;
142 #pragma omp critical
143 printf ("nested_func: tn=%d: i=%d, p=%d, x=%d, j=%d, q=%d, y=%d\n",
144 tn, i, p, x, j, q, y);
146 omp_unset_lock (&lock);
151 foo (10, 11, 12);
153 i = 101; j = 102; k = 103;
154 foo (20, 21, 22);
156 #endif
158 /* Testcase for checking access to variables from within a nested parallel
159 region. */
161 void
162 nested_parallel (void)
164 int i = 1, j = 2;
165 int l = -1;
167 omp_set_nested (1);
168 omp_set_dynamic (0);
169 #pragma omp parallel num_threads (2) private (l)
171 int num = omp_get_thread_num ();
172 omp_set_lock_in_order (num, &lock);
174 int nthr = omp_get_num_threads ();
175 int off = num * nthr;
176 int k = off + 101;
177 l = off + 102;
178 #pragma omp parallel num_threads (2) shared (num)
180 int inner_num = omp_get_thread_num ();
181 omp_set_lock_in_order (inner_num, &lock2);
183 #pragma omp critical
184 printf ("nested_parallel (inner threads): outer thread num = %d, thread num = %d\n", num, inner_num);
186 omp_unset_lock (&lock2);
188 #pragma omp critical
189 printf ("nested_parallel (outer threads) %d: k = %d, l = %d\n", num, k, l);
191 omp_unset_lock (&lock);
196 main (int argc, char **argv)
198 omp_init_lock (&lock);
199 omp_init_lock (&lock2);
201 single_scope ();
202 multi_scope ();
203 #if HAVE_NESTED_FUNCTION_SUPPORT
204 nested_func ();
205 #endif
206 nested_parallel ();
208 omp_destroy_lock (&lock);
209 omp_destroy_lock (&lock2);
211 return 0;