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 "inner_net_dev_manager.h"
11 : #include "log.h"
12 :
13 : namespace Hccl {
14 51 : InnerNetDevManager &InnerNetDevManager::GetInstance()
15 : {
16 51 : static InnerNetDevManager instance;
17 51 : return instance;
18 : }
19 :
20 7 : HcclResult InnerNetDevManager::AddDevice(const NetDevInfo &info, HcclNetDevice *&device)
21 : {
22 7 : device = new(nothrow) HcclNetDevice(info);
23 7 : if(device == nullptr) {
24 0 : HCCL_ERROR("new HcclNetDevice fail, devId[%u]", info.devId);
25 0 : return HCCL_E_PTR;
26 : }
27 :
28 7 : InnerNetDev* innerNetDev = nullptr;
29 7 : auto it = netDevMap_.find(info);
30 7 : if (it == netDevMap_.end()) {
31 7 : innerNetDev = new(nothrow) InnerNetDev(info);
32 7 : if(innerNetDev == nullptr || !innerNetDev->GetIsValid()) {
33 0 : HCCL_ERROR("new InnerNetDev fail, devId[%u]", info.devId);
34 0 : if (innerNetDev != nullptr) {
35 0 : delete innerNetDev;
36 0 : innerNetDev = nullptr;
37 : }
38 0 : delete device;
39 0 : device = nullptr;
40 0 : return HCCL_E_PARA;
41 : }
42 7 : netDevMap_.insert(std::make_pair(info, std::unique_ptr<InnerNetDev>(innerNetDev)));
43 7 : netDevCnt_[info] = 1;
44 : } else {
45 0 : innerNetDev = it->second.get();
46 0 : netDevCnt_[info]++;
47 : }
48 7 : device->SetInnerNetDev(innerNetDev);
49 7 : return HCCL_SUCCESS;
50 : }
51 :
52 7 : HcclResult InnerNetDevManager::RemoveDevice(const NetDevInfo &info)
53 : {
54 7 : auto cntIt = netDevCnt_.find(info);
55 7 : if (cntIt == netDevCnt_.end()) {
56 3 : HCCL_ERROR("find HcclNetDevice fail, devId[%u]", info.devId);
57 1 : return HCCL_E_PTR;
58 : }
59 :
60 6 : cntIt->second--;
61 6 : if (cntIt->second == 0) {
62 6 : netDevMap_.erase(info);
63 6 : netDevCnt_.erase(cntIt);
64 : }
65 6 : return HCCL_SUCCESS;
66 : }
67 :
68 6 : HcclResult InnerNetDevManager::DeleteDevice(Hccl::HcclNetDevice *device)
69 : {
70 6 : if (device == nullptr) {
71 0 : return HCCL_SUCCESS;
72 : }
73 6 : HcclResult ret = RemoveDevice(device->GetNetDevInfo());
74 6 : if (ret != HCCL_SUCCESS) {
75 0 : HCCL_ERROR("delete netDev fail, devId[%u]", device->GetNetDevInfo().devId);
76 0 : return HCCL_E_PARA;
77 : }
78 6 : delete device;
79 6 : device = nullptr;
80 6 : return HCCL_SUCCESS;
81 : }
82 : } // namespace Hccl
|