* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "eid_info_mgr.h"
#include "hccl_common.h"
#include "orion_adpt_utils.h"
namespace hcomm {
EidInfoMgr &EidInfoMgr::GetInstance(const uint32_t devicePhyId)
{
static EidInfoMgr eidInfoMgr[MAX_MODULE_DEVICE_NUM + 1];
uint32_t devPhyId = devicePhyId;
if (devPhyId >= MAX_MODULE_DEVICE_NUM) {
HCCL_WARNING("[EidInfoMgr][%s] use the backup device, devPhyId[%u] should be "
"less than %u.", __func__, devPhyId, MAX_MODULE_DEVICE_NUM);
devPhyId = MAX_MODULE_DEVICE_NUM;
}
eidInfoMgr[devPhyId].devPhyId_ = devPhyId;
return eidInfoMgr[devPhyId];
}
HcclResult EidInfoMgr::Init()
{
const RaInfo raInfo{NetworkMode::NETWORK_OFFLINE, devPhyId_};
CHK_RET(RaGetDevEidInfos(raInfo, eidInfos_));
initflag_ = true;
if (eidInfos_.empty()) {
HCCL_ERROR("[EidInfoMgr][%s] failed to find any eid info, "
"devPhyId[%u].", __func__, devPhyId_);
return HcclResult::HCCL_E_NOT_FOUND;
}
for (const auto& eidInfo : eidInfos_) {
Hccl::IpAddress ipAddr{};
CHK_RET(CommAddrToIpAddress(eidInfo.commAddr, ipAddr));
eidInfoMap_[ipAddr] = eidInfo;
}
return HcclResult::HCCL_SUCCESS;
}
HcclResult EidInfoMgr::GetEidInfos(std::vector<DevEidInfo> &eidInfos)
{
std::unique_lock<std::mutex> lock(innerMutex_);
if (!initflag_) {
CHK_RET(Init());
}
eidInfos.assign(eidInfos_.begin(), eidInfos_.end());
HCCL_INFO("[EidInfoMgr][%s] found %zu eid info, devPhyId[%d].",
__func__, eidInfos.size(), devPhyId_);
return HCCL_SUCCESS;
}
HcclResult EidInfoMgr::GetEidInfoByAddr(const CommAddr &commAddr, DevEidInfo &eidInfo)
{
std::unique_lock<std::mutex> lock(innerMutex_);
if (!initflag_) {
CHK_RET(Init());
}
Hccl::IpAddress ipAddr{};
CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
const auto eidAddr = Hccl::IpAddress(ipAddr.GetEid());
const auto addrIter = eidInfoMap_.find(eidAddr);
if (addrIter == eidInfoMap_.end()) {
HCCL_ERROR("[EidInfoMgr][%s] failed to find eid info by ip addr[%s], "
"devPhyId[%u].", __func__, ipAddr.Describe().c_str(), devPhyId_);
return HcclResult::HCCL_E_NOT_FOUND;
}
eidInfo = addrIter->second;
return HcclResult::HCCL_SUCCESS;
}
};