在动态加载的库中定义的结构

Structure defined in a dynamically loaded library

我正在动态加载 cudart(Cuda 运行 时间库)以仅访问 cudaGetDeviceProperties 函数。这个需要两个参数:

我不包括 cuda_runtime.h header 是为了避免获得我不想使用的额外常量、宏、枚举、class...。

但是,我需要 cudaDeviceProp 结构。有没有办法在不重新定义它的情况下获得它?我写了下面的代码:

struct cudaDeviceProp;

class CudaRTGPUInfoDL
{   
    typedef int(*CudaDriverVersion)(int*);
    typedef int(*CudaRunTimeVersion)(int*);
    typedef int(*CudaDeviceProperties)(cudaDeviceProp*,int);

public:
    struct Properties
    {
        char   name[256];                           /**< ASCII string identifying device */
        size_t totalGlobalMem;                      /**< Global memory available on device in bytes */
        size_t sharedMemPerBlock;                   /**< Shared memory available per block in bytes */
        int    regsPerBlock;                        /**< 32-bit registers available per block */
        int    warpSize;                            /**< Warp size in threads */
        size_t memPitch;                            /**< Maximum pitch in bytes allowed by memory copies */
        /*... Tons of members follow..*/
    };

public:
CudaRTGPUInfoDL();
~CudaRTGPUInfoDL();

int getCudaDriverVersion();
int getCudaRunTimeVersion();
const Properties& getCudaDeviceProperties();

private:
    QLibrary                library;

private:
    CudaDriverVersion       cuDriverVer;
    CudaRunTimeVersion      cuRTVer;
    CudaDeviceProperties    cuDeviceProp;

    Properties              properties;
};

如大家所见,我只是"copy-pasted"结构体的声明

为了获取GPU属性,我简单地使用了这个方法:

const CudaRTGPUInfoDL::Properties& CudaRTGPUInfoDL::getCudaDeviceProperties()
{
    // Unsafe but needed.
    cuDeviceProp(reinterpret_cast<cudaDeviceProp*>(&properties), 0);
    return properties;
}

感谢您的回答。

如果您需要完整的结构,您应该定义它(可能通过包含适当的 header)。

如果您只是要传递引用或指针,例如在您展示的方法中,那么它不需要是完整的,只需向前声明即可:

class cudaDeviceProp;