For循环内部并行区域 [英] For-loop inside parallel region

查看:105
本文介绍了For循环内部并行区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在并行区域内有一个for循环,for循环是否会再次并行化,还是每个线程都将执行自己的for循环?

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;
    }
}

推荐答案

是的,此代码将导致OpenMP在parallel区域所产生的线程之间并行化for循环.但是,我认为您当前的for语句对于OpenMP并行化无效.您需要显式提供整数循环变量,开始和结束以及增量表达式.

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.

实际上,您的代码将等效于使用#pragma omp parallel for reduction(+: sum)的单个循环.

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

有关MDSN的详细信息

这篇关于For循环内部并行区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆