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