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 <math.h>
12 : #include "alg_template_register.h"
13 : #include "all_reduce_doubling.h"
14 :
15 : namespace hccl {
16 :
17 : // Doubling算法实现AllReduce,只用于server内通信
18 0 : AllReduceDoubling::AllReduceDoubling(const HcclDispatcher dispatcher) : AlgTemplateBase(dispatcher)
19 : {
20 0 : }
21 :
22 0 : AllReduceDoubling::~AllReduceDoubling()
23 : {
24 0 : }
25 :
26 0 : HcclResult AllReduceDoubling::Prepare(u64 reduceAttrBitMap, HcomCollOpInfo *opInfo)
27 : {
28 0 : reduceAttr_ = reduceAttrBitMap;
29 0 : return HCCL_SUCCESS;
30 : }
31 :
32 0 : HcclResult AllReduceDoubling::RunAsync(const u32 rank, const u32 rankSize, const std::vector<LINK> &links)
33 : {
34 0 : HCCL_INFO("[AllReduceDoubling] runAsync rank[%u] ranksize[%u] inputMem[%p] outputMem[%p] count[%llu]", \
35 : rank, rankSize, inputMem_.ptr(), outputMem_.ptr(), count_);
36 :
37 : // 基本的检查
38 0 : CHK_RET(SimpleCheck(rank, rankSize, links));
39 :
40 : // 判断rank_size == 1
41 0 : if (rankSize == 1) {
42 : // 对于Doubling,input和output必须是两块不同的内存
43 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, outputMem_, inputMem_, stream_));
44 0 : return HCCL_SUCCESS;
45 : }
46 :
47 : // 设置Slices
48 0 : if (slices_.size() != 0) {
49 0 : HCCL_WARNING("[AllReduceDoubling] slices_ will be not used in executor.");
50 : }
51 :
52 : // 执行算法
53 0 : CHK_RET(RunAllReduce(rank, rankSize, links));
54 :
55 0 : HCCL_INFO("AllReduceDoubling finished: rank[%u] ranksize[%u]", rank, rankSize);
56 0 : return HCCL_SUCCESS;
57 : }
58 :
59 0 : HcclResult AllReduceDoubling::SimpleCheck(const u32 rank, const u32 rankSize, const std::vector<LINK> &links)
60 : {
61 : // 判断stream, dispatcher是否为空
62 0 : CHK_SMART_PTR_NULL(dispatcher_);
63 0 : CHK_PTR_NULL(stream_.ptr());
64 :
65 : // 判断Memory是否为空
66 0 : CHK_PRT_RET(!inputMem_, HCCL_ERROR("[AllReduceDoubling] rank[%u] inputmem is null", rank), HCCL_E_PTR);
67 0 : CHK_PRT_RET(!outputMem_, HCCL_ERROR("[AllReduceDoubling] rank[%u] outputmem is null", rank), HCCL_E_PTR);
68 :
69 : // 必须有两块memory
70 0 : CHK_PRT_RET(inputMem_ == outputMem_,
71 : HCCL_ERROR("[AllReduceDoubling] rank[%u] inputMem and outputMem should be different", rank), HCCL_E_PARA);
72 :
73 : // 判断links数量是否正确
74 0 : CHK_PRT_RET(links.size() < rankSize, HCCL_ERROR("[AllReduceDoubling] rank[%u] link size[%llu] is less than "
75 : "rank size[%u]", rank, links.size(), rankSize), HCCL_E_PARA);
76 :
77 : // 判断rankSize是否为2的幂次
78 0 : CHK_PRT_RET((rankSize & (rankSize - 1)) != 0, HCCL_ERROR("[AllReduceDoubling] rankSize must be power of 2, "
79 : "but get rankSize=%u", rankSize), HCCL_E_PARA);
80 0 : return HCCL_SUCCESS;
81 : }
82 :
83 0 : HcclResult AllReduceDoubling::RunAllReduce(const u32 rank, const u32 rankSize, const std::vector<LINK> &links)
84 : {
85 0 : u64 totalSize = count_ * SIZE_TABLE[dataType_];
86 0 : DeviceMem cclInMem = inputMem_.range(0, totalSize);
87 0 : DeviceMem cclOutMem = outputMem_.range(0, totalSize);
88 :
89 0 : u32 nSteps = static_cast<u32>(log2(rankSize));
90 0 : for (u32 step = 0; step < nSteps; step++) {
91 : // 计算邻居并获取link
92 0 : u32 neighbor = rank ^ (1 << step);
93 0 : const LINK &link = links[neighbor];
94 0 : CHK_PTR_NULL(link);
95 :
96 : // 拷贝数据,避免读写冲突
97 0 : if (step == 0) { // 把本端的cclIn拷到cclOut(cclIn是整个template的入口)
98 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, cclOutMem, cclInMem, stream_));
99 : } else { // 把本端的cclOut拷到cclIn(cclOut是上一步的结果)
100 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, cclInMem, cclOutMem, stream_));
101 : }
102 :
103 : // Ack
104 0 : CHK_RET(link->TxAck(stream_));
105 0 : CHK_RET(link->RxAck(stream_));
106 :
107 : // 从对端的cclIn读到本端的cclOut
108 0 : void *remMemPtr = nullptr;
109 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &remMemPtr));
110 0 : DeviceMem remoteCclInMem = DeviceMem::create(static_cast<u8 *>(remMemPtr), totalSize);
111 0 : CHK_RET(HcclReduceAsync(dispatcher_, remoteCclInMem.ptr(), count_, dataType_, reductionOp_,
112 : stream_, cclOutMem.ptr(), link->GetRemoteRank(), link->GetLinkType(), INLINE_REDUCE_BIT));
113 :
114 : // DataSignal
115 0 : CHK_RET(link->TxDataSignal(stream_));
116 0 : CHK_RET(link->RxDataSignal(stream_));
117 0 : }
118 0 : return HCCL_SUCCESS;
119 0 : }
120 : REGISTER_TEMPLATE(TemplateType::TEMPLATE_ALL_REDUCE_DOUBLING, AllReduceDoubling);
121 : } // ~~ namespace hccl
|