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 "coll_comm_aicpu_mgr.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 HcclKernelEntrance(void* args)
32 : {
33 2 : if (args == nullptr) {
34 3 : HCCL_ERROR("HcclKernelEntrance Args is null.");
35 1 : return 1;
36 : }
37 :
38 : #ifdef CCL_KERNEL_AICPU
39 : RegisterProfCallBack();
40 : #endif
41 :
42 1 : auto* kernelParam = reinterpret_cast<HcclKernelParamLite*>(args);
43 1 : AicpuUtils::GetInstance().CreateSingleInstance(args);
44 1 : CHK_RET(AicpuUtils::GetInstance().Init());
45 1 : NsRecoveryHandlerFunc::GetInstance();
46 1 : CHK_RET(DlHalFunctionV2::GetInstance().DlHalFunctionInit());
47 :
48 1 : u32 commIdIndex = kernelParam->comm.idIndex;
49 3 : HCCL_RUN_INFO(
50 : "HcclKernelEntrance begin, OpType[%s] algName[%s] commIdIndex[%u] commId[%s] opTag[%s], devPhyId[%u] "
51 : "myRank[%u] rankSize[%u] oneSidedComm[%d] opIndex[%u]",
52 : kernelParam->op.algOperator.opType.Describe().c_str(), kernelParam->algName, commIdIndex,
53 : kernelParam->comm.commId, kernelParam->opTag, kernelParam->comm.devPhyId, kernelParam->comm.myRank,
54 : kernelParam->comm.rankSize, kernelParam->oneSidedComm, kernelParam->comm.opIndex_);
55 :
56 1 : Hccl::CommunicatorImplLite* communicatorImplLite = CommunicatorImplLiteMgr::GetInstance().Get(commIdIndex);
57 1 : if (communicatorImplLite == nullptr) {
58 0 : HCCL_ERROR("HcclKernelEntrance communicatorImplLite is null.");
59 0 : return 1;
60 : }
61 :
62 1 : CHK_RET(AicpuUtils::GetInstance().WaitCommFree(communicatorImplLite, __func__));
63 1 : if (communicatorImplLite->LoadWithOpBasedMode(kernelParam) != 0) {
64 3 : HCCL_ERROR("HcclKernelEntrance LoadWithOpBasedMode failed.");
65 1 : return 1;
66 : }
67 :
68 0 : HCCL_INFO("HcclKernelEntrance success.");
69 0 : unique_lock<std::mutex> aicpuLock(communicatorImplLite->GetAicpuMc2Mutex());
70 0 : communicatorImplLite->SetIsUsed(false);
71 0 : aicpuLock.unlock();
72 0 : return 0;
73 0 : }
74 :
75 2 : uint32_t HcclUpdateCommKernelEntrance(void* args)
76 : {
77 2 : if (args == nullptr) {
78 3 : HCCL_ERROR("[NsRecovery] HcclUpdateCommKernelEntrance Args is null.");
79 1 : return 1;
80 : }
81 :
82 1 : auto* kernelParam = reinterpret_cast<HcclKernelParamLite*>(args);
83 1 : u32 commIdIndex = kernelParam->comm.idIndex;
84 3 : HCCL_INFO("[NsRecovery] HcclUpdateCommKernelEntrance begin, commIdIndex[%u]", commIdIndex);
85 :
86 1 : Hccl::CommunicatorImplLite* communicatorImplLite = CommunicatorImplLiteMgr::GetInstance().Get(commIdIndex);
87 1 : if (communicatorImplLite == nullptr) {
88 0 : HCCL_ERROR("HcclUpdateCommKernelEntrance communicatorImplLite is null.");
89 0 : return 1;
90 : }
91 :
92 1 : CHK_RET(AicpuUtils::GetInstance().WaitCommFree(communicatorImplLite, __func__));
93 1 : communicatorImplLite->UpdateComm(kernelParam);
94 1 : unique_lock<std::mutex> aicpuLock(communicatorImplLite->GetAicpuMc2Mutex());
95 1 : communicatorImplLite->SetIsUsed(false);
96 1 : aicpuLock.unlock();
97 3 : HCCL_INFO("[NsRecovery] HcclUpdateCommKernelEntrance success.");
98 1 : return 0;
99 1 : }
100 :
101 0 : uint32_t HcclDpuTaskexpShmemRestore(void* args)
102 : {
103 0 : if (args == nullptr) {
104 0 : HCCL_ERROR("HcclDpuTaskexpShmemRestore Args is null.");
105 0 : return 1;
106 : }
107 : struct AicpuKernelLaunchParam {
108 : char commId[COMM_NAME_MAX_LENGTH];
109 : void* taskexceptionVa;
110 : u64 memorySize;
111 : uint32_t deviceId;
112 : };
113 0 : auto* kernelParam = reinterpret_cast<AicpuKernelLaunchParam*>(args);
114 0 : if (kernelParam->taskexceptionVa == nullptr) {
115 0 : HCCL_ERROR("taskexceptionVa is nullptr, please check communicatorImpl init");
116 0 : return 1;
117 : }
118 0 : errno_t ret = memset_s(
119 0 : kernelParam->taskexceptionVa, kernelParam->memorySize, 0, kernelParam->memorySize); // 避免背景线程读到脏数据
120 0 : if (ret != EOK) {
121 0 : HCCL_ERROR("kernelParam->taskexceptionVa[%p] set 0 Fail, return[%d]", kernelParam->taskexceptionVa, ret);
122 0 : return 1;
123 : }
124 0 : std::string commId = kernelParam->commId;
125 : {
126 0 : std::lock_guard<std::mutex> lock(g_taskExpDevMemMapMutex);
127 0 : auto it = g_taskExpDevMemMap.find(commId);
128 0 : if (it == g_taskExpDevMemMap.end()) {
129 0 : g_taskExpDevMemMap.insert({commId, kernelParam->taskexceptionVa});
130 : }
131 0 : } // 只在通信域创建时保存一次,通信域销毁时该处会同步销毁,不存在需要更新的场景
132 0 : HCCL_INFO(
133 : "HcclDpuTaskexpShmemRestore success. commId[%s], deviceId[%u], taskexceptionVa[%p], memorySize[%llu]",
134 : commId.c_str(), kernelParam->deviceId, kernelParam->taskexceptionVa, kernelParam->memorySize);
135 0 : return 0;
136 0 : }
137 : }
|