Commit | Line | Data |
---|---|---|
4c12d936 KB |
1 | /* This testcase is part of GDB, the GNU debugger. |
2 | ||
b811d2c2 | 3 | Copyright 2017-2020 Free Software Foundation, Inc. |
4c12d936 KB |
4 | |
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. | |
9 | ||
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. | |
14 | ||
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/>. */ | |
17 | ||
18 | #include <stdio.h> | |
19 | #include <omp.h> | |
20 | ||
21 | /* Testcase for checking access to variables in a single / outer scope. | |
22 | Make sure that variables not referred to in the parallel section are | |
23 | accessible from the debugger. */ | |
24 | ||
25 | void | |
26 | single_scope (void) | |
27 | { | |
28 | static int s1 = -41, s2 = -42, s3 = -43; | |
29 | int i1 = 11, i2 = 12, i3 = 13; | |
30 | ||
31 | #pragma omp parallel num_threads (2) shared (s1, i1) private (s2, i2) | |
32 | { | |
33 | int thread_num = omp_get_thread_num (); | |
34 | ||
35 | s2 = 100 * (thread_num + 1) + 2; | |
36 | i2 = s2 + 10; | |
37 | ||
38 | #pragma omp critical | |
39 | printf ("single_scope: thread_num=%d, s1=%d, i1=%d, s2=%d, i2=%d\n", | |
40 | thread_num, s1, i1, s2, i2); | |
41 | } | |
42 | ||
43 | printf ("single_scope: s1=%d, s2=%d, s3=%d, i1=%d, i2=%d, i3=%d\n", | |
44 | s1, s2, s3, i1, i2, i3); | |
45 | } | |
46 | ||
47 | static int file_scope_var = 9876; | |
48 | ||
49 | /* Testcase for checking access to variables from parallel region | |
50 | nested within more than one lexical scope. Of particular interest | |
51 | are variables which are not referenced in the parallel section. */ | |
52 | ||
53 | void | |
54 | multi_scope (void) | |
55 | { | |
56 | int i01 = 1, i02 = 2; | |
57 | ||
58 | { | |
59 | int i11 = 11, i12 = 12; | |
60 | ||
61 | { | |
62 | int i21 = -21, i22 = 22; | |
63 | ||
64 | #pragma omp parallel num_threads (2) \ | |
65 | firstprivate (i01) \ | |
66 | shared (i11) \ | |
67 | private (i21) | |
68 | { | |
69 | int thread_num = omp_get_thread_num (); | |
70 | i21 = 100 * (thread_num + 1) + 21; | |
71 | ||
72 | #pragma omp critical | |
73 | printf ("multi_scope: thread_num=%d, i01=%d, i11=%d, i21=%d\n", | |
74 | thread_num, i01, i11, i21); | |
75 | } | |
76 | ||
77 | printf ("multi_scope: i01=%d, i02=%d, i11=%d, " | |
78 | "i12=%d, i21=%d, i22=%d\n", | |
79 | i01, i02, i11, i12, i21, i22); | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | /* Nested functions in C is a GNU extension. Some non-GNU compilers | |
85 | define __GNUC__, but they don't support nested functions. So, | |
86 | unfortunately, we can't use that for our test. */ | |
87 | #if HAVE_NESTED_FUNCTION_SUPPORT | |
88 | ||
89 | /* Testcase for checking access of variables from within parallel | |
90 | region in a lexically nested function. */ | |
91 | ||
92 | void | |
93 | nested_func (void) | |
94 | { | |
95 | static int s1 = -42; | |
96 | int i = 1, j = 2, k = 3; | |
97 | ||
98 | void | |
99 | foo (int p, int q, int r) | |
100 | { | |
101 | int x = 4; | |
102 | ||
103 | { | |
104 | int y = 5, z = 6; | |
105 | #pragma omp parallel num_threads (2) shared (i, p, x) private (j, q, y) | |
106 | { | |
107 | int tn = omp_get_thread_num (); | |
108 | ||
109 | j = 1000 * (tn + 1); | |
110 | q = j + 1; | |
111 | y = q + 1; | |
112 | #pragma omp critical | |
113 | printf ("nested_func: tn=%d: i=%d, p=%d, x=%d, j=%d, q=%d, y=%d\n", | |
114 | tn, i, p, x, j, q, y); | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | foo (10, 11, 12); | |
120 | ||
121 | i = 101; j = 102; k = 103; | |
122 | foo (20, 21, 22); | |
123 | } | |
124 | #endif | |
125 | ||
126 | /* Testcase for checking access to variables from within a nested parallel | |
127 | region. */ | |
128 | ||
129 | void | |
130 | nested_parallel (void) | |
131 | { | |
132 | int i = 1, j = 2; | |
133 | int l = -1; | |
134 | ||
135 | omp_set_nested (1); | |
136 | omp_set_dynamic (0); | |
137 | #pragma omp parallel num_threads (2) private (l) | |
138 | { | |
139 | int num = omp_get_thread_num (); | |
140 | int nthr = omp_get_num_threads (); | |
141 | int off = num * nthr; | |
142 | int k = off + 101; | |
143 | l = off + 102; | |
144 | #pragma omp parallel num_threads (2) shared (num) | |
145 | { | |
146 | int inner_num = omp_get_thread_num (); | |
147 | #pragma omp critical | |
148 | printf ("nested_parallel (inner threads): outer thread num = %d, thread num = %d\n", num, inner_num); | |
149 | } | |
150 | #pragma omp critical | |
151 | printf ("nested_parallel (outer threads) %d: k = %d, l = %d\n", num, k, l); | |
152 | } | |
153 | } | |
154 | ||
155 | int | |
156 | main (int argc, char **argv) | |
157 | { | |
158 | single_scope (); | |
159 | multi_scope (); | |
160 | #if HAVE_NESTED_FUNCTION_SUPPORT | |
161 | nested_func (); | |
162 | #endif | |
163 | nested_parallel (); | |
164 | return 0; | |
165 | } | |
166 |