Search code examples
cglobal-variablespragma

pragma omp shortcuts in c


I have some global variables that I need to use inside a parallelized section of my program. So I wrote this:

#define N 2000
int x[N], matrix[N][N]

int main(int argc, char **argv)   
...
#pragma omp parallel
{
  #pragma omp privatethread(x,matrix)
  #pragma omp for
  for(int k=0; k<100;++k)
    /*some function to modify values of x and matrix differently 
     from run to run*/
  ...
}
...

Is the part with #pragma equivalent to:

...
#pragma omp privatethread(x,matrix) parallel for
...

Is there a difference between declaring #pragma omp parallel then #pragma omp privatethread and declaring first #pragma omp privatethread and then #pragma omp parallel for? Do i have to put a { after the privathread part?

#pragma omp privatethread(x,matrix)
{ //<-- here
#pragma omp for

Finally, is that a good way to work with large vectors in c or is it better to use malloc inside my pragma omp and remove the privatethread part? (please let me know if it's better that I ask this ending question in a new post)


Solution

  • 1.The below code gives you an error. Even if you remove the for.

    ...
    #pragma omp privatethread(x,matrix) parallel for
    ...
    

    The declaration must follow this syntax

    #pragma omp threadprivate (list)
    

    2.There is no difference between,

    #pragma omp parallel
    {
       #pragma omp privatethread(list)
       //CODE
    }
    

    and

    #pragma omp privatethread(list) 
    #pragma omp parallel
    {
      //CODE
    } 
    

    As you can see you don't have to put { brackets after the #pragma omp privatethread .This statement doesn't run any code in parallel it just makes the global scope variable local and persistent to a thread.