问题
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