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