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 #include <thread>
18 #include <atomic>
19 #include <memory>
20 #include <mutex>
21 #include <algorithm>
22 #include <utility>
23 #include "utils/log_adapter.h"
24 #include "include/common/utils/utils.h"
25 #include "utils/ms_context.h"
26 #include "mindio/mindio_adapter.h"
27
28 namespace mindspore {
29 namespace mindio {
30
31 std::shared_ptr<MindIOAdapter> MindIOAdapter::inst_mindio_ = nullptr;
32
GetInstance()33 std::shared_ptr<MindIOAdapter> MindIOAdapter::GetInstance() {
34 static std::once_flag inst_mindio_init_flag_ = {};
35 std::call_once(inst_mindio_init_flag_, [&]() {
36 MS_LOG(INFO) << "Start create new mindio adapter instance.";
37 if (inst_mindio_ == nullptr) {
38 inst_mindio_ = std::make_shared<MindIOAdapter>();
39 auto env = common::GetEnv("MS_ENABLE_MINDIO_GRACEFUL_EXIT");
40 auto libPath = common::GetEnv("MS_MINDIO_TTP_LIB_PATH");
41 auto context = MsContext::GetInstance();
42 bool isEnable = false;
43 int execute_mode = context->get_param<int>(MS_CTX_EXECUTION_MODE);
44 if (env == "true" && execute_mode == kGraphMode && !libPath.empty()) {
45 MS_LOG(INFO) << "TTP env is enable and found custom mindio so path.";
46 void *handle = dlopen(libPath.c_str(), RTLD_LAZY);
47 if (handle) {
48 MS_LOG(INFO) << "Found mindio so.";
49 auto startFunc = DlsymWithCast<TTP_NotifyStartUpdatingOsFunPtr>(handle, "MindioTtpSetOptimStatusUpdating");
50 auto endFunc = DlsymWithCast<TTP_NotifyEndUpdatingOsFunPtr>(handle, "MindioTtpSetOptimStatusFinished");
51 if (startFunc && endFunc) {
52 MS_LOG(INFO) << "Found mindio symbols.";
53 inst_mindio_->SetOsStateNotifyCallBack(startFunc, endFunc);
54 isEnable = true;
55 }
56 }
57 }
58 inst_mindio_->isEnable = isEnable;
59 MS_LOG(INFO) << "Finish create new mindio adapter instance, isEnable:" << isEnable;
60 }
61 });
62 MS_EXCEPTION_IF_NULL(inst_mindio_);
63 return inst_mindio_;
64 }
65
IsEnable()66 bool MindIOAdapter::IsEnable() { return isEnable; }
67
SetOsStateNotifyCallBack(const TTP_NotifyStartUpdatingOsFunObj & optStart,const TTP_NotifyEndUpdatingOsFunObj & optEnd)68 void MindIOAdapter::SetOsStateNotifyCallBack(const TTP_NotifyStartUpdatingOsFunObj &optStart,
69 const TTP_NotifyEndUpdatingOsFunObj &optEnd) {
70 _optStart = optStart;
71 _optEnd = optEnd;
72 }
NotifyStartUpdatingOs()73 void MindIOAdapter::NotifyStartUpdatingOs() {
74 if (_optStart != nullptr) {
75 auto ret = _optStart(-1);
76 MS_LOG(INFO) << "Notify start updating optimizer event to mindio. ret=" << ret;
77 }
78 }
NotifyEndUpdatingOs()79 void MindIOAdapter::NotifyEndUpdatingOs() {
80 if (_optEnd != nullptr) {
81 auto ret = _optEnd(-1);
82 MS_LOG(INFO) << "Notify Finish updating optimizer event to mindio. ret=" << ret;
83 }
84 }
85 } // namespace mindio
86 } // namespace mindspore
87