Assume I have a method that multiplies two std::vector
:
double multiply(std::vector<double> const& a, std::vector<double> const& b){
double tmp(0);
/*here I could easily do a parallelization with*/
/*#pragma omp parallel loop for*/
for(unsigned int i=0;i<a.size();i++){
tmp += a[i]*b[i];
}
return tmp;
}
If I set in this function the pragma macro, a call to multiply(...)
will run
on all threads.
Now assume that somewehere else I want to do many vector multiplication :
void many_multiplication(std::vector<double>* a, std::vector<double>* b, unsigned int N){
/*here I could easily do a parallelization with*/
/*#pragma omp parallel loop for*/
for(unsigned int i=0;i<N;i++){
for(unsigned int j=0;j<N;j++){
multiply(a[i],b[j]);
}
}
}
I could also do the parallelization the same way. But this will lead to unwanted nested parallelism.
How can I check that if multiply(..)
is called within a parallel region,
then the pragma
macro of multiply(...)
is "turn off". And if it's called
from a non-parallel region, then it's "turn on".