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 3075 : HostMem::HostMem(void* ptr, u64 size, bool owner, bool isRtsMem)
16 3075 : : ptr_(ptr),
17 3075 : size_(size),
18 3075 : owner_(owner),
19 3075 : isRtsMem_(isRtsMem)
20 3075 : {}
21 :
22 1458 : HostMem::HostMem(const HostMem& that) : ptr_(that.ptr()), size_(that.size_), owner_(false), isRtsMem_(that.isRtsMem_) {}
23 :
24 1610 : HostMem::HostMem(HostMem&& that) : ptr_(that.ptr_), size_(that.size_), owner_(that.owner_), isRtsMem_(that.isRtsMem_)
25 : {
26 1610 : that.ptr_ = nullptr;
27 1610 : that.size_ = 0;
28 1610 : that.owner_ = false;
29 1610 : }
30 :
31 11935 : HostMem::~HostMem()
32 : {
33 11935 : if (owner_ && ptr_) {
34 2102 : HCCL_DEBUG("size_[%llu Byte]", size_);
35 :
36 2105 : if (isRtsMem_) {
37 2103 : HcclResult ret = hrtFreeHost(ptr_);
38 2103 : 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 11938 : }
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 2102 : HostMem HostMem::alloc(u64 size, bool isRtsMem)
65 : {
66 2102 : void* ptr = nullptr;
67 2102 : if (isRtsMem) {
68 2100 : HcclResult ret = hrtMallocHost(&ptr, size);
69 2102 : if (ret != HCCL_SUCCESS) {
70 0 : HCCL_ERROR("[HostMem][Alloc]rtMallocHost error, ret[%d], size[%llu Byte]", ret, size);
71 : }
72 : } else {
73 2 : ptr = new (std::nothrow) u8[size];
74 : }
75 2104 : if (ptr == nullptr) {
76 0 : HCCL_WARNING("HostMem alloc ptr null");
77 : }
78 2104 : HostMem mem(ptr, size, true, isRtsMem);
79 2104 : return mem;
80 : }
81 :
82 968 : HostMem HostMem::create(void* ptr, u64 size)
83 : {
84 968 : HostMem mem(ptr, size, false);
85 968 : 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 1456 : HostMem HostMem::operator=(HostMem&& that)
101 : {
102 1456 : if (&that != this) {
103 1456 : ptr_ = that.ptr_;
104 1456 : size_ = that.size_;
105 1456 : owner_ = that.owner_;
106 1456 : isRtsMem_ = that.isRtsMem_;
107 : }
108 :
109 1456 : that.ptr_ = nullptr;
110 1456 : that.size_ = 0;
111 1456 : that.owner_ = false;
112 :
113 1456 : 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
|