我正在尝试加速大型程序的特定部分,异常处理是在高层完成的,最初的代码看起来像
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 aparallel
region must cause execution to resume within the sameparallel
region, and the same thread that threw the exception must catch it.
像 GCC 这样的编译器通过使用类似于以下的包罗万象的结构包装并行区域的代码来强制执行此要求:
try
{
...
}
catch
{
terminate();
}
因此,任何到达并行
区域末尾但未捕获的异常都将导致程序中止。
该规则实际上更严格,因为它也适用于 OpenMP 结构,例如 for
、ritic
、single
等。这样的构造必须被抛出它的线程捕获在同一个构造中。在您的情况下,是 for
构造导致终止,因为它的隐式 catch-all 处理程序在 parallel
区域的隐式 catch-all 处理程序之前到达。
关于c++ - 在 Openmp 区域内抛出 std::runtime_error 会使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737531/