openmp_wrapper.h 3.54 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_INCLUDE_LIGHTGBM_UTILS_OPENMP_WRAPPER_H_
#define LIGHTGBM_INCLUDE_LIGHTGBM_UTILS_OPENMP_WRAPPER_H_
7

8
9
10
11
12
13
14
15
16
#include <LightGBM/export.h>

// this can only be changed by LGBM_SetMaxThreads()
LIGHTGBM_EXTERN_C int LGBM_MAX_NUM_THREADS;

// this is modified by OMP_SET_NUM_THREADS(), for example
// by passing num_thread through params
LIGHTGBM_EXTERN_C int LGBM_DEFAULT_NUM_THREADS;

17
#ifdef _OPENMP
18

19
20
21
22
#include <LightGBM/utils/log.h>

#include <omp.h>

23
#include <exception>
24
#include <memory>
25
#include <mutex>
26
#include <stdexcept>
27
28
#include <vector>

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
    Get number of threads to use in OpenMP parallel regions.

    By default, this will return the result of omp_get_max_threads(),
    which is OpenMP-implementation dependent but generally can be controlled
    by environment variable OMP_NUM_THREADS.

    ref:
      - https://www.openmp.org/spec-html/5.0/openmpsu112.html
      - https://gcc.gnu.org/onlinedocs/libgomp/omp_005fget_005fmax_005fthreads.html
*/
LIGHTGBM_EXTERN_C int OMP_NUM_THREADS();

/*
    Update the default number of threads that'll be used in OpenMP parallel
    regions for LightGBM routines where the number of threads aren't directly
    supplied.
*/
LIGHTGBM_EXTERN_C void OMP_SET_NUM_THREADS(int num_threads);
48

49
class ThreadExceptionHelper {
Nikita Titov's avatar
Nikita Titov committed
50
 public:
51
52
  ThreadExceptionHelper() {
    ex_ptr_ = nullptr;
53
54
  }

55
  ~ThreadExceptionHelper() {
56
57
58
59
60
61
62
63
64
    ReThrow();
  }
  void ReThrow() {
    if (ex_ptr_ != nullptr) {
      std::rethrow_exception(ex_ptr_);
    }
  }
  void CaptureException() {
    // only catch first exception.
65
66
67
    if (ex_ptr_ != nullptr) {
      return;
    }
68
    std::unique_lock<std::mutex> guard(lock_);
69
70
71
    if (ex_ptr_ != nullptr) {
      return;
    }
72
73
    ex_ptr_ = std::current_exception();
  }
Nikita Titov's avatar
Nikita Titov committed
74
75

 private:
76
77
78
79
80
81
  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
82
83
84
85
86
87
88
89
90
#define OMP_LOOP_EX_END()                 \
  }                                       \
  catch (std::exception & ex) {           \
    Log::Warning(ex.what());              \
    omp_except_helper.CaptureException(); \
  }                                       \
  catch (...) {                           \
    omp_except_helper.CaptureException(); \
  }
91
92
#define OMP_THROW_EX() omp_except_helper.ReThrow()

93
#else
94

95
/*
Andrew Ziem's avatar
Andrew Ziem committed
96
 * To be compatible with OpenMP, define a nothrow macro which is used by gcc
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 * 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

111
#ifdef _MSC_VER
112
  #pragma warning(disable : 4068)  // disable unknown pragma warning
113
114
115
116
117
118
119
120
#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 **/
121
  inline void OMP_SET_NUM_THREADS(int) __GOMP_NOTHROW {}
122
123
  inline int omp_get_thread_num() __GOMP_NOTHROW {return 0;}
  inline int OMP_NUM_THREADS() __GOMP_NOTHROW { return 1; }
124
#ifdef __cplusplus
125
}  // extern "C"
126
127
128
129
130
131
132
#endif

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

133
134
#endif

135
#endif  // LIGHTGBM_INCLUDE_LIGHTGBM_UTILS_OPENMP_WRAPPER_H_