C++11 在动态加载的库中定义的结构
我正在动态加载cudart(Cuda运行时库),以便仅访问C++11 在动态加载的库中定义的结构,c++11,dynamic-linking,C++11,Dynamic Linking,我正在动态加载cudart(Cuda运行时库),以便仅访问cudaGetDeviceProperties函数。这需要两个参数: 在运行时库的头中定义的cudaDeviceProp结构 表示设备ID的整数 我没有包括cuda_runtime.h头,以便不获取额外的常量、宏、枚举、类。。。我不想用的 但是,我需要cudaDeviceProp结构。有没有一种方法可以不重新定义就得到它?我编写了以下代码: struct cudaDeviceProp; class CudaRTGPUInfoDL {
cudaGetDeviceProperties
函数。这需要两个参数:
- 在运行时库的头中定义的
结构李>cudaDeviceProp
- 表示设备ID的整数
cuda_runtime.h
头,以便不获取额外的常量、宏、枚举、类。。。我不想用的
但是,我需要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;
};
struct-cudaDeviceProp;
类CudaRTGPUInfoDL
{
typedef int(*CudaDriverVersion)(int*);
typedef int(*CudaRunTimeVersion)(int*);
类型定义int(*cudaDeviceProp属性)(cudaDeviceProp*,int);
公众:
结构属性
{
字符名[256];/**
正如大家所看到的,我只是简单地“复制粘贴”了结构的声明
为了获得GPU属性,我只需使用以下方法:
const CudaRTGPUInfoDL::Properties& CudaRTGPUInfoDL::getCudaDeviceProperties()
{
// Unsafe but needed.
cuDeviceProp(reinterpret_cast<cudaDeviceProp*>(&properties), 0);
return properties;
}
const CudaRTGPUInfoDL::Properties&CudaRTGPUInfoDL::getCudaDeviceProperties()
{
//不安全但需要。
cuDeviceProp(重新解释铸件和属性),0;
归还财产;
}
谢谢你的回答。如果你需要完整的结构,你应该定义它(可能包括适当的标题) 如果只是传递引用或指针,例如在所显示的方法中,则不需要完整,只需向前声明即可:
class cudaDeviceProp;
如果您需要完整的结构,您应该定义它(可能包括适当的标题) 如果只是传递引用或指针,例如在所显示的方法中,则不需要完整,只需向前声明即可:
class cudaDeviceProp;
不幸的是,我需要它是完整的。我认为有一种神奇的方法可以做到不包含标题。不幸的是,我需要它是完整的。我认为有一种神奇的方法可以在不包含标题的情况下实现这一点是未定义的行为,但可能在当前编译器中工作。下一次编译器更新?谁知道呢,我会找到另一条路的。我将包括所需的标题,不必像Toby建议的那样费心处理。请注意,按照您在此处生成的指针
cuDeviceProp(reinterpret_cast(&properties),0)
是未定义的行为,但可能在当前编译器中工作。下一次编译器更新?谁知道呢,我会找到另一条路的。我将包括所需的标题,不麻烦与托比建议。