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 "hccl_aicpu_interface.h"
12 :
13 : #include <sstream>
14 : #include <dlfcn.h>
15 : #include "common/aicpu_hccl_common.h"
16 : #include "common/aicpu_hccl_def.h"
17 : #include "common/aicpu_sqe_context.h"
18 : #include "profiling_manager_device.h"
19 : #include "framework/aicpu_hccl_process.h"
20 : #include "utils/hccl_aicpu_utils.h"
21 : #include "framework/aicpu_communicator.h"
22 : #include "utils/aicpu_hdc_utils.h"
23 : #include "hccl_group_utils.h"
24 : #include "hccl_dl.h"
25 : #include "hccl_diag.h"
26 :
27 : extern "C" {
28 0 : __attribute__((visibility("default"))) uint32_t RunAicpuKfcResInitV2(void* args)
29 : {
30 0 : if (args == nullptr) {
31 0 : HCCL_ERROR("args is null.");
32 0 : return HCCL_E_PARA;
33 : }
34 :
35 0 : KFCResInitTask* ctxArgs = reinterpret_cast<KFCResInitTask*>(args);
36 0 : HCCL_INFO("RunAicpuKfcResInitV2 isCustom %u, context %#llx", ctxArgs->isCustom, ctxArgs->context);
37 0 : if (ctxArgs->context == 0) { // for OneSideComm
38 0 : CHK_RET(hrtSetWorkModeAicpu(true));
39 0 : HCCL_INFO("RunAicpuKfcResInitV2 done as context is null, set aicpu work mode");
40 0 : return HCCL_SUCCESS;
41 : }
42 0 : return AicpuHcclProcess::AicpuRpcResInitV2(reinterpret_cast<HcclOpResParam*>(ctxArgs->context), ctxArgs->isCustom);
43 : }
44 :
45 1 : __attribute__((visibility("default"))) uint32_t RunAicpuRpcSrvLaunchV2(void* args)
46 : {
47 1 : if (args == nullptr) {
48 0 : HCCL_ERROR("RunAicpuRpcSrvLaunchV2 args is null.");
49 0 : return HCCL_E_PARA;
50 : }
51 :
52 1 : KFCTaskComm* task = reinterpret_cast<KFCTaskComm*>(args);
53 1 : OpTilingData* tilingData = reinterpret_cast<OpTilingData*>(reinterpret_cast<std::uintptr_t>(task) + sizeof(u64));
54 1 : if (tilingData == nullptr) {
55 0 : HCCL_ERROR("RunAicpuRpcSrvLaunchV2 tilingData args is null.");
56 0 : return HCCL_E_PARA;
57 : }
58 :
59 1 : const HcclCMDType opType = static_cast<HcclCMDType>(tilingData->opType);
60 1 : if ((opType == HcclCMDType::HCCL_CMD_BATCH_GET) || (opType == HcclCMDType::HCCL_CMD_BATCH_PUT)) {
61 0 : return AicpuHcclProcess::HandleOneSideService(tilingData);
62 : }
63 :
64 1 : HcclOpResParam* commParam = reinterpret_cast<HcclOpResParam*>(task->context);
65 1 : if (commParam == nullptr) {
66 0 : HCCL_ERROR("RunAicpuRpcSrvLaunchV2 context args is null.");
67 0 : return HCCL_E_PARA;
68 : }
69 1 : HCCL_INFO("RunAicpuRpcSrvLaunchV2 KFCTask task %p, context %p, tilingData %p", task, commParam, tilingData);
70 :
71 1 : std::string group = commParam->hcomId;
72 1 : hccl::HcclCommAicpu* hcclCommAicpu = AicpuHcclProcess::AicpuGetCommbyGroup(group);
73 1 : if (hcclCommAicpu == nullptr) {
74 0 : HCCL_ERROR("RunAicpuRpcSrvLaunchV2 get Hcclcomm error group[%s], tag[%s]", commParam->hcomId, tilingData->tag);
75 0 : return HCCL_E_INTERNAL;
76 : }
77 1 : HCCL_INFO(
78 : "[RunAicpuRpcSrvLaunchV2] isZeroCopy [%d], isSymmetricMemory [%d], workflowMode[%d]", tilingData->isZeroCopy,
79 : tilingData->isSymmetricMemory, tilingData->workflowMode);
80 1 : hcclCommAicpu->SetZeroCopyEnable(tilingData->isZeroCopy);
81 1 : hcclCommAicpu->SetSymmetricMemoryEnable(tilingData->isSymmetricMemory);
82 1 : DfxExtendInfo* dfxInfo = hcclCommAicpu->GetDfxExtendInfo();
83 1 : if ((dfxInfo->cqeStatus != dfx::CqeStatus::kDefault) || (dfxInfo->pollStatus == PollStatus::kStopAsException)) {
84 0 : AicpuHcclProcess::AicpuReleaseCommbyGroup(group);
85 0 : HCCL_ERROR(
86 : "RunAicpuRpcSrvLaunchV2 exist errors before, cqeStatus:%d, pollStatus:%d, group[%s], sqeType[%u]",
87 : dfxInfo->cqeStatus, dfxInfo->pollStatus, commParam->hcomId, dfxInfo->cqeException.sqeType);
88 0 : if (dfxInfo->cqeException.sqeType == RT_STARS_SQE_TYPE_SDMA) {
89 0 : return TS_ERROR_AICPU_SDMA;
90 : }
91 0 : return HCCL_E_INTERNAL;
92 : }
93 1 : SetWorkflowMode(static_cast<HcclWorkflowMode>(tilingData->workflowMode));
94 1 : HCCL_DEBUG("[NsRecovery]check the suspending status in hcclCommAicpu");
95 1 : HcclComSuspendingFlag kfcFlag = HcclComSuspendingFlag ::isResume;
96 1 : CHK_RET(hcclCommAicpu->GetSuspendingFlag(kfcFlag));
97 1 : if (kfcFlag == HcclComSuspendingFlag::isSuspending) {
98 1 : HCCL_RUN_WARNING("[NsRecovery] the op should not be launched in hcclCommAicpu on the suspending status");
99 1 : HCCL_RUN_WARNING("[NsRecovery] Possible reason: flag not refresh, not enough time between resume and launch");
100 1 : AicpuHcclProcess::AicpuReleaseCommbyGroup(group);
101 1 : return 0;
102 : }
103 0 : hcclCommAicpu->SetNsStopLaunchStatus(false);
104 0 : HcclResult res = AicpuHcclProcess::AicpuRunRpcServerV2(hcclCommAicpu, tilingData, commParam);
105 0 : AicpuHcclProcess::AicpuReleaseCommbyGroup(group);
106 0 : if (res != HCCL_SUCCESS) {
107 0 : if (res == HCCL_E_OPRETRY_FAIL) {
108 0 : HCCL_RUN_INFO("Retry failed, support step retry");
109 0 : return TS_ERROR_RETRY_CONSTRAINT;
110 0 : } else if (res != HCCL_E_SUSPENDING) {
111 0 : if (dfxInfo->cqeException.sqeType == RT_STARS_SQE_TYPE_SDMA) {
112 0 : HCCL_ERROR("run AicpuRunRpcServerV2 failed. ret[%u]", TS_ERROR_AICPU_SDMA);
113 0 : return TS_ERROR_AICPU_SDMA;
114 : }
115 0 : HCCL_ERROR("run AicpuRunRpcServerV2 failed. ret[%d]", res);
116 0 : return res;
117 : } else {
118 0 : HCCL_INFO("aicpu is suspended");
119 0 : return AICPUSUSPENDING_ERROR;
120 : }
121 : }
122 0 : HCCL_INFO("end RunAicpuRpcSrvLaunchV2");
123 0 : return 0;
124 1 : }
125 :
126 1 : __attribute__((visibility("default"))) uint32_t RunAicpuNotifyRecord(void* args)
127 : {
128 1 : if (args == nullptr) {
129 0 : HCCL_ERROR("RunAicpuNotifyRecord args is null.");
130 0 : return HCCL_E_PARA;
131 : }
132 1 : ThreadNotifyRecordParam* param = reinterpret_cast<ThreadNotifyRecordParam*>(args);
133 1 : HCCL_INFO("%s src[0x%llx], dst[0x%llx], Idx[%u]", __func__, param->thread, param->dstThread, param->dstNotifyIdx);
134 : // 保留通信域管理 - 保证生命周期安全
135 1 : if (HcommAcquireComm(param->commName) != HCCL_SUCCESS) {
136 0 : HCCL_ERROR("%s HcommAcquireComm fail, commName[%s]", __func__, param->commName);
137 0 : return HCCL_E_INTERNAL;
138 : }
139 1 : HcclDfxOpInfo dfxOpInfoRecord{};
140 1 : dfxOpInfoRecord.cpuWaitAicpuNotifyIdx = 0;
141 1 : dfxOpInfoRecord.cpuTsThread = param->thread;
142 1 : dfxOpInfoRecord.dataType = param->dataType;
143 1 : if (HcclDfxRegOpInfoByCommId(param->commName, reinterpret_cast<void*>(&dfxOpInfoRecord)) != HCCL_SUCCESS) {
144 0 : HCCL_ERROR("%s HcclDfxRegOpInfoByCommId fail, commName[%s].", __func__, param->commName);
145 0 : HcommReleaseComm(param->commName);
146 0 : return HCCL_E_INTERNAL;
147 : }
148 :
149 1 : if (HcommProfilingReportKernelStartTask(param->thread, param->commName) != HCCL_SUCCESS) {
150 0 : HCCL_ERROR(
151 : "%s failed to report record, thread %lu, param->commName %s.", __func__, param->thread, param->commName);
152 0 : HcommReleaseComm(param->commName);
153 0 : return HCCL_E_INTERNAL;
154 : }
155 1 : int32_t ret = HcommThreadNotifyRecordOnThread(param->thread, param->dstThread, param->dstNotifyIdx);
156 1 : if (ret != HCCL_SUCCESS) {
157 0 : HCCL_ERROR("RunAicpuNotifyRecord failed. ret[%d]", ret);
158 0 : HcommReleaseComm(param->commName);
159 0 : return ret;
160 : }
161 1 : if (HcommProfilingReportKernelEndTask(param->thread, param->commName) != HCCL_SUCCESS) {
162 0 : HCCL_ERROR(
163 : "%s failed to report record, thread %lu, param->commName %s.", __func__, param->thread, param->commName);
164 0 : HcommReleaseComm(param->commName);
165 0 : return HCCL_E_INTERNAL;
166 : }
167 1 : if (HcommProfilingReportDeviceOp(param->commName) != HCCL_SUCCESS) {
168 0 : HCCL_ERROR("%s HcommProfilingReportDeviceOp[record] fail, commName[%s].", __func__, param->commName);
169 0 : HcommReleaseComm(param->commName);
170 0 : return HCCL_E_INTERNAL;
171 : }
172 :
173 1 : HCCL_INFO("RunAicpuNotifyRecord success.");
174 1 : if (HcommReleaseComm(param->commName) != HCCL_SUCCESS) {
175 0 : HCCL_ERROR("%s HcommReleaseComm fail, commName[%s]", __func__, param->commName);
176 0 : return HCCL_E_INTERNAL;
177 : }
178 :
179 1 : return HCCL_SUCCESS;
180 : }
181 :
182 1 : __attribute__((visibility("default"))) uint32_t RunAicpuNotifyWait(void* args)
183 : {
184 1 : if (args == nullptr) {
185 0 : HCCL_ERROR("RunAicpuNotifyWait args is null.");
186 0 : return HCCL_E_PARA;
187 : }
188 1 : ThreadNotifyWaitParam* param = reinterpret_cast<ThreadNotifyWaitParam*>(args);
189 1 : HCCL_INFO("[RunAicpuNotifyWait] thread[0x%llx], notifyIdx[%u]", param->thread, param->notifyIdx);
190 : // 保留通信域管理 - 保证生命周期安全
191 1 : if (HcommAcquireComm(param->commName) != HCCL_SUCCESS) {
192 0 : HCCL_ERROR("%s HcommAcquireComm fail, commName[%s]", __func__, param->commName);
193 0 : return HCCL_E_INTERNAL;
194 : }
195 1 : HcclDfxOpInfo dfxOpInfoWait{};
196 1 : dfxOpInfoWait.cpuWaitAicpuNotifyIdx = 0;
197 1 : dfxOpInfoWait.cpuTsThread = param->thread;
198 1 : dfxOpInfoWait.dataType = param->dataType;
199 1 : if (HcclDfxRegOpInfoByCommId(param->commName, reinterpret_cast<void*>(&dfxOpInfoWait)) != HCCL_SUCCESS) {
200 0 : HCCL_ERROR("%s HcclDfxRegOpInfoByCommId fail, commName[%s].", __func__, param->commName);
201 0 : HcommReleaseComm(param->commName);
202 0 : return HCCL_E_INTERNAL;
203 : }
204 :
205 1 : if (HcommProfilingReportKernelStartTask(param->thread, param->commName) != HCCL_SUCCESS) {
206 0 : HCCL_ERROR(
207 : "%s failed to report wait, thread %lu, param->commName %s.", __func__, param->thread, param->commName);
208 0 : HcommReleaseComm(param->commName);
209 0 : return HCCL_E_INTERNAL;
210 : }
211 1 : int32_t ret = HcommThreadNotifyWaitOnThreadWithDefaultTimeout(param->thread, param->notifyIdx);
212 1 : if (ret != HCCL_SUCCESS) {
213 0 : HCCL_ERROR("RunAicpuNotifyWait failed. ret[%d]", ret);
214 0 : HcommReleaseComm(param->commName);
215 0 : return ret;
216 : }
217 1 : if (HcommProfilingReportKernelEndTask(param->thread, param->commName) != HCCL_SUCCESS) {
218 0 : HCCL_ERROR(
219 : "%s failed to report wait, thread %lu, param->commName %s.", __func__, param->thread, param->commName);
220 0 : HcommReleaseComm(param->commName);
221 0 : return HCCL_E_INTERNAL;
222 : }
223 1 : if (HcommProfilingReportDeviceOp(param->commName) != HCCL_SUCCESS) {
224 0 : HCCL_ERROR("%s HcommProfilingReportDeviceOp[wait] fail, commName[%s].", __func__, param->commName);
225 0 : HcommReleaseComm(param->commName);
226 0 : return HCCL_E_INTERNAL;
227 : }
228 :
229 1 : HCCL_INFO("RunAicpuNotifyWait success.");
230 1 : if (HcommReleaseComm(param->commName) != HCCL_SUCCESS) {
231 0 : HCCL_ERROR("%s HcommReleaseComm fail, commName[%s]", __func__, param->commName);
232 0 : return HCCL_E_INTERNAL;
233 : }
234 1 : return HCCL_SUCCESS;
235 : }
236 :
237 : } // extern "C"
|