openmp_wrapper.h 3.09 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2017 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
5
6
#ifndef LIGHTGBM_OPENMP_WRAPPER_H_
#define LIGHTGBM_OPENMP_WRAPPER_H_
7

8
#ifdef _OPENMP
9

10
11
12
13
#include <LightGBM/utils/log.h>

#include <omp.h>

14
#include <exception>
15
#include <memory>
16
#include <mutex>
17
#include <stdexcept>
18
19
#include <vector>

20
21
22
23
24
25
26
27
inline int OMP_NUM_THREADS() {
  int ret = 1;
#pragma omp parallel
#pragma omp master
  { ret = omp_get_num_threads(); }
  return ret;
}

28
29
30
31
32
33
34
35
36
inline void OMP_SET_NUM_THREADS(int num_threads) {
  static const int default_omp_num_threads = OMP_NUM_THREADS();
  if (num_threads > 0) {
    omp_set_num_threads(num_threads);
  } else {
    omp_set_num_threads(default_omp_num_threads);
  }
}

37
class ThreadExceptionHelper {
Nikita Titov's avatar
Nikita Titov committed
38
 public:
39
40
  ThreadExceptionHelper() {
    ex_ptr_ = nullptr;
41
42
  }

43
  ~ThreadExceptionHelper() {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    ReThrow();
  }
  void ReThrow() {
    if (ex_ptr_ != nullptr) {
      std::rethrow_exception(ex_ptr_);
    }
  }
  void CaptureException() {
    // only catch first exception.
    if (ex_ptr_ != nullptr) { return; }
    std::unique_lock<std::mutex> guard(lock_);
    if (ex_ptr_ != nullptr) { return; }
    ex_ptr_ = std::current_exception();
  }
Nikita Titov's avatar
Nikita Titov committed
58
59

 private:
60
61
62
63
64
65
  std::exception_ptr ex_ptr_;
  std::mutex lock_;
};

#define OMP_INIT_EX() ThreadExceptionHelper omp_except_helper
#define OMP_LOOP_EX_BEGIN() try {
Guolin Ke's avatar
Guolin Ke committed
66
67
68
69
70
71
72
73
74
#define OMP_LOOP_EX_END()                 \
  }                                       \
  catch (std::exception & ex) {           \
    Log::Warning(ex.what());              \
    omp_except_helper.CaptureException(); \
  }                                       \
  catch (...) {                           \
    omp_except_helper.CaptureException(); \
  }
75
76
#define OMP_THROW_EX() omp_except_helper.ReThrow()

77
#else
78

79
/*
Andrew Ziem's avatar
Andrew Ziem committed
80
 * To be compatible with OpenMP, define a nothrow macro which is used by gcc
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 * openmp, but not by clang.
 * See also https://github.com/dmlc/dmlc-core/blob/3106c1cbdcc9fc9ef3a2c1d2196a7a6f6616c13d/include/dmlc/omp.h#L14
 */
#if defined(__clang__)
#undef __GOMP_NOTHROW
#define __GOMP_NOTHROW
#elif defined(__cplusplus)
#undef __GOMP_NOTHROW
#define __GOMP_NOTHROW throw()
#else
#undef __GOMP_NOTHROW
#define __GOMP_NOTHROW __attribute__((__nothrow__))
#endif

95
#ifdef _MSC_VER
96
  #pragma warning(disable : 4068)  // disable unknown pragma warning
97
98
99
100
101
102
103
104
#endif

#ifdef __cplusplus
  extern "C" {
#endif
  /** Fall here if no OPENMP support, so just
      simulate a single thread running.
      All #pragma omp should be ignored by the compiler **/
105
  inline void omp_set_num_threads(int) __GOMP_NOTHROW {}  // NOLINT (no cast done here)
106
  inline void OMP_SET_NUM_THREADS(int) __GOMP_NOTHROW {}
107
108
109
110
  inline int omp_get_num_threads() __GOMP_NOTHROW {return 1;}
  inline int omp_get_max_threads() __GOMP_NOTHROW {return 1;}
  inline int omp_get_thread_num() __GOMP_NOTHROW {return 0;}
  inline int OMP_NUM_THREADS() __GOMP_NOTHROW { return 1; }
111
#ifdef __cplusplus
112
}  // extern "C"
113
114
115
116
117
118
119
#endif

#define OMP_INIT_EX()
#define OMP_LOOP_EX_BEGIN()
#define OMP_LOOP_EX_END()
#define OMP_THROW_EX()

120
121
122
123
124
#endif



#endif /* LIGHTGBM_OPENMP_WRAPPER_H_ */