1 /**
2 * Copyright 2022 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 #include "runtime/device/stream_synchronizer.h"
18
19 #include "include/backend/distributed/collective/collective_manager.h"
20 #include "include/backend/distributed/recovery/recovery_context.h"
21 #include "utils/ms_context.h"
22
23 namespace mindspore {
24 namespace device {
25 using distributed::collective::CollectiveManager;
26 using distributed::recovery::RecoveryContext;
27
28 std::mutex StreamSynchronizer::instance_lock_;
29 std::shared_ptr<StreamSynchronizer> StreamSynchronizer::instance_ = nullptr;
30
Initialize()31 void StreamSynchronizer::Initialize() {
32 // Non disaster recovery mode does not need to start thread and timeout mechanisms.
33 if (!RecoveryContext::GetInstance()->enable_recovery()) {
34 return;
35 }
36
37 worker_thread_ = std::thread(&StreamSynchronizer::DoSyncStreamTask, this);
38 }
39
Finalize()40 void StreamSynchronizer::Finalize() {
41 {
42 std::unique_lock<std::mutex> lock(task_mutex_);
43 stop_ = true;
44 }
45 do_sync_stream_cv_.notify_all();
46 if (worker_thread_.joinable()) {
47 worker_thread_.join();
48 }
49 device_context_ = nullptr;
50 }
51
SyncStream(const std::string & device_name,uint32_t timeout)52 bool StreamSynchronizer::SyncStream(const std::string &device_name, uint32_t timeout) {
53 std::unique_lock<std::mutex> reentrant_lock(reentrant_mutex_);
54 auto ms_context = MsContext::GetInstance();
55 MS_EXCEPTION_IF_NULL(ms_context);
56 uint32_t device_id = ms_context->get_param<uint32_t>(MS_CTX_DEVICE_ID);
57
58 const auto &device_context =
59 device::DeviceContextManager::GetInstance().GetOrCreateDeviceContext({device_name, device_id});
60 MS_EXCEPTION_IF_NULL(device_context);
61
62 // If disable recovery or timeout==0, sync stream directly to improve performance.
63 if (!RecoveryContext::GetInstance()->enable_recovery() || timeout == 0) {
64 device_context->Initialize();
65 return device_context->device_res_manager_->SyncAllStreams();
66 }
67
68 std::unique_lock<std::mutex> lock(task_mutex_);
69 if (stop_) {
70 MS_LOG(EXCEPTION) << "The synchronization stream task has stopped";
71 }
72 device_context_ = device_context;
73 do_sync_stream_cv_.notify_one();
74
75 if (time_out_cv_.wait_for(lock, std::chrono::seconds(timeout)) == std::cv_status::no_timeout) {
76 if (!sync_stream_ret_) {
77 MS_LOG(ERROR) << "Synchronize stream failed.";
78 }
79 return sync_stream_ret_;
80 } else {
81 CollectiveManager::instance()->set_need_reinit(true);
82 if (!CollectiveManager::instance()->Finalize()) {
83 MS_LOG(ERROR) << "Finalize collective manager failed.";
84 return false;
85 }
86 time_out_cv_.wait(lock, [this]() { return device_context_ == nullptr; });
87 MS_LOG(WARNING) << "Synchronize stream timeout.";
88 return true;
89 }
90 }
91
DoSyncStreamTask()92 void StreamSynchronizer::DoSyncStreamTask() {
93 for (;;) {
94 {
95 std::unique_lock<std::mutex> lock(task_mutex_);
96 do_sync_stream_cv_.wait(lock, [this]() { return stop_ || device_context_ != nullptr; });
97 if (stop_) {
98 return;
99 }
100 }
101
102 device_context_->Initialize();
103 // Really sync stream.
104 sync_stream_ret_ = device_context_->device_res_manager_->SyncAllStreams();
105
106 {
107 std::unique_lock<std::mutex> lock(task_mutex_);
108 device_context_ = nullptr;
109 }
110 time_out_cv_.notify_one();
111 }
112 }
113 } // namespace device
114 } // namespace mindspore
115