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