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

Generated by: LCOV version 2.0-1