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