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 <numeric>
12 : #include "mc2_handler.h"
13 :
14 : namespace hccl {
15 :
16 1 : Mc2HandlerPub::Mc2HandlerPub()
17 : {
18 : // mc2TurnNum_ 用于提供turnNum的地址
19 1 : std::iota(mc2TurnNum_, mc2TurnNum_ + MC2_MAX_TURN * MC2_MAX_RANK_NUM, 0);
20 1 : }
21 :
22 : HcclResult
23 3 : Mc2HandlerPub::Mc2WaitValue(HcclDispatcher dispatcherPtr, hccl::Stream& stream, Mc2Handler* mc2Handler, u32 step)
24 : {
25 3 : if (mc2Handler == nullptr) {
26 1 : HCCL_INFO("[Mc2HandlerPub][Mc2WaitValue] mc2Handler is NULL.");
27 1 : return HCCL_SUCCESS;
28 2 : } else if (mc2Handler->repeatCnt < 1 || step >= mc2Handler->rankSize) {
29 : // repeatCnt 至少为1
30 : // step 校验 范围为[0, rankSize-1]
31 1 : HCCL_ERROR("[Mc2HandlerPub][Mc2WaitValue] error parameter, repeatCnt %u, step %u", mc2Handler->repeatCnt, step);
32 1 : return HCCL_E_PARA;
33 1 : } else if (mc2Handler->stepSize == 0 || (step + 1) % mc2Handler->stepSize != 0) {
34 0 : HCCL_INFO(
35 : "[Mc2HandlerPub][Mc2WaitValue] no need to add wait task, step %u, stepSize %u.", step,
36 : mc2Handler->stepSize);
37 0 : return HCCL_SUCCESS;
38 : }
39 : // 第 repeatCnt轮通信,turnNum范围为[(repeatCnt -1)*rankSize + 1, repeatCnt*rankSize]
40 : // 计算当前轮次的turnNum值
41 1 : u32 turnNum = (mc2Handler->repeatCnt - 1) * (mc2Handler->rankSize) + (step + 1);
42 1 : HCCL_INFO(
43 : "[Mc2HandlerPub][Mc2WaitValue] repeatCnt %u, rankSize %u, step %u, turnNum %u", mc2Handler->repeatCnt,
44 : mc2Handler->rankSize, step, turnNum);
45 1 : u64 valueAddr = mc2Handler->valueAddr + sizeof(uint32_t) * turnNum;
46 1 : CHK_RET(HcclDispatcherWaitValue(dispatcherPtr, stream, mc2Handler->commitAddr, valueAddr, false));
47 1 : return HCCL_SUCCESS;
48 : }
49 :
50 1 : HcclResult Mc2HandlerPub::Mc2WriteValue(HcclDispatcher dispatcherPtr, hccl::Stream& stream, Mc2Handler* mc2Handler)
51 : {
52 1 : if (mc2Handler == nullptr) {
53 1 : HCCL_INFO("[Mc2HandlerPub][Mc2WriteValue] mc2Handler is NULL.");
54 1 : return HCCL_SUCCESS;
55 : }
56 0 : turnNumForWrite_++;
57 0 : u32 turnNum = (mc2Handler->repeatCnt - 1) * (mc2Handler->rankSize) + turnNumForWrite_;
58 0 : HCCL_INFO("[Mc2HandlerPub][Mc2WriteValue] turnNumForWrite %u, turnNum %u.", turnNumForWrite_, turnNum);
59 0 : u64 valueAddr = mc2Handler->valueAddr + sizeof(uint32_t) * turnNum;
60 0 : CHK_RET(HcclDispatcherWriteValue(dispatcherPtr, stream, mc2Handler->finishAddr, valueAddr));
61 0 : return HCCL_SUCCESS;
62 : }
63 : } // namespace hccl
|