For-loop inside parallel region

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 06:25:09

问题


If there is a for-loop inside a parallel region, would for-loop be parallelized again or every thread will execute its own for-loop?

T sum;

#pragma omp parallel
{
    #pragma omp for reduction(+: sum)
    for (;;)
    {
        T priv_var;

        sum += priv_var;
    }
}

回答1:


Yes, this code will cause OpenMP to parallelise the for loop across the threads that are spawned by the parallel region. However, I believe that your current for statement is invalid for OpenMP parallelisation. You need to explicitly provide an integer loop variable, start and end, and increment expression.

In effect, your code will then be equivalent to a single loop with #pragma omp parallel for reduction(+: sum).

More information on MDSN



来源:https://stackoverflow.com/questions/11493265/for-loop-inside-parallel-region

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!