LCOV - code coverage report
Current view: top level - legacy/ascend950/unified_platform/resource/transport - transport_urma_mem.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.7 % 58 52
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 5 5

            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 "transport_urma_mem.h"
      12              : 
      13              : namespace Hccl {
      14            1 : TransportUrmaMem::TransportUrmaMem(
      15            1 :     BaseMemTransport* transport, hcomm::RmaBufferMgr<BufferKey<uintptr_t, u64>, shared_ptr<HcclBuf>>& remoteHcclBufMgr)
      16            1 :     : transport_(transport),
      17            1 :       remoteHcclBufMgr_(remoteHcclBufMgr)
      18            1 : {}
      19              : 
      20            3 : TransportUrmaMem::~TransportUrmaMem() { HCCL_INFO("TransportUrmaMem Destroy"); }
      21              : 
      22            2 : HcclResult TransportUrmaMem::FillRmaBufferSlice(
      23              :     const RmaOpMem& localMem, const RmaOpMem& remoteMem, RmaBufferSlice& localRmaBufferSlice,
      24              :     RmtRmaBufferSlice& remoteRmaBufferSlice)
      25              : {
      26            2 :     void* remoteAddr = remoteMem.addr;
      27            2 :     void* localAddr = localMem.addr;
      28            2 :     u64 byteSize = std::min(remoteMem.size, localMem.size);
      29            2 :     auto localKey = BufferKey<uintptr_t, u64>(reinterpret_cast<uintptr_t>(localAddr), byteSize);
      30            2 :     auto remoteKey = BufferKey<uintptr_t, u64>(reinterpret_cast<uintptr_t>(remoteAddr), byteSize);
      31              : 
      32            2 :     auto localBuffer = LocalUbRmaBufferManager::GetInstance()->Find(localKey);
      33            2 :     CHK_PRT_RET(
      34              :         !localBuffer.first,
      35              :         HCCL_ERROR(
      36              :             "[TransportUrmaMem][FillRmaBufferSlice] Can't find localBuffer by key {%p, %llu}", localAddr, byteSize),
      37              :         HCCL_E_INTERNAL);
      38              : 
      39            2 :     auto remoteHcclBuf = remoteHcclBufMgr_.Find(remoteKey);
      40            2 :     CHK_PRT_RET(
      41              :         !remoteHcclBuf.first,
      42              :         HCCL_ERROR(
      43              :             "[TransportUrmaMem][FillRmaBufferSlice] Can't find remoteBuffer by key {%p, %llu}", remoteAddr, byteSize),
      44              :         HCCL_E_INTERNAL);
      45            2 :     auto remoteBuffer = static_cast<RemoteUbRmaBuffer*>(remoteHcclBuf.second->handle);
      46              : 
      47              :     u64 localDataOffSet
      48            2 :         = static_cast<u8*>(localAddr) - static_cast<u8*>((void*)(localBuffer.second->GetBuf()->GetAddr()));
      49              :     u64 remoteDataOffSet
      50            2 :         = static_cast<u8*>(remoteAddr) - static_cast<u8*>(reinterpret_cast<void*>(remoteBuffer->GetAddr()));
      51              : 
      52              :     localRmaBufferSlice.addr
      53            2 :         = reinterpret_cast<u64>(static_cast<u8*>((void*)(localBuffer.second->GetBuf()->GetAddr())) + localDataOffSet);
      54            2 :     localRmaBufferSlice.size = byteSize;
      55            2 :     localRmaBufferSlice.buf = localBuffer.second.get();
      56              : 
      57            2 :     remoteRmaBufferSlice.addr = reinterpret_cast<u64>(remoteBuffer->GetAddr() + remoteDataOffSet);
      58            2 :     remoteRmaBufferSlice.size = byteSize;
      59            2 :     remoteRmaBufferSlice.buf = remoteBuffer;
      60              : 
      61            6 :     HCCL_INFO(
      62              :         "[TransportUrmaMem][FillRmaBufferSlice] Local [%p], buff[%llu], offset[%llu], after mapping is [%llu], "
      63              :         "Datasize is [%llu].",
      64              :         localAddr, localBuffer.second->GetBuf()->GetAddr(), localDataOffSet, localRmaBufferSlice.addr, byteSize);
      65              : 
      66            6 :     HCCL_INFO(
      67              :         "[TransportUrmaMem][FillRmaBufferSlice] rmt [%p], buff[%llu], offset[%llu], after mapping is [%llu], Datasize "
      68              :         "is [%llu].",
      69              :         remoteAddr, remoteBuffer->GetAddr(), remoteDataOffSet, remoteRmaBufferSlice.addr, byteSize);
      70              : 
      71            2 :     return HCCL_SUCCESS;
      72            2 : }
      73              : 
      74              : // 2 is sizeof(float16), 8 is sizeof(float64), 2 is sizeof(bfloat16)..
      75              : constexpr u32 SIZE_TABLE[HCCL_DATA_TYPE_RESERVED]
      76              :     = {sizeof(s8),
      77              :        sizeof(s16),
      78              :        sizeof(s32),
      79              :        2,
      80              :        sizeof(float),
      81              :        sizeof(s64),
      82              :        sizeof(u64),
      83              :        sizeof(u8),
      84              :        sizeof(u16),
      85              :        sizeof(u32),
      86              :        8,
      87              :        2,
      88              :        16,
      89              :        2,
      90              :        1,
      91              :        1,
      92              :        1,
      93              :        1};
      94              : 
      95            2 : inline HcclResult SalGetDataTypeSize(HcclDataType dataType, u32& dataTypeSize)
      96              : {
      97            2 :     if ((dataType >= HCCL_DATA_TYPE_INT8) && (dataType < HCCL_DATA_TYPE_RESERVED)) {
      98            2 :         dataTypeSize = SIZE_TABLE[dataType];
      99              :     } else {
     100            0 :         HCCL_ERROR(
     101              :             "[Get][DataTypeSize]errNo[0x%016llx] get date size failed. dataType[%u] is invalid.",
     102              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), dataType);
     103            0 :         return HCCL_E_PARA;
     104              :     }
     105            2 :     return HCCL_SUCCESS;
     106              : }
     107              : 
     108            2 : HcclResult TransportUrmaMem::BatchBufferSlice(
     109              :     const HcclOneSideOpDesc* oneSideDescs, u32 descNum, RmaBufferSlice* localRmaBufferSlice,
     110              :     RmtRmaBufferSlice* remoteRmaBufferSlice)
     111              : {
     112            6 :     HCCL_INFO("[TransportUrmaMem][BatchBufferSlice] BatchBufferSlice Start");
     113              : 
     114              :     // 参数校验
     115            2 :     CHK_PTR_NULL(oneSideDescs);
     116            2 :     CHK_PTR_NULL(localRmaBufferSlice);
     117            2 :     CHK_PTR_NULL(remoteRmaBufferSlice);
     118              : 
     119            2 :     RmaOpMem remoteMem[MAX_DESC_NUM] = {};
     120            2 :     RmaOpMem localMem[MAX_DESC_NUM] = {};
     121              : 
     122            2 :     if (descNum > MAX_DESC_NUM) {
     123            0 :         THROW<InternalException>(
     124            0 :             StringFormat("[TransportUrmaMem][BatchBufferSlice] Desc item[%u] is out of range.", descNum));
     125              :     }
     126              : 
     127            4 :     for (u32 i = 0; i < descNum; i++) {
     128            2 :         if (oneSideDescs[i].count == 0) {
     129            0 :             HCCL_WARNING("[TransportUrmaMem][BatchBufferSlice] Desc item[%u] count is 0.", i);
     130              :         }
     131            2 :         u32 unitSize{0};
     132            6 :         HCCL_INFO("[TransportUrmaMem][BatchBufferSlice] SalGetDataTypeSize start");
     133            2 :         if (SalGetDataTypeSize(oneSideDescs[i].dataType, unitSize) != HCCL_SUCCESS) {
     134            0 :             THROW<InternalException>(StringFormat("[TransportUrmaMem][BatchBufferSlice] Get dataType size failed!"));
     135              :         }
     136              : 
     137            2 :         u64 byteSize = oneSideDescs[i].count * unitSize;
     138            2 :         remoteMem[i] = {oneSideDescs[i].remoteAddr, byteSize};
     139            2 :         localMem[i] = {oneSideDescs[i].localAddr, byteSize};
     140            6 :         HCCL_INFO("[TransportUrmaMem][BatchBufferSlice] FillRmaBufferSlice start");
     141            2 :         CHK_RET(FillRmaBufferSlice(localMem[i], remoteMem[i], localRmaBufferSlice[i], remoteRmaBufferSlice[i]));
     142              :     }
     143            2 :     return HCCL_SUCCESS;
     144              : }
     145              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1