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.

rt_callback_manager.cc 3.8 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "hybrid/executor/rt_callback_manager.h"
  17. #include "framework/common/ge_inner_error_codes.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "framework/common/util.h"
  20. namespace ge {
  21. namespace hybrid {
  22. CallbackManager::CallbackManager(rtStream_t stream) : stream_(stream) {
  23. }
  24. Status CallbackManager::RegisterCallback(rtCallback_t callback, void *user_data) {
  25. GELOGD("To register callback");
  26. rtEvent_t event = nullptr;
  27. GE_CHK_RT_RET(rtEventCreate(&event));
  28. auto rt_ret = rtEventRecord(event, stream_);
  29. if (rt_ret != RT_ERROR_NONE) {
  30. GELOGE(RT_FAILED, "Failed to invoke rtEventRecord, error code = %d", rt_ret);
  31. (void) rtEventDestroy(event);
  32. return RT_FAILED;
  33. }
  34. auto cb = std::pair<rtCallback_t, void *>(callback, user_data);
  35. auto entry = std::pair<rtEvent_t, std::pair<rtCallback_t, void *>>(event, std::move(cb));
  36. if (!callback_queue_.Push(entry)) {
  37. (void) rtEventDestroy(event);
  38. return INTERNAL_ERROR;
  39. }
  40. GELOGD("Registering callback successfully");
  41. return SUCCESS;
  42. }
  43. Status CallbackManager::Init() {
  44. rtContext_t ctx = nullptr;
  45. GE_CHK_RT_RET(rtCtxGetCurrent(&ctx));
  46. ret_future_ = std::async(std::launch::async, [&](rtContext_t context) ->Status {
  47. return CallbackProcess(context);
  48. }, ctx);
  49. if (!ret_future_.valid()) {
  50. GELOGE(INTERNAL_ERROR, "Failed to init callback manager.");
  51. return INTERNAL_ERROR;
  52. }
  53. return SUCCESS;
  54. }
  55. Status CallbackManager::CallbackProcess(rtContext_t context) {
  56. GE_CHK_RT_RET(rtCtxSetCurrent(context));
  57. std::pair<rtEvent_t, std::pair<rtCallback_t, void *>> entry;
  58. while (true) {
  59. if (!callback_queue_.Pop(entry)) {
  60. GELOGI("CallbackManager stopped");
  61. return INTERNAL_ERROR;
  62. }
  63. auto event = entry.first;
  64. if (event == nullptr) {
  65. return SUCCESS;
  66. }
  67. auto rt_err = rtEventSynchronize(event);
  68. if (rt_err != RT_ERROR_NONE) {
  69. GELOGE(RT_FAILED, "rtEventSynchronize failed. ret = %d", rt_err);
  70. GE_CHK_RT(rtEventDestroy(event));
  71. return RT_FAILED;
  72. }
  73. GE_CHK_RT(rtEventDestroy(event));
  74. auto cb_func = entry.second.first;
  75. auto cb_args = entry.second.second;
  76. cb_func(cb_args);
  77. }
  78. }
  79. Status CallbackManager::Destroy() {
  80. GELOGI("To destroy callback manager.");
  81. if (!ret_future_.valid()) {
  82. GELOGI("CallbackManager not initialized.");
  83. return SUCCESS;
  84. }
  85. std::pair<rtEvent_t, std::pair<rtCallback_t, void *>> eof_entry;
  86. eof_entry.first = nullptr;
  87. callback_queue_.Push(eof_entry);
  88. auto ret = ret_future_.get();
  89. GELOGI("Callback manager ended. ret = %u", ret);
  90. return ret;
  91. }
  92. void CallbackManager::RtCallbackFunc(void *data) {
  93. GELOGD("To invoke callback function");
  94. auto callback_func = reinterpret_cast<std::function<void()> *>(data);
  95. (*callback_func)();
  96. delete callback_func;
  97. }
  98. Status CallbackManager::RegisterCallback(const std::function<void()> &callback) {
  99. auto func = std::unique_ptr<std::function<void()>>(new(std::nothrow) std::function<void()>(callback));
  100. GE_CHECK_NOTNULL(func);
  101. GELOGD("Callback registered");
  102. return RegisterCallback(RtCallbackFunc, func.release());
  103. }
  104. } // namespace hybrid
  105. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示