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 MEM_DEVICE_PUB_H
12 : #define MEM_DEVICE_PUB_H
13 :
14 : #include "hccl_common.h"
15 : #include "hccl/base.h"
16 :
17 : namespace hccl {
18 : class DeviceMem {
19 : public:
20 : /* * 默认构造函数, 只生成无效的DeviceMem对象 */
21 57467 : explicit DeviceMem() : ptr_(nullptr), size_(0), owner_(false) {}
22 : explicit DeviceMem(void* ptr, u64 size, bool owner = false);
23 : /* * 拷贝构造函数, 用于DeviceMem::create的返回
24 : 新实例对源实例的ptr无所有权, 析构时不释放ptr
25 : 源实例保留原来对ptr的所有权, 析构时释放ptr */
26 : DeviceMem(const DeviceMem& that);
27 :
28 : /* * 移动构造函数, 用于DeviceMem::alloc的返回
29 : 新实例对源实例的ptr有所有权, 析构时释放ptr
30 : 源实例放弃原来对ptr的所有权, 析构时不释放ptr */
31 : DeviceMem(DeviceMem&& that) noexcept;
32 :
33 : ~DeviceMem();
34 : void free();
35 :
36 : /**
37 : 通过静态成员函数来创建DeviceMem对象,目的如下:
38 : 1)
39 : 根据入参实例化,用create
40 : 临时申请用,用alloc
41 : 否则先调用底层函数申请memory,再用create会造成下层实现代码上移(rt_malloc)
42 : 造成代码维护困难
43 : 2)
44 : 语义上类似C语言申请内存(malloc)的方式,好理解
45 : */
46 : static DeviceMem alloc(u64 size, bool level2Address = false);
47 : static HcclResult alloc(DeviceMem& mem, u64 size, bool level2Address = false);
48 : static DeviceMem create(void* ptr, u64 size);
49 :
50 : /* * 部分操作符声明or重载, 期望达到类似memory指针操作那样来操作Mem对象 */
51 : /* * 重载move-assignment运算符, 用于alloc返回
52 : 左值对象对右值对象的ptr有所有权, 析构时释放ptr
53 : 右值对象放弃其原来对ptr的所有权, 析构时不释放ptr */
54 : DeviceMem operator=(DeviceMem&& that);
55 :
56 : /* * 重载copy-assignment运算符, 用于create返回和普通的DeviceMem对象拷贝
57 : 左值对象对右值对象的ptr无所有权, 析构时释放ptr
58 : 右值对象保留其原来对ptr的所有权, 析构时不释放ptr */
59 : DeviceMem& operator=(const DeviceMem& that);
60 :
61 : // "bool"运算符(可执行if(object){...}的操作判断该DeviceMem是否为空)
62 211 : operator bool() const { return ptr_ != nullptr; }
63 :
64 : // "=="运算符
65 13 : bool operator==(const DeviceMem& that) const { return (ptr_ == that.ptr()) && (size_ == that.size()); }
66 :
67 : // "!="运算符
68 3 : bool operator!=(const DeviceMem& that) const { return (ptr_ != that.ptr()) || (size_ != that.size()); }
69 :
70 : // 取地址
71 37605 : void* ptr() const { return ptr_; }
72 :
73 : /* * 内联成员函数 */
74 361798886 : u64 size() const { return size_; }
75 :
76 : /* * 在当前mem实例中截取一段形成新的Mem实例 */
77 : DeviceMem range(u64 offset, u64 size) const;
78 : void* ptr_; /* * memory地址 */
79 : protected:
80 : private:
81 : explicit DeviceMem(u64 size);
82 :
83 : u64 size_; /* * memory的size, 单位 : 字节 */
84 : bool owner_; /* * 类实例资源owner, 类似std::shared_ptr的做法 */
85 : };
86 : } // namespace hccl
87 :
88 : #endif /* MEM_DEVICE_PUB_H */
|