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 "comm_ahc_base_pub.h"
12 : #include <iostream>
13 : #include <fstream>
14 :
15 : namespace hccl {
16 :
17 : HcclResult
18 0 : CommAHCBaseInfo::GetNBNslbDstRanks(const u32 rank, const std::vector<u32> commGroups, std::vector<u32>& dstRanks)
19 : {
20 0 : CHK_PRT_RET(
21 : rank >= commGroups.size(),
22 : HCCL_ERROR(
23 : "[CalcNBTransportReq][CalcDstRanks] rank [%u] exceed commGroups Size [%u] error", rank, commGroups.size()),
24 : HCCL_E_INTERNAL);
25 :
26 0 : for (auto i = 0; static_cast<u32>(1 << i) < commGroups.size(); ++i) {
27 : // 正方向第2^i个节点的rank号
28 0 : const u32 targetRankPos = static_cast<u32>(rank + (1 << i)) % commGroups.size();
29 0 : dstRanks.push_back(commGroups[targetRankPos]);
30 :
31 : // 反方向第2^i个节点的rank号
32 0 : const u32 targetRankNeg = static_cast<u32>(rank + commGroups.size() - (1 << i)) % commGroups.size();
33 :
34 0 : HCCL_DEBUG(
35 : "[CalcNBTransportReq][CalcDstRanks] local rank[%u], remote rank[%u]", commGroups[rank],
36 : commGroups[targetRankNeg]);
37 :
38 0 : dstRanks.push_back(commGroups[targetRankNeg]);
39 : }
40 :
41 0 : return HCCL_SUCCESS;
42 : }
43 :
44 : HcclResult
45 0 : CommAHCBaseInfo::GetNHRNslbDstRanks(const u32 rank, const std::vector<u32> commGroups, std::vector<u32>& dstRanks)
46 : {
47 0 : CHK_PRT_RET(
48 : rank >= commGroups.size(),
49 : HCCL_ERROR(
50 : "[CalcNHRTransportReq][CalcDstRanks] rank [%u] exceed commGroups Size [%u] error", rank, commGroups.size()),
51 : HCCL_E_INTERNAL);
52 :
53 0 : for (auto i = 0; static_cast<u32>(1 << i) < commGroups.size(); ++i) {
54 : // 正方向第2^i个节点的rank号
55 0 : const u32 targetRankPos = static_cast<u32>(rank + (1 << i)) % commGroups.size();
56 0 : dstRanks.push_back(commGroups[targetRankPos]);
57 :
58 : // 反方向第2^i个节点的rank号
59 0 : const u32 targetRankNeg = static_cast<u32>(rank + commGroups.size() - (1 << i)) % commGroups.size();
60 :
61 0 : HCCL_DEBUG(
62 : "[CalcNHRTransportReq][CalcDstRanks] local rank[%u], remote rank[%u]", commGroups[rank],
63 : commGroups[targetRankNeg]);
64 :
65 0 : dstRanks.push_back(commGroups[targetRankNeg]);
66 : }
67 :
68 0 : return HCCL_SUCCESS;
69 : }
70 :
71 : HcclResult
72 0 : CommAHCBaseInfo::GetRingNslbDstRanks(const u32 rank, const std::vector<u32> commGroups, std::vector<u32>& dstRanks)
73 : {
74 0 : CHK_PRT_RET(
75 : rank >= commGroups.size(),
76 : HCCL_ERROR(
77 : "[CalcRingTransportReq][CalcDstRanks] rank [%u] exceed commGroups Size [%u] error", rank,
78 : commGroups.size()),
79 : HCCL_E_INTERNAL);
80 :
81 : // 正方向下一个节点的rank号
82 0 : const u32 targetRankPos = static_cast<u32>(rank + 1) % commGroups.size();
83 0 : dstRanks.push_back(commGroups[targetRankPos]);
84 :
85 : // 反方向下一个节点的rank号
86 0 : const u32 targetRankNeg = static_cast<u32>(rank + commGroups.size() - 1) % commGroups.size();
87 :
88 0 : HCCL_DEBUG(
89 : "[CalcRingTransportReq][CalcDstRanks] local rank[%u], remote rank[%u]", commGroups[rank],
90 : commGroups[targetRankNeg]);
91 :
92 0 : dstRanks.push_back(commGroups[targetRankNeg]);
93 :
94 0 : return HCCL_SUCCESS;
95 : }
96 :
97 0 : HcclResult CommAHCBaseInfo::GetDstRanksByType(
98 : AHCTemplateType type, const u32 rank, const std::vector<u32> commGroups, std::vector<u32>& dstRanks)
99 : {
100 0 : if (type == AHCTemplateType::AHC_TEMPLATE_NB) {
101 0 : CHK_RET(GetRingNslbDstRanks(rank, commGroups, dstRanks));
102 : }
103 0 : if (type == AHCTemplateType::AHC_TEMPLATE_NHR) {
104 0 : CHK_RET(GetRingNslbDstRanks(rank, commGroups, dstRanks));
105 : }
106 0 : if (type == AHCTemplateType::AHC_TEMPLATE_NHR) {
107 0 : CHK_RET(GetRingNslbDstRanks(rank, commGroups, dstRanks));
108 : }
109 0 : return HCCL_SUCCESS;
110 : }
111 :
112 : } // namespace hccl
|