You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.h 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * \file src/core/include/megbrain/common.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #pragma once
  12. #include "megbrain_build_config.h"
  13. #include <memory>
  14. #include <string>
  15. #include <mutex>
  16. #include <exception>
  17. #include <cstdint>
  18. #include <cstddef>
  19. #include <cstdarg>
  20. #include <cstdlib>
  21. namespace mgb {
  22. /* ================ compiler related ================ */
  23. //! comma to be used in macros for template arguments
  24. #define MGB_COMMA ,
  25. //! branch prediction hint: likely to take
  26. #define mgb_likely(v) __builtin_expect(static_cast<bool>(v), 1)
  27. //! branch prediction hint: unlikely to take
  28. #define mgb_unlikely(v) __builtin_expect(static_cast<bool>(v), 0)
  29. //! mark a var to be used
  30. #define MGB_MARK_USED_VAR(var) static_cast<void>(var)
  31. //! remove padding in a struct
  32. #define MGB_PACKED __attribute__((packed))
  33. //! ask the compiler to not inline a function
  34. #define MGB_NOINLINE __attribute__((noinline))
  35. //! warn if result of a function is not used
  36. #define MGB_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  37. /* ================ exception and assertion ================ */
  38. #ifndef mgb_trap
  39. #define mgb_trap() __builtin_trap()
  40. #endif
  41. #if MGB_ENABLE_EXCEPTION
  42. //! throw raw exception object
  43. #define mgb_throw_raw(_exc...) throw _exc
  44. //! try block
  45. #define MGB_TRY try
  46. //! catch block
  47. #define MGB_CATCH(_decl, _stmt) \
  48. catch(_decl) _stmt
  49. #else
  50. #if MGB_ENABLE_LOGGING
  51. #define mgb_throw_raw(_exc...) ::mgb::__on_exception_throw__(_exc)
  52. void __on_exception_throw__(const std::exception &exc)
  53. __attribute__((noreturn));
  54. #else
  55. #define mgb_throw_raw(_exc...) mgb_trap()
  56. #endif
  57. #define MGB_TRY
  58. #define MGB_CATCH(_decl, _stmt)
  59. #endif // MGB_ENABLE_EXCEPTION
  60. //! used after try-catch block, like try-finally construct in python
  61. #define MGB_FINALLY(_stmt) \
  62. MGB_CATCH(..., {_stmt; throw; }) \
  63. _stmt
  64. #if MGB_ENABLE_LOGGING
  65. //! throw exception with given message
  66. #define mgb_throw(_exc, _msg...) mgb_throw_raw(_exc(::mgb::ssprintf(_msg)))
  67. #else
  68. //! throw exception with given message
  69. #define mgb_throw(_exc, _msg...) mgb_throw_raw(_exc(""))
  70. #endif
  71. //! throw exception with given message if condition is true
  72. #define mgb_throw_if(_cond, _exc, _msg...) \
  73. do { \
  74. if (mgb_unlikely((_cond))) \
  75. mgb_throw(_exc, _msg); \
  76. } while(0)
  77. // assert
  78. #if MGB_ASSERT_LOC
  79. /*!
  80. * \brief extended assert
  81. * extra diagnostics message (in printf format) could be printed when assertion
  82. * fails; the asserted expression is guaranteed to be evaluated
  83. */
  84. #define mgb_assert(expr, msg...) \
  85. do { \
  86. if (mgb_unlikely(!(expr))) \
  87. ::mgb::__assert_fail__(__FILE__, __LINE__, \
  88. __PRETTY_FUNCTION__, # expr, ##msg); \
  89. } while(0)
  90. void __assert_fail__(
  91. const char *file, int line, const char *func,
  92. const char *expr, const char *msg_fmt = 0, ...)
  93. __attribute__((format(printf, 5, 6), noreturn));
  94. #else
  95. #define mgb_assert(expr, msg...) \
  96. do { \
  97. if (mgb_unlikely(!(expr))) \
  98. ::mgb::__assert_fail__(); \
  99. } while(0)
  100. void __assert_fail__() __attribute__((noreturn));
  101. #endif // MGB_ASSERT_LOC
  102. /* ================ logging ================ */
  103. //! caused by need remve sensitive words at opt release
  104. #if MGB_ENABLE_LOGGING
  105. #define mgb_log_debug(fmt...) \
  106. _mgb_do_log(::mgb::LogLevel::DEBUG, __FILE__, __func__, __LINE__, fmt)
  107. #define mgb_log(fmt...) \
  108. _mgb_do_log(::mgb::LogLevel::INFO, __FILE__, __func__, __LINE__, fmt)
  109. #define mgb_log_warn(fmt...) \
  110. _mgb_do_log(::mgb::LogLevel::WARN, __FILE__, __func__, __LINE__, fmt)
  111. #define mgb_log_error(fmt...) \
  112. _mgb_do_log(::mgb::LogLevel::ERROR, __FILE__, __func__, __LINE__, fmt)
  113. #else
  114. #define mgb_log_debug(fmt...) \
  115. _mgb_do_log(::mgb::LogLevel::DEBUG, "", "", 1, fmt)
  116. #define mgb_log(fmt...) \
  117. _mgb_do_log(::mgb::LogLevel::INFO, "", "", 1, fmt)
  118. #define mgb_log_warn(fmt...) \
  119. _mgb_do_log(::mgb::LogLevel::WARN, "", "", 1, fmt)
  120. #define mgb_log_error(fmt...) \
  121. _mgb_do_log(::mgb::LogLevel::ERROR, "", "", 1, fmt)
  122. #endif
  123. enum class LogLevel { DEBUG, INFO, WARN, ERROR };
  124. typedef void(*LogHandler)(LogLevel level,
  125. const char *file, const char *func, int line, const char *fmt,
  126. va_list ap);
  127. /*!
  128. * \brief set logging level
  129. * messages lower than given level would not be sent to log handler
  130. *
  131. * \return previous log level
  132. */
  133. LogLevel set_log_level(LogLevel level);
  134. /*!
  135. * \brief set callback for receiving log requests
  136. * \return previous log handler
  137. */
  138. LogHandler set_log_handler(LogHandler handler);
  139. #if MGB_ENABLE_LOGGING
  140. void __log__(LogLevel level, const char *file, const char *func, int line,
  141. const char *fmt, ...)
  142. __attribute__((format(printf, 5, 6)));
  143. #define _mgb_do_log ::mgb::__log__
  144. //! make a string used for log
  145. #define mgb_ssprintf_log ::mgb::ssprintf
  146. //! v if log is enabled, and "" if not
  147. #define mgb_cstr_log(v) v
  148. #else
  149. #define _mgb_do_log(...) do{} while(0)
  150. #define mgb_ssprintf_log(...) ::std::string{}
  151. #define mgb_cstr_log(v) ""
  152. #endif // MGB_ENABLE_LOGGING
  153. /* ================ misc ================ */
  154. #if MGB_ENABLE_GETENV
  155. #define MGB_GETENV ::std::getenv
  156. #else
  157. #define MGB_GETENV(_name) static_cast<char*>(nullptr)
  158. #endif
  159. // use some macro tricks to get lock guard with unique variable name
  160. #define MGB_TOKENPASTE(x, y) x ## y
  161. #define MGB_TOKENPASTE2(x, y) MGB_TOKENPASTE(x, y)
  162. #define MGB_LOCK_GUARD_CTOR(mtx) MGB_TOKENPASTE2(__lock_guard_, __LINE__)(mtx)
  163. #define MGB_LOCK_GUARD(mtx) \
  164. std::lock_guard<decltype(mtx)> MGB_LOCK_GUARD_CTOR(mtx)
  165. #define MGB_LOCK_GUARD_UNIQUE(mtx) \
  166. std::unique_lock<decltype(mtx)> MGB_LOCK_GUARD_CTOR(mtx)
  167. #define MGB_LOCK_GUARD_SHARED(mtx) \
  168. std::shared_lock<decltype(mtx)> MGB_LOCK_GUARD_CTOR(mtx)
  169. /*!
  170. * \brief printf-like std::string constructor
  171. */
  172. std::string ssprintf(const char *fmt, ...)
  173. __attribute__((format(printf, 1, 2)));
  174. std::string svsprintf(const char *fmt, va_list ap);
  175. #if 0
  176. // used for win32 with vs prior to 2015
  177. const char* convert_fmt_str(const char *fmt);
  178. static inline const char* operator "" _fmt(const char *fmt, std::size_t) {
  179. return convert_fmt_str(fmt);
  180. }
  181. #else
  182. static inline constexpr const char* convert_fmt_str(const char *fmt) {
  183. return fmt;
  184. }
  185. static inline constexpr const char* operator "" _fmt(
  186. const char *fmt, std::size_t) {
  187. return convert_fmt_str(fmt);
  188. }
  189. inline constexpr std::size_t operator"" _z(unsigned long long n) {
  190. return n;
  191. }
  192. #endif
  193. } // namespace mgb
  194. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台