Unverified Commit 89da9902 authored by Benjamin Sergeant's avatar Benjamin Sergeant Committed by GitHub
Browse files

openmp_wrapper.h stubs signature use __GOMP_NOTHROW (#3923)



* openmp_wrapper.h stubs signature use __GOMP_NOTHROW

Fix #3915. OpenMP stubs do not use the noexcept attibute which is present in the gcc version of openmp, and which trigger compilation errors as seen below. dmlc-core uses the same technique and macro.

/usr/lib/gcc/x86_64-linux-gnu/9/include/omp.h:114:12: error: declaration of ‘int omp_get_thread_num() noexcept’ has a different exception specifier
  114 | extern int omp_get_thread_num (void) __GOMP_NOTHROW;
      |            ^~~~~~~~~~~~~~~~~~
...
xxx/include/LightGBM/utils/openmp_wrapper.h:81:14: note: from previous declaration ‘int omp_get_thread_num()’
   81 |   inline int omp_get_thread_num() {return 0;}
      |              ^~~~~~~~~~~~~~~~~~

* move __GOMP_NOTHROW definition in the no open mp stub conditional branch

* Update include/LightGBM/utils/openmp_wrapper.h

Yes make sense, just changed it
Co-authored-by: default avatarJames Lamb <jaylamb20@gmail.com>

* add NOLINT macro to disable cpplint on a safe line of code
Co-authored-by: default avatarJames Lamb <jaylamb20@gmail.com>
parent 5abf8bb9
......@@ -4,6 +4,7 @@
*/
#ifndef LIGHTGBM_OPENMP_WRAPPER_H_
#define LIGHTGBM_OPENMP_WRAPPER_H_
#ifdef _OPENMP
#include <LightGBM/utils/log.h>
......@@ -66,6 +67,22 @@ class ThreadExceptionHelper {
#else
/*
* To be compatible with openmp, define a nothrow macro which is used by gcc
* 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
#ifdef _MSC_VER
#pragma warning(disable : 4068) // disable unknown pragma warning
#endif
......@@ -76,11 +93,11 @@ class ThreadExceptionHelper {
/** Fall here if no OPENMP support, so just
simulate a single thread running.
All #pragma omp should be ignored by the compiler **/
inline void omp_set_num_threads(int) {}
inline int omp_get_num_threads() {return 1;}
inline int omp_get_max_threads() {return 1;}
inline int omp_get_thread_num() {return 0;}
inline int OMP_NUM_THREADS() { return 1; }
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; }
#ifdef __cplusplus
} // extern "C"
#endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment