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 : #ifndef AICPU_LAUNCH_MANAGER_H
12 : #define AICPU_LAUNCH_MANAGER_H
13 :
14 : #include "hccl_common.h"
15 : #include "stream_pub.h"
16 : #include "aicpu_operator_pub.h"
17 : #include "thread.h"
18 : #include "hccl/hccl_res.h"
19 : #include "hccl_independent_common.h"
20 : #include "local_notify.h"
21 : #include "aicpu_init_param.h"
22 :
23 : constexpr uint32_t THREAD_UNIQUE_ID_MAX_SIZE = 6000;
24 : constexpr uint32_t NOTIFY_UNIQUE_ID_MAX_SIZE = THREAD_UNIQUE_ID_MAX_SIZE * hccl::HCCL_THREAD_NOTIFY_MAX_NUM;
25 : constexpr uint32_t NOTIFY_DEVICE_ID_MAX_SIZE = 21 * hccl::HCCL_THREAD_NOTIFY_MAX_NUM;
26 : constexpr uint32_t NAME_SIZE = 64;
27 : constexpr uint32_t SIGNAL_DEV_STREAM_MAX_NUM = 200;
28 : struct DevAicpuThreadConfig {
29 : // 如要新增配置类字段,在此处添加
30 : };
31 :
32 : struct ThreadMgrAicpuParam {
33 : u32 threadNum;
34 : char hcomId[HCOMID_MAX_SIZE];
35 : char threadParam[SIGNAL_DEV_STREAM_MAX_NUM][THREAD_UNIQUE_ID_MAX_SIZE]; // 含序列化后thread信息,约40KB
36 : void* deviceHandle;
37 : s32 deviceLogicId{-1}; // 基础通信使用
38 : u32 deviceType{0}; // 基础通信使用
39 : DevAicpuThreadConfig threadConfig; // 收编thread配置类变量
40 : CommEngine engine{COMM_ENGINE_RESERVED}; // 通信引擎类型,由Host侧Thread对象携带
41 : };
42 :
43 : struct DevAicpuNotifyConfig {
44 : // 如要新增配置类字段,在此处添加
45 : };
46 :
47 : struct NotifyMgrAicpuParam {
48 : u32 notifyNum;
49 : char hcomId[HCOMID_MAX_SIZE];
50 : char notifyParam[NOTIFY_UNIQUE_ID_MAX_SIZE]; // 含序列化后notify信息
51 : void* deviceHandle;
52 : bool freeFlag;
53 : DevAicpuNotifyConfig notifyConfig; // 收编notify配置类变量
54 : };
55 :
56 : namespace hccl {
57 :
58 : struct ApiParamDef {
59 : uint64_t commContext{};
60 : char kernelName[NAME_SIZE] = {};
61 : char soName[NAME_SIZE] = {};
62 : char opName[NAME_SIZE] = {};
63 :
64 : ApiParamDef(const char* kName, const char* sName, const char* oName)
65 : {
66 : auto ret = strncpy_s(kernelName, NAME_SIZE, kName, NAME_SIZE - 1);
67 : if (ret != EOK) {
68 : HCCL_ERROR("[ApiParamDef][Check]strncpy_s kernelName failed, ret[%d]", ret);
69 : kernelName[0] = '\0';
70 : }
71 : ret = strncpy_s(soName, NAME_SIZE, sName, NAME_SIZE - 1);
72 : if (ret != EOK) {
73 : HCCL_ERROR("[ApiParamDef][Check]strncpy_s soName failed, ret[%d]", ret);
74 : soName[0] = '\0';
75 : }
76 : ret = strncpy_s(opName, NAME_SIZE, oName, NAME_SIZE - 1);
77 : if (ret != EOK) {
78 : HCCL_ERROR("[ApiParamDef][Check]strncpy_s opName failed, ret[%d]", ret);
79 : opName[0] = '\0';
80 : }
81 : }
82 : };
83 :
84 : struct ThreadKernelLaunchConfig {
85 : std::string commId; // 通信ID
86 : aclrtBinHandle binHandle; // 自定义二进制句柄
87 : std::string kernelName; // 核函数名称
88 : bool needDeviceInfo; // 是否需要设备信息
89 : uint32_t timeoutSec; // 超时时间(秒)
90 : bool needProfiling; // 是否需要性能分析
91 : bool isSupplementNotify; // 是否是补充notify kernel
92 :
93 1 : ThreadKernelLaunchConfig(
94 : const std::string& cid, aclrtBinHandle binHandle, const std::string& name, bool needDev, uint32_t timeout,
95 : bool profiling, bool isSupplementNotify)
96 1 : : commId(cid),
97 1 : binHandle(binHandle),
98 1 : kernelName(name),
99 1 : needDeviceInfo(needDev),
100 1 : timeoutSec(timeout),
101 1 : needProfiling(profiling),
102 1 : isSupplementNotify(isSupplementNotify)
103 1 : {}
104 : };
105 :
106 : class AicpuLaunchMgr {
107 : public:
108 : AicpuLaunchMgr() = default;
109 : ~AicpuLaunchMgr() = default;
110 : template <typename OpParam, typename ApiParam>
111 : static HcclResult KernelLaunch(OpParam& opParam, ApiParam& apiParam, rtStream_t aicpuInitStream);
112 : static HcclResult ThreadKernelLaunchImpl(
113 : std::vector<std::shared_ptr<Thread>>& newThreads, std::unique_ptr<ThreadHandle[]>& aicpuHandle,
114 : const ThreadKernelLaunchConfig& config);
115 : static HcclResult ThreadKernelLaunchForComm(
116 : std::vector<std::shared_ptr<Thread>>& newThreads, const std::string& commId,
117 : std::unique_ptr<ThreadHandle[]>& aicpuHandle, aclrtBinHandle binHandle);
118 : static HcclResult ThreadKernelLaunchForBase(
119 : std::vector<std::shared_ptr<Thread>>& newThreads, std::unique_ptr<ThreadHandle[]>& aicpuHandle,
120 : aclrtBinHandle binHandle);
121 : static HcclResult SupplementNotifyKernelLaunch(
122 : std::vector<std::shared_ptr<Thread>>& newThreads, const std::string& commId,
123 : std::unique_ptr<ThreadHandle[]>& aicpuHandle, aclrtBinHandle binHandle);
124 : static HcclResult
125 : ThreadKernelLaunchDestroy(ThreadHandle* threadHandles, uint32_t listNum, aclrtBinHandle binHandle);
126 : static HcclResult NotifyKernelLaunchAlloc(
127 : std::vector<std::unique_ptr<LocalNotify>>& newNotifys, const std::string& commId,
128 : std::unique_ptr<NotifyHandle[]>& hostHandle, aclrtBinHandle binCustomHandle);
129 : static HcclResult NotifyKernelLaunchFree(
130 : std::vector<NotifyHandle>& aicpuNotifys, uint32_t notifyNum, const std::string& commId,
131 : aclrtBinHandle binCustomHandle);
132 : static HcclResult KernelLaunchAicpuCustom(
133 : uint64_t context, std::string kernelName, rtStream_t aicpuInitStream, aclrtBinHandle binCustomHandle);
134 :
135 : private:
136 : HcclResult AiCpuStreamAllocAndGet(rtStream_t& aiCpuStream);
137 : static HcclResult PrepareAicpuNotifyParam(
138 : NotifyMgrAicpuParam& opParam, const std::string& commId, size_t notifyNum, bool freeFlag, void* deviceHandle);
139 : static HcclResult LaunchNotifyKernel(NotifyMgrAicpuParam& opParam, aclrtBinHandle binCustomHandle);
140 : Stream opStream_;
141 : };
142 : } // namespace hccl
143 : #endif
|