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 : #ifndef __AICPU_ZERO_COPY_EXCHANGER_H__
12 : #define __AICPU_ZERO_COPY_EXCHANGER_H__
13 :
14 : #include <atomic>
15 : #include <set>
16 : #include <unordered_set>
17 : #include <functional>
18 : #include <hccl/hccl_types.h>
19 : #include "aicpu_operator_pub.h"
20 : #include "coll_alg_param.h"
21 : #include "zero_copy/zero_copy_address_mgr.h"
22 : #include "op_unfold_cache_entry.h"
23 :
24 : namespace hccl {
25 : class AicpuZeroCopyExchanger {
26 : public:
27 : AicpuZeroCopyExchanger(
28 : u32 rank, u32 rankSize, const HcclOpResParam* resParam, std::function<bool()> needStop, u32 timeoutSec = 120,
29 : u32 deviceNumPerAggregation = MAX_MODULE_DEVICE_NUM, u32 taskMonitorInterval = 0);
30 : ~AicpuZeroCopyExchanger();
31 :
32 : HcclResult
33 : ExchangeAddress(const std::string& tag, void* localInput, void* localOutput, AlgResourceResponse* algResResponse);
34 :
35 : // 将每个remote rank对应的user input/output addr暴露给HcclCommAicpu, 为OpUnfoldCache做准备
36 : HcclResult PrepareRemoteUserMemRanges(
37 : const uint64_t inputSize, const uint64_t outputSize, std::vector<OpUnfoldMemRange>& userInputMemRanges,
38 : std::vector<OpUnfoldMemRange>& userOutputMemRanges) const;
39 :
40 : // yxg-debug 提供提前退出的方法退出阻塞接口
41 : private:
42 : // 这里使用volatile修饰,是为了避免数据写到CPU cache中,必须要写到内存中
43 : struct FlagData {
44 : volatile u64 inAddr; // 通信的input
45 : volatile u64 outAddr; // 通信的output
46 : volatile u64
47 : flag; // flag标志,表明其那面数据是否有效,必须放置到最后一个成员,这样能保证sdma copy先写数据,后修改flag
48 : };
49 :
50 : struct TagRes {
51 : std::set<u32> remoteRanks; // 本tag所有的通信对端rank
52 : std::vector<LINK> links; // 本tag所有的通信对端link
53 : std::vector<void*> remotePtrs; // batchSdma copy使用的入参
54 : std::vector<void*> selfPtrs;
55 : std::vector<FlagData> selfData;
56 : std::vector<size_t> sizes;
57 : std::vector<u32> rankIds; // 维测信息使用
58 : };
59 :
60 : static constexpr u64 INVALID_DATA = 0; // 数据无效
61 : static constexpr u64 VALID_DATA = 1; // 数据有效
62 :
63 0 : void MemFence() const
64 : {
65 : /* 内存屏障,即阻止编译器重排变量读写,也阻止CPU重排变量读写 */
66 : std::atomic_thread_fence(std::memory_order_seq_cst);
67 0 : }
68 :
69 : // 判断是否所有的ipc都是有效的,如果无效则报错
70 : bool IsAllIpcAddressValid();
71 : bool IsSupportZeroCopyLinkType(LinkType linkType);
72 :
73 : /* 尝试从data中读数据,只有flag是valid时可以读, 读成功需要置invalid, 如果失败则返回EAGIN表示需要重试 */
74 : HcclResult TryToRead(FlagData& data, u64& in, u64& out);
75 :
76 : HcclResult GetRemoteRanks(TagRes& tagRes, OpCommTransport& opTransportResponse);
77 : HcclResult PrepareTagRes(const std::string& tag, OpCommTransport& opTransportResponse);
78 : HcclResult GetRemoteAddr();
79 : HcclResult BatchSetLocalAddrToRemote(void* in, void* out);
80 : HcclResult UpdateTransportAddress();
81 : std::string DumpLinkInfo(std::set<u32>& doneRanks);
82 :
83 : // 存放进行地址交换后的对端input/output
84 : u64 inAddrs_[MAX_MODULE_DEVICE_NUM]{};
85 : u64 outAddrs_[MAX_MODULE_DEVICE_NUM]{};
86 : u32 rankId_{INVALID_VALUE_RANKID};
87 : u32 rankSize_{INVALID_VALUE_RANKSIZE};
88 :
89 : const HcclOpResParam* resParam_{nullptr};
90 : std::function<bool()> needStop_{};
91 : u32 timeoutSec_ = 120;
92 : std::unordered_map<std::string, TagRes> tagRes_{};
93 : TagRes* current_{nullptr}; // 表明当前正在使用的tag资源
94 :
95 : static ZeroCopyAddressMgr globalAddrMgr_;
96 :
97 : u32 deviceNumPerAggregation_ = MAX_MODULE_DEVICE_NUM;
98 : u32 taskMonitorInterval_ = 0;
99 : };
100 : } // namespace hccl
101 :
102 : #endif
|