7

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".

1
  • Note also a small typo in your example code: your multiple function adds the values from its input vectors Commented Jul 20, 2015 at 18:54

2 Answers 2

8

Nested parallelism is disabled by default, unless enabled specificially by setting OMP_NESTED to true or by calling omp_set_nested(1); (§2.3.2 of the OpenMP specification) Explicitly modifying the settings for nesting as suggested by Avi Ginsburg is a bad idea. Instead, you should use conditional parallel execution based on the level of nesting:

double multiply(std::vector<double> const& a, std::vector<double> const& b){
    double tmp(0);
    int active_levels = omp_get_active_level();
    #pragma omp parallel for reduction(+:tmp) if(active_level < 1)
    for(unsigned int i=0;i<a.size();i++){
        tmp += a[i]+b[i];
    }
    return tmp;
}

omp_get_active_level() returns the number of active parallel regions that enclose the thread at the moment the call is made. It returns 0 if called from outside a parallel region or with inactive outer region(s). Thanks to the if(active_level < 1) clause, the parallel region will only be activated, i.e. run in parallel, if it is not enclosed in an active region, regardless of the setting for nesting.

If your compiler does not support OpenMP 3.0 or higher (e.g. with any version of MS Visual C/C++ Compiler), then omp_in_parallel() call can be used instead:

double multiply(std::vector<double> const& a, std::vector<double> const& b){
    double tmp(0);
    int in_parallel = omp_in_parallel();
    #pragma omp parallel for reduction(+:tmp) if(in_parallel == 0)
    for(unsigned int i=0;i<a.size();i++){
        tmp += a[i]+b[i];
    }
    return tmp;
}

omp_in_parallel() returns non-zero if at least one enclosing parallel region is active, but does not provide information about the depth of nesting, i.e. is a bit less flexible.

In any case, writing such code is a bad practice. You should simply leave the parallel regions as they are and allow the end user choose whether nested parallelism should be enabled or not.

2
  • Interesting. From what version of OMP does omp_get_active_level exist? I usually use VS (stuck at 2.0) and I know it doesn't exist there. Commented Jul 20, 2015 at 17:35
  • Individual control over the different levels of parallelism was introduced in OpenMP 3.0. With pre-3.0 compilers one could use omp_in_parallel() instead. Commented Jul 20, 2015 at 18:17
1

Add the pragma to both functions. You can turn the nested parallelism on and off with omp_set_nested(int val) (zero for off, non-zero for on).

So, if you wanted nested parallelism on in your program in general, but off for the many_multiplication function, you would implement many_multiplication as follows:

void many_multiplication(std::vector<double>* a, std::vector<double>* b, unsigned int N){
    omp_set_nested(0);
    #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]);
        }
    }
    omp_set_nested(1);
}
4
  • thx. but if I understand your answer and if I never want nested parallelism, I could just add the pragama to both functions and when multiply is called from outside a parallel region, it will then parallelize the loop for but when it's called within a parallel region it won't parallelize the loop.
    – PinkFloyd
    Commented Jul 20, 2015 at 16:47
  • Yes, but if you never wanted nested parallelism, you can define the environment variable OMP_NESTED as true instead. Commented Jul 20, 2015 at 16:49
  • Sorry, I meant OMP_NESTED should be defined as false to disable nested parallelism. If undefined, false is the default. Commented Jul 20, 2015 at 17:19
  • 1
    Calling omp_set_nested(1); at the end enables unconditionally nesting, no matter what the value of OMP_NESTED and therefore what the expectation of the end user might be. Commented Jul 20, 2015 at 17:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.