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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. Status CallbackManager::RegisterCallback(rtCallback_t callback, void *user_data) {
  24. GELOGD("To register callback");
  25. rtEvent_t event = nullptr;
  26. GE_CHK_RT_RET(rtEventCreate(&event));
  27. GE_CHK_RT_RET(rtEventRecord(event, stream_));
  28. auto cb = std::pair<rtCallback_t, void *>(callback, user_data);
  29. auto entry = std::pair<rtEvent_t, std::pair<rtCallback_t, void *>>(event, std::move(cb));
  30. if (!callback_queue_.Push(entry)) {
  31. return INTERNAL_ERROR;
  32. }
  33. GELOGD("Registering callback successfully");
  34. return SUCCESS;
  35. }
  36. Status CallbackManager::Init() {
  37. rtContext_t ctx = nullptr;
  38. GE_CHK_RT_RET(rtCtxGetCurrent(&ctx));
  39. ret_future_ = std::async([&](rtContext_t context) -> Status { return CallbackProcess(context); }, ctx);
  40. if (!ret_future_.valid()) {
  41. GELOGE(INTERNAL_ERROR, "Failed to init callback manager.");
  42. return INTERNAL_ERROR;
  43. }
  44. return SUCCESS;
  45. }
  46. Status CallbackManager::CallbackProcess(rtContext_t context) {
  47. GE_CHK_RT_RET(rtCtxSetCurrent(context));
  48. std::pair<rtEvent_t, std::pair<rtCallback_t, void *>> entry;
  49. while (true) {
  50. if (!callback_queue_.Pop(entry)) {
  51. GELOGI("CallbackManager stopped");
  52. return INTERNAL_ERROR;
  53. }
  54. auto event = entry.first;
  55. if (event == nullptr) {
  56. return SUCCESS;
  57. }
  58. auto rt_err = rtEventSynchronize(event);
  59. if (rt_err != RT_ERROR_NONE) {
  60. GELOGE(RT_FAILED, "rtEventSynchronize failed. ret = %d", rt_err);
  61. GE_CHK_RT(rtEventDestroy(event));
  62. return RT_FAILED;
  63. }
  64. GE_CHK_RT(rtEventDestroy(event));
  65. auto cb_func = entry.second.first;
  66. auto cb_args = entry.second.second;
  67. cb_func(cb_args);
  68. }
  69. }
  70. Status CallbackManager::Destroy() {
  71. GELOGI("To destroy callback manager.");
  72. if (!ret_future_.valid()) {
  73. GELOGI("CallbackManager not initialized.");
  74. return SUCCESS;
  75. }
  76. std::pair<rtEvent_t, std::pair<rtCallback_t, void *>> eof_entry;
  77. eof_entry.first = nullptr;
  78. callback_queue_.Push(eof_entry);
  79. auto ret = ret_future_.get();
  80. GELOGI("Callback manager ended. ret = %u", ret);
  81. return ret;
  82. }
  83. void CallbackManager::RtCallbackFunc(void *data) {
  84. GELOGD("To invoke callback function");
  85. auto callback_func = reinterpret_cast<std::function<void()> *>(data);
  86. (*callback_func)();
  87. delete callback_func;
  88. }
  89. Status CallbackManager::RegisterCallback(const std::function<void()> &callback) {
  90. auto func = std::unique_ptr<std::function<void()>>(new (std::nothrow) std::function<void()>(callback));
  91. GE_CHECK_NOTNULL(func);
  92. GELOGD("Callback registered");
  93. return RegisterCallback(RtCallbackFunc, func.release());
  94. }
  95. } // namespace hybrid
  96. } // namespace ge

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