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 PROFILER_BASE_PUB_H
12 : #define PROFILER_BASE_PUB_H
13 :
14 : #include <memory>
15 : #include <mutex>
16 : #include <string>
17 : #include <map>
18 : #include <thread>
19 :
20 : #include <hccl/hccl_types.h>
21 : #include "hccl_common.h"
22 : #include "common.h"
23 : #include "workflow_pub.h"
24 : #include "dispatcher_task_types.h"
25 : #include "alg_profiling.h"
26 : #include "profiler_base_pub_extend.h"
27 :
28 : namespace hccl {
29 : enum class StepType { STEP_STAGE = 0, STEP_STEP, STEP_MAX };
30 :
31 : enum class OpDict { SUM = 0, PROD, MAX, MIN };
32 :
33 : enum class DataType { DINT8 = 0, DINT16, DINT32, DFP16, DFP32, DINT64, DUINT64 };
34 :
35 : struct GroupRankInfo {
36 : u32 rankSize{0};
37 : u32 rankId{0};
38 : u32 remoteRankId{INVALID_VALUE_RANKSIZE};
39 : };
40 :
41 : struct OpDataInfo {
42 : u64 count{0};
43 : const void* src{nullptr};
44 : const void* dst{nullptr};
45 : u32 index{0};
46 : u32 rootId{0};
47 : u32 deviceId{0};
48 : HcclDataType dataType{HcclDataType::HCCL_DATA_TYPE_RESERVED};
49 : HcclReduceOp reduceType{HcclReduceOp::HCCL_REDUCE_RESERVED};
50 : struct timeval tv {
51 : 0
52 : };
53 : };
54 :
55 : struct StreamRecordInfo {
56 : s32 planeId;
57 : AlgType algType;
58 : std::string tag;
59 27 : StreamRecordInfo() = default;
60 27 : StreamRecordInfo(s32 plane, const AlgType& type, const std::string& strTag)
61 27 : : planeId(plane),
62 27 : algType(type),
63 27 : tag(strTag)
64 27 : {}
65 0 : StreamRecordInfo(const StreamRecordInfo& that) : planeId(that.planeId), algType(that.algType), tag(that.tag) {}
66 27 : StreamRecordInfo& operator=(const StreamRecordInfo& that)
67 : {
68 27 : if (&that != this) {
69 27 : planeId = that.planeId;
70 27 : algType = that.algType;
71 27 : tag = that.tag;
72 : }
73 27 : return *this;
74 : }
75 : };
76 :
77 : class ProfilerBase {
78 : public:
79 : /* * 输出文本时, 获取op, dataType的字符串以及单位数据长度的数组 */
80 : static const std::array<uint32_t, HCCL_REDUCE_RESERVED> opString;
81 : static const std::array<uint32_t, HCCL_DATA_TYPE_RESERVED> dataTypeString;
82 : static const std::array<s32, HCCL_DATA_TYPE_RESERVED> sizeOf;
83 :
84 : explicit ProfilerBase(u32 deviceLogicId);
85 : virtual ~ProfilerBase();
86 :
87 : virtual HcclResult Run(const StepData& stepData) = 0;
88 : virtual HcclResult Flush() = 0;
89 : static HcclResult AddStream(s32 streamID, const std::string& tag, s32 planeID, const AlgType& algType);
90 : static HcclResult DelStream(s32 streamID);
91 : static HcclResult AddTag(
92 : const std::string& tag, const std::string& group, const HcclWorkflowMode& workFlowMode, bool isSendRecv = false,
93 : bool isAiv = false);
94 : static HcclResult DelTag(const std::string& tag);
95 : static HcclResult AddOpData(
96 : const std::string& tag, u64 count, const void* src, const void* dst, HcclDataType dataType, u32 rootId,
97 : const std::string& group, HcclReduceOp reduceType = HCCL_REDUCE_RESERVED);
98 : static HcclResult DelOpData(const std::string& tag);
99 : static HcclResult AddGroupRankInfo(
100 : const std::string& group, u32 rankSize, u32 rankId, bool isSendRecv = false,
101 : u32 remoteRankId = INVALID_VALUE_RANKSIZE);
102 : static HcclResult DelGroupRankInfo(const std::string& tag);
103 : static HcclResult GetTagByStream(u32& streamID, std::string& tag);
104 : static HcclResult GetAlgTypeByStream(u32& streamID, AlgType& algType);
105 : static HcclResult GetGroupNameByTag(const std::string& tag, std::string& group);
106 : static HcclResult GetRankInfoByGroup(const std::string& group, GroupRankInfo& groupRankInfo);
107 : static HcclResult GetOpDataInfoByTag(const std::string& tag, OpDataInfo& opDataInfo);
108 : static HcclResult AddGroupUdi(const std::string& group, const std::string& udi);
109 : static HcclResult DelGroupUdi(const std::string& group);
110 : static HcclResult GetUdiByGroup(const std::string& group, std::string& udi);
111 : static void GetSubmittedOpCnt(u32& index);
112 : virtual HcclResult Save(u32& streamID, u32& taskID, TaskType& taskType, const TaskParaDMA& para) = 0;
113 : virtual HcclResult Save(u32& streamID, u32& taskID, TaskType& taskType, const TaskParaReduce& para) = 0;
114 : virtual HcclResult Save(u32& streamID, u32& taskID, TaskType& taskType, const TaskParaNotify& para) = 0;
115 : virtual HcclResult Save(u32 streamID, u32 taskID, const TaskParaAiv& para) = 0;
116 : virtual HcclResult Save(u32& streamID, u32& taskID, const void* descBuf = nullptr, size_t descBufLen = 0) = 0;
117 : virtual HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType& taskType, const TaskParaDMA& para)
118 : = 0;
119 : virtual HcclResult
120 : Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType& taskType, const TaskParaReduce& para)
121 : = 0;
122 : virtual HcclResult
123 : Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType& taskType, const TaskParaNotify& para)
124 : = 0;
125 : virtual HcclResult
126 : Save(u32 captureStreamID, u32 streamID, u32 taskID, const void* descBuf = nullptr, size_t descBufLen = 0)
127 : = 0;
128 : virtual HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, const TaskParaAiv& paraAiv) = 0;
129 : virtual HcclResult SaveToLog(const TaskParaHost& paraHost) = 0;
130 :
131 : protected:
132 : static std::array<std::map<s32, StreamRecordInfo>, MAX_MODULE_DEVICE_NUM> streamRecordInfoMap_;
133 : static std::array<std::map<const std::string, const std::string>, MAX_MODULE_DEVICE_NUM> tagGroupMap_;
134 : static std::array<std::map<const std::string, const HcclWorkflowMode>, MAX_MODULE_DEVICE_NUM> tagModeMap_;
135 : static std::array<std::mutex, MAX_MODULE_DEVICE_NUM> streamMutex_;
136 : static std::array<std::map<const std::string, GroupRankInfo>, MAX_MODULE_DEVICE_NUM> groupRankMap_;
137 : static std::array<std::map<const std::string, OpDataInfo>, MAX_MODULE_DEVICE_NUM> tagOpDataMap_;
138 : static std::array<std::map<const std::string, u32>, MAX_MODULE_DEVICE_NUM> groupIndexMap_;
139 : static std::array<std::map<const std::string, u32>, MAX_MODULE_DEVICE_NUM> aivGroupIndexMap_;
140 : static std::array<std::map<const std::string, u32>, MAX_MODULE_DEVICE_NUM> sendRecvGroupIndexMap_;
141 : static std::array<std::map<const std::string, std::string>, MAX_MODULE_DEVICE_NUM> groupUdiMap_;
142 : const u32 deviceLogicId_;
143 : static bool isSendRecv_[MAX_MODULE_DEVICE_NUM];
144 : static u32 index_[MAX_MODULE_DEVICE_NUM];
145 :
146 : private:
147 : };
148 : } // namespace hccl
149 :
150 : #endif /* PROFILER_BASE_PUB_H */
|