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 <mutex>
14 : #include <string>
15 : #include <memory>
16 : #include <unordered_map>
17 : #include "hccl_common.h"
18 : #include "mem_device_pub.h"
19 :
20 : namespace hccl {
21 : struct ShareCCLMem {
22 : DeviceMem cclBuffer = DeviceMem();
23 : uint64_t refCount{0}; // 引用计数
24 : };
25 :
26 : // 进程粒度的内存管理单例
27 : class ShareCCLbufferMgr {
28 : public:
29 : static ShareCCLbufferMgr& GetInstance(); // 获取单例
30 390 : ~ShareCCLbufferMgr() = default;
31 :
32 : HcclResult RecordShareCCLbuffer(const std::string& bufferName);
33 : HcclResult CreateShareCCLbuffer(const std::string& bufferName, u64 bufferSize, DeviceMem& cclBuffer);
34 : HcclResult FreeShareCCLbuffer(const std::string& bufferName);
35 : HcclResult CheckCCLbuffConflict(const std::string& bufferName, s32 streamId);
36 :
37 : private:
38 : HcclResult CreateDevMem(u64 size, DeviceMem& buffer);
39 :
40 : std::mutex lock_; // 锁保证多线程访问安全
41 : u64 shareBufferSize_ = 0; // 共享buffer大小
42 : std::unordered_map<std::string, s32> streamIdMap_; // bufferName与streamID映射关系
43 : std::unordered_map<std::string, ShareCCLMem> memRecord_; // 记录内存的次数
44 : };
45 : } // namespace hccl
|