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.

exception.cpp 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * \file src/core/impl/exception.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 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/exception.h"
  12. #include "megbrain/common.h"
  13. #include "megbrain/utils/debug.h"
  14. #include "megbrain/comp_node_env.h"
  15. using namespace mgb;
  16. namespace {
  17. class MegDNNErrorHandler final: public megdnn::ErrorHandler {
  18. static MegDNNErrorHandler inst;
  19. void do_on_megdnn_error(const std::string &msg) override {
  20. mgb_throw_raw(MegDNNError{msg});
  21. }
  22. void do_on_tensor_reshape_error(const std::string &msg) override {
  23. mgb_throw_raw(TensorReshapeError{msg});
  24. }
  25. public:
  26. MegDNNErrorHandler() {
  27. set_handler(this);
  28. }
  29. };
  30. MegDNNErrorHandler MegDNNErrorHandler::inst;
  31. }
  32. void MegBrainError::init()
  33. {
  34. m_msg.append("\n");
  35. #if MGB_ENABLE_DEBUG_UTIL
  36. debug::backtrace(2).fmt_to_str(m_msg);
  37. static bool print_exc = MGB_GETENV("MGB_PRINT_EXC");
  38. if (print_exc) {
  39. fprintf(stderr, "mgb: exception occurred: %s\n", m_msg.c_str());
  40. }
  41. #endif
  42. }
  43. CudaError::CudaError(const std::string &msg):
  44. SystemError(msg)
  45. {
  46. m_msg.append(get_cuda_extra_info());
  47. }
  48. std::string CudaError::get_cuda_extra_info() {
  49. #if MGB_CUDA
  50. // get last error and clear error
  51. auto err = cudaGetLastError();
  52. int dev = -1;
  53. cudaGetDevice(&dev);
  54. size_t free_byte = 0, total_byte = 0;
  55. cudaMemGetInfo(&free_byte, &total_byte);
  56. constexpr double SIZE2MB = 1.0 / 1024 / 1024;
  57. return ssprintf("(last_err=%d(%s) "
  58. "device=%d mem_free=%.3fMiB mem_tot=%.3fMiB)",
  59. err, cudaGetErrorString(err),
  60. dev, free_byte * SIZE2MB, total_byte * SIZE2MB);
  61. #else
  62. return "cuda disabled at compile time";
  63. #endif
  64. }
  65. AtlasError::AtlasError(const std::string &msg):
  66. SystemError(msg)
  67. {
  68. }
  69. ROCmError::ROCmError(const std::string &msg):
  70. SystemError(msg)
  71. {
  72. m_msg.append(get_rocm_extra_info());
  73. }
  74. std::string ROCmError::get_rocm_extra_info() {
  75. #if MGB_ROCM
  76. // get last error and clear error
  77. auto err = hipGetLastError();
  78. int dev = -1;
  79. hipGetDevice(&dev);
  80. size_t free_byte = 0, total_byte = 0;
  81. hipMemGetInfo(&free_byte, &total_byte);
  82. constexpr double SIZE2MB = 1.0 / 1024 / 1024;
  83. return ssprintf("(last_err=%d(%s) "
  84. "device=%d mem_free=%.3fMiB mem_tot=%.3fMiB)",
  85. err, hipGetErrorString(err),
  86. dev, free_byte * SIZE2MB, total_byte * SIZE2MB);
  87. #else
  88. return "rocm disabled at compile time";
  89. #endif
  90. }
  91. CnrtError::CnrtError(const std::string& msg) : SystemError(msg) {
  92. m_msg.append(get_cnrt_extra_info());
  93. }
  94. std::string CnrtError::get_cnrt_extra_info() {
  95. #if MGB_CAMBRICON
  96. // get last error
  97. auto err = cnrtGetLastErr();
  98. return ssprintf("(last_err=%d(%s))", err, cnrtGetErrorStr(err));
  99. #else
  100. return "cnrt disabled at compile time";
  101. #endif
  102. }
  103. CndevError::CndevError(const std::string& msg) : SystemError(msg) {}
  104. CnmlError::CnmlError(const std::string& msg) : SystemError(msg) {}
  105. bool mgb::has_uncaught_exception() {
  106. #if MGB_ENABLE_EXCEPTION
  107. #if __cplusplus > 201402L
  108. // C++17; see https://stackoverflow.com/questions/38456127/what-is-the-value-of-cplusplus-for-c17
  109. return std::uncaught_exceptions() != 0;
  110. #else
  111. return std::uncaught_exception();
  112. #endif
  113. #else
  114. return false;
  115. #endif
  116. }
  117. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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