【发布时间】:2013-06-15 19:51:36
【问题描述】:
我正在使用 CUDA,我创建了一个 int2_ 类来处理复整数。
ComplexTypes.h文件中的类声明如下:
namespace LibraryNameSpace
{
class int2_ {
public:
int x;
int y;
// Constructors
__host__ __device__ int2_(const int,const int);
__host__ __device__ int2_();
// etc.
// Equalities with other types
__host__ __device__ const int2_& operator=(const int);
__host__ __device__ const int2_& operator=(const float);
// etc.
};
}
ComplexTypes.cpp文件中的类实现如下:
#include "ComplexTypes.h"
__host__ __device__ LibraryNameSpace::int2_::int2_(const int x_,const int y_) { x=x_; y=y_;}
__host__ __device__ LibraryNameSpace::int2_::int2_() {}
// etc.
__host__ __device__ const LibraryNameSpace::int2_& LibraryNameSpace::int2_::operator=(const int a) { x = a; y = 0.; return *this; }
__host__ __device__ const LibraryNameSpace::int2_& LibraryNameSpace::int2_::operator=(const float a) { x = (int)a; y = 0.; return *this; }
// etc.
一切正常。在main(包括ComplexTypes.h)中,我可以处理int2_ 数字。
在CudaMatrix.cu 文件中,我现在包含ComplexTypes.h 并定义和正确实例化__global__ 函数:
template <class T1, class T2>
__global__ void evaluation_matrix(T1* data_, T2* ob, int NumElements)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if(i < NumElements) data_[i] = ob[i];
}
template __global__ void evaluation_matrix(LibraryNameSpace::int2_*,int*,int);
CudaMatrix.cu 文件的情况似乎与main 函数对称。然而,编译器抱怨:
Error 19 error : Unresolved extern function '_ZN16LibraryNameSpace5int2_aSEi' C:\Users\Documents\Project\Test\Testing_Files\ptxas simpleTest
请考虑:
- 在将实现移至单独的文件之前,在
main文件中包含声明和实现时,一切正常。 - 有问题的指令是
data_[i] = ob[i]。
有人知道发生了什么吗?
【问题讨论】:
-
大概你没有
ComplexTypes.cpp文件,而是你传递给nvcc 的ComplexTypes.cu文件,否则__host__ __device__不应该编译... -
我找到了解决问题的方法。我已将其发布为答案,希望它对其他用户有用。
标签: c++ class cuda unresolved-external