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