LCOV - code coverage report
Current view: top level - base_comm/resources/endpoint_pairs/channels - builtin_channel_ops.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 77.4 % 106 82
Test Date: 2026-08-18 17:47:01 Functions: 84.2 % 19 16

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2026 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 BUILTIN_CHANNEL_OPS_H
      12              : #define BUILTIN_CHANNEL_OPS_H
      13              : 
      14              : #include "hcomm_nic_plugin.h"
      15              : #include "nic_plugin_manager.h"
      16              : #include "channel.h"
      17              : #include "channel_process.h"
      18              : #include "dtype_common.h"
      19              : // CreateBuiltinChannel 已调用 channelPtr->Init(),此处 no-op,此接口内置流程暂未调用。
      20            0 : inline int32_t BuiltinChannelInit(void* ctx)
      21              : {
      22              :     (void)ctx;
      23            0 :     return HCCL_SUCCESS;
      24              : }
      25              : 
      26              : // channel 生命周期由 g_ChannelMap 的 unique_ptr 管理,此处 no-op,此接口内置流程暂未调用。
      27            0 : inline int32_t BuiltinChannelDestroy(void* ctx)
      28              : {
      29              :     (void)ctx;
      30            0 :     return HCCL_SUCCESS;
      31              : }
      32              : 
      33            0 : inline int32_t BuiltinGetStatus(void* ctx, int32_t* status)
      34              : {
      35            0 :     auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
      36            0 :     CHK_PTR_NULL(channelPtr);
      37            0 :     auto channelStatus = channelPtr->GetStatus();
      38            0 :     switch (channelStatus) {
      39            0 :         case hcomm::ChannelStatus::FAILED:
      40            0 :             *status = hcomm::HCOMM_CHANNEL_STATUS_FAILED;
      41            0 :             break;
      42            0 :         case hcomm::ChannelStatus::SOCKET_TIMEOUT:
      43            0 :             *status = hcomm::HCOMM_CHANNEL_STATUS_TIMEOUT;
      44            0 :             break;
      45            0 :         case hcomm::ChannelStatus::READY:
      46            0 :             *status = hcomm::HCOMM_CHANNEL_STATUS_READY;
      47            0 :             break;
      48            0 :         default:
      49            0 :             *status = hcomm::HCOMM_CHANNEL_STATUS_CONNECTING;
      50            0 :             break;
      51              :     }
      52            0 :     return HCCL_SUCCESS;
      53              : }
      54              : 
      55              : template <typename Op>
      56           16 : inline int32_t BuiltinRwNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, Op&& op)
      57              : {
      58           16 :     HCCL_INFO(
      59              :         "[%s] START. thread[0x%llx], channel[0x%llx], dst[0x%llx], src[0x%llx], len[%llu].", __func__, thread, ctx, dst,
      60              :         src, len);
      61              : 
      62              :     (void)thread;
      63           16 :     CHK_PTR_NULL(src);
      64           14 :     CHK_PTR_NULL(dst);
      65              : 
      66           12 :     HcclResult ret = HCCL_SUCCESS;
      67              :     DevType devType;
      68           12 :     CHK_RET(hrtGetDeviceType(devType));
      69           12 :     if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960) {
      70           10 :         auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
      71           10 :         CHK_PTR_NULL(channelPtr);
      72           10 :         ret = op(channelPtr, dst, src, len);
      73           10 :     } else {
      74            2 :         ret = HCCL_E_NOT_SUPPORT;
      75              :     }
      76           12 :     CHK_PRT_RET(
      77              :         ret != HCCL_SUCCESS,
      78              :         HCCL_ERROR(
      79              :             "[%s] FAIL. thread[0x%llx], channel[0x%llx], dst[0x%llx], src[0x%llx], len[%llu].", __func__, thread, ctx,
      80              :             dst, src, len),
      81              :         ret);
      82            8 :     HCCL_INFO("[%s] SUCCESS.", __func__);
      83            8 :     return HCCL_SUCCESS;
      84              : }
      85              : 
      86            8 : inline int32_t BuiltinWriteNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len)
      87              : {
      88           16 :     return BuiltinRwNbiOnThread(ctx, thread, dst, src, len, [](hcomm::Channel* ch, void* d, const void* s, uint64_t l) {
      89            5 :         return ch->Write(d, s, l);
      90           16 :     });
      91              : }
      92              : 
      93            1 : inline int32_t BuiltinWriteNbi(void* ctx, void* dst, const void* src, uint64_t len)
      94              : {
      95            1 :     return BuiltinWriteNbiOnThread(ctx, 0, dst, src, len);
      96              : }
      97              : 
      98            8 : inline int32_t BuiltinWriteWithNotifyNbiOnThread(
      99              :     void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx)
     100              : {
     101            8 :     HCCL_INFO(
     102              :         "[%s] START. thread[0x%llx], channel[0x%llx], dst[0x%llx], src[0x%llx], len[%llu], remoteNotifyIdx[%u].",
     103              :         __func__, thread, ctx, dst, src, len, remoteNotifyIdx);
     104              : 
     105              :     (void)thread;
     106            8 :     CHK_PTR_NULL(src);
     107            7 :     CHK_PTR_NULL(dst);
     108            6 :     HcclResult ret = HCCL_SUCCESS;
     109              :     DevType devType;
     110            6 :     CHK_RET(hrtGetDeviceType(devType));
     111            6 :     if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960 || thread == 0) {
     112            5 :         auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
     113            5 :         CHK_PTR_NULL(channelPtr);
     114            5 :         ret = channelPtr->WriteWithNotify(dst, src, len, remoteNotifyIdx);
     115            5 :     } else {
     116            1 :         ret = HCCL_E_NOT_SUPPORT;
     117              :     }
     118            6 :     CHK_PRT_RET(
     119              :         ret != HCCL_SUCCESS,
     120              :         HCCL_ERROR(
     121              :             "[%s] FAIL. thread[0x%llx], channel[0x%llx], dst[0x%llx], src[0x%llx], len[%llu], remoteNotifyIdx[%u].",
     122              :             __func__, thread, ctx, dst, src, len, remoteNotifyIdx),
     123              :         ret);
     124            4 :     HCCL_INFO("[%s] SUCCESS.", __func__);
     125            4 :     return HCCL_SUCCESS;
     126              : }
     127              : 
     128            1 : inline int32_t BuiltinWriteWithNotifyNbi(void* ctx, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx)
     129              : {
     130            1 :     return BuiltinWriteWithNotifyNbiOnThread(ctx, 0, dst, src, len, remoteNotifyIdx);
     131              : }
     132              : 
     133              : // 供A5/A6接口BuiltinNotifyWait调用(原流程HcommChannelNotifyWait->HcommChannelNotifyWaitOnThread),A2/A3内置不走该接口
     134            1 : inline int32_t BuiltinNotifyWaitOnThread(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx, uint32_t timeOut)
     135              : {
     136              :     (void)thread;
     137            1 :     HCCL_INFO(
     138              :         "[%s] START. thread[0x%llx], channel[0x%llx], localNotifyIdx[%u], timeOut[%u].", __func__, thread, ctx,
     139              :         localNotifyIdx, timeOut);
     140            1 :     auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
     141            1 :     CHK_PTR_NULL(channelPtr);
     142            1 :     HcclResult ret = channelPtr->NotifyWait(localNotifyIdx, timeOut);
     143            1 :     CHK_PRT_RET(
     144              :         ret != HCCL_SUCCESS,
     145              :         HCCL_ERROR(
     146              :             "[%s] FAIL. thread[0x%llx], channel[0x%llx], localNotifyIdx[%u], timeOut[%u].", __func__, thread, ctx,
     147              :             localNotifyIdx, timeOut),
     148              :         ret);
     149            1 :     HCCL_INFO("[%s] SUCCESS.", __func__);
     150            1 :     return HCCL_SUCCESS;
     151              : }
     152              : 
     153            1 : inline int32_t BuiltinNotifyWait(void* ctx, uint32_t localNotifyIdx, uint32_t timeOut)
     154              : {
     155              :     DevType devType;
     156            1 :     CHK_RET(hrtGetDeviceType(devType));
     157            1 :     if (devType != DevType::DEV_TYPE_950 && devType != DevType::DEV_TYPE_960) {
     158            0 :         return HCCL_E_NOT_SUPPORT;
     159              :     }
     160            1 :     return BuiltinNotifyWaitOnThread(ctx, 0, localNotifyIdx, timeOut);
     161              : }
     162              : 
     163              : // 供A5/A6接口BuiltinNotifyRecord调用(原流程HcommChannelNotifyRecord->HcommChannelNotifyRecordOnThread),A2/A3内置不走该接口
     164            1 : inline int32_t BuiltinNotifyRecordOnThread(void* ctx, ThreadHandle thread, uint32_t remoteNotifyIdx)
     165              : {
     166              :     (void)thread;
     167            1 :     HCCL_INFO(
     168              :         "[%s] START. thread[0x%llx], channel[0x%llx], remoteNotifyIdx[%u].", __func__, thread, ctx, remoteNotifyIdx);
     169            1 :     auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
     170            1 :     CHK_PTR_NULL(channelPtr);
     171            1 :     HcclResult ret = channelPtr->NotifyRecord(remoteNotifyIdx);
     172            1 :     CHK_PRT_RET(
     173              :         ret != HCCL_SUCCESS,
     174              :         HCCL_ERROR(
     175              :             "[%s] FAIL. thread[0x%llx], channel[0x%llx], remoteNotifyIdx[%u].", __func__, thread, ctx, remoteNotifyIdx),
     176              :         ret);
     177            1 :     HCCL_INFO("[%s] SUCCESS.", __func__);
     178            1 :     return HCCL_SUCCESS;
     179              : }
     180              : 
     181            1 : inline int32_t BuiltinNotifyRecord(void* ctx, uint32_t remoteNotifyIdx)
     182              : {
     183              :     DevType devType;
     184            1 :     CHK_RET(hrtGetDeviceType(devType));
     185            1 :     if (devType != DevType::DEV_TYPE_950 && devType != DevType::DEV_TYPE_960) {
     186            0 :         return HCCL_E_NOT_SUPPORT;
     187              :     }
     188            1 :     return BuiltinNotifyRecordOnThread(ctx, 0, remoteNotifyIdx);
     189              : }
     190              : 
     191            8 : inline int32_t BuiltinReadNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len)
     192              : {
     193           16 :     return BuiltinRwNbiOnThread(ctx, thread, dst, src, len, [](hcomm::Channel* ch, void* d, const void* s, uint64_t l) {
     194            5 :         return ch->Read(d, s, l);
     195           16 :     });
     196              : }
     197              : 
     198            1 : inline int32_t BuiltinReadNbi(void* ctx, void* dst, const void* src, uint64_t len)
     199              : {
     200            1 :     return BuiltinReadNbiOnThread(ctx, 0, dst, src, len);
     201              : }
     202              : 
     203            6 : inline int32_t BuiltinFenceOnThread(void* ctx, ThreadHandle thread)
     204              : {
     205            6 :     HCCL_INFO("[%s] START. thread[0x%llx], channel[0x%llx].", __func__, thread, ctx);
     206              : 
     207              :     (void)thread;
     208            6 :     HcclResult ret = HCCL_SUCCESS;
     209              :     DevType devType;
     210            6 :     CHK_RET(hrtGetDeviceType(devType));
     211            6 :     if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960 || thread == 0) {
     212            5 :         auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
     213            5 :         CHK_PTR_NULL(channelPtr);
     214            5 :         ret = channelPtr->ChannelFence();
     215            5 :     } else {
     216            1 :         ret = HCCL_E_NOT_SUPPORT;
     217              :     }
     218            6 :     CHK_PRT_RET(
     219              :         ret != HCCL_SUCCESS, HCCL_ERROR("[%s] FAIL. thread[0x%llx], channel[0x%llx].", __func__, thread, ctx), ret);
     220            4 :     HCCL_INFO("[%s] SUCCESS.", __func__);
     221            4 :     return HCCL_SUCCESS;
     222              : }
     223              : 
     224            1 : inline int32_t BuiltinFence(void* ctx) { return BuiltinFenceOnThread(ctx, 0); }
     225              : 
     226              : // 以下接口内置不支持,复用 nic_plugin_manager.cc 中的 DefaultChannel* 默认实现
     227              : 
     228              : inline HcommNicChannelOps g_BuiltinChannelOps = {
     229              :     {HCOMM_NIC_CHANNEL_OPS_VERSION, HCOMM_NIC_CHANNEL_OPS_MAGIC_WORD, sizeof(HcommNicChannelOps), 0},
     230              :     BuiltinChannelInit,                                        // init
     231              :     BuiltinChannelDestroy,                                     // destroy
     232              :     BuiltinGetStatus,                                          // getStatus
     233              :     BuiltinWriteNbi,                                           // writeNbi
     234              :     BuiltinWriteNbiOnThread,                                   // writeNbiOnThread
     235              :     hcomm::DefaultChannelWriteOnThread,                        // writeOnThread
     236              :     BuiltinWriteWithNotifyNbi,                                 // writeWithNotifyNbi
     237              :     BuiltinWriteWithNotifyNbiOnThread,                         // writeWithNotifyNbiOnThread
     238              :     hcomm::DefaultChannelWriteWithNotifyOnThread,              // writeWithNotifyOnThread
     239              :     hcomm::DefaultChannelWriteReduceOnThread,                  // writeReduceOnThread
     240              :     hcomm::DefaultChannelWriteReduceWithNotifyOnThread,        // writeReduceWithNotifyOnThread
     241              :     BuiltinReadNbi,                                            // readNbi
     242              :     BuiltinReadNbiOnThread,                                    // readNbiOnThread
     243              :     hcomm::DefaultChannelReadOnThread,                         // readOnThread
     244              :     hcomm::DefaultChannelReadReduceOnThread,                   // readReduceOnThread
     245              :     BuiltinNotifyRecord,                                       // notifyRecord
     246              :     BuiltinNotifyRecordOnThread,                               // notifyRecordOnThread
     247              :     BuiltinNotifyWait,                                         // notifyWait
     248              :     BuiltinNotifyWaitOnThread,                                 // notifyWaitOnThread
     249              :     hcomm::DefaultChannelNotifyWaitOnThreadWithDefaultTimeout, // notifyWaitOnThreadWithDefaultTimeout
     250              :     hcomm::DefaultChannelBatchTransferOnThread,                // batchTransferOnThread
     251              :     BuiltinFence,                                              // fence
     252              :     BuiltinFenceOnThread,                                      // fenceOnThread
     253              :     hcomm::DefaultChannelDrainOnThread,                        // drainOnThread
     254              : };
     255              : 
     256              : #endif // BUILTIN_CHANNEL_OPS_H
        

Generated by: LCOV version 2.0-1