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 "aiv_channel_helper.h"
12 : #include "channel_process.h"
13 : #include "channel.h"
14 : #include "aicpu_ts_roce_channel_v2.h"
15 : #include "aiv_urma_channel.h"
16 : #include "comm_engine_utils.h"
17 :
18 : using namespace hcomm;
19 :
20 6 : HcclResult AivChannelHelper::FillDevEntities(
21 : const ChannelHandle* channelList, uint32_t listNum, const HcommChannelDesc* channelDescs,
22 : const int32_t* linkStatusList)
23 : {
24 6 : CHK_PTR_NULL(channelList);
25 6 : CHK_PTR_NULL(linkStatusList);
26 6 : CHK_PRT_RET((listNum == 0), HCCL_ERROR("[%s]Invalid listNum, listNum[%u]", __func__, listNum), HCCL_E_PARA);
27 :
28 12 : for (uint32_t i = 0; i < listNum; i++) {
29 6 : if (linkStatusList[i] != HCOMM_CHANNEL_STATUS_READY) {
30 6 : continue;
31 : }
32 2 : CommProtocol protocol = channelDescs[i].remoteEndpoint.protocol;
33 2 : void* channelPtr = nullptr;
34 2 : CHK_RET(ChannelProcess::ChannelGet(channelList[i], &channelPtr));
35 2 : CHK_PTR_NULL(channelPtr);
36 2 : auto* channel = static_cast<Channel*>(channelPtr);
37 2 : if (channel->IsDeviceEntityReady()) {
38 2 : continue;
39 : }
40 :
41 0 : if (protocol == COMM_PROTOCOL_ROCE) {
42 0 : auto* roceChannel = static_cast<AicpuTsRoceChannelV2*>(channelPtr);
43 0 : CHK_RET(roceChannel->FillDevChannelEntity());
44 : }
45 :
46 0 : if (protocol == COMM_PROTOCOL_UB_CTP || protocol == COMM_PROTOCOL_UBC_TP || protocol == COMM_PROTOCOL_UB_RTP) {
47 0 : auto* aivChannel = static_cast<AivUrmaChannel*>(channelPtr);
48 0 : CHK_RET(aivChannel->FillChannelEntityToDevice());
49 : }
50 :
51 0 : channel->SetDeviceEntityReady();
52 0 : HCCL_INFO("[%s] channel[%u] fill dev entity success.", __func__, i);
53 : }
54 6 : return HCCL_SUCCESS;
55 : }
56 :
57 6 : HcclResult AivChannelHelper::HandleStatus(
58 : const ChannelHandle* channelList, uint32_t listNum, const HcommChannelDesc* channelDescs,
59 : const std::vector<int32_t>& linkStatusList, int32_t* statusList)
60 : {
61 6 : HcclResult fillRet = FillDevEntities(channelList, listNum, channelDescs, linkStatusList.data());
62 6 : if (fillRet != HCCL_SUCCESS) {
63 0 : HCCL_ERROR("[%s] FillDevEntities failed, ret[%d]", __func__, fillRet);
64 0 : return HCCL_E_INTERNAL;
65 : }
66 12 : for (uint32_t i = 0; i < listNum; i++) {
67 6 : statusList[i] = linkStatusList[i];
68 : }
69 6 : return HCCL_SUCCESS;
70 : }
71 :
72 0 : HcclResult AivChannelHelper::PreAllocChannels(
73 : ChannelHandle* targetChannels, ChannelHandle* userChannels, HcommChannelDesc* channelDescs, uint32_t channelNum)
74 : {
75 0 : CHK_PTR_NULL(targetChannels);
76 0 : CHK_PTR_NULL(userChannels);
77 0 : CHK_PRT_RET(
78 : (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
79 :
80 0 : bool needD2HMap = false;
81 0 : for (uint32_t i = 0; i < channelNum; i++) {
82 0 : CommProtocol protocol = channelDescs[i].remoteEndpoint.protocol;
83 :
84 0 : if (protocol == COMM_PROTOCOL_ROCE) {
85 0 : needD2HMap = true;
86 0 : auto* channel = reinterpret_cast<AicpuTsRoceChannelV2*>(targetChannels[i]);
87 0 : CHK_PTR_NULL(channel);
88 0 : CHK_RET(channel->PreAllocDevChannelEntity(&userChannels[i]));
89 0 : HCCL_INFO(
90 : "[%s] channel[%u] pre-alloc dev entity success, devEntityPtr[%p]", __func__, i,
91 : reinterpret_cast<void*>(static_cast<uintptr_t>(userChannels[i])));
92 0 : } else if (
93 0 : protocol == COMM_PROTOCOL_UB_CTP || protocol == COMM_PROTOCOL_UBC_TP || protocol == COMM_PROTOCOL_UB_RTP) {
94 0 : needD2HMap = true;
95 0 : auto* channel = reinterpret_cast<AivUrmaChannel*>(targetChannels[i]);
96 0 : CHK_PTR_NULL(channel);
97 :
98 0 : void* devChannelEntity = nullptr;
99 0 : HcclResult ret = channel->PreAllocChannelEntityToDevice(&devChannelEntity);
100 0 : CHK_PRT_RET(
101 : ret != HCCL_SUCCESS,
102 : HCCL_ERROR("[%s] channel[%u] PreAllocChannelEntityToDevice failed, ret[%d]", __func__, i, ret), ret);
103 0 : CHK_PTR_NULL(devChannelEntity);
104 0 : userChannels[i] = static_cast<ChannelHandle>(reinterpret_cast<uintptr_t>(devChannelEntity));
105 0 : HCCL_INFO(
106 : "[%s] channel[%u] pre-alloc dev entity success, devEntityPtr[%p]", __func__, i,
107 : reinterpret_cast<void*>(static_cast<uintptr_t>(userChannels[i])));
108 0 : } else {
109 0 : userChannels[i] = targetChannels[i];
110 0 : HCCL_INFO(
111 : "[%s] AIV engine channel protocol not supported pre-alloc, idx[%u], protocol[%d]. "
112 : "Return host channel handle.",
113 : __func__, i, static_cast<int>(protocol));
114 : }
115 : }
116 :
117 0 : if (needD2HMap) {
118 0 : CHK_RET(ChannelProcess::FillChannelD2HMap(userChannels, targetChannels, channelNum));
119 : }
120 0 : return HCCL_SUCCESS;
121 : }
|