Medial Code Documentation
Loading...
Searching...
No Matches
openmp_wrapper.h
1#ifndef LIGHTGBM_OPENMP_WRAPPER_H_
2#define LIGHTGBM_OPENMP_WRAPPER_H_
3#ifdef _OPENMP
4
5#include <omp.h>
6#include <exception>
7#include <stdexcept>
8#include <mutex>
9#include <vector>
10#include <memory>
11#include "log.h"
12
13class ThreadExceptionHelper {
14public:
15 ThreadExceptionHelper() {
16 ex_ptr_ = nullptr;
17 }
18
19 ~ThreadExceptionHelper() {
20 ReThrow();
21 }
22 void ReThrow() {
23 if (ex_ptr_ != nullptr) {
24 std::rethrow_exception(ex_ptr_);
25 }
26 }
27 void CaptureException() {
28 // only catch first exception.
29 if (ex_ptr_ != nullptr) { return; }
30 std::unique_lock<std::mutex> guard(lock_);
31 if (ex_ptr_ != nullptr) { return; }
32 ex_ptr_ = std::current_exception();
33 }
34private:
35 std::exception_ptr ex_ptr_;
36 std::mutex lock_;
37};
38
39#define OMP_INIT_EX() ThreadExceptionHelper omp_except_helper
40#define OMP_LOOP_EX_BEGIN() try {
41#define OMP_LOOP_EX_END() } \
42catch(std::exception& ex) { Log::Warning(ex.what()); omp_except_helper.CaptureException(); } \
43catch(...) { omp_except_helper.CaptureException(); }
44#define OMP_THROW_EX() omp_except_helper.ReThrow()
45
46#else
47
48#ifdef _MSC_VER
49 #pragma warning(disable: 4068) // disable unknown pragma warning
50#endif
51
52#ifdef __cplusplus
53 extern "C" {
54#endif
58 inline void omp_set_num_threads(int) {}
59 inline void omp_set_nested(int) {}
60 inline int omp_get_num_threads() {return 1;}
61 inline int omp_get_thread_num() {return 0;}
62#ifdef __cplusplus
63}; // extern "C"
64#endif
65
66#define OMP_INIT_EX()
67#define OMP_LOOP_EX_BEGIN()
68#define OMP_LOOP_EX_END()
69#define OMP_THROW_EX()
70
71#endif
72
73
74
75#endif /* LIGHTGBM_OPENMP_WRAPPER_H_ */
header to handle OpenMP compatibility issues