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 <atomic>
12 : #include <hccl/hccl_types.h>
13 : #include "hccd_impl_pml.h"
14 : #include "hccd_comm.h"
15 : namespace hccl {
16 0 : HccdComm::HccdComm(std::string identifier) : impl_(nullptr), identifier_(identifier) {}
17 :
18 0 : HccdComm::~HccdComm() { impl_ = nullptr; }
19 :
20 0 : HcclResult HccdComm::init(HcclCommParams& params, const RankTable_t& rankTable)
21 : {
22 0 : HCCL_INFO("HccdComm init workmode [%d]", params.commWorkMode);
23 :
24 0 : CHK_RET(InitImpl());
25 :
26 : /* 强行将最后一个字符置0, 确保其可以做字符串操作 */
27 0 : params.id.internal[HCCL_ROOT_INFO_BYTES - 1] = '\0';
28 :
29 : /* 入参判断 */
30 0 : if (params.rank >= params.totalRanks) {
31 0 : HCCL_ERROR(
32 : "[HcclComm][Init]errNo[0x%016llx] rank[%u] out of range[0, %u]", HCCL_ERROR_CODE(HCCL_E_PARA), params.rank,
33 : params.totalRanks - 1);
34 0 : return HCCL_E_PARA;
35 : }
36 0 : params.identifier = identifier_;
37 0 : CHK_RET(impl_->AtomicInitSet()); /* 初始化竞争, 只允许被初始化一次 */
38 0 : HcclResult ret = impl_->Init(params, rankTable); /* 初始化实例, 失败则重新开放初始化竞争 */
39 0 : if (ret != HCCL_SUCCESS) {
40 0 : HCCL_ERROR("[HcclComm][Init]errNo[0x%016llx] hccl initialize failed", HCCL_ERROR_CODE(ret));
41 0 : impl_->AtomicInitClear();
42 0 : return ret;
43 : }
44 :
45 0 : HCCL_RUN_INFO(
46 : "hccdCommInitInfo:commId[%s], rank[%u], totalRanks[%u], serverId[%s], deviceType[%d],"
47 : "logicDevId[%d], identifier[%s]",
48 : params.id.internal, params.rank, params.totalRanks, params.serverId.c_str(), params.deviceType,
49 : params.logicDevId, params.identifier.c_str());
50 0 : return HCCL_SUCCESS;
51 : }
52 :
53 0 : HcclResult HccdComm::RegisterMemory(void* buffer, uint64_t size) { return impl_->RegisterMemory(buffer, size); }
54 :
55 0 : HcclResult HccdComm::UnregisterMemory(void* buffer) { return impl_->UnregisterMemory(buffer); }
56 :
57 0 : HcclResult HccdComm::Isend(
58 : void* buffer, s32 count, HcclDataType dataType, u32 peerRank, s32 tag, HcclRequest& request, u32 userRequire) const
59 : {
60 : /* 入参检查 */
61 0 : CHK_RET(impl_->CheckCount(count));
62 0 : CHK_RET(impl_->CheckDataType(dataType, false));
63 0 : return impl_->Isend(buffer, count, dataType, peerRank, tag, request, userRequire);
64 : }
65 :
66 0 : HcclResult HccdComm::Improbe(u32 peerRank, s32 tag, s32& flag, HcclMessage& msgHandle, HcclStatus& status) const
67 : {
68 0 : return impl_->Improbe(peerRank, tag, flag, msgHandle, status);
69 : }
70 :
71 0 : HcclResult HccdComm::Imrecv(void* buffer, s32 count, HcclDataType dataType, HcclMessage msg, HcclRequest& request) const
72 : {
73 : /* 入参检查 */
74 0 : CHK_RET(impl_->CheckCount(count));
75 0 : CHK_RET(impl_->CheckDataType(dataType, false));
76 0 : return impl_->Imrecv(buffer, count, dataType, msg, request);
77 : }
78 :
79 0 : HcclResult HccdComm::HcclTest(HcclRequest hcclRequest, s32& flag, HcclStatus& compState) const
80 : {
81 0 : return impl_->HcclTest(hcclRequest, flag, compState);
82 : }
83 :
84 0 : HcclResult HccdComm::GetUserRank(u32& userRank)
85 : {
86 0 : userRank = impl_->GetUserRank();
87 0 : return HCCL_SUCCESS;
88 : }
89 :
90 0 : HcclResult HccdComm::GetRankSize(u32& rankSize)
91 : {
92 0 : rankSize = impl_->GetRankSize();
93 0 : return HCCL_SUCCESS;
94 : }
95 :
96 0 : const std::string& HccdComm::GetIdentifier() { return identifier_; }
97 :
98 0 : HcclResult HccdComm::InitImpl()
99 : {
100 0 : impl_.reset(new (std::nothrow) HccdImplPml());
101 0 : CHK_SMART_PTR_NULL(impl_);
102 0 : return HCCL_SUCCESS;
103 : }
104 :
105 0 : HcclResult HccdComm::GetUniqueId(HcclRootInfo* uniqueId)
106 : {
107 0 : CHK_PTR_NULL(uniqueId);
108 :
109 0 : std::string uniqueIdGot = HccdImplPml::GetUniqueId();
110 0 : s32 ret = snprintf_s(
111 0 : uniqueId->internal, HCCL_ROOT_INFO_BYTES, HCCL_ROOT_INFO_BYTES - 1, "%s%s", "hccl-", uniqueIdGot.c_str());
112 0 : CHK_PRT_RET(
113 : (ret == -1),
114 : HCCL_ERROR("[Get][UniqueId]errNo[0x%016llx] get unique id failed,uniqueId[%p]", HCCL_ERROR_CODE(ret), uniqueId),
115 : HCCL_E_MEMORY);
116 :
117 0 : return HCCL_SUCCESS;
118 0 : }
119 : } // namespace hccl
|