openmp : check if nested parallesim

前端 未结 2 1603
北海茫月
北海茫月 2020-12-19 11:49

Assume I have a method that multiplies two std::vector :

double multiply(std::vector const& a, std::vector const         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-19 12:26

    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 const& a, std::vector 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

    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 const& a, std::vector 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

    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.

提交回复
热议问题