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