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 <string>
11 : #include <unordered_map>
12 :
13 : #include "log.h"
14 : #include "channel.h"
15 : #include "./aicpu/aicpu_ts_urma_channel.h"
16 : #include "comm_engine_utils.h"
17 : #include "protocol_utils.h"
18 :
19 : #include "./aicpu/aicpu_ts_p2p_channel.h"
20 : #include "./aicpu/aicpu_ts_uboe_channel.h"
21 : #include "./aicpu/aicpu_ts_ubg_channel.h"
22 : #include "./aicpu/aicpu_ts_roce_channel.h"
23 : #include "./host/host_cpu_roce_channel.h"
24 : #include "./host/host_cpu_urma_channel.h"
25 : #include "./ccu/ccu_urma_channel.h"
26 : #include "./aiv/aiv_ub_mem_channel.h"
27 : #include "./aiv/aiv_urma_channel.h"
28 : #include "./aicpu/aicpu_ts_hccs_channel.h"
29 : #include "./aicpu/aicpu_ts_roce_channel_v2.h"
30 :
31 : namespace hcomm {
32 : std::unordered_map<ChannelHandle, ChannelHandle> channelD2HHandleMap_;
33 :
34 23 : HcclResult Channel::CreateChannel(
35 : EndpointHandle endpointHandle, CommEngine engine,
36 : HcommChannelDesc channelDesc, std::shared_ptr<Channel>& channelPtr)
37 : {
38 23 : DevType deviceType = DevType::DEV_TYPE_COUNT;
39 23 : CHK_RET(hrtGetDeviceType(deviceType));
40 23 : std::shared_ptr<Channel> uniqueChannelPtr;
41 23 : switch (engine) {
42 1 : case COMM_ENGINE_CPU:
43 1 : if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_ROCE) {
44 0 : EXCEPTION_CATCH(uniqueChannelPtr = std::make_unique<HostCpuRoceChannel>(endpointHandle, channelDesc),
45 : return HCCL_E_PARA);
46 0 : break;
47 : }
48 1 : if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBC_CTP ||
49 1 : channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBC_TP) {
50 0 : EXCEPTION_CATCH(uniqueChannelPtr = std::make_unique<HostCpuUrmaChannel>(endpointHandle, channelDesc),
51 : return HCCL_E_PARA);
52 0 : break;
53 : }
54 1 : HCCL_ERROR("[Channel][%s] Engine[COMM_ENGINE_CPU] not support Protocol[%d]",
55 : __func__, channelDesc.remoteEndpoint.protocol);
56 1 : return HCCL_E_NOT_SUPPORT;
57 1 : case COMM_ENGINE_CPU_TS:
58 1 : HCCL_ERROR("[Channel][%s] CommEngine[COMM_ENGINE_CPU_TS] not support", __func__);
59 1 : return HCCL_E_NOT_SUPPORT;
60 4 : case COMM_ENGINE_AICPU:
61 : case COMM_ENGINE_AICPU_TS:
62 4 : if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBOE) {
63 0 : uniqueChannelPtr.reset(new (std::nothrow) AicpuTsUboeChannel(endpointHandle, channelDesc));
64 4 : } else if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBG) {
65 0 : if (deviceType != DevType::DEV_TYPE_950 && deviceType != DevType::DEV_TYPE_960) {
66 0 : HCCL_ERROR("[Channel][%s] UBG protocol only support DEV_TYPE_950/960, current deviceType=%d",
67 : __func__, static_cast<int>(deviceType));
68 0 : return HCCL_E_NOT_SUPPORT;
69 : }
70 0 : uniqueChannelPtr.reset(new (std::nothrow) AicpuTsUbgChannel(endpointHandle, channelDesc));
71 4 : } else if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_PCIE) {
72 0 : uniqueChannelPtr.reset(new (std::nothrow) AicpuTsP2pChannel(endpointHandle, channelDesc));
73 4 : } else if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_ROCE) {
74 2 : if (deviceType == DevType::DEV_TYPE_950 || deviceType == DevType::DEV_TYPE_960) {
75 0 : uniqueChannelPtr = std::make_unique<AicpuTsRoceChannelV2>(endpointHandle, channelDesc, engine);
76 : } else {
77 2 : uniqueChannelPtr = std::make_unique<AicpuTsRoceChannel>(endpointHandle, channelDesc);
78 : }
79 2 : } else if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBC_CTP ||
80 2 : channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBC_TP) {
81 0 : uniqueChannelPtr.reset(new (std::nothrow) AicpuTsUrmaChannel(endpointHandle, channelDesc));
82 2 : } else if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_HCCS) {
83 2 : uniqueChannelPtr.reset(
84 2 : new (std::nothrow) AicpuTsHccsChannel(endpointHandle, channelDesc));
85 : } else {
86 0 : HCCL_ERROR("[Channel][%s] invalid protocol for engine[%s], protocol[%s]",
87 : __func__, GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(),
88 : GetEnumToString(GetCommProtocolStrMap(), channelDesc.remoteEndpoint.protocol).c_str());
89 0 : return HCCL_E_NOT_SUPPORT;
90 : }
91 4 : break;
92 1 : case COMM_ENGINE_AIV:
93 1 : if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_ROCE &&
94 0 : (deviceType == DevType::DEV_TYPE_950 || deviceType == DevType::DEV_TYPE_960)) {
95 0 : uniqueChannelPtr = std::make_unique<AicpuTsRoceChannelV2>(endpointHandle, channelDesc, engine);
96 1 : } else if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBC_CTP ||
97 1 : channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBC_TP ||
98 1 : channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBG) {
99 1 : if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBG &&
100 1 : deviceType != DevType::DEV_TYPE_950 && deviceType != DevType::DEV_TYPE_960) {
101 0 : HCCL_ERROR("[Channel][%s] UBG protocol only support DEV_TYPE_950/960, current deviceType=%d",
102 : __func__, static_cast<int>(deviceType));
103 0 : return HCCL_E_NOT_SUPPORT;
104 : }
105 1 : uniqueChannelPtr.reset(new (std::nothrow) AivUrmaChannel(endpointHandle, channelDesc));
106 : } else {
107 0 : uniqueChannelPtr.reset(new (std::nothrow) AivUbMemChannel(endpointHandle, channelDesc));
108 : }
109 1 : break;
110 15 : case COMM_ENGINE_CCU:
111 15 : uniqueChannelPtr.reset(
112 15 : new (std::nothrow) CcuUrmaChannel(endpointHandle, channelDesc));
113 15 : break;
114 1 : default:
115 1 : HCCL_ERROR("[Channel][%s] invalid type of CommEngine", __func__);
116 1 : return HCCL_E_NOT_FOUND;
117 : }
118 20 : CHK_PTR_NULL(uniqueChannelPtr);
119 20 : uniqueChannelPtr->engine_ = engine;
120 20 : CHK_RET_UNAVAIL(uniqueChannelPtr->Init());
121 18 : channelPtr = std::move(uniqueChannelPtr);
122 18 : return HCCL_SUCCESS;
123 23 : }
124 :
125 2 : ChannelStatus Channel::TransportStatusToChannelStatus(Hccl::TransportStatus ts)
126 : {
127 2 : switch (ts) {
128 0 : case Hccl::TransportStatus::INIT:
129 0 : return ChannelStatus::INIT;
130 0 : case Hccl::TransportStatus::SOCKET_OK:
131 0 : return ChannelStatus::SOCKET_OK;
132 0 : case Hccl::TransportStatus::SOCKET_TIMEOUT:
133 0 : return ChannelStatus::SOCKET_TIMEOUT;
134 2 : case Hccl::TransportStatus::READY:
135 2 : return ChannelStatus::READY;
136 0 : default:
137 0 : HCCL_ERROR("[Channel][%s] Invalid TransportStatus[%d]", __func__, ts);
138 0 : return ChannelStatus::FAILED;
139 : }
140 : }
141 :
142 0 : HcclResult Channel::UpdateMemInfo(HcommMemHandle *memHandles, uint32_t memHandleNum)
143 : {
144 0 : HCCL_WARNING("[UpdateMemInfo] not support.");
145 0 : return HCCL_SUCCESS;
146 : }
147 :
148 1 : HcommChannelKind Channel::GetChannelKind() const
149 : {
150 1 : return channelKind_;
151 : }
152 :
153 1 : HcclResult Channel::Serialize(std::shared_ptr<hccl::DeviceMem> &out)
154 : {
155 1 : out.reset();
156 1 : return HCCL_E_NOT_SUPPORT;
157 : }
158 :
159 0 : void Channel::AddPtrArrayDevMem(std::shared_ptr<hccl::DeviceMem> ptrArrayMem)
160 : {
161 0 : if (ptrArrayMem == nullptr || !(*ptrArrayMem)) {
162 0 : HCCL_WARNING("[Channel][%s] invalid ptrArrayMem.", __func__);
163 0 : return;
164 : }
165 0 : ptrArrayDevMems_.push_back(std::move(ptrArrayMem));
166 : }
167 :
168 3 : void Channel::ReleasePtrArrayDevMems()
169 : {
170 3 : ptrArrayDevMems_.clear();
171 3 : }
172 : } // namespace hcomm
|