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 : #include "mem_host.h"
12 : #include "adapter_rts.h"
13 :
14 : namespace hccl {
15 2912 : HostMem::HostMem(void *ptr, u64 size, bool owner, bool isRtsMem) : ptr_(ptr), size_(size), owner_(owner),
16 2912 : isRtsMem_(isRtsMem)
17 : {
18 2912 : }
19 :
20 1305 : HostMem::HostMem(const HostMem &that) : ptr_(that.ptr()), size_(that.size_), owner_(false), isRtsMem_(that.isRtsMem_)
21 : {
22 1305 : }
23 :
24 1600 : HostMem::HostMem(HostMem &&that) : ptr_(that.ptr_), size_(that.size_), owner_(that.owner_), isRtsMem_(that.isRtsMem_)
25 : {
26 1600 : that.ptr_ = nullptr;
27 1600 : that.size_ = 0;
28 1600 : that.owner_ = false;
29 1600 : }
30 :
31 11379 : HostMem::~HostMem()
32 : {
33 11379 : if (owner_ && ptr_) {
34 2042 : HCCL_DEBUG("size_[%llu Byte]", size_);
35 :
36 2043 : if (isRtsMem_) {
37 2041 : HcclResult ret = hrtFreeHost(ptr_);
38 2040 : if (ret != HCCL_SUCCESS) {
39 0 : HCCL_WARNING("rt_free error, ret[%d]", ret);
40 : }
41 : } else {
42 2 : delete[] static_cast<u8 *>(ptr_);
43 : }
44 : }
45 11379 : }
46 :
47 2 : void HostMem::free()
48 : {
49 2 : if (ptr_) {
50 0 : HCCL_DEBUG("[HostMem][free] size_[%llu Byte]", size_);
51 :
52 0 : if (isRtsMem_) {
53 0 : HcclResult ret = hrtFreeHost(ptr_);
54 0 : if (ret != HCCL_SUCCESS) {
55 0 : HCCL_WARNING("[HostMem][free] rt_free error, ret[%d]", ret);
56 : }
57 : } else {
58 0 : delete[] static_cast<u8 *>(ptr_);
59 : }
60 0 : ptr_ = nullptr;
61 : }
62 2 : }
63 :
64 2036 : HostMem HostMem::alloc(u64 size, bool isRtsMem)
65 : {
66 2036 : void *ptr = nullptr;
67 2036 : if (isRtsMem) {
68 2036 : HcclResult ret = hrtMallocHost(&ptr, size);
69 2041 : if (ret != HCCL_SUCCESS) {
70 0 : HCCL_ERROR("[HostMem][Alloc]rtMallocHost error, ret[%d], size[%llu Byte]", ret, size);
71 : }
72 : } else {
73 0 : ptr = new (std::nothrow) u8[size];
74 : }
75 2043 : if (ptr == nullptr) {
76 0 : HCCL_WARNING("HostMem alloc ptr null");
77 : }
78 2043 : HostMem mem(ptr, size, true, isRtsMem);
79 2042 : return mem;
80 : }
81 :
82 866 : HostMem HostMem::create(void *ptr, u64 size)
83 : {
84 866 : HostMem mem(ptr, size, false);
85 866 : return mem;
86 : }
87 :
88 1 : HostMem &HostMem::operator=(const HostMem &that)
89 : {
90 1 : if (&that != this) {
91 1 : ptr_ = that.ptr();
92 1 : size_ = that.size_;
93 1 : owner_ = false;
94 1 : isRtsMem_ = that.isRtsMem_;
95 : }
96 :
97 1 : return *this;
98 : }
99 :
100 1303 : HostMem HostMem::operator=(HostMem &&that)
101 : {
102 1303 : if (&that != this) {
103 1303 : ptr_ = that.ptr_;
104 1303 : size_ = that.size_;
105 1303 : owner_ = that.owner_;
106 1303 : isRtsMem_ = that.isRtsMem_;
107 : }
108 :
109 1303 : that.ptr_ = nullptr;
110 1303 : that.size_ = 0;
111 1303 : that.owner_ = false;
112 :
113 1303 : return *this;
114 : }
115 :
116 5 : HostMem HostMem::range(u64 offset, u64 size) const
117 : {
118 5 : HostMem mem;
119 5 : if (ptr_ != nullptr && (offset + size) <= size_) {
120 3 : mem = HostMem(static_cast<void *>(static_cast<s8 *>(ptr_) + offset), size, false);
121 : } else {
122 2 : HCCL_WARNING("HostMem range[%llu] size[%llu Byte] error or ptr null", offset + size, size_);
123 : }
124 5 : return mem;
125 0 : }
126 : } // namespace hccl
|