c++ - 在 Openmp 区域内抛出 std::runtime_error 会使程序崩溃

标签 c++ exception openmp

我正在尝试加速大型程序的特定部分,异常处理是在高层完成的,最初的代码看起来像

for(....)
{
...
   if(...)
   {
    throw std:: runtime_error("Terminated by user")
   }
}

现在我已将其更改为类似的内容

#pragma omp parallel for ...
for(....)
{
...
   if(...)
   {
    throw std:: runtime_error("Terminated by user")
   }
}

现在,如果触发终止,程序就会崩溃,我期望这里的异常处理可以以一种优雅的方式完成,而无需更改更高级别的内容?

最佳答案

OpenMP specification要求某个线程抛出的异常必须由同一线程并在同一并行区域内处理(第 2.5 节,第 49 页):

A throw executed inside a parallel region must cause execution to resume within the same parallel region, and the same thread that threw the exception must catch it.

像 GCC 这样的编译器通过使用类似于以下的包罗万象的结构包装并行区域的代码来强制执行此要求:

try
{
   ...
}
catch
{
   terminate();
}

因此,任何到达并行区域末尾但未捕获的异常都将导致程序中止。

该规则实际上更严格,因为它也适用于 OpenMP 结构,例如 forriticsingle 等。这样的构造必须被抛出它的线程捕获在同一个构造中。在您的情况下,是 for 构造导致终止,因为它的隐式 catch-all 处理程序在 parallel 区域的隐式 catch-all 处理程序之前到达。

关于c++ - 在 Openmp 区域内抛出 std::runtime_error 会使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737531/

相关文章:

java - 模仿java中的printStackTrace()方法

python - 如果第一个元素是异常,为什么提升元组有效?

OpenMP #pragma num_threads

c++ - 等待条件变量破坏类

c++ - 理解 std::future::then 的延续

c++ - std::async 使用同一个线程,我的代码没有实现并行。

python - Tensorflow OMP : Error #15 when training

c++ - 如何正确清理 vector/ map ?

jquery - Python Pyramid View 中的异常管理

c - 是否仍然值得在 OpenMP 实现上投入时间?