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.

comp_node_env.cpp 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * \file src/core/impl/comp_node_env.cpp
  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. #include "megbrain/comp_node_env.h"
  12. #include "megbrain/exception.h"
  13. #include "megbrain/system.h"
  14. #include "megbrain/utils/metahelper.h"
  15. #include "megbrain/version_symbol.h"
  16. #include "megdnn/version.h"
  17. #if MGB_CUDA
  18. #include "megcore_cuda.h"
  19. #if MGB_ENABLE_DEBUG_UTIL
  20. #include <nvToolsExtCudaRt.h>
  21. #endif
  22. #endif
  23. using namespace mgb;
  24. /* =================== MegDNNHandle =================== */
  25. MGB_TYPEINFO_OBJ_IMPL(MegDNNHandle);
  26. int MegDNNHandle::sm_default_dbg_level = 0;
  27. MegDNNHandle& MegDNNHandle::get(const CompNodeEnv& env) {
  28. auto maker = [&]() { return std::make_shared<MegDNNHandle>(env); };
  29. return env.get_user_data<MegDNNHandle>(maker);
  30. }
  31. MegDNNHandle::MegDNNHandle(const CompNodeEnv& env) {
  32. auto megdnn_version = megdnn::get_version();
  33. mgb_throw_if(
  34. megdnn_version.major != MEGDNN_MAJOR ||
  35. megdnn_version.minor < MEGDNN_MINOR,
  36. SystemError,
  37. "incompatible megdnn version: compiled with %d.%d, get %d.%d.%d "
  38. "at runtime",
  39. MEGDNN_MAJOR, MEGDNN_MINOR, megdnn_version.major,
  40. megdnn_version.minor, megdnn_version.patch);
  41. bool init = false;
  42. #if MGB_CUDA
  43. if (env.property().type == CompNode::DeviceType::CUDA) {
  44. megcoreCreateDeviceHandle(&m_dev_hdl, megcorePlatformCUDA,
  45. env.cuda_env().device, 0);
  46. megcore::createComputingHandleWithCUDAContext(&m_comp_hdl, m_dev_hdl, 0,
  47. {env.cuda_env().stream, make_async_error_info(env)});
  48. init = true;
  49. }
  50. #endif
  51. if (env.property().type == CompNode::DeviceType::CPU) {
  52. megcoreCreateDeviceHandle(&m_dev_hdl, megcorePlatformCPU);
  53. megcoreCreateComputingHandleWithCPUDispatcher(&m_comp_hdl, m_dev_hdl,
  54. env.cpu_env().dispatcher);
  55. init = true;
  56. }
  57. mgb_assert(init);
  58. int level = sm_default_dbg_level;
  59. if (auto set = MGB_GETENV("MGB_USE_MEGDNN_DBG")) {
  60. level = std::stol(set);
  61. mgb_log_warn("use megdnn handle with debug level: %d", level);
  62. }
  63. // handle may have been implemented when device type is cadence.
  64. if (!m_megdnn_handle) {
  65. m_megdnn_handle = megdnn::Handle::make(m_comp_hdl, level);
  66. }
  67. }
  68. MegDNNHandle::~MegDNNHandle() noexcept {
  69. m_megdnn_handle.reset();
  70. #if MGB_NEED_MEGDNN_ASYNC_ERROR
  71. m_async_error_info_devptr.reset();
  72. #endif
  73. if (m_comp_hdl) {
  74. megcoreDestroyComputingHandle(m_comp_hdl);
  75. }
  76. if (m_dev_hdl) {
  77. megcoreDestroyDeviceHandle(m_dev_hdl);
  78. }
  79. }
  80. #if MGB_NEED_MEGDNN_ASYNC_ERROR
  81. megcore::AsyncErrorInfo* MegDNNHandle::make_async_error_info(
  82. const CompNodeEnv& env) {
  83. auto cn = env.comp_node();
  84. auto del = [cn](megcore::AsyncErrorInfo* ptr) {
  85. if (ptr) {
  86. cn.free_device(ptr);
  87. }
  88. };
  89. megcore::AsyncErrorInfo zero_info{0, nullptr, "", {0,0,0,0}};
  90. auto ptr = static_cast<megcore::AsyncErrorInfo*>(
  91. env.comp_node().alloc_device(sizeof(zero_info)));
  92. cn.copy_to_device(ptr, &zero_info, sizeof(zero_info));
  93. cn.sync();
  94. m_async_error_info_devptr = {ptr, del};
  95. return m_async_error_info_devptr.get();
  96. }
  97. #endif
  98. /* =================== misc =================== */
  99. #if MGB_CUDA
  100. void mgb::_on_cuda_error(const char* expr, cudaError_t err, const char* file,
  101. const char* func, int line) {
  102. mgb_throw(CudaError, "cuda error %d: %s (%s at %s:%s:%d)", int(err),
  103. cudaGetErrorString(err), expr, file, func, line);
  104. }
  105. void CompNodeEnv::init_cuda_async(int dev, CompNode comp_node,
  106. const ContinuationCtx<cudaStream_t>& cont) {
  107. m_comp_node = comp_node;
  108. mgb_assert(!m_user_data_container && !m_async_init_need_wait);
  109. m_cuda_env.device = dev;
  110. m_property.type = DeviceType::CUDA;
  111. MGB_CUDA_CHECK(cudaGetDeviceProperties(&m_cuda_env.device_prop, dev));
  112. {
  113. auto&& prop = m_cuda_env.device_prop;
  114. m_property.mem_alignment =
  115. std::max(prop.textureAlignment, prop.texturePitchAlignment);
  116. }
  117. std::atomic_bool tid_set{false};
  118. auto worker = [this, cont, &tid_set]() {
  119. sys::set_thread_name("async_cuda_init");
  120. m_async_init_tid = std::this_thread::get_id();
  121. tid_set.store(true);
  122. bool stream_done = false;
  123. MGB_MARK_USED_VAR(stream_done);
  124. MGB_TRY {
  125. m_cuda_env.activate();
  126. MGB_CUDA_CHECK(cudaStreamCreateWithFlags(&m_cuda_env.stream,
  127. cudaStreamNonBlocking));
  128. stream_done = true;
  129. m_user_data_container = std::make_unique<UserDataContainer>();
  130. #if MGB_ENABLE_DEBUG_UTIL
  131. nvtxNameCudaStreamA(m_cuda_env.stream,
  132. m_comp_node.to_string().c_str());
  133. #endif
  134. cont.next(m_cuda_env.stream);
  135. // megdnn is initialized here; must be placed after cont.next()
  136. // which handles comp node init
  137. mgb_assert(
  138. m_property.mem_alignment ==
  139. MegDNNHandle::get(*this).handle()->alignment_requirement());
  140. }
  141. MGB_CATCH(std::exception & exc, {
  142. mgb_log_error("async cuda init failed: %s", exc.what());
  143. if (stream_done) {
  144. cudaStreamDestroy(m_cuda_env.stream);
  145. }
  146. cont.err(exc);
  147. throw;
  148. })
  149. };
  150. m_async_init_need_wait = true;
  151. m_async_init_future = std::async(std::launch::async, worker);
  152. while (!tid_set.load())
  153. std::this_thread::yield();
  154. mgb_assert(m_async_init_tid != std::this_thread::get_id());
  155. }
  156. #endif
  157. void CompNodeEnv::init_cpu(const CpuEnv& env, CompNode comp_node) {
  158. m_comp_node = comp_node;
  159. mgb_assert(!m_user_data_container);
  160. m_property.type = DeviceType::CPU;
  161. m_cpu_env = env;
  162. m_user_data_container = std::make_unique<UserDataContainer>();
  163. m_property.mem_alignment =
  164. MegDNNHandle::get(*this).handle()->alignment_requirement();
  165. }
  166. void CompNodeEnv::fini() {
  167. ensure_async_init_finished();
  168. m_user_data_container.reset();
  169. #if MGB_CUDA
  170. if (m_property.type == DeviceType::CUDA) {
  171. m_cuda_env.activate();
  172. MGB_CUDA_CHECK(cudaStreamDestroy(m_cuda_env.stream));
  173. }
  174. #endif
  175. }
  176. #if MGB_ENABLE_COMP_NODE_ASYNC_INIT
  177. void CompNodeEnv::wait_async_init() {
  178. if (std::this_thread::get_id() == m_async_init_tid)
  179. return;
  180. MGB_LOCK_GUARD(m_async_init_mtx);
  181. if (m_async_init_need_wait.load()) {
  182. m_async_init_future.wait();
  183. m_async_init_need_wait.store(false);
  184. m_async_init_future.get();
  185. }
  186. }
  187. #endif
  188. void CompNodeEnv::on_bad_device_type(DeviceType expected) const {
  189. mgb_throw(MegBrainError, "bad device type: expected=%d actual=%d",
  190. static_cast<int>(expected), static_cast<int>(m_property.type));
  191. }
  192. MGB_VERSION_SYMBOL3(MEGDNN, MEGDNN_MAJOR, MEGDNN_MINOR, MEGDNN_PATCH);
  193. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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