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 "aicpu_ts_channel_helper.h"
11 : #include "channel_process.h"
12 : #include "channel.h"
13 : #include "launch_aicpu.h"
14 : #include "launch_device.h"
15 : #include "comm_engine_utils.h"
16 : #include "adapter_rts_common.h"
17 :
18 : using namespace hcomm;
19 :
20 : aclrtBinHandle AicpuTsChannelHelper::g_BinHandle = nullptr;
21 : std::mutex AicpuTsChannelHelper::g_BinHandleMtx;
22 :
23 0 : HcclResult AicpuTsChannelHelper::TryFillCtxList(
24 : ChannelHandle* hostChannelHandles, uint32_t listNum, const hccl::DeviceMem& deviceChannelList, void*& outCtxList,
25 : bool& isCtxMode)
26 : {
27 0 : auto* firstCh = reinterpret_cast<Channel*>(hostChannelHandles[0]);
28 0 : CHK_PTR_NULL(firstCh);
29 0 : auto* firstHelper = firstCh->GetAicpuTsHelper();
30 0 : CHK_PTR_NULL(firstHelper);
31 0 : isCtxMode = (firstHelper->GetCtxPtr() != nullptr);
32 0 : HCCL_INFO("[%s] isCtxMode[%d].", __func__, isCtxMode);
33 0 : if (!isCtxMode) {
34 0 : return HCCL_SUCCESS;
35 : }
36 0 : std::vector<void*> ctxVec(listNum);
37 0 : for (uint32_t i = 0; i < listNum; i++) {
38 0 : auto* ch = reinterpret_cast<Channel*>(hostChannelHandles[i]);
39 0 : CHK_PTR_NULL(ch);
40 0 : auto* helper = ch->GetAicpuTsHelper();
41 0 : ctxVec[i] = helper ? helper->GetCtxPtr() : nullptr;
42 : }
43 0 : HcclResult ret = hrtMemSyncCopy(
44 0 : deviceChannelList.ptr(), listNum * sizeof(void*), ctxVec.data(), listNum * sizeof(void*),
45 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE);
46 0 : if (ret != HCCL_SUCCESS) {
47 0 : HCCL_ERROR("[TryFillCtxList] hrtMemSyncCopy failed, ret[%d]", ret);
48 0 : return ret;
49 : }
50 0 : outCtxList = deviceChannelList.ptr();
51 0 : return HCCL_SUCCESS;
52 0 : }
53 :
54 15 : HcclResult AicpuTsChannelHelper::EnsureKernelBinLoaded(CommEngine engine)
55 : {
56 15 : if (engine != COMM_ENGINE_AICPU && engine != COMM_ENGINE_AICPU_TS) {
57 12 : HCCL_INFO(
58 : "[%s] engine[%s] kernel loading not required", __func__,
59 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
60 12 : return HCCL_SUCCESS;
61 : }
62 3 : std::lock_guard<std::mutex> lock(g_BinHandleMtx);
63 3 : if (g_BinHandle != nullptr) {
64 0 : return HCCL_SUCCESS;
65 : }
66 3 : std::string jsonPath;
67 3 : CHK_RET(hccl::GetKernelFilePath(jsonPath));
68 3 : jsonPath += "ccl_kernel.json";
69 :
70 3 : HcclResult ret = hccl::LoadBinaryFromFile(jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 0, g_BinHandle);
71 3 : CHK_PRT_RET(
72 : ret != HCCL_SUCCESS, HCCL_ERROR("[%s] load aicpu file fail, path[%s]", __func__, jsonPath.c_str()), ret);
73 3 : return HCCL_SUCCESS;
74 3 : }
75 :
76 3 : HcclResult AicpuTsChannelHelper::LaunchKernel(
77 : const ChannelHandle* channelList, uint32_t listNum, CommEngine engine, const HcommChannelDesc* channelDescs,
78 : aclrtBinHandle binHandle)
79 : {
80 3 : CHK_PTR_NULL(channelList);
81 3 : CHK_PRT_RET((listNum == 0), HCCL_ERROR("[%s]Invalid listNum, listNum[%u]", __func__, listNum), HCCL_E_PARA);
82 :
83 : // 过滤出未就绪的子集,避免重复下 kernel 导致 device 侧 channel 对象泄漏
84 3 : std::vector<ChannelHandle> subHostHandles;
85 3 : std::vector<HcommChannelDesc> subDescs;
86 3 : std::vector<Channel*> subChannels;
87 3 : subHostHandles.reserve(listNum);
88 3 : subDescs.reserve(listNum);
89 3 : subChannels.reserve(listNum);
90 6 : for (uint32_t i = 0; i < listNum; i++) {
91 3 : void* ch = nullptr;
92 3 : CHK_RET(ChannelProcess::ChannelGet(channelList[i], &ch));
93 3 : CHK_PTR_NULL(ch);
94 3 : auto* channel = static_cast<Channel*>(ch);
95 3 : if (channel->IsDeviceEntityReady()) {
96 3 : continue;
97 : }
98 0 : subHostHandles.push_back(reinterpret_cast<ChannelHandle>(ch));
99 0 : subDescs.push_back(channelDescs[i]);
100 0 : subChannels.push_back(channel);
101 : }
102 3 : if (subHostHandles.empty()) {
103 3 : HCCL_INFO("[%s] all channels already ready, skip kernel launch.", __func__);
104 3 : return HCCL_SUCCESS;
105 : }
106 : // 序列化 + kernel launch
107 0 : std::vector<ChannelHandle> devHandles(subHostHandles.size());
108 0 : CHK_RET(ChannelProcess::LaunchChannelKernel(
109 : devHandles.data(), subHostHandles.data(), subDescs.data(), subHostHandles.size(), binHandle));
110 0 : for (auto* channel : subChannels) {
111 0 : channel->SetDeviceEntityReady();
112 : }
113 0 : HCCL_INFO("[%s] aicpu kernel launch success, launched[%zu]/total[%u].", __func__, subHostHandles.size(), listNum);
114 0 : return HCCL_SUCCESS;
115 3 : }
116 :
117 7 : HcclResult AicpuTsChannelHelper::HandleStatus(
118 : const ChannelHandle* channelList, uint32_t listNum, CommEngine engine, const HcommChannelDesc* channelDescs,
119 : const std::vector<int32_t>& linkStatusList, int32_t* statusList)
120 : {
121 7 : bool allReady = true;
122 11 : for (uint32_t i = 0; i < listNum; i++) {
123 8 : if (linkStatusList[i] != HCOMM_CHANNEL_STATUS_READY) {
124 4 : allReady = false;
125 4 : break;
126 : }
127 : }
128 7 : if (!allReady) {
129 9 : for (uint32_t i = 0; i < listNum; i++) {
130 5 : if (linkStatusList[i] == HCOMM_CHANNEL_STATUS_FAILED) {
131 2 : statusList[i] = HCOMM_CHANNEL_STATUS_FAILED;
132 3 : } else if (linkStatusList[i] == HCOMM_CHANNEL_STATUS_TIMEOUT) {
133 1 : statusList[i] = HCOMM_CHANNEL_STATUS_TIMEOUT;
134 : } else {
135 2 : statusList[i] = HCOMM_CHANNEL_STATUS_CONNECTING;
136 : }
137 : }
138 4 : return HCCL_SUCCESS;
139 : }
140 3 : CHK_RET(EnsureKernelBinLoaded(engine));
141 3 : HcclResult kernelRet = LaunchKernel(channelList, listNum, engine, channelDescs, g_BinHandle);
142 3 : if (kernelRet != HCCL_SUCCESS) {
143 0 : HCCL_ERROR("[%s] LaunchKernel failed, ret[%d]", __func__, kernelRet);
144 0 : return HCCL_E_INTERNAL;
145 : }
146 6 : for (uint32_t i = 0; i < listNum; i++) {
147 3 : statusList[i] = linkStatusList[i];
148 : }
149 3 : return HCCL_SUCCESS;
150 : }
151 :
152 3 : HcclResult AicpuTsChannelHelper::PreAllocChannels(
153 : ChannelHandle* targetChannels, ChannelHandle* userChannels, HcommChannelDesc* channelDescs, uint32_t channelNum)
154 : {
155 3 : CHK_PTR_NULL(targetChannels);
156 3 : CHK_PTR_NULL(userChannels);
157 3 : CHK_PRT_RET(
158 : (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
159 :
160 5 : for (uint32_t i = 0; i < channelNum; i++) {
161 3 : auto* channel = reinterpret_cast<Channel*>(targetChannels[i]);
162 3 : CHK_PTR_NULL(channel);
163 2 : auto* helper = channel->GetAicpuTsHelper();
164 2 : CHK_PTR_NULL(helper);
165 2 : CHK_RET(helper->PreAllocCtx(userChannels[i]));
166 : // 清零 ctx device 内存,防止脏数据 magic 误匹配
167 2 : CHK_RET(hrtMemSet(helper->GetCtxPtr(), sizeof(HcommAicpuChannelCtx), sizeof(HcommAicpuChannelCtx)));
168 : }
169 :
170 2 : CHK_RET(ChannelProcess::FillChannelD2HMap(userChannels, targetChannels, channelNum));
171 2 : return HCCL_SUCCESS;
172 : }
|