C++ and OpenMP

General OpenMP discussion

C++ and OpenMP

Postby erotavlas » Fri Feb 17, 2012 1:27 am

Hi,

I have two questions about openMP. The first one is related to C++. I would like to parallelize a for loop inside a method of a class and the can the data members of the class (private or public) be shared inside an openMP construct (as myVector1, myMatrix, myVector2)?
Code: Select all
int i, j;
#pragma omp parallel default(none) shared(myVector1, myMatrix, myVector2) private(i, j)
{
#pragma omp for schedule (auto)
for(i = 0; i < myVector.size(); ++i)
{
   for(j = 0; j < myVector2.size(); ++j)
   {
      myVector[i] = myMatrix[i][j] * myVector2[j];
   }
}
}

The second question is related to the preprocessor. Can I use the openMP compiler directives inside a preprocessor directive to have or not the parallel code?

Code: Select all
int i, j;
#if PARALLEL
#pragma omp parallel default(none) shared(myVector1, myMatrix, myVector2) private(i, j)
{
#pragma omp for schedule (auto)
#endif
for(i = 0; i < myVector.size(); ++i)
{
   for(j = 0; j < myVector2.size(); ++j)
   {
      myVector[i] = myMatrix[i][j] * myVector2[j];
   }
}
#if PARALLEL
}
#endif


Thank you
erotavlas
 
Posts: 2
Joined: Fri Feb 17, 2012 1:26 am

Re: C++ and OpenMP

Postby erotavlas » Tue Feb 21, 2012 7:47 am

Hi,

I have found the answers to both my questions. The first answer is in the manual of openMP:
[CITE]
shared variable: A variable that is part of another variable (as an array or structure element)
cannot be shared independently of the other components, except for static data
members of C++ classes.
[/CITE]
Therefore the members of a class are shared by default inside a method of the class.
The second answer is yes because the openMP directives are directives of the compiler and so are processed after the pre-processor directive.

Regards,

Salvatore
erotavlas
 
Posts: 2
Joined: Fri Feb 17, 2012 1:26 am


Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 12 guests