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