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 : };
52 :
53 : struct StreamRecordInfo {
54 : s32 planeId;
55 : AlgType algType;
56 : std::string tag;
57 27 : StreamRecordInfo() = default;
58 27 : StreamRecordInfo(s32 plane, const AlgType& type, const std::string& strTag)
59 27 : : planeId(plane),
60 27 : algType(type),
61 27 : tag(strTag)
62 27 : {}
63 0 : StreamRecordInfo(const StreamRecordInfo& that) : planeId(that.planeId), algType(that.algType), tag(that.tag) {}
64 27 : StreamRecordInfo& operator=(const StreamRecordInfo& that)
65 : {
66 27 : if (&that != this) {
67 27 : planeId = that.planeId;
68 27 : algType = that.algType;
69 27 : tag = that.tag;
70 : }
71 27 : return *this;
72 : }
73 : };
74 :
75 : class ProfilerBase {
76 : public:
77 : /* * 输出文本时, 获取op, dataType的字符串以及单位数据长度的数组 */
78 : static const std::array<uint32_t, HCCL_REDUCE_RESERVED> opString;
79 : static const std::array<uint32_t, HCCL_DATA_TYPE_RESERVED> dataTypeString;
80 : static const std::array<s32, HCCL_DATA_TYPE_RESERVED> sizeOf;
81 :
82 : explicit ProfilerBase(u32 deviceLogicId);
83 : virtual ~ProfilerBase();
84 :
85 : virtual HcclResult Run(const StepData& stepData) = 0;
86 : virtual HcclResult Flush() = 0;
87 : static HcclResult AddStream(s32 streamID, const std::string& tag, s32 planeID, const AlgType& algType);
88 : static HcclResult DelStream(s32 streamID);
89 : static HcclResult AddTag(
90 : const std::string& tag, const std::string& group, const HcclWorkflowMode& workFlowMode, bool isSendRecv = false,
91 : bool isAiv = false);
92 : static HcclResult DelTag(const std::string& tag);
93 : static HcclResult AddOpData(
94 : const std::string& tag, u64 count, const void* src, const void* dst, HcclDataType dataType, u32 rootId,
95 : const std::string& group, HcclReduceOp reduceType = HCCL_REDUCE_RESERVED);
96 : static HcclResult DelOpData(const std::string& tag);
97 : static HcclResult AddGroupRankInfo(
98 : const std::string& group, u32 rankSize, u32 rankId, bool isSendRecv = false,
99 : u32 remoteRankId = INVALID_VALUE_RANKSIZE);
100 : static HcclResult DelGroupRankInfo(const std::string& tag);
101 : static HcclResult GetTagByStream(u32& streamID, std::string& tag);
102 : static HcclResult GetAlgTypeByStream(u32& streamID, AlgType& algType);
103 : static HcclResult GetGroupNameByTag(const std::string& tag, std::string& group);
104 : static HcclResult GetRankInfoByGroup(const std::string& group, GroupRankInfo& groupRankInfo);
105 : static HcclResult GetOpDataInfoByTag(const std::string& tag, OpDataInfo& opDataInfo);
106 : static HcclResult AddGroupUdi(const std::string& group, const std::string& udi);
107 : static HcclResult DelGroupUdi(const std::string& group);
108 : static HcclResult GetUdiByGroup(const std::string& group, std::string& udi);
109 : static void GetSubmittedOpCnt(u32& index);
110 : virtual HcclResult Save(u32& streamID, u32& taskID, TaskType& taskType, const TaskParaDMA& para) = 0;
111 : virtual HcclResult Save(u32& streamID, u32& taskID, TaskType& taskType, const TaskParaReduce& para) = 0;
112 : virtual HcclResult Save(u32& streamID, u32& taskID, TaskType& taskType, const TaskParaNotify& para) = 0;
113 : virtual HcclResult Save(u32 streamID, u32 taskID, const TaskParaAiv& para) = 0;
114 : virtual HcclResult Save(u32& streamID, u32& taskID, const void* descBuf = nullptr, size_t descBufLen = 0) = 0;
115 : virtual HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType& taskType, const TaskParaDMA& para)
116 : = 0;
117 : virtual HcclResult
118 : Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType& taskType, const TaskParaReduce& para)
119 : = 0;
120 : virtual HcclResult
121 : Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType& taskType, const TaskParaNotify& para)
122 : = 0;
123 : virtual HcclResult
124 : Save(u32 captureStreamID, u32 streamID, u32 taskID, const void* descBuf = nullptr, size_t descBufLen = 0)
125 : = 0;
126 : virtual HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, const TaskParaAiv& paraAiv) = 0;
127 : virtual HcclResult SaveToLog(const TaskParaHost& paraHost) = 0;
128 :
129 : protected:
130 : static std::array<std::map<s32, StreamRecordInfo>, MAX_MODULE_DEVICE_NUM> streamRecordInfoMap_;
131 : static std::array<std::map<const std::string, const std::string>, MAX_MODULE_DEVICE_NUM> tagGroupMap_;
132 : static std::array<std::map<const std::string, const HcclWorkflowMode>, MAX_MODULE_DEVICE_NUM> tagModeMap_;
133 : static std::array<std::mutex, MAX_MODULE_DEVICE_NUM> streamMutex_;
134 : static std::array<std::map<const std::string, GroupRankInfo>, MAX_MODULE_DEVICE_NUM> groupRankMap_;
135 : static std::array<std::map<const std::string, OpDataInfo>, MAX_MODULE_DEVICE_NUM> tagOpDataMap_;
136 : static std::array<std::map<const std::string, u32>, MAX_MODULE_DEVICE_NUM> groupIndexMap_;
137 : static std::array<std::map<const std::string, u32>, MAX_MODULE_DEVICE_NUM> aivGroupIndexMap_;
138 : static std::array<std::map<const std::string, u32>, MAX_MODULE_DEVICE_NUM> sendRecvGroupIndexMap_;
139 : static std::array<std::map<const std::string, std::string>, MAX_MODULE_DEVICE_NUM> groupUdiMap_;
140 : const u32 deviceLogicId_;
141 : static bool isSendRecv_[MAX_MODULE_DEVICE_NUM];
142 : static u32 index_[MAX_MODULE_DEVICE_NUM];
143 :
144 : private:
145 : };
146 : } // namespace hccl
147 :
148 : #endif /* PROFILER_BASE_PUB_H */
|