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 "zero_copy_address_mgr.h"
12 : #include "adapter_rts_common.h"
13 :
14 : namespace hccl {
15 0 : HcclResult ZeroCopyAddressMgr::InitRingBuffer()
16 : {
17 0 : if (ringBuffer_.ptr() != nullptr) {
18 0 : return HCCL_SUCCESS;
19 : }
20 :
21 0 : CHK_RET(DeviceMem::alloc(ringBuffer_, ZERO_COPY_BUFFER_MAX_MAP_COUNT * sizeof(ZeroCopyRingBufferItem)));
22 0 : CHK_RET(hrtMemSet(ringBuffer_.ptr(), ringBuffer_.size(), ringBuffer_.size()));
23 :
24 0 : CHK_RET(DeviceMem::alloc(ringBufferCtl_, sizeof(u32) + sizeof(u32)));
25 0 : CHK_RET(hrtMemSet(ringBufferCtl_.ptr(), ringBufferCtl_.size(), ringBufferCtl_.size()));
26 :
27 0 : devRingBufBase_ = reinterpret_cast<ZeroCopyRingBufferItem*>(ringBuffer_.ptr());
28 0 : devRingHead_ = reinterpret_cast<u32*>(ringBufferCtl_.ptr());
29 0 : devRingTail_ = devRingHead_ + 1;
30 :
31 0 : HCCL_RUN_INFO(
32 : "[ZeroCopyAddressMgr][InitRingBuffer] ringbuffer[%p] len[%lu] bufferCtl[%p] len[%lu] head[%p] tail[%p]",
33 : ringBuffer_.ptr(), ringBuffer_.size(), ringBufferCtl_.ptr(), ringBufferCtl_.size(), devRingHead_, devRingTail_);
34 :
35 0 : HCCL_INFO(
36 : "[ZeroCopyAddressMgr][InitRingBuffer] ringbuffer[%p] head[%p] tail[%p]", devRingBufBase_, devRingHead_,
37 : devRingTail_);
38 0 : return HCCL_SUCCESS;
39 : }
40 :
41 0 : HcclResult ZeroCopyAddressMgr::PushOne(ZeroCopyRingBufferItem& item)
42 : {
43 0 : std::lock_guard<std::mutex> guard(processRingBufferLock_);
44 0 : if (!needPushOne) {
45 0 : HCCL_DEBUG("[ZeroCopyAddressMgr][PushOne] don't need push");
46 0 : return HCCL_SUCCESS;
47 : }
48 :
49 : // 检测RingBuffer是否已经初始化,没有的话就初始化一下
50 0 : CHK_RET(InitRingBuffer());
51 :
52 0 : u32 head = 0;
53 0 : CHK_RET(hrtMemSyncCopy(
54 : &head, sizeof(head), devRingHead_, sizeof(head), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_DEVICE_TO_HOST));
55 0 : u32 tail = 0;
56 0 : CHK_RET(hrtMemSyncCopy(
57 : &tail, sizeof(tail), devRingTail_, sizeof(tail), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_DEVICE_TO_HOST));
58 :
59 0 : u32 updateTail = (tail + 1) % ZERO_COPY_BUFFER_MAX_MAP_COUNT;
60 0 : CHK_PRT_RET(
61 : updateTail == head,
62 : HCCL_ERROR(
63 : "[ZeroCopyAddressMgr][PushOne] ring buffer is full head[%u] tail[%u] capacity[%u]", head, tail,
64 : ZERO_COPY_BUFFER_MAX_MAP_COUNT),
65 : HCCL_E_INTERNAL);
66 :
67 0 : HCCL_INFO(
68 : "[ZeroCopyAddressMgr][PushOne] type[%d] head[%u] tail[%u] updateTail[%u] tailAddr[%p]", item.type, head, tail,
69 : updateTail, devRingBufBase_ + tail);
70 0 : CHK_RET(hrtMemSyncCopy(
71 : devRingBufBase_ + tail, sizeof(ZeroCopyRingBufferItem), &item, sizeof(ZeroCopyRingBufferItem),
72 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
73 0 : CHK_RET(hrtMemSyncCopy(
74 : devRingTail_, sizeof(updateTail), &updateTail, sizeof(updateTail),
75 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
76 :
77 0 : return HCCL_SUCCESS;
78 0 : }
79 :
80 : } // namespace hccl
|