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 NOTIFY_BASE_H
12 : #define NOTIFY_BASE_H
13 :
14 : #include "adapter_rts.h"
15 : #include "adapter_hal.h"
16 : #include "hccl/base.h"
17 :
18 : #include "mem_name_repository_pub.h"
19 : #include "dispatcher_pub.h"
20 :
21 : namespace hccl {
22 :
23 : enum class NotifyType {
24 : RUNTIME_NOTIFY = 0,
25 : RUNTIME_NOTIFY_MC2,
26 : BARE_NOTIFY,
27 : ESCHED_EVENT,
28 : NOTIFY_TYPE_RESERVED
29 : };
30 :
31 : constexpr s32 IPC_NOTIFY_PID_ARRAY_SIZE = 1;
32 :
33 : using HcclIpcRtsNotify = struct TagHcclIpcRtsNotify {
34 : u8 ipcName[HCCL_IPC_MEM_NAME_LEN] = {0};
35 : bool withIpc;
36 : HcclRtNotify ptr;
37 : u32 id;
38 : u64 offset;
39 :
40 5364 : TagHcclIpcRtsNotify() : withIpc(false), ptr(nullptr), id(INVALID_UINT), offset(INVALID_U64)
41 : {
42 5364 : }
43 : };
44 :
45 : using HcclNotifyInfo = struct TagHcclNotifyInfo {
46 : u32 type;
47 : HcclIpcRtsNotify ipcNotify;
48 :
49 5364 : TagHcclNotifyInfo() : type(INVALID_UINT)
50 : {
51 5364 : }
52 :
53 3 : TagHcclNotifyInfo(const TagHcclNotifyInfo& that) : type(that.type), ipcNotify(that.ipcNotify)
54 : {
55 3 : }
56 :
57 : TagHcclNotifyInfo(const TagHcclNotifyInfo&& that) : type(that.type), ipcNotify(that.ipcNotify)
58 : {
59 : }
60 :
61 : TagHcclNotifyInfo &operator=(const TagHcclNotifyInfo &that)
62 : {
63 : if (&that != this) {
64 : type = that.type;
65 : ipcNotify = that.ipcNotify;
66 : }
67 : return *this;
68 : }
69 : };
70 :
71 : class NotifyBase {
72 : public:
73 5363 : explicit NotifyBase(NotifyType notifyType) : notifyType(notifyType)
74 : {
75 5363 : notifyInfo_.type = static_cast<u32>(notifyType);
76 5363 : }
77 :
78 1 : NotifyBase(NotifyType notifyType, HcclNotifyInfo notifyInfo) : notifyType(notifyType), notifyInfo_(notifyInfo)
79 : {
80 1 : }
81 :
82 : NotifyBase(NotifyType notifyType, HcclSignalInfo notifyInfo) : notifyType(notifyType)
83 : {
84 : HCCL_ERROR("[NotifyConstructor]Does not support this interface.");
85 : }
86 :
87 5367 : virtual ~NotifyBase()
88 5367 : {
89 5367 : }
90 :
91 1 : HcclResult Serialize(std::vector<u8> &byteVector)
92 : {
93 1 : std::vector<u8> data = CustomTypeToVectorByte<HcclNotifyInfo>(notifyInfo_);
94 1 : if (data.empty() || data.size() > NOTIFY_INFO_LENGTH) {
95 0 : HCCL_ERROR("serialize msgSize[%u] > NOTIFY_INFO_LENGTH[%u]", data.size(), NOTIFY_INFO_LENGTH);
96 0 : return HCCL_E_INTERNAL;
97 : }
98 :
99 1 : std::vector<u8> paddingData(NOTIFY_INFO_LENGTH - data.size(), 0);
100 1 : HCCL_DEBUG("[Serialize]data size[%u], paddingData[%u].", data.size(), paddingData.size());
101 1 : data.insert(data.end(), paddingData.begin(), paddingData.end());
102 :
103 1 : byteVector = data;
104 :
105 1 : return HCCL_SUCCESS;
106 1 : }
107 :
108 1 : static HcclResult Deserialize(const std::vector<u8>& byteVector, HcclNotifyInfo ¬ifyInfo)
109 : {
110 1 : CHK_PRT_RET(byteVector.size() < sizeof(HcclNotifyInfo),
111 : HCCL_ERROR("[Deserialize]expected byteVector size[%zu] < sizeof(HcclNotifyInfo)[%zu]",
112 : byteVector.size(), sizeof(HcclNotifyInfo)), HCCL_E_PARA);
113 1 : CHK_SAFETY_FUNC_RET(memcpy_s((u8 *)¬ifyInfo, sizeof(HcclNotifyInfo),
114 : &byteVector[0], sizeof(HcclNotifyInfo)));
115 1 : return HCCL_SUCCESS;
116 : }
117 :
118 : virtual HcclResult Alloc() = 0;
119 : virtual HcclResult Destroy() = 0;
120 :
121 : virtual HcclResult Open() = 0;
122 : virtual HcclResult Close() = 0;
123 :
124 : virtual HcclResult Wait(Stream& stream, HcclDispatcher dispatcher, s32 stage, u32 timeOut) = 0;
125 : virtual HcclResult Post(Stream& stream, HcclDispatcher dispatcher, s32 stage) = 0;
126 :
127 : virtual HcclResult Wait(Stream& stream, HcclDispatcher dispatcher, s32 stage, u32 timeOut,
128 : u32 userRank, u32 remoteUserRank) = 0;
129 : virtual HcclResult Wait(Stream& stream, u32 timeOut) = 0;
130 : virtual HcclResult Post(Stream& stream, HcclDispatcher dispatcher, s32 stage,
131 : u32 remoteUserRank) = 0;
132 : virtual HcclResult Post(Stream& stream) = 0;
133 : virtual HcclResult SetIpc() = 0;
134 : virtual HcclResult Grant(s64 recvId) = 0;
135 0 : virtual void Break()
136 : {
137 0 : HCCL_ERROR("[Break]Does not support this interface.");
138 0 : return;
139 : }
140 :
141 0 : virtual HcclResult GetNotifyData(HcclSignalInfo ¬ifyInfo)
142 : {
143 0 : HCCL_ERROR("[GetNotifyData]Does not support this interface.");
144 0 : return HCCL_E_NOT_SUPPORT;
145 : }
146 :
147 0 : virtual HcclResult SetNotifyData(const HcclSignalInfo ¬ifyInfo)
148 : {
149 0 : HCCL_ERROR("[SetNotifyData]Does not support this interface.");
150 0 : return HCCL_E_NOT_SUPPORT;
151 : }
152 :
153 0 : virtual HcclResult GetNotifyOffset(u64 &offset)
154 : {
155 0 : HCCL_ERROR("[GetNotifyOffset]Does not support this interface.");
156 0 : return HCCL_E_NOT_SUPPORT;
157 : }
158 :
159 5362 : inline HcclRtNotify ptr()
160 : {
161 5362 : return notifyPtr;
162 : }
163 :
164 : protected:
165 : NotifyType notifyType;
166 : HcclNotifyInfo notifyInfo_;
167 : HcclRtNotify notifyPtr{nullptr};
168 : };
169 : }
170 :
171 : #endif // NOTIFY_BASE_H
|