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 1644 : DevBuffer::DevBuffer(uintptr_t devAddr, std::size_t devSize) : Buffer(devSize), selfOwned(false)
19 : {
20 1644 : addr_ = devAddr;
21 1644 : size_ = devSize;
22 1644 : }
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,
31 : static_cast<u32>(ACL_MEM_TYPE_HIGH_BAND_WIDTH)));
32 942 : }
33 :
34 1711 : std::shared_ptr<DevBuffer> DevBuffer::Create(uintptr_t devAddr, std::size_t devSize)
35 : {
36 1711 : if (devAddr == 0 || devSize == 0) {
37 130 : return nullptr;
38 : }
39 1581 : return std::shared_ptr<DevBuffer>(new DevBuffer(devAddr, devSize));
40 : }
41 117 : DevBuffer::DevBuffer(std::size_t allocSize, std::uint32_t policy, PolicyTag /*tag*/) : Buffer(allocSize), selfOwned(true)
42 : {
43 117 : if (allocSize == 0) {
44 0 : std::string msg = "allocaSize should not be 0!";
45 0 : THROW<InternalException>(msg);
46 0 : }
47 234 : addr_ = reinterpret_cast<uintptr_t>(HrtMalloc(allocSize,
48 : static_cast<int>(ACL_MEM_TYPE_HIGH_BAND_WIDTH) |
49 117 : static_cast<int>(policy)));
50 117 : }
51 :
52 117 : std::shared_ptr<DevBuffer> DevBuffer::CreateHugePageBuf(std::size_t size){
53 234 : return std::make_shared<DevBuffer>(size, static_cast<int>(ACL_MEM_MALLOC_HUGE_ONLY), PolicyTag{});
54 : }
55 :
56 4458 : DevBuffer::~DevBuffer()
57 : {
58 2702 : if (selfOwned) {
59 1058 : DECTOR_TRY_CATCH("Buffer", HrtFree(reinterpret_cast<void *>(addr_)))
60 : }
61 4458 : }
62 :
63 166 : std::string DevBuffer::Describe() const
64 : {
65 166 : return StringFormat("DevBuffer[addr=0x%llx, size=0x%llx, selfOwned=%d]", addr_, size_, selfOwned);
66 : }
67 :
68 2 : bool DevBuffer::GetSelfOwned() const
69 : {
70 2 : return selfOwned;
71 : }
72 :
73 : } // namespace Hccl
|