For循环内部并行区域 [英] For-loop inside parallel region
问题描述
如果在并行区域内有一个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)
.
这篇关于For循环内部并行区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!