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

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

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