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 HCCL_ORDER_LAUNCH_H
12 : #define HCCL_ORDER_LAUNCH_H
13 :
14 : #include <unordered_set>
15 : #include <map>
16 : #include <memory>
17 : #include "hccl/hccl_types.h"
18 : #include "stream_pub.h"
19 : #include "hccl_common.h"
20 : #include "local_notify.h"
21 : #include "adapter_rts.h"
22 : #include "log.h"
23 :
24 : namespace hccl {
25 : struct OrderLaunchResMgr {
26 : u64 context;
27 : Stream opbaseStream; // 单算子模式使用
28 : Stream aclgraphStream; // aclgraph模式使用
29 : bool resValid;
30 : bool contextInitialized; // 标记context是否已初始化,一旦初始化就不会变回INVALID_U64)
31 :
32 0 : OrderLaunchResMgr()
33 0 : : context(INVALID_U64),
34 0 : opbaseStream(),
35 0 : aclgraphStream(),
36 0 : resValid(false),
37 0 : contextInitialized(false)
38 0 : {}
39 :
40 : // 检查context是否匹配,不匹配时需要更新context但保留stream
41 : bool IsCtxMatch(u64 currentContext) const { return context == currentContext && resValid; }
42 :
43 : // 更新context
44 0 : void UpdateContext(u64 newContext)
45 : {
46 0 : if (context != newContext) {
47 0 : HCCL_INFO("[OrderLaunchResMgr] context updated: [0x%llx] -> [0x%llx]", context, newContext);
48 0 : context = newContext;
49 0 : resValid = false; // context变化认为资源暂时无效
50 : }
51 0 : }
52 :
53 : // 设置context为已初始化状态(一旦调用,context就不会再变回INVALID_U64)
54 0 : void MarkContextInitialized(u64 newContext)
55 : {
56 0 : if (context == INVALID_U64 || context != newContext) {
57 0 : HCCL_INFO("[OrderLaunchResMgr] Mark context initialized: [0x%llx] -> [0x%llx]", context, newContext);
58 0 : context = newContext;
59 0 : contextInitialized = true; // 标记为已初始化
60 0 : resValid = false;
61 : }
62 0 : }
63 :
64 : // 析构资源
65 0 : void DestroyResources()
66 : {
67 : // 析构资源位置预留,后续根据需要实现
68 0 : }
69 : };
70 :
71 : class OrderLaunch {
72 : public:
73 : static OrderLaunch& GetInstance(s32 deviceLogicID);
74 : HcclResult RegisterOrderLaunch(const std::string& group);
75 : HcclResult UnRegisterOrderLaunch(const std::string& group);
76 :
77 : HcclResult AclgraphLaunchInOrderToOrderStream(
78 : std::string& group, const Stream& kernelStream, std::shared_ptr<LocalNotify> notify0,
79 : std::shared_ptr<LocalNotify> notify1, u32 timeOut, HcclRtEvent event);
80 : HcclResult AclgraphLaunchInOrderToKernelStream(std::string& group, const Stream& kernelStream, HcclRtEvent event);
81 :
82 : HcclResult OpbaseLaunchInOrder(
83 : std::string& group, const Stream& kernelStream, std::shared_ptr<LocalNotify> notify0,
84 : std::shared_ptr<LocalNotify> notify1, u32 timeOut);
85 :
86 : HcclResult HcomLaunchInOrder(
87 : std::string& group, const Stream& kernelStream, u32 graphId, std::shared_ptr<LocalNotify> notify0,
88 : std::shared_ptr<LocalNotify> notify1, u32 timeOut);
89 :
90 : HcclResult SetHcomStream(u32 graphId, const Stream& hcomAttachedStream);
91 :
92 : private:
93 : OrderLaunch();
94 : ~OrderLaunch();
95 :
96 : void DestroyRes();
97 : HcclResult LaunchInOrder(
98 : std::string& group, const Stream& kernelStream, const Stream& hostOrderStream,
99 : std::shared_ptr<LocalNotify> notify0, std::shared_ptr<LocalNotify> notify1, u32 timeOut);
100 : HcclResult InitGroupCtx(const std::string& group);
101 : HcclResult EnsureOrderStreamForGroup(std::string& group, u64 context, Stream& orderStream);
102 : HcclResult GetCurrentContext(u64& currentContext);
103 :
104 : std::mutex streamMutex_;
105 : std::unordered_map<std::string, u64> groupCtxMap_; // group -> context 映射
106 : std::unordered_map<u64, std::unordered_set<std::string>> contextGroupsMap_; // context -> groups 映射
107 : std::unordered_map<u64, OrderLaunchResMgr> contextResMgrMap_; // context -> OrderLaunchResMgr (stream由context管理)
108 : std::unordered_map<u32, Stream> hcomStreamMap_; // 图模式使用
109 : bool initialized_ = false;
110 : };
111 : } // namespace hccl
112 : #endif
|