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 : #pragma once
12 :
13 : #include <set>
14 : #include <map>
15 : #include <mutex>
16 : #include <memory>
17 : #include <unordered_set>
18 :
19 : #include <hccl/hccl_comm.h>
20 : #include <hccl/hccl_inner.h>
21 : #include "hccl_mem_defs.h"
22 : #include "global_mem_record.h"
23 : #include "hccl_network_pub.h"
24 : #include "hccl_socket_manager.h"
25 :
26 : namespace hccl {
27 :
28 : // 进程粒度的内存管理单例
29 : class GlobalMemRegMgr {
30 : public:
31 : static GlobalMemRegMgr& GetInstance(); // 获取单例
32 : ~GlobalMemRegMgr();
33 :
34 1 : inline bool CheckHandleIsValid(void* handle) const
35 : {
36 1 : return handle != nullptr && validHandlePtrSet.find(handle) != validHandlePtrSet.cend();
37 : }
38 :
39 : HcclResult Reg(const HcclMem* mem, void** memRecordHandle); // 登记一段内存
40 : HcclResult DeReg(void* memRecordHandle); // 删除一段内存记录
41 : HcclResult GetNetDevCtx(NicType nicType, const HcclIpAddress& ipAddr, u32 port, HcclNetDevCtx& netDevCtx);
42 : HcclResult InitNic();
43 : HcclResult DeInitNic();
44 : HcclResult Destroy();
45 :
46 : private:
47 : HcclResult CheckOverlapAndInsert(GlobalMemRecord& memRecord, void** memRecordHandle);
48 : HcclResult CheckOneSidedBackupAndSetDevId(
49 : const HcclIpAddress& ipAddr, u32& backupDevPhyId, u32& backupDevLogicId,
50 : std::vector<HcclIpAddress>& localIpList, bool& isOneSidedTaskAndBackupInitA3);
51 :
52 : std::set<GlobalMemRecord> memRecordSet_{}; // 内存记录,按照type>addr>size排序
53 : std::unordered_set<void*> validHandlePtrSet{}; // 记录handle地址,用于校验用户传入的是否是handle的地址
54 : std::mutex lock_; // 锁保证多线程访问安全
55 : std::map<PortInfo, std::pair<NicType, HcclNetDevCtx>> netDevCtxMap_{};
56 : u32 devicePhyId_{INVALID_UINT};
57 : s32 deviceLogicId_{INVALID_INT};
58 : bool nicInited_{false};
59 : bool isInited_{false};
60 : std::unique_ptr<HcclSocketManager> socketManager_;
61 : std::mutex netDevCtxMtx_;
62 : };
63 :
64 : } // namespace hccl
|