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