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 HCOM_COMMOM_V2_H
12 : #define HCOM_COMMOM_V2_H
13 : #include <map>
14 : #include <memory>
15 : #include <string>
16 : #include <vector>
17 : #include <algorithm>
18 :
19 : #include "hccl/base.h"
20 : #include "hccl_common_v2.h"
21 : #include "hccl/hccl_types.h"
22 : #include "hccl_communicator.h"
23 : #include "log.h"
24 : #include <hccl/hccl_types.h>
25 : #include "ccu_driver_handle.h"
26 :
27 : const u64 RANKTABLE_FILE_MAX_SIZE = 1024ULL * 1024 * 1024;
28 : constexpr s32 HOST_DEVICE_ID = -1; // device id 无效值
29 : u64 GetFileSize(const std::string& path);
30 :
31 : using HcclGroupParamsV2 = struct TagHcclGroupParamsInfoV2 {
32 : /* * group的基本构建信息,节点数及本节点在group中的编号、
33 : 本节点在worldgroup中的编号、group的所有ranks */
34 : u32 worldRank; /* * 用于标识world内不同节点 */
35 : u32 groupRank; /* * 用于标识group内不同节点 */
36 : u32 serverNum; /* * 用于标识group内服务器总数 */
37 : u32 totalRanks; /* * 用于指示group内的节点总数, rank范围[0, totalRanks-1] */
38 : std::vector<u32> groupRanks; // 内部存储wordrankid,其下标表示groupid
39 : std::shared_ptr<Hccl::HcclCommunicator> pComm;
40 : u32 refCounter = 0;
41 : bool destroyFlag = false;
42 : };
43 :
44 79 : MAKE_ENUM(DeviceStatus, DEVICE_IDLE = 0, DEVICE_RECOVERED, DEVICE_READY);
45 :
46 : constexpr u32 MAX_NUM_COMM_USING_MS = 1;
47 : struct CcuStatus {
48 : std::vector<std::string> useMsCommIds{};
49 : std::vector<std::string> useSchedCommIds{};
50 :
51 : void RemoveCommId(const std::string& commId);
52 : bool IsMsAvailable(const std::string& commId) const;
53 : HcclResult InsertCommId(const std::string& commId, bool isUsingCcuMs, bool isUsingCcuSched);
54 : HcclResult InsertMsCommId(const std::string& commId);
55 : void InsertSchedCommId(const std::string& commId);
56 : };
57 :
58 : using HcclCommInfoV2 = struct HcclCommInfoCtxV2 {
59 : s32 devId{HOST_DEVICE_ID};
60 : std::shared_ptr<Hccl::HcclCommunicator> pComm{nullptr};
61 : Hccl::CommParams commParams;
62 : std::map<std::string, HcclGroupParamsV2> hcclGroupMap;
63 : std::mutex groupParamsLock; // 操作hcclGroupMap前加锁
64 : bool isUsed{false};
65 : DeviceStatus status{DeviceStatus::DEVICE_IDLE}; // Deivce状态
66 : u64 step{0}; // 新增
67 : CcuStatus ccuStatus; // 管理ccu资源使用情况
68 :
69 66 : ~HcclCommInfoCtxV2() {
70 66 : hcclGroupMap.clear();
71 66 : pComm = nullptr;
72 66 : }
73 : };
74 :
75 : extern std::mutex g_commInfoV2CtxMutex;
76 :
77 : class CommManager {
78 : public:
79 : CommManager(const CommManager &that) = delete;
80 : CommManager &operator=(const CommManager &that) = delete;
81 :
82 : static CommManager &GetInstance(s32 deviceLogicId);
83 : HcclCommInfoV2 &GetCommInfoV2();
84 : void PrintChannelInfo();
85 : std::function<void()> GetPrintChannelInfoCallback();
86 : std::shared_ptr<Hccl::CcuDriverHandle> GetCcuDriver();
87 : void DeinitCcuDriver();
88 : s32 deviceLogicId{0};
89 : HcclResult SetCommAcceleratorV2(Hccl::HcclCommunicator *communicator, int32_t accelerator);
90 :
91 : private:
92 66 : CommManager() = default;
93 : bool isCcuAvailable{true};
94 :
95 : std::shared_ptr<Hccl::CcuDriverHandle> ccuDriverHandle{nullptr};
96 : HcclCommInfoV2 commInfoV2{};
97 : };
98 :
99 : #ifdef __cplusplus
100 : extern "C" {
101 : #endif // __cplusplus
102 : HcclCommInfoV2 &GetCommInfoV2(void);
103 : HcclResult HcomDestroyV2(void);
104 : HcclResult GetHcomRankListV2(u32 rankNum, const u32 *rankIds, HcclGroupParamsV2 ¶ms);
105 : HcclResult HcomCreateGroupImplV2(const std::string &group, u32 rankNum, const std::vector<u32> &rankIds);
106 : HcclResult HcomDestroyGroupImplV2(const std::string &group);
107 : HcclResult HcomGetWorldRankFromGroupRankV2(const char *group, u32 groupRank, u32 *worldRank);
108 : HcclResult HcomGetGroupRankFromWorldRankV2(u32 worldRank, const char *group, u32 *groupRank);
109 : HcclResult HcomGetRankSizeV2(const char *group, u32 *rankSize);
110 : HcclResult HcomGetCommV2(void **commV2);
111 : HcclResult HcomGetGroupParamsV2(const char *group, void* groupParams, void **commV2);
112 : HcclResult HcomInitByFileV2(const char *rankTablePath, const char *identify);
113 : HcclResult HcomInitByStringV2(const char *rankTableM, const char *identify);
114 : HcclResult CallSingletons();
115 : HcclResult CcuResAllocAndCtxMgrInit(s32 deviceLogicId);
116 : HcclResult HcomGetCcuTaskInfo(const std::string &group, void *tilingData, void *ccuTaskGroup);
117 : #ifdef __cplusplus
118 : }
119 : #endif // __cplusplus
120 : #endif /* HCCL_COMM_PUB_H */
|