LCOV - code coverage report
Current view: top level - base_comm/primitives/api_c_adpt/nic_plugin - nic_plugin_manager.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 6 6
Test Date: 2026-08-29 17:38:31 Functions: 100.0 % 4 4

            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 HCOMM_NIC_PLUGIN_MANAGER_H
      12              : #define HCOMM_NIC_PLUGIN_MANAGER_H
      13              : 
      14              : #include <stddef.h>
      15              : #include <stdint.h>
      16              : 
      17              : #include "hcomm_c_adpt.h"
      18              : #include "hcomm_nic_plugin.h"
      19              : 
      20              : namespace hcomm {
      21              : 
      22              : struct NicPluginEntry {
      23              :     void* soHandle;
      24              :     const HcommNicPluginInfo* info;
      25              :     HcommNicPluginCreateEndpointFunc createEndpoint;
      26              :     HcommNicPluginCreateChannelFunc createChannel;
      27              : };
      28              : 
      29              : constexpr uintptr_t HCOMM_PLUGIN_HANDLE_FLAG = (static_cast<uintptr_t>(1) << 63);
      30              : constexpr int32_t COMM_PROTOCOL_CUSTOM_BASE = 1000;
      31              : 
      32              : class Channel;
      33              : 
      34            2 : inline ChannelHandle MakePluginChHandle(ChannelHandle h)
      35              : {
      36            2 :     return static_cast<ChannelHandle>(static_cast<uintptr_t>(h) | HCOMM_PLUGIN_HANDLE_FLAG);
      37              : }
      38              : 
      39           82 : inline ::hcomm::Channel* ChannelFromHandle(ChannelHandle h)
      40              : {
      41              :     return reinterpret_cast<::hcomm::Channel*>(
      42           82 :         static_cast<uintptr_t>(h) & ~static_cast<uintptr_t>(HCOMM_PLUGIN_HANDLE_FLAG));
      43              : }
      44              : 
      45              : #define IS_PLUGIN_HANDLE(h) ((((static_cast<uintptr_t>(h))) & ::hcomm::HCOMM_PLUGIN_HANDLE_FLAG) != 0)
      46              : #define MAKE_PLUGIN_CH_HANDLE(p) ::hcomm::MakePluginChHandle(p)
      47              : #define CHANNEL_FROM_HANDLE(h) ::hcomm::ChannelFromHandle(h)
      48              : 
      49              : void LoadAllNicPlugins();
      50              : const NicPluginEntry* FindHostNicPlugin(CommProtocol protocol);
      51              : bool ValidatePluginInfo(
      52              :     const char* soPath, const HcommNicPluginInfo* info, HcommNicPluginCreateEndpointFunc createEndpoint,
      53              :     HcommNicPluginCreateChannelFunc createChannel);
      54              : 
      55              : HcommResult FillDefaultEndpointOps(const HcommNicEndpointOps* src, HcommNicEndpointOps** outOps);
      56              : 
      57              : int32_t DefaultEndpointInit(void* ctx);
      58              : int32_t DefaultEndpointRegisterMemory(void* ctx, const CommMem* mem, const char* tag, void** handle);
      59              : int32_t DefaultEndpointUnregisterMemory(void* ctx, void* handle);
      60              : int32_t DefaultEndpointMemoryExport(void* ctx, void* handle, void** desc, uint32_t* descLen);
      61              : int32_t DefaultEndpointMemoryImport(void* ctx, const void* desc, uint32_t descLen, CommMem* outMem);
      62              : int32_t DefaultEndpointMemoryUnimport(void* ctx, const void* desc, uint32_t descLen);
      63              : int32_t DefaultEndpointGetListenPort(void* ctx, uint32_t* port);
      64              : 
      65              : // HcommNicEndpointOps 新增成员时,在此追加一行 F(op字段名, 默认实现函数) 即可,destroy要求插件必须实现
      66              : #define FOR_EACH_ENDPOINT_OP_DEFAULT(F)                  \
      67              :     F(init, DefaultEndpointInit)                         \
      68              :     F(registerMemory, DefaultEndpointRegisterMemory)     \
      69              :     F(unregisterMemory, DefaultEndpointUnregisterMemory) \
      70              :     F(memoryExport, DefaultEndpointMemoryExport)         \
      71              :     F(memoryImport, DefaultEndpointMemoryImport)         \
      72              :     F(memoryUnimport, DefaultEndpointMemoryUnimport)     \
      73              :     F(getListenPort, DefaultEndpointGetListenPort)
      74              : 
      75              : // 供 FOR_EACH_ENDPOINT_OP_DEFAULT 使用的填充器
      76              : #define FILL_ENDPOINT_OP_DEFAULT(op, defaultFunc)                                               \
      77              :     if (!IsPluginOpAvailable(dst, offsetof(HcommNicEndpointOps, op), sizeof(decltype(dst->op))) \
      78              :         || dst->op == nullptr) {                                                                \
      79              :         dst->op = defaultFunc;                                                                  \
      80              :     }
      81              : 
      82              : HcommResult FillDefaultChannelOps(const HcommNicChannelOps* src, HcommNicChannelOps** outOps);
      83              : 
      84              : int32_t DefaultChannelInit(void* ctx);
      85              : int32_t DefaultChannelGetStatus(void* ctx, int32_t* status);
      86              : int32_t DefaultChannelWriteNbi(void* ctx, void* dst, const void* src, uint64_t len);
      87              : int32_t DefaultChannelWriteNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
      88              : int32_t DefaultChannelWriteOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
      89              : int32_t DefaultChannelWriteWithNotifyNbi(void* ctx, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
      90              : int32_t DefaultChannelWriteWithNotifyNbiOnThread(
      91              :     void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
      92              : int32_t DefaultChannelWriteWithNotifyOnThread(
      93              :     void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
      94              : int32_t DefaultChannelWriteReduceOnThread(
      95              :     void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
      96              :     HcommReduceOp reduceOp);
      97              : int32_t DefaultChannelWriteReduceWithNotifyOnThread(
      98              :     void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
      99              :     HcommReduceOp reduceOp, uint32_t remoteNotifyIdx);
     100              : int32_t DefaultChannelReadNbi(void* ctx, void* dst, const void* src, uint64_t len);
     101              : int32_t DefaultChannelReadNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
     102              : int32_t DefaultChannelReadOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
     103              : int32_t DefaultChannelReadReduceOnThread(
     104              :     void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
     105              :     HcommReduceOp reduceOp);
     106              : int32_t DefaultChannelNotifyRecord(void* ctx, uint32_t remoteNotifyIdx);
     107              : int32_t DefaultChannelNotifyRecordOnThread(void* ctx, ThreadHandle thread, uint32_t remoteNotifyIdx);
     108              : int32_t DefaultChannelNotifyWait(void* ctx, uint32_t localNotifyIdx, uint32_t timeOut);
     109              : int32_t DefaultChannelNotifyWaitOnThread(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx, uint32_t timeOut);
     110              : int32_t DefaultChannelNotifyWaitOnThreadWithDefaultTimeout(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx);
     111              : int32_t DefaultChannelBatchTransferOnThread(
     112              :     void* ctx, ThreadHandle thread, const HcommBatchTransferDesc* transferDescs, uint32_t transferDescNum);
     113              : int32_t DefaultChannelFence(void* ctx);
     114              : int32_t DefaultChannelFenceOnThread(void* ctx, ThreadHandle thread);
     115              : int32_t DefaultChannelDrainOnThread(void* ctx, ThreadHandle thread);
     116              : 
     117              : // HcommNicChannelOps 新增成员时,在此追加一行 F(op字段名, 默认实现函数) 即可,destroy要求插件必须实现
     118              : #define FOR_EACH_CHANNEL_OP_DEFAULT(F)                                                          \
     119              :     F(init, DefaultChannelInit)                                                                 \
     120              :     F(getStatus, DefaultChannelGetStatus)                                                       \
     121              :     F(writeNbi, DefaultChannelWriteNbi)                                                         \
     122              :     F(writeNbiOnThread, DefaultChannelWriteNbiOnThread)                                         \
     123              :     F(writeOnThread, DefaultChannelWriteOnThread)                                               \
     124              :     F(writeWithNotifyNbi, DefaultChannelWriteWithNotifyNbi)                                     \
     125              :     F(writeWithNotifyNbiOnThread, DefaultChannelWriteWithNotifyNbiOnThread)                     \
     126              :     F(writeWithNotifyOnThread, DefaultChannelWriteWithNotifyOnThread)                           \
     127              :     F(writeReduceOnThread, DefaultChannelWriteReduceOnThread)                                   \
     128              :     F(writeReduceWithNotifyOnThread, DefaultChannelWriteReduceWithNotifyOnThread)               \
     129              :     F(readNbi, DefaultChannelReadNbi)                                                           \
     130              :     F(readNbiOnThread, DefaultChannelReadNbiOnThread)                                           \
     131              :     F(readOnThread, DefaultChannelReadOnThread)                                                 \
     132              :     F(readReduceOnThread, DefaultChannelReadReduceOnThread)                                     \
     133              :     F(notifyRecord, DefaultChannelNotifyRecord)                                                 \
     134              :     F(notifyRecordOnThread, DefaultChannelNotifyRecordOnThread)                                 \
     135              :     F(notifyWait, DefaultChannelNotifyWait)                                                     \
     136              :     F(notifyWaitOnThread, DefaultChannelNotifyWaitOnThread)                                     \
     137              :     F(notifyWaitOnThreadWithDefaultTimeout, DefaultChannelNotifyWaitOnThreadWithDefaultTimeout) \
     138              :     F(batchTransferOnThread, DefaultChannelBatchTransferOnThread)                               \
     139              :     F(fence, DefaultChannelFence)                                                               \
     140              :     F(fenceOnThread, DefaultChannelFenceOnThread)                                               \
     141              :     F(drainOnThread, DefaultChannelDrainOnThread)
     142              : 
     143              : // 供 FOR_EACH_CHANNEL_OP_DEFAULT 使用的填充器
     144              : #define FILL_CHANNEL_OP_DEFAULT(op, defaultFunc)                                               \
     145              :     if (!IsPluginOpAvailable(dst, offsetof(HcommNicChannelOps, op), sizeof(decltype(dst->op))) \
     146              :         || dst->op == nullptr) {                                                               \
     147              :         dst->op = defaultFunc;                                                                 \
     148              :     }
     149              : 
     150              : bool ValidateEndpointOps(const HcommNicEndpointOps* ops);
     151              : bool ValidateChannelOps(const HcommNicChannelOps* ops);
     152              : 
     153              : template <typename PluginOps>
     154           80 : bool IsPluginOpAvailable(const PluginOps* ops, size_t opOffset, size_t opSize)
     155              : {
     156           80 :     return ops != nullptr && ops->header.size >= opOffset + opSize;
     157              : }
     158              : 
     159              : template <typename PluginOps>
     160              : void DestroyPluginCtx(PluginOps* ops, void* pluginCtx)
     161              : {
     162              :     if (ops != nullptr && IsPluginOpAvailable(ops, offsetof(PluginOps, destroy), sizeof(ops->destroy))
     163              :         && ops->destroy != nullptr) {
     164              :         int32_t ret = ops->destroy(pluginCtx);
     165              :         if (ret != HCCL_SUCCESS) {
     166              :             HCCL_WARNING("[%s] plugin destroy failed, ret[%d].", __func__, ret);
     167              :         }
     168              :     }
     169              : }
     170              : 
     171              : } // namespace hcomm
     172              : 
     173              : #endif // HCOMM_NIC_PLUGIN_MANAGER_H
        

Generated by: LCOV version 2.0-1