treelite
omp_exception.h
Go to the documentation of this file.
1 
7 #ifndef TREELITE_DETAIL_OMP_EXCEPTION_H_
8 #define TREELITE_DETAIL_OMP_EXCEPTION_H_
9 
10 #include <treelite/error.h>
11 
12 #include <exception>
13 #include <mutex>
14 
15 namespace treelite {
16 
20 class OMPException {
21  private:
22  // exception_ptr member to store the exception
23  std::exception_ptr omp_exception_;
24  // mutex to be acquired during catch to set the exception_ptr
25  std::mutex mutex_;
26 
27  public:
31  template <typename Function, typename... Parameters>
32  void Run(Function f, Parameters... params) {
33  try {
34  f(params...);
35  } catch (treelite::Error& ex) {
36  std::lock_guard<std::mutex> lock(mutex_);
37  if (!omp_exception_) {
38  omp_exception_ = std::current_exception();
39  }
40  } catch (std::exception& ex) {
41  std::lock_guard<std::mutex> lock(mutex_);
42  if (!omp_exception_) {
43  omp_exception_ = std::current_exception();
44  }
45  }
46  }
47 
51  void Rethrow() {
52  if (this->omp_exception_) {
53  std::rethrow_exception(this->omp_exception_);
54  }
55  }
56 };
57 
58 } // namespace treelite
59 
60 #endif // TREELITE_DETAIL_OMP_EXCEPTION_H_
OMP Exception class catches, saves and rethrows exception from OMP blocks.
Definition: omp_exception.h:20
void Run(Function f, Parameters... params)
Parallel OMP blocks should be placed within Run to save exception.
Definition: omp_exception.h:32
void Rethrow()
should be called from the main thread to rethrow the exception
Definition: omp_exception.h:51
Exception class used throughout the Treelite codebase.
Definition: contiguous_array.h:14
Exception class that will be thrown by Treelite.
Definition: error.h:18