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 4.0 kB

5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 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. Status CallbackManager::RegisterCallback(rtStream_t stream, rtCallback_t callback, void *user_data) {
  23. GELOGD("To register callback");
  24. rtEvent_t event = nullptr;
  25. GE_CHK_RT_RET(rtEventCreate(&event));
  26. auto rt_ret = rtEventRecord(event, stream);
  27. if (rt_ret != RT_ERROR_NONE) {
  28. GELOGE(RT_FAILED, "[Invoke][rtEventRecord] failed, error code = %d", rt_ret);
  29. REPORT_CALL_ERROR("E19999", "Invoke rtEventRecord failed, error code = %d", rt_ret);
  30. (void) rtEventDestroy(event);
  31. return RT_FAILED;
  32. }
  33. auto cb = std::pair<rtCallback_t, void *>(callback, user_data);
  34. auto entry = std::pair<rtEvent_t, std::pair<rtCallback_t, void *>>(event, std::move(cb));
  35. if (!callback_queue_.Push(entry)) {
  36. (void) rtEventDestroy(event);
  37. return INTERNAL_ERROR;
  38. }
  39. GELOGD("Registering callback successfully");
  40. return SUCCESS;
  41. }
  42. Status CallbackManager::Init() {
  43. rtContext_t ctx = nullptr;
  44. GE_CHK_RT_RET(rtCtxGetCurrent(&ctx));
  45. ret_future_ = std::async(std::launch::async, [&](rtContext_t context) ->Status {
  46. return CallbackProcess(context);
  47. }, ctx);
  48. if (!ret_future_.valid()) {
  49. GELOGE(INTERNAL_ERROR, "[Check][ShareState]Failed to init callback manager.");
  50. REPORT_INNER_ERROR("E19999", "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, "[Invoke][rtEventSynchronize] failed. ret = %d", rt_err);
  70. REPORT_CALL_ERROR("E19999", "Invoke rtEventSynchronize failed, ret = %d.", rt_err);
  71. GE_CHK_RT(rtEventDestroy(event));
  72. return RT_FAILED;
  73. }
  74. GE_CHK_RT(rtEventDestroy(event));
  75. auto cb_func = entry.second.first;
  76. auto cb_args = entry.second.second;
  77. cb_func(cb_args);
  78. }
  79. }
  80. Status CallbackManager::Destroy() {
  81. GELOGI("To destroy callback manager.");
  82. if (!ret_future_.valid()) {
  83. GELOGI("CallbackManager not initialized.");
  84. return SUCCESS;
  85. }
  86. std::pair<rtEvent_t, std::pair<rtCallback_t, void *>> eof_entry;
  87. eof_entry.first = nullptr;
  88. callback_queue_.Push(eof_entry);
  89. auto ret = ret_future_.get();
  90. GELOGI("Callback manager ended. ret = %u", ret);
  91. return ret;
  92. }
  93. void CallbackManager::RtCallbackFunc(void *data) {
  94. GELOGD("To invoke callback function");
  95. auto callback_func = reinterpret_cast<std::function<void()> *>(data);
  96. (*callback_func)();
  97. delete callback_func;
  98. }
  99. Status CallbackManager::RegisterCallback(rtStream_t stream, const std::function<void()> &callback) {
  100. auto func = std::unique_ptr<std::function<void()>>(new(std::nothrow) std::function<void()>(callback));
  101. GE_CHECK_NOTNULL(func);
  102. GELOGD("Callback registered");
  103. return RegisterCallback(stream, RtCallbackFunc, func.release());
  104. }
  105. } // namespace hybrid
  106. } // namespace ge

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