How to catch exception out of loop parallel OpenMP

How to catch exception out of loop parallel OpenMP

Hi all,

I have a problem when using OpenMP on Intel parallel, I don't know how to catch exception out of loop parallel.

The problem is explaind as below:

Context:
// program code with C language
Void Foo()
{
        #pragma omp parallel for schedule(auto) num_threads(3)
        For(I = 0; I < n; i++)
        {
                  Appear exception.
        }
}

// test code with C++ language
Void TestFoo()
{
        try
        {
               Foo();
        }
       Catch(...)
       {
              //print something
       }
}
Test program crashed and can't catch exception of Foo().

Follow spec 3.0 of OpenMP say reason that:
"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 C/C++ must catch it.."

For more detail, please refer to link: http://www.openmp.org/mp-documents/spec30.pdf (page 35).

Question:

Have any way to catch exception of Foo() at Test code?

Thanks,

TuanHD.

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

This is a general question about OpenMP, not about the specific details of the implementation of the Intel Open-Source openMP runtime. You would therefore be better off asking it in a much more general forum such as http://stackoverflow.com.

(The answer is clear from your question, though, you quote the part of the standard that forbids what you are trying to do...)

The recommended way is to insert the try/catch inside the parallel region.

// program code with C language
Void Foo()
{
  int HadCatch = 0; // or YourCatchType_t YourCatch;
  #pragma omp parallel for schedule(auto) num_threads(3) shared(HadCatch)
  for(i = 0; i < n; i++)
  {
    try
    {
      Appear exception.
    }
    catch(...)
    {
       #pragma omp critical
       {
         //print something
         ++HadCatch; // or HadCatch.happened();
       }
    }
  } // for
  if(HadCatch) throw(HadCatch);
}
// test code with C++ language
Void TestFoo()
{
  Foo();
}

Jim Dempsey

www.quickthreadprogramming.com

Leave a Comment

Please sign in to add a comment. Not a member? Join today