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