1 // RUN: %libomp-compile-and-run
3 * Threadprivate is tested in 2 ways:
4 * 1. The global variable declared as threadprivate should have
5 * local copy for each thread. Otherwise race condition and
7 * 2. If the value of local copy is retained for the two adjacent
10 #include "omp_testsuite.h"
15 static int myvalue
= 0;
17 #pragma omp threadprivate(sum0)
18 #pragma omp threadprivate(myvalue)
20 int test_omp_threadprivate()
32 #pragma omp parallel private(i)
36 for (i
= 1; i
<= LOOPCOUNT
; i
++) {
42 } /*end of critical */
43 } /* end of parallel */
44 known_sum
= (LOOPCOUNT
* (LOOPCOUNT
+ 1)) / 2;
45 if (known_sum
!= sum
) {
46 fprintf (stderr
, " known_sum = %d, sum = %d\n", known_sum
, sum
);
49 /* the next parallel region is just used to get the number of threads*/
55 size
=omp_get_num_threads();
56 data
=(int*) malloc(size
*sizeof(int));
61 for (iter
= 0; iter
< 100; iter
++) {
62 my_random
= rand(); /* random number generator is
63 called inside serial region*/
65 /* the first parallel region is used to initialize myvalue
66 and the array with my_random+rank */
70 rank
= omp_get_thread_num ();
71 myvalue
= data
[rank
] = my_random
+ rank
;
74 /* the second parallel region verifies that the
75 value of "myvalue" is retained */
76 #pragma omp parallel reduction(+:num_failed)
79 rank
= omp_get_thread_num ();
80 num_failed
= num_failed
+ (myvalue
!= data
[rank
]);
81 if(myvalue
!= data
[rank
]) {
82 fprintf (stderr
, " myvalue = %d, data[rank]= %d\n",
88 return (known_sum
== sum
) && !num_failed
;
89 } /* end of check_threadprivate*/
96 for(i
= 0; i
< REPETITIONS
; i
++) {
97 if(!test_omp_threadprivate()) {