• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2024 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 
17 #ifndef MINDSPORE_CCSRC_INCLUDE_BACKEND_DEVICE_SYNCHRONIZER_UTILS_H
18 #define MINDSPORE_CCSRC_INCLUDE_BACKEND_DEVICE_SYNCHRONIZER_UTILS_H
19 
20 #ifndef BUILD_LITE
21 #include "runtime/graph_scheduler/actor/kernel_async_infer_actor.h"
22 #include "runtime/graph_scheduler/actor/kernel_async_resize_actor.h"
23 #include "runtime/graph_scheduler/actor/kernel_async_launch_actor.h"
24 #endif
25 
26 namespace mindspore {
WaitAsyncResizeAndLaunchFinish()27 static inline void WaitAsyncResizeAndLaunchFinish() {
28 // The runtime pipeline: InferShape->ResizeKernelMod->LaunchKernel, the latter cannot wait for the former, otherwise
29 // deadlock may occur.
30 // 1. infer shape task needs to wait for resize and kernel launch.
31 // 2. Internally, the resize task only needs to wait for the kernel launch
32 #ifndef BUILD_LITE
33   if (runtime::ActorDispatcher::enable_runtime_multi_pipeline()) {
34     const auto &cur_thread_id = std::this_thread::get_id();
35     if (cur_thread_id != runtime::KernelAsyncResizeActor::GetInstance()->actor_thread_id() &&
36         cur_thread_id != runtime::KernelAsyncLaunchActor::GetInstance()->actor_thread_id()) {
37       runtime::KernelAsyncInferActor::GetInstance()->Wait();
38     }
39 
40     if (cur_thread_id != runtime::KernelAsyncLaunchActor::GetInstance()->actor_thread_id()) {
41       runtime::KernelAsyncResizeActor::GetInstance()->Wait();
42     }
43   }
44 
45   if (runtime::ActorDispatcher::enable_async_launch_kernel()) {
46     runtime::KernelAsyncLaunchActor::GetInstance()->Wait();
47   }
48 #endif
49 }
50 }  // namespace mindspore
51 #endif  // MINDSPORE_CCSRC_INCLUDE_BACKEND_DEVICE_SYNCHRONIZER_UTILS_H
52