openmp_wrapper.h 2.8 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
class ThreadExceptionHelper {
Nikita Titov's avatar
Nikita Titov committed
29
 public:
30
31
  ThreadExceptionHelper() {
    ex_ptr_ = nullptr;
32
33
  }

34
  ~ThreadExceptionHelper() {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    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
49
50

 private:
51
52
53
54
55
56
  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
57
58
59
60
61
62
63
64
65
#define OMP_LOOP_EX_END()                 \
  }                                       \
  catch (std::exception & ex) {           \
    Log::Warning(ex.what());              \
    omp_except_helper.CaptureException(); \
  }                                       \
  catch (...) {                           \
    omp_except_helper.CaptureException(); \
  }
66
67
#define OMP_THROW_EX() omp_except_helper.ReThrow()

68
#else
69

70
/*
Andrew Ziem's avatar
Andrew Ziem committed
71
 * To be compatible with OpenMP, define a nothrow macro which is used by gcc
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 * 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

86
#ifdef _MSC_VER
87
  #pragma warning(disable : 4068)  // disable unknown pragma warning
88
89
90
91
92
93
94
95
#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 **/
96
97
98
99
100
  inline void omp_set_num_threads(int) __GOMP_NOTHROW {}  // NOLINT (no cast done here)
  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; }
101
#ifdef __cplusplus
102
}  // extern "C"
103
104
105
106
107
108
109
#endif

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

110
111
112
113
114
#endif



#endif /* LIGHTGBM_OPENMP_WRAPPER_H_ */