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 "aicpu_kernel_launcher.h"
12 : #include <memory>
13 : #include "internal_exception.h"
14 : #include "coll_service_device_mode.h"
15 : #include "aicpu_ins_preprocessor.h"
16 : #include "coll_service_ai_cpu_impl.h"
17 : #include "exception_util.h"
18 :
19 : namespace Hccl {
20 :
21 : template <class T, class U>
22 : u16 CalcFieldOffset(T* target, U* base)
23 : {
24 : return static_cast<u16>(reinterpret_cast<const char*>(target) - reinterpret_cast<const char*>(base));
25 : }
26 :
27 0 : void AicpuKernelLauncher::AicpuKernelLaunch(const Stream& stream, const string& algName) const
28 : {
29 0 : HCCL_INFO("[AicpuKernelLauncher::%s] start.", __func__);
30 :
31 0 : auto op = comm->GetCurrentCollOperator();
32 0 : HcclKernelLaunchParam param;
33 :
34 0 : s32 ret = strcpy_s(param.kernel.algName, sizeof(param.kernel.algName), algName.data());
35 0 : if (ret != EOK) {
36 0 : THROW<InternalException>(StringFormat("AicpuKernelLaunch, strcpy_s algName failed!"));
37 : }
38 :
39 0 : ret = strcpy_s(param.kernel.opTag, sizeof(param.kernel.opTag), op->opTag.data());
40 0 : if (ret != EOK) {
41 0 : THROW<InternalException>(StringFormat("AicpuKernelLaunch, strcpy_s opTag failed!"));
42 : }
43 :
44 0 : HCCL_INFO(
45 : "AicpuKernelLauncher::AicpuKernelLaunch param.kernel.algName: %s, opTag %s", param.kernel.algName,
46 : op->opTag.c_str());
47 :
48 0 : auto aicpuInsPreprocessor = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService())->GetAicpuInsPreprocessor();
49 0 : DevBuffer* mem = aicpuInsPreprocessor->GetAicpuResBuffer(algName);
50 0 : param.kernel.binaryResAddr = mem->GetAddr();
51 0 : param.kernel.binaryResSize = mem->GetSize();
52 0 : aicpuInsPreprocessor->SetAicpuResExisted(algName);
53 :
54 0 : SetHcclKernelLaunchParam(param);
55 :
56 0 : AddPostToUserStream(stream);
57 0 : const auto funcHandle = comm->GetAicpuKernelFuncHandle(param.kernelName);
58 :
59 : aclrtLaunchKernelAttr attr;
60 0 : attr.id = ACL_RT_LAUNCH_KERNEL_ATTR_TIMEOUT;
61 0 : attr.value.timeout = comm->GetNotifyTimeoutCfg().GetNotifyTimeout();
62 : aclrtLaunchKernelCfg cfg;
63 0 : cfg.attrs = &attr;
64 0 : cfg.numAttrs = 1;
65 0 : constexpr u32 numBlocks = 1;
66 0 : if (op->opMode == OpMode::OPBASE) {
67 0 : HrtAicpuLaunchKernelWithHostArgs(
68 0 : funcHandle, numBlocks, comm->GetAicpuStreamManager().GetFreeStream()->GetPtr(), &cfg, ¶m.kernel,
69 : sizeof(HcclKernelParamLite));
70 0 : HCCL_INFO(
71 : "[AicpuKernelLauncher][AicpuKernelLaunch] param.kernel.algName: %s OPBASE mode "
72 : "HrtAicpuLaunchKernelWithHostArgs end!",
73 : param.kernel.algName);
74 0 : } else if (op->opMode == OpMode::OFFLOAD) {
75 0 : HrtAicpuLaunchKernelWithHostArgs(
76 : funcHandle, numBlocks, stream.GetPtr(), &cfg, ¶m.kernel, sizeof(HcclKernelParamLite));
77 0 : HCCL_INFO(
78 : "[AicpuKernelLauncher][AicpuKernelLaunch] param.kernel.algName: %s OFFLOAD mode "
79 : "HrtAicpuLaunchKernelWithHostArgs end!",
80 : param.kernel.algName);
81 : }
82 0 : AddWaitToUserStream(stream);
83 0 : HCCL_INFO("[AicpuKernelLauncher::%s] end.", __func__);
84 0 : }
85 :
86 1 : void AicpuKernelLauncher::SetOpbaseBufferParam(HcclKernelLaunchParam& param, CollOperator& op) const
87 : {
88 3 : HCCL_INFO("[AicpuKernelLauncher::%s] start.", __func__);
89 :
90 1 : auto buffer = comm->GetCclBuffer();
91 1 : param.kernel.comm.opBaseScratch.addr = buffer->GetAddr();
92 1 : param.kernel.comm.opBaseScratch.size = buffer->GetSize();
93 1 : InitAicpuLocBufLite(param.kernel.comm.opBaseScratch, buffer->GetAddr(), buffer->GetSize(), "opBaseScratch");
94 1 : if (op.inputMem != nullptr) {
95 2 : InitAicpuLocBufLite(param.kernel.op.input, op.inputMem->GetAddr(), op.inputMem->GetSize(), "inputMem");
96 : }
97 :
98 1 : if (op.outputMem != nullptr) {
99 2 : InitAicpuLocBufLite(param.kernel.op.output, op.outputMem->GetAddr(), op.outputMem->GetSize(), "outputMem");
100 : }
101 3 : HCCL_INFO(
102 : "[AicpuKernelLauncher::%s] end, SetOpbaseBufferParam param.kernel.comm.opBaseScratch.addr %llu, "
103 : "param.kernel.comm.opBaseScratch.size %llu",
104 : __func__, param.kernel.comm.opBaseScratch.addr, param.kernel.comm.opBaseScratch.size);
105 1 : }
106 :
107 1 : void AicpuKernelLauncher::SetOffloadBufferParam(HcclKernelLaunchParam& param, CollOperator& op) const
108 : {
109 3 : HCCL_INFO("[AicpuKernelLauncher::%s] start.", __func__);
110 :
111 1 : auto offloadInput = comm->GetDataBufferManager().Get(op.opTag, BufferType::INPUT);
112 1 : if (offloadInput != nullptr) {
113 0 : InitAicpuLocBufLite(param.kernel.op.input, op.inputMem->GetAddr(), op.inputMem->GetSize(), "inputMem");
114 : }
115 :
116 1 : auto offloadOuput = comm->GetDataBufferManager().Get(op.opTag, BufferType::OUTPUT);
117 1 : if (offloadOuput != nullptr) {
118 0 : InitAicpuLocBufLite(param.kernel.op.output, op.outputMem->GetAddr(), op.outputMem->GetSize(), "outputMem");
119 : }
120 :
121 1 : auto offloadScartch = comm->GetDataBufferManager().Get(op.opTag, BufferType::SCRATCH);
122 1 : if (offloadScartch != nullptr) {
123 0 : InitAicpuLocBufLite(param.kernel.op.scratch, op.scratchMem->GetAddr(), op.scratchMem->GetSize(), "scratchMem");
124 : }
125 :
126 3 : HCCL_INFO("[AicpuKernelLauncher::%s] end.", __func__);
127 1 : }
128 :
129 2 : void AicpuKernelLauncher::SetHcclKernelLaunchParam(HcclKernelLaunchParam& param) const
130 : {
131 6 : HCCL_INFO("[AicpuKernelLauncher::%s] start.", __func__);
132 :
133 2 : CollOperator op = *comm->GetCurrentCollOperator();
134 :
135 2 : param.kernel.comm.idIndex = comm->GetIdIndex();
136 2 : param.kernel.comm.myRank = comm->GetMyRank();
137 2 : param.kernel.comm.rankSize = comm->GetRankSize();
138 2 : param.kernel.comm.devType = comm->GetDevType();
139 2 : param.kernel.comm.devPhyId = comm->GetDevicePhyId();
140 2 : auto collService = comm->GetCollService();
141 2 : param.kernel.comm.opCounterAddr = static_cast<u64>(collService->GetOpCounterBuf()->GetAddr());
142 :
143 2 : if (op.opMode == OpMode::OPBASE) {
144 1 : SetOpbaseBufferParam(param, op);
145 : } else {
146 1 : SetOffloadBufferParam(param, op);
147 : }
148 :
149 2 : param.kernel.op.algOperator.opMode = op.opMode;
150 2 : param.kernel.op.algOperator.opType = op.opType;
151 2 : param.kernel.op.algOperator.reduceOp = op.reduceOp;
152 2 : param.kernel.op.algOperator.dataType = op.dataType;
153 2 : param.kernel.op.algOperator.dataCount = op.dataCount;
154 2 : param.kernel.op.algOperator.root = op.root;
155 2 : if (op.opType == OpType::ALLTOALL) {
156 0 : param.kernel.op.algOperator.all2AllDataDes = op.all2AllDataDes;
157 2 : } else if (op.opType == OpType::ALLTOALLV) {
158 : auto aicpuInsPreprocessor
159 0 : = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService())->GetAicpuInsPreprocessor();
160 0 : aicpuInsPreprocessor->SetAicpuKernelLaunchParam(param);
161 : }
162 :
163 2 : param.kernel.op.sendRecvRemoteRank = op.sendRecvRemoteRank;
164 :
165 6 : HCCL_INFO("[AicpuKernelLauncher::%s] end.", __func__);
166 2 : }
167 :
168 : // 待解决:当前方案未确定此处的取值
169 : constexpr u32 HOST_DEVICE_SYNC_TIMEOUT = 1000;
170 :
171 0 : void AicpuKernelLauncher::AddPostToUserStream(const Stream& stream) const
172 : {
173 0 : auto postNotify = comm->GetHostDeviceSyncNotifyManager().GetDeviceWaitNotify();
174 :
175 0 : postNotify->Post(stream);
176 0 : }
177 :
178 0 : void AicpuKernelLauncher::AddWaitToUserStream(const Stream& stream) const
179 : {
180 0 : auto waitNotify = comm->GetHostDeviceSyncNotifyManager().GetHostWaitNotify();
181 :
182 0 : waitNotify->Wait(stream, HOST_DEVICE_SYNC_TIMEOUT);
183 0 : }
184 :
185 : } // namespace Hccl
|