Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "coll_comm_config_consistency.h"
12 :
13 : namespace hccl {
14 229 : CollCommConfigConsistency::CollCommConfigConsistency() {}
15 :
16 229 : CollCommConfigConsistency::~CollCommConfigConsistency()
17 : {
18 229 : HCCL_INFO("[CollCommConfigConsistency][~CollCommConfigConsistency] CollCommConfigConsistency deinit");
19 229 : remoteExchangeInfoMap_.clear();
20 229 : ResetExchangeInfo();
21 229 : }
22 :
23 7 : HcclResult CollCommConfigConsistency::AddExchangeInfo(const void* data, uint32_t length)
24 : {
25 7 : CHK_PTR_NULL(data);
26 7 : if (length > 0 && length <= HCCL_EXCHANGE_INFO_LEN) {
27 7 : exchangeInfoBuf_.resize(length);
28 7 : s32 sRet = memcpy_s(exchangeInfoBuf_.data(), length, data, length);
29 7 : CHK_PRT_RET(sRet != EOK, HCCL_ERROR("[AddExchangeInfo] memcpy_s failed, ret[%d]", sRet), HCCL_E_MEMORY);
30 7 : HCCL_INFO("[AddExchangeInfo] success, length[%u].", length);
31 7 : } else {
32 0 : HCCL_ERROR("[AddExchangeInfo] length[%u] is illegal", length);
33 0 : return HCCL_E_PARA;
34 : }
35 :
36 7 : return HCCL_SUCCESS;
37 : }
38 :
39 : HcclResult
40 2 : CollCommConfigConsistency::GetExchangeInfo(uint32_t remoteRank, uint32_t length, void* data, uint32_t* actualLength)
41 : {
42 2 : CHK_PTR_NULL(data);
43 2 : auto iter = remoteExchangeInfoMap_.find(remoteRank);
44 2 : if (iter == remoteExchangeInfoMap_.end()) {
45 0 : *actualLength = 0;
46 0 : HCCL_INFO("[GetExchangeInfo] remoteExchangeInfoMap_ is null");
47 0 : return HCCL_SUCCESS;
48 : }
49 2 : *actualLength = static_cast<uint32_t>(iter->second.size());
50 :
51 2 : if (length == *actualLength || *actualLength == 0) {
52 2 : s32 sRet = memcpy_s(data, length, iter->second.data(), iter->second.size());
53 2 : CHK_PRT_RET(sRet != EOK, HCCL_ERROR("[GetExchangeInfo] memcpy_s failed, ret[%d]", sRet), HCCL_E_MEMORY);
54 2 : } else {
55 0 : HCCL_ERROR("[GetExchangeInfo] failed, length[%u] actualLength[%u].", length, *actualLength);
56 0 : return HCCL_E_PARA;
57 : }
58 :
59 : // 读后清除
60 2 : remoteExchangeInfoMap_.erase(iter);
61 2 : HCCL_INFO("[GetExchangeInfo] success, remoteRank[%u], actualLength[%u].", remoteRank, *actualLength);
62 2 : return HCCL_SUCCESS;
63 : }
64 :
65 2 : HcclResult CollCommConfigConsistency::StoreRemoteExchangeInfo(uint32_t remoteRank, std::vector<u8>& data)
66 : {
67 2 : remoteExchangeInfoMap_[remoteRank] = std::move(data);
68 2 : HCCL_INFO(
69 : "[StoreRemoteExchangeInfo] success, remoteRank[%u], length[%zu].", remoteRank,
70 : remoteExchangeInfoMap_[remoteRank].size());
71 2 : return HCCL_SUCCESS;
72 : }
73 :
74 233 : HcclResult CollCommConfigConsistency::ResetExchangeInfo()
75 : {
76 233 : exchangeInfoBuf_.clear();
77 233 : HCCL_INFO("[ResetExchangeInfo] exchange info state cleared.");
78 233 : return HCCL_SUCCESS;
79 : }
80 :
81 4 : HcclResult CollCommConfigConsistency::GetExchangeInfoBuf(std::vector<u8>& exchangeInfoBuf)
82 : {
83 4 : exchangeInfoBuf = exchangeInfoBuf_;
84 4 : return HCCL_SUCCESS;
85 : }
86 :
87 3 : uint32_t CollCommConfigConsistency::GetExchangeInfoLen() const
88 : {
89 3 : return static_cast<uint32_t>(exchangeInfoBuf_.size());
90 : }
91 :
92 : } // namespace hccl
|