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 19 : HcclResult Channel::CreateChannel(
35 : EndpointHandle endpointHandle, CommEngine engine,
36 : HcommChannelDesc channelDesc, std::shared_ptr<Channel>& channelPtr)
37 : {
38 19 : DevType deviceType = DevType::DEV_TYPE_COUNT;
39 19 : CHK_RET(hrtGetDeviceType(deviceType));
40 19 : std::shared_ptr<Channel> uniqueChannelPtr;
41 19 : 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 0 : case COMM_ENGINE_AIV:
93 0 : 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 0 : } else if (channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBC_CTP ||
97 0 : channelDesc.remoteEndpoint.protocol == COMM_PROTOCOL_UBC_TP) {
98 0 : uniqueChannelPtr.reset(new (std::nothrow) AivUrmaChannel(endpointHandle, channelDesc));
99 : } else {
100 0 : uniqueChannelPtr.reset(new (std::nothrow) AivUbMemChannel(endpointHandle, channelDesc));
101 : }
102 0 : break;
103 12 : case COMM_ENGINE_CCU:
104 12 : uniqueChannelPtr.reset(
105 12 : new (std::nothrow) CcuUrmaChannel(endpointHandle, channelDesc));
106 12 : break;
107 1 : default:
108 1 : HCCL_ERROR("[Channel][%s] invalid type of CommEngine", __func__);
109 1 : return HCCL_E_NOT_FOUND;
110 : }
111 16 : CHK_PTR_NULL(uniqueChannelPtr);
112 16 : uniqueChannelPtr->engine_ = engine;
113 16 : CHK_RET_UNAVAIL(uniqueChannelPtr->Init());
114 14 : channelPtr = std::move(uniqueChannelPtr);
115 14 : return HCCL_SUCCESS;
116 19 : }
117 :
118 2 : ChannelStatus Channel::TransportStatusToChannelStatus(Hccl::TransportStatus ts)
119 : {
120 2 : switch (ts) {
121 0 : case Hccl::TransportStatus::INIT:
122 0 : return ChannelStatus::INIT;
123 0 : case Hccl::TransportStatus::SOCKET_OK:
124 0 : return ChannelStatus::SOCKET_OK;
125 0 : case Hccl::TransportStatus::SOCKET_TIMEOUT:
126 0 : return ChannelStatus::SOCKET_TIMEOUT;
127 2 : case Hccl::TransportStatus::READY:
128 2 : return ChannelStatus::READY;
129 0 : default:
130 0 : HCCL_ERROR("[Channel][%s] Invalid TransportStatus[%d]", __func__, ts);
131 0 : return ChannelStatus::FAILED;
132 : }
133 : }
134 :
135 0 : HcclResult Channel::UpdateMemInfo(HcommMemHandle *memHandles, uint32_t memHandleNum)
136 : {
137 0 : HCCL_WARNING("[UpdateMemInfo] not support.");
138 0 : return HCCL_SUCCESS;
139 : }
140 :
141 1 : HcommChannelKind Channel::GetChannelKind() const
142 : {
143 1 : return channelKind_;
144 : }
145 :
146 1 : HcclResult Channel::Serialize(std::shared_ptr<hccl::DeviceMem> &out)
147 : {
148 1 : out.reset();
149 1 : return HCCL_E_NOT_SUPPORT;
150 : }
151 :
152 0 : void Channel::AddPtrArrayDevMem(std::shared_ptr<hccl::DeviceMem> ptrArrayMem)
153 : {
154 0 : if (ptrArrayMem == nullptr || !(*ptrArrayMem)) {
155 0 : HCCL_WARNING("[Channel][%s] invalid ptrArrayMem.", __func__);
156 0 : return;
157 : }
158 0 : ptrArrayDevMems_.push_back(std::move(ptrArrayMem));
159 : }
160 :
161 3 : void Channel::ReleasePtrArrayDevMems()
162 : {
163 3 : ptrArrayDevMems_.clear();
164 3 : }
165 : } // namespace hcomm
|