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