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 "kernel_entrance.h"
12 : #include "aicpu_comm_destroy_func.h"
13 : #include "communicator_impl_lite_manager.h"
14 : #include "ub_conn_lite_mgr.h"
15 : #include "aicpu_daemon_service.h"
16 : #include "task_exception_func.h"
17 : #include "ns_recovery_handler_func.h"
18 : #include "task_exception_handler_lite.h"
19 : #include "log.h"
20 : #include "inc/aicpu_utils.h"
21 : #ifdef CCL_KERNEL_AICPU
22 : #include "profiling_command_handle_lite.h"
23 : #include "aicpu_indop_process.h"
24 : #endif
25 :
26 : std::unordered_map<std::string, void*> g_taskExpDevMemMap;
27 : std::mutex g_taskExpDevMemMapMutex;
28 : extern "C" {
29 : using namespace Hccl;
30 :
31 2 : uint32_t SetOldA5CommToCommMgr(std::string group, Hccl::CommunicatorImplLite *communicatorImplLite) {
32 : #ifdef CCL_KERNEL_AICPU
33 : CollCommAicpuMgr *collCommAicpuMgr = nullptr;
34 : HcclResult ret = AicpuIndopProcess::AcquireAicpuCommMgr(group, &collCommAicpuMgr);
35 : if (ret != HcclResult::HCCL_SUCCESS) {
36 : HCCL_ERROR("%s Acquire aicpu commMgr failed, group[%s].", __func__, group.c_str());
37 : return 1;
38 : }
39 : CHK_PRT_RET(collCommAicpuMgr == nullptr, HCCL_ERROR("%s collCommAicpuMgr is null, group[%s]", __func__, group.c_str()), 1);
40 : collCommAicpuMgr->SetOldA5Comm(communicatorImplLite);
41 : HCCL_INFO("Acquire AicpuCommMgr success");
42 : #endif
43 2 : return 0;
44 : }
45 :
46 2 : uint32_t HcclKernelEntrance(void *args)
47 : {
48 2 : if (args == nullptr) {
49 3 : HCCL_ERROR("HcclKernelEntrance Args is null.");
50 1 : return 1;
51 : }
52 :
53 : #ifdef CCL_KERNEL_AICPU
54 : RegisterProfCallBack();
55 : #endif
56 :
57 1 : auto *kernelParam = reinterpret_cast<HcclKernelParamLite *>(args);
58 1 : AicpuUtils::GetInstance().CreateSingleInstance(args);
59 1 : CHK_RET(AicpuUtils::GetInstance().Init());
60 1 : NsRecoveryHandlerFunc::GetInstance();
61 1 : CHK_RET(DlHalFunctionV2::GetInstance().DlHalFunctionInit());
62 :
63 1 : u32 commIdIndex = kernelParam->comm.idIndex;
64 3 : HCCL_RUN_INFO("HcclKernelEntrance begin, OpType[%s] algName[%s] commIdIndex[%u] commId[%s] opTag[%s], devPhyId[%u] myRank[%u] rankSize[%u] oneSidedComm[%d] opIndex[%u]",
65 : kernelParam->op.algOperator.opType.Describe().c_str(), kernelParam->algName, commIdIndex, kernelParam->comm.commId,
66 : kernelParam->opTag, kernelParam->comm.devPhyId, kernelParam->comm.myRank, kernelParam->comm.rankSize, kernelParam->oneSidedComm, kernelParam->comm.opIndex_);
67 :
68 1 : Hccl::CommunicatorImplLite *communicatorImplLite = CommunicatorImplLiteMgr::GetInstance().Get(commIdIndex);
69 1 : if (communicatorImplLite == nullptr) {
70 0 : HCCL_ERROR("HcclKernelEntrance communicatorImplLite is null.");
71 0 : return 1;
72 : }
73 :
74 2 : if (SetOldA5CommToCommMgr(kernelParam->comm.commId, communicatorImplLite) != 0) {
75 0 : HCCL_ERROR("SetOldA5CommToCommMgr failed.");
76 0 : return 1;
77 : }
78 :
79 1 : CHK_RET(AicpuUtils::GetInstance().WaitCommFree(communicatorImplLite, __func__));
80 1 : if (communicatorImplLite->LoadWithOpBasedMode(kernelParam) != 0) {
81 3 : HCCL_ERROR("HcclKernelEntrance LoadWithOpBasedMode failed.");
82 1 : return 1;
83 : }
84 :
85 0 : HCCL_INFO("HcclKernelEntrance success.");
86 0 : unique_lock<std::mutex> aicpuLock(communicatorImplLite->GetAicpuMc2Mutex());
87 0 : communicatorImplLite->SetIsUsed(false);
88 0 : aicpuLock.unlock();
89 0 : return 0;
90 0 : }
91 :
92 2 : uint32_t HcclUpdateCommKernelEntrance(void *args)
93 : {
94 2 : if (args == nullptr) {
95 3 : HCCL_ERROR("[NsRecovery] HcclUpdateCommKernelEntrance Args is null.");
96 1 : return 1;
97 : }
98 :
99 1 : auto *kernelParam = reinterpret_cast<HcclKernelParamLite *>(args);
100 1 : u32 commIdIndex = kernelParam->comm.idIndex;
101 3 : HCCL_INFO("[NsRecovery] HcclUpdateCommKernelEntrance begin, commIdIndex[%u]", commIdIndex);
102 :
103 1 : Hccl::CommunicatorImplLite *communicatorImplLite = CommunicatorImplLiteMgr::GetInstance().Get(commIdIndex);
104 1 : if (communicatorImplLite == nullptr) {
105 0 : HCCL_ERROR("HcclUpdateCommKernelEntrance communicatorImplLite is null.");
106 0 : return 1;
107 : }
108 :
109 2 : if (SetOldA5CommToCommMgr(kernelParam->comm.commId, communicatorImplLite) != 0) {
110 0 : HCCL_ERROR("SetOldA5CommToCommMgr failed.");
111 0 : return 1;
112 : }
113 1 : CHK_RET(AicpuUtils::GetInstance().WaitCommFree(communicatorImplLite, __func__));
114 1 : communicatorImplLite->UpdateComm(kernelParam);
115 1 : unique_lock<std::mutex> aicpuLock(communicatorImplLite->GetAicpuMc2Mutex());
116 1 : communicatorImplLite->SetIsUsed(false);
117 1 : aicpuLock.unlock();
118 3 : HCCL_INFO("[NsRecovery] HcclUpdateCommKernelEntrance success.");
119 1 : return 0;
120 1 : }
121 :
122 0 : uint32_t HcclDpuTaskexpShmemRestore(void *args)
123 : {
124 0 : if (args == nullptr) {
125 0 : HCCL_ERROR("HcclDpuTaskexpShmemRestore Args is null.");
126 0 : return 1;
127 : }
128 : struct AicpuKernelLaunchParam {
129 : char commId[COMM_NAME_MAX_LENGTH];
130 : void *taskexceptionVa;
131 : u64 memorySize;
132 : uint32_t deviceId;
133 : };
134 0 : auto *kernelParam = reinterpret_cast<AicpuKernelLaunchParam *>(args);
135 0 : if (kernelParam->taskexceptionVa == nullptr) {
136 0 : HCCL_ERROR("taskexceptionVa is nullptr, please check communicatorImpl init");
137 0 : return 1;
138 : }
139 0 : errno_t ret = memset_s(kernelParam->taskexceptionVa, kernelParam->memorySize, 0, kernelParam->memorySize); // 避免背景线程读到脏数据
140 0 : if (ret != EOK) {
141 0 : HCCL_ERROR("kernelParam->taskexceptionVa[%p] set 0 Fail, return[%d]", kernelParam->taskexceptionVa, ret);
142 0 : return 1;
143 : }
144 0 : std::string commId = kernelParam->commId;
145 : {
146 0 : std::lock_guard<std::mutex> lock(g_taskExpDevMemMapMutex);
147 0 : auto it = g_taskExpDevMemMap.find(commId);
148 0 : if (it == g_taskExpDevMemMap.end()) {
149 0 : g_taskExpDevMemMap.insert({commId, kernelParam->taskexceptionVa});
150 : }
151 0 : } // 只在通信域创建时保存一次,通信域销毁时该处会同步销毁,不存在需要更新的场景
152 0 : HCCL_INFO("HcclDpuTaskexpShmemRestore success. commId[%s], deviceId[%u], taskexceptionVa[%p], memorySize[%llu]",
153 : commId.c_str(), kernelParam->deviceId, kernelParam->taskexceptionVa, kernelParam->memorySize);
154 0 : return 0;
155 0 : }
156 : }
|