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 1 : CommunicatorImplLiteMgr::~CommunicatorImplLiteMgr()
51 : {
52 3 : HCCL_INFO("CommunicatorImplLiteMgr Destroy");
53 1 : }
54 :
55 30 : CommunicatorImplLiteMgr &CommunicatorImplLiteMgr::GetInstance()
56 : {
57 30 : static CommunicatorImplLiteMgr communicatorLiteMgr;
58 30 : return communicatorLiteMgr;
59 : }
60 :
61 2 : CommunicatorImplLite *CommunicatorImplLiteMgr::Get(const u32 commIdIndex)
62 : {
63 2 : std::lock_guard<std::mutex> lock(serialMutex);
64 : // 通过commIdIndex查找communicatorImplLites中是否存在,不存在再处理资源
65 2 : auto iter = communicatorImplLites.find(commIdIndex);
66 2 : if (iter != communicatorImplLites.end()) {
67 0 : HCCL_INFO("CommunicatorImplLiteMgr::find commIdIndex [%u] in communicatorImplLites", commIdIndex);
68 0 : unique_lock<mutex> aicpuLock(communicatorImplLites[commIdIndex]->GetAicpuMc2Mutex());
69 0 : communicatorImplLites[commIdIndex]->SetIsFirstUsedToFalse();
70 0 : aicpuLock.unlock();
71 0 : return communicatorImplLites[commIdIndex].get();
72 0 : }
73 :
74 : try {
75 2 : communicatorImplLites[commIdIndex] = make_unique<CommunicatorImplLite>(commIdIndex);
76 0 : } catch (...) {
77 0 : HCCL_ERROR("new CommunicatorImplLite failed, commIdIndex[%u]", commIdIndex);
78 0 : return nullptr;
79 0 : }
80 :
81 2 : return communicatorImplLites[commIdIndex].get();
82 2 : }
83 :
84 6 : std::vector<CommunicatorImplLite *> CommunicatorImplLiteMgr::GetAll()
85 : {
86 6 : std::lock_guard<std::mutex> lock(serialMutex);
87 6 : std::vector<CommunicatorImplLite *> vec;
88 18 : for (auto iter = communicatorImplLites.begin(); iter != communicatorImplLites.end(); iter++) {
89 12 : if (iter->second != nullptr) {
90 12 : vec.push_back(iter->second.get());
91 : }
92 : }
93 6 : return vec;
94 6 : }
95 :
96 0 : void CommunicatorImplLiteMgr::DestroyComm(u32 commIdIndex)
97 : {
98 0 : std::lock_guard<std::mutex> lock(serialMutex);
99 0 : HCCL_INFO("Destroy comm start commIdIndex[%u]", commIdIndex);
100 0 : communicatorImplLites.erase(commIdIndex);
101 0 : HCCL_INFO("Destroy comm success commIdIndex[%u]", commIdIndex);
102 0 : }
103 : } // namespace Hccl
|