C++ exceptions in OpenMP parallel regions: What to do?

最后都变了- 提交于 2019-12-13 06:09:58

问题


I've been reading about the topic and I would like to know if I got things right.

Since an exception thrown in a thread must be handled in the thread, I found by searching the web that what people do most often is to create some means to report the exceptions threads have dealt with after they all finish their tasks. Now, as an example, suppose I have a reduction operation, like a sum, and a thread returns NaN, it means that the other threads just wasted time.

Since prematurely ending a parallel region is not (?) an OpenMP thing, what else can we do to stop everything and throw an exception out of the parallel region as soon as a thread throws one?

I've read this post where the author uses ''flush'' to set a flag whose state all threads consult. I quote the code here:

int n(0);
bool abort = false;

#pragma omp parallel for
for ( i=0; i<1000; ++i )
{
    #pragma omp flush (abort)
    if (!abort) {

        //Update progress
        if ( pCall->Aborted ) {
            abort = true;
            #pragma omp flush (abort)
        }

        #pragma omp critical(UpdateProgress)
        pCall->Notify("Progress...", n*100/1000);

        //Do something....

        #pragma omp critical(UpdateProgress)
        ++n;
    }
}

if (abort)
{
    throw abort_exception;
}

The post dates 2007. I would like to know:

  • Did I get things right? (Can't exceptions be thrown out of parallel regions?)
  • Is the most adopted approach this one of reporting exceptions after all threads are done, and hence we must wait perhaps considerable time after the first exception is thrown to know that?
  • What about the code snippet above? What remarks should be made about the impact on performance? Can it be rewritten to be more efficient? What would be an alternative?

Thank you.

来源:https://stackoverflow.com/questions/36664669/c-exceptions-in-openmp-parallel-regions-what-to-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!