kir*_*off 4 c++ multithreading openmp multiprocess
我正在使用OpenMP优化一些代码.如果NO_VALUE在一个循环中遇到,我希望它能够打破.但是,编译器告诉我openMP不允许这样做.我怎么能处理这个?
#pragma omp parallel
{
#pragma omp for reduction(+:functionEvaluation) schedule(dynamic) nowait
for (int j=m_colStart;j<m_colEnd+1;j++)
{
double d = command_->execute().toDouble();
if(d==NO_VALUE)
{
functionEvaluation = NO_VALUE;
break;
}
else
{
functionEvaluation += d;
}
delete command_;
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?谢谢!
Qt没有问题.
你不能在使用openMP并行化的循环中休息.
这背后的推理:假设你有一个包含10次迭代的循环.现在,你想在迭代5中打破循环.你说,
if (iteration==5)
break;
Run Code Online (Sandbox Code Playgroud)
现在进入并行上下文,您已经创建了10个线程,并且每个线程并行执行各自的迭代.因此,当thread5达到某个条件时,您需要所有其他线程
正如您所看到的,在大多数情况下这是不可能/不实用的.因此,OpenMP不允许在并行for循环中使用break.
现在,假设您知道在您的循环中某种破坏条件是非常罕见的,并且您很可能会在并行循环中中断.
Thread i
Check input condition
If input leads to breaking flag a marker break_loop
******** barrier across all threads *************
if flag break_loop is true
then
discontinue this operation
mark a variable
discontinue all the remaining iterations
endif
do your normal work
Run Code Online (Sandbox Code Playgroud)
这只是一个框架.您将不得不查看角落情况,锁定等.为此,您可以让每个线程在迭代之前检查其输入.如果他们的输入条件导致循环中断,他们会设置一个con