[Omp] static variables

Jakub Jelinek jakub at redhat.com
Wed Nov 30 10:15:32 PST 2005


On Wed, Nov 30, 2005 at 06:04:20PM +0000, Patricia Bittencourt Sampaio wrote:
>      I have some doubt about static variables at
> OpenMP. When I have a piece of code like this:
>  
> 
>   static void routine_A ()
>   {
>        static double d, sum;
> 
>        .
>        .
>        .
>   }
> 
>   int main()
>   {
>        #pragma omp parallel private(it)
>        {
>             for (it = 1; it <= NITER; it++) 
>                  routine_A();
>             
>        }
>               
>   }
> 
>   In this case, each thread created at the parallel
> region will execute routine_A with a number of times
> equal to NITER. 
>   The variables "d" and "sum" will be private of each
> thread, although, because of the behaviour of static

No, they are shared.  Both d and sum variables aren't referenced
in the parallel construct, so OpenMP 2.5 2.8.1.2 applies.
And the very first C/C++ rule there is:
"Static variables declared in called routines in the region are shared."

If you want the variables private to each thread, but let
all NITER calls in the same thread see the same variable,
you need to mark them
#pragma omp threadprivate (d, sum)

	Jakub




More information about the Omp mailing list