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 "dev_buffer.h"
12 : #include "string_util.h"
13 : #include "internal_exception.h"
14 : #include "exception_util.h"
15 : #include "orion_adapter_rts.h"
16 : namespace Hccl {
17 :
18 1654 : DevBuffer::DevBuffer(uintptr_t devAddr, std::size_t devSize) : Buffer(devSize), selfOwned(false)
19 : {
20 1654 : addr_ = devAddr;
21 1654 : size_ = devSize;
22 1654 : }
23 :
24 942 : DevBuffer::DevBuffer(std::size_t allocSize) : Buffer(allocSize), selfOwned(true)
25 : {
26 942 : if (allocSize == 0) {
27 1 : std::string msg = "allocaSize should not be 0!";
28 1 : THROW<InternalException>(msg);
29 1 : }
30 941 : addr_ = reinterpret_cast<uintptr_t>(HrtMalloc(allocSize, static_cast<u32>(ACL_MEM_TYPE_HIGH_BAND_WIDTH)));
31 942 : }
32 :
33 1721 : std::shared_ptr<DevBuffer> DevBuffer::Create(uintptr_t devAddr, std::size_t devSize)
34 : {
35 1721 : if (devAddr == 0 || devSize == 0) {
36 130 : return nullptr;
37 : }
38 1591 : return std::shared_ptr<DevBuffer>(new DevBuffer(devAddr, devSize));
39 : }
40 117 : DevBuffer::DevBuffer(std::size_t allocSize, std::uint32_t policy, PolicyTag /*tag*/)
41 : : Buffer(allocSize),
42 117 : selfOwned(true)
43 : {
44 117 : if (allocSize == 0) {
45 0 : std::string msg = "allocaSize should not be 0!";
46 0 : THROW<InternalException>(msg);
47 0 : }
48 117 : addr_ = reinterpret_cast<uintptr_t>(
49 117 : HrtMalloc(allocSize, static_cast<int>(ACL_MEM_TYPE_HIGH_BAND_WIDTH) | static_cast<int>(policy)));
50 117 : }
51 :
52 117 : std::shared_ptr<DevBuffer> DevBuffer::CreateHugePageBuf(std::size_t size)
53 : {
54 234 : return std::make_shared<DevBuffer>(size, static_cast<int>(ACL_MEM_MALLOC_HUGE_ONLY), PolicyTag{});
55 : }
56 :
57 4478 : DevBuffer::~DevBuffer()
58 : {
59 2712 : if (selfOwned) {
60 1058 : DECTOR_TRY_CATCH("Buffer", HrtFree(reinterpret_cast<void*>(addr_)))
61 : }
62 4478 : }
63 :
64 170 : std::string DevBuffer::Describe() const
65 : {
66 170 : return StringFormat("DevBuffer[addr=0x%llx, size=0x%llx, selfOwned=%d]", addr_, size_, selfOwned);
67 : }
68 :
69 2 : bool DevBuffer::GetSelfOwned() const { return selfOwned; }
70 :
71 : } // namespace Hccl
|