Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "communicator_impl_lite_manager.h"
12 : #include "aicpu_daemon_service.h"
13 : #include "aicpu_comm_destroy_func.h"
14 : #include "ns_recovery_handler_func.h"
15 : #include "task_exception_func.h"
16 : #include "task_exception_handler_lite.h"
17 : namespace Hccl {
18 :
19 1 : CommunicatorImplLiteMgr::CommunicatorImplLiteMgr()
20 : {
21 3 : HCCL_INFO("CommunicatorImplLiteMgr:: start");
22 : static auto commandToBackGroud = CommandToBackGroud::Default;
23 3 : HCCL_INFO("CommunicatorImplLiteMgr:: gen daemon service run func");
24 0 : static auto daemonServiceRun = [](void* info) {
25 0 : AicpuDaemonService::GetInstance().ServiceRun(info);
26 0 : };
27 3 : HCCL_INFO("CommunicatorImplLiteMgr:: gen daemon service stop func");
28 0 : static auto daemonServiceStop = [](void* info) {
29 0 : AicpuDaemonService::GetInstance().ServiceStop(info);
30 0 : };
31 :
32 : // 注册守护进程函数
33 1 : AicpuDaemonService::GetInstance().Register(&TaskExceptionFunc::GetInstance());
34 1 : AicpuDaemonService::GetInstance().Register(&AicpuCommDestroyFunc::GetInstance());
35 1 : AicpuDaemonService::GetInstance().Register(&NsRecoveryHandlerFunc::GetInstance());
36 : // 注册TaskExceptionFunc回调函数
37 1 : TaskExceptionHandlerLite::GetInstance();
38 :
39 : // 启动背景线程
40 1 : if (StartMC2MaintenanceThread != nullptr) {
41 0 : StartMC2MaintenanceThread(daemonServiceRun, &commandToBackGroud, daemonServiceStop, &commandToBackGroud);
42 0 : HCCL_INFO("[CommunicatorImplLiteMgr] start BackGround thread success.");
43 : } else {
44 3 : HCCL_WARNING("Aicpu api StartMC2MaintenanceThread func is nullptr");
45 : }
46 :
47 3 : HCCL_INFO("CommunicatorImplLiteMgr::end");
48 1 : }
49 :
50 4 : CommunicatorImplLiteMgr::~CommunicatorImplLiteMgr() { HCCL_INFO("CommunicatorImplLiteMgr Destroy"); }
51 :
52 31 : CommunicatorImplLiteMgr& CommunicatorImplLiteMgr::GetInstance()
53 : {
54 31 : static CommunicatorImplLiteMgr communicatorLiteMgr;
55 31 : return communicatorLiteMgr;
56 : }
57 :
58 2 : CommunicatorImplLite* CommunicatorImplLiteMgr::Get(const u32 commIdIndex)
59 : {
60 2 : std::lock_guard<std::mutex> lock(serialMutex);
61 : // 通过commIdIndex查找communicatorImplLites中是否存在,不存在再处理资源
62 2 : auto iter = communicatorImplLites.find(commIdIndex);
63 2 : if (iter != communicatorImplLites.end()) {
64 0 : HCCL_INFO("CommunicatorImplLiteMgr::find commIdIndex [%u] in communicatorImplLites", commIdIndex);
65 0 : unique_lock<mutex> aicpuLock(communicatorImplLites[commIdIndex]->GetAicpuMc2Mutex());
66 0 : communicatorImplLites[commIdIndex]->SetIsFirstUsedToFalse();
67 0 : aicpuLock.unlock();
68 0 : return communicatorImplLites[commIdIndex].get();
69 0 : }
70 :
71 : try {
72 2 : communicatorImplLites[commIdIndex] = make_unique<CommunicatorImplLite>(commIdIndex);
73 0 : } catch (...) {
74 0 : HCCL_ERROR("new CommunicatorImplLite failed, commIdIndex[%u]", commIdIndex);
75 0 : return nullptr;
76 0 : }
77 :
78 2 : return communicatorImplLites[commIdIndex].get();
79 2 : }
80 :
81 6 : std::vector<CommunicatorImplLite*> CommunicatorImplLiteMgr::GetAll()
82 : {
83 6 : std::lock_guard<std::mutex> lock(serialMutex);
84 6 : std::vector<CommunicatorImplLite*> vec;
85 18 : for (auto iter = communicatorImplLites.begin(); iter != communicatorImplLites.end(); iter++) {
86 12 : if (iter->second != nullptr) {
87 12 : vec.push_back(iter->second.get());
88 : }
89 : }
90 6 : return vec;
91 6 : }
92 :
93 0 : void CommunicatorImplLiteMgr::DestroyComm(u32 commIdIndex)
94 : {
95 0 : std::lock_guard<std::mutex> lock(serialMutex);
96 0 : HCCL_INFO("Destroy comm start commIdIndex[%u]", commIdIndex);
97 0 : communicatorImplLites.erase(commIdIndex);
98 0 : HCCL_INFO("Destroy comm success commIdIndex[%u]", commIdIndex);
99 0 : }
100 : } // namespace Hccl
|