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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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-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/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. CnrtError::CnrtError(const std::string& msg) : SystemError(msg) {
  70. m_msg.append(get_cnrt_extra_info());
  71. }
  72. std::string CnrtError::get_cnrt_extra_info() {
  73. #if MGB_CAMBRICON
  74. // get last error
  75. auto err = cnrtGetLastErr();
  76. return ssprintf("(last_err=%d(%s))", err, cnrtGetErrorStr(err));
  77. #else
  78. return "cnrt disabled at compile time";
  79. #endif
  80. }
  81. CndevError::CndevError(const std::string& msg) : SystemError(msg) {}
  82. CnmlError::CnmlError(const std::string& msg) : SystemError(msg) {}
  83. bool mgb::has_uncaught_exception() {
  84. #if MGB_ENABLE_EXCEPTION
  85. #if __cplusplus > 201402L
  86. // C++17; see https://stackoverflow.com/questions/38456127/what-is-the-value-of-cplusplus-for-c17
  87. return std::uncaught_exceptions() != 0;
  88. #else
  89. return std::uncaught_exception();
  90. #endif
  91. #else
  92. return false;
  93. #endif
  94. }
  95. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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