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 : #include "coll_comm_mgr.h"
11 : #include "ns_recovery/task_abort_handler.h"
12 : #include "cluster_monitor.h"
13 :
14 : namespace hccl {
15 :
16 : CollCommMgr* CollCommMgr::instance_ = nullptr;
17 : static std::once_flag instanceFlag;
18 :
19 301 : CollCommMgr* CollCommMgr::GetInstance()
20 : {
21 301 : std::call_once(instanceFlag, [&] {
22 6 : instance_ = new CollCommMgr();
23 6 : });
24 301 : return instance_;
25 : }
26 :
27 151 : hcomm::ClusterMonitor &CollCommMgr::GetClusterMonitor(s32 deviceLogicId)
28 : {
29 151 : if (static_cast<u32>(deviceLogicId) >= MAX_MODULE_DEVICE_NUM) {
30 0 : HCCL_WARNING("[ClusterMonitor][%s]deviceLogicId[%d] >= %u, invalid",
31 : __func__, deviceLogicId, MAX_MODULE_DEVICE_NUM);
32 0 : return clusterMonitor_[0];
33 : }
34 151 : return clusterMonitor_[deviceLogicId];
35 : }
36 :
37 31 : HcclResult CollCommMgr::TryReserveCcuMsComm(s32 deviceLogicId, const std::string &commId, bool &reserved)
38 : {
39 31 : reserved = false;
40 31 : if (deviceLogicId < 0 || static_cast<u32>(deviceLogicId) >= MAX_MODULE_DEVICE_NUM || commId.empty()) {
41 2 : HCCL_ERROR("[%s] invalid parameter, deviceLogicId[%d], max device num[%u], commId empty[%d].", __func__,
42 : deviceLogicId, MAX_MODULE_DEVICE_NUM, commId.empty());
43 2 : return HCCL_E_PARA;
44 : }
45 :
46 29 : std::lock_guard<std::mutex> lock(ccuMsCommMutex_);
47 29 : auto &owner = ccuMsCommIds_[deviceLogicId];
48 29 : if (owner.empty()) {
49 21 : owner = commId;
50 21 : reserved = true;
51 : }
52 29 : return HCCL_SUCCESS;
53 29 : }
54 :
55 10 : void CollCommMgr::ReleaseCcuMsComm(s32 deviceLogicId, const std::string &commId)
56 : {
57 10 : if (deviceLogicId < 0 || static_cast<u32>(deviceLogicId) >= MAX_MODULE_DEVICE_NUM) {
58 0 : HCCL_WARNING("[%s] deviceLogicId[%d] is invalid, max device num[%u].", __func__, deviceLogicId,
59 : MAX_MODULE_DEVICE_NUM);
60 0 : return;
61 : }
62 :
63 10 : std::lock_guard<std::mutex> lock(ccuMsCommMutex_);
64 10 : auto &owner = ccuMsCommIds_[deviceLogicId];
65 10 : if (owner == commId) {
66 9 : owner.clear();
67 : }
68 10 : }
69 :
70 98 : void CollCommMgr::RegisteCollComm(CollComm* collComm)
71 : {
72 98 : std::lock_guard<std::mutex> lock(mutex_);
73 98 : allCollComms_[collComm->GetCommId()] = collComm;
74 : // 注册到需要的地方
75 98 : HcclTaskAbortHandler::GetInstance().Register(collComm);
76 98 : }
77 :
78 134 : void CollCommMgr::UnRegisteCollComm(CollComm* collComm)
79 : {
80 134 : std::lock_guard<std::mutex> lock(mutex_);
81 134 : allCollComms_.erase(collComm->GetCommId());
82 : // 从通信域里面注销
83 134 : HcclTaskAbortHandler::GetInstance().UnRegister(collComm);
84 134 : (void)GetClusterMonitor(collComm->GetDeviceLogicId()).UnRegisterToClusterMonitor(collComm);
85 134 : }
86 :
87 0 : std::unordered_map<std::string, CollComm*> CollCommMgr::GetAllCollComms()
88 : {
89 0 : return allCollComms_;
90 : }
91 :
92 : }
|