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 "global_mem_record.h"
12 : #include <sstream>
13 :
14 : namespace hccl {
15 8 : GlobalMemRecord::GlobalMemRecord(const HcclMem* mem)
16 8 : : type_(mem->type),
17 8 : addr_(mem->addr),
18 8 : size_(mem->size),
19 8 : pLock_(std::make_unique<std::mutex>())
20 8 : {}
21 :
22 1 : GlobalMemRecord::GlobalMemRecord(const HcclMem& mem)
23 1 : : type_(mem.type),
24 1 : addr_(mem.addr),
25 1 : size_(mem.size),
26 1 : pLock_(std::make_unique<std::mutex>())
27 1 : {}
28 :
29 6 : GlobalMemRecord::GlobalMemRecord(GlobalMemRecord&& other) noexcept
30 6 : : type_(other.type_),
31 6 : addr_(other.addr_),
32 6 : size_(other.size_),
33 6 : pLock_(std::move(other.pLock_)),
34 6 : boundComm_(std::move(other.boundComm_))
35 6 : {}
36 :
37 5 : bool GlobalMemRecord::HasOverlap(const GlobalMemRecord& other) const
38 : {
39 5 : if (type_ != other.GetMemType()) {
40 : // 不同类型不判断
41 1 : return false;
42 : }
43 :
44 4 : const auto thisBegin = reinterpret_cast<uintptr_t>(addr_);
45 4 : const auto thisEnd = thisBegin + size_;
46 4 : const auto otherBegin = reinterpret_cast<uintptr_t>(other.GetAddr());
47 4 : const auto otherEnd = otherBegin + other.GetSize();
48 :
49 4 : return (thisBegin < otherEnd) && (otherBegin < thisEnd);
50 : }
51 :
52 1 : HcclResult GlobalMemRecord::BindToComm(const std::string& commIdentifier)
53 : {
54 1 : std::unique_lock<std::mutex> lock(*pLock_);
55 1 : const auto insertRet = boundComm_.insert(commIdentifier);
56 :
57 1 : CHK_PRT_RET(
58 : insertRet.second == false,
59 : HCCL_ERROR(
60 : "[GlobalMemRecord][BindToComm] The mem[%s] has been bound to the comm[%s] already.", PrintInfo().c_str(),
61 : commIdentifier.c_str()),
62 : HCCL_E_PARA);
63 :
64 1 : HCCL_INFO(
65 : "[GlobalMemRecord][BindToComm] The mem[%s] is bound to the comm[%s].", PrintInfo().c_str(),
66 : commIdentifier.c_str());
67 :
68 1 : return HCCL_SUCCESS;
69 1 : }
70 :
71 1 : HcclResult GlobalMemRecord::UnbindFromComm(const std::string& commIdentifier)
72 : {
73 1 : std::unique_lock<std::mutex> lock(*pLock_);
74 1 : const auto eraseCount = boundComm_.erase(commIdentifier);
75 :
76 1 : CHK_PRT_RET(
77 : eraseCount == 0,
78 : HCCL_ERROR(
79 : "[GlobalMemRecord][UnbindFromComm] The mem[%s] is not bound to the comm[%s].", PrintInfo().c_str(),
80 : commIdentifier.c_str()),
81 : HCCL_E_PARA);
82 :
83 1 : HCCL_INFO(
84 : "[GlobalMemRecord][UnbindFromComm] The mem[%s] has been unbound from the comm[%s].", PrintInfo().c_str(),
85 : commIdentifier.c_str());
86 :
87 1 : return HCCL_SUCCESS;
88 1 : }
89 :
90 25 : std::string GlobalMemRecord::PrintInfo() const
91 : {
92 25 : std::stringstream ss;
93 25 : if (type_ == HCCL_MEM_TYPE_DEVICE) {
94 17 : ss << "type:DEVICE, ";
95 8 : } else if (type_ == HCCL_MEM_TYPE_HOST) {
96 8 : ss << "type:HOST, ";
97 : }
98 :
99 25 : ss << "addr:" << addr_ << ", ";
100 25 : ss << "size:" << size_;
101 50 : return ss.str();
102 25 : }
103 :
104 : } // namespace hccl
|