1 | // RUN: %libomp-compile-and-run |
2 | #include <stdio.h> |
3 | #include <math.h> |
4 | #include "omp_testsuite.h" |
5 | |
6 | /* Utility function do spend some time in a loop */ |
7 | static void do_some_work() |
8 | { |
9 | int i; |
10 | double sum = 0; |
11 | for(i = 0; i < 1000; i++){ |
12 | sum += sqrt (x: (double) i); |
13 | } |
14 | } |
15 | |
16 | int sum1; |
17 | #pragma omp threadprivate(sum1) |
18 | |
19 | int test_omp_for_private() |
20 | { |
21 | int sum = 0; |
22 | int sum0; |
23 | int known_sum; |
24 | |
25 | sum0 = 0; /* setting (global) sum0 = 0 */ |
26 | |
27 | #pragma omp parallel |
28 | { |
29 | sum1 = 0; /* setting sum1 in each thread to 0 */ |
30 | { /* begin of orphaned block */ |
31 | int i; |
32 | #pragma omp for private(sum0) schedule(static,1) |
33 | for (i = 1; i <= LOOPCOUNT; i++) { |
34 | sum0 = sum1; |
35 | #pragma omp flush |
36 | sum0 = sum0 + i; |
37 | do_some_work (); |
38 | #pragma omp flush |
39 | sum1 = sum0; |
40 | } |
41 | } /* end of orphaned block */ |
42 | |
43 | #pragma omp critical |
44 | { |
45 | sum = sum + sum1; |
46 | } /*end of critical*/ |
47 | } /* end of parallel*/ |
48 | known_sum = (LOOPCOUNT * (LOOPCOUNT + 1)) / 2; |
49 | return (known_sum == sum); |
50 | } |
51 | |
52 | int main() |
53 | { |
54 | int i; |
55 | int num_failed=0; |
56 | |
57 | for(i = 0; i < REPETITIONS; i++) { |
58 | if(!test_omp_for_private()) { |
59 | num_failed++; |
60 | } |
61 | } |
62 | return num_failed; |
63 | } |
64 | |