我正在定义类int2_
,float2_
和double2_
来处理 C++ 和 CUDA 中的复杂算术。我想重载运算符=
以混合分配上述类和int
,float
和double
类型的对象。
我的实现如下:
class float2_;
class double2_;
class int2_ {
public:
int x;
int y;
int2_() : x(), y() {}
__host__ __device__ inline const int2_& operator=(const int a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const float a) { x = (int)a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const double a) { x = (int)a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const int2_ a) { x = a.x; y = a.y; return *this; }
__host__ __device__ inline const int2_& operator=(const float2_ a);
__host__ __device__ inline const int2_& operator=(const double2_ a);
};
class float2_ {
public:
float x;
float y;
float2_() : x(), y() {}
__host__ __device__ inline const float2_& operator=(const int a) { x = (float)a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const float a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const double a) { x = (float)a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const int2_ a) { x = (float)a.x; y = (float)a.y; return *this; }
__host__ __device__ inline const float2_& operator=(const float2_ a) { x = a.x; y = a.y; return *this; }
__host__ __device__ inline const float2_& operator=(const double2_ a);
};
class double2_ {
public:
double x;
double y;
double2_() : x(), y() {}
__host__ __device__ inline const double2_& operator=(const int a) { x = (double)a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const float a) { x = (double)a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const double a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const int2_ a) { x = (double)a.x; y = (double)a.y;return *this; }
__host__ __device__ inline const double2_& operator=(const float2_ a) { x = (double)a.x; y = (double)a.y;return *this; }
__host__ __device__ inline const double2_& operator=(const double2_ a) { x = a.x; y = a.y; return *this; }
};
__host__ __device__ inline const int2_& int2_::operator=(const float2_ a) { x = (int)a.x; y = (int)a.y; return *this; }
__host__ __device__ inline const int2_& int2_::operator=(const double2_ a) { x = (int)a.x; y = (int)a.y; return *this; }
__host__ __device__ inline const float2_& float2_::operator=(const double2_ a) { x = (float)a.x; y = (float)a.y; return *this; }
但是,我在内核中收到编译错误
template <class A, class T1, class T2>
__global__ inline void evaluation_matrix(T1 *data_, const Expr<A,T2> e, int NumElements)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if(i < NumElements) data_[i] = e[i];
}
whene
是一个表达式。错误信息是
calling a __host__ function("float2_::float2_") from a __global__
function("evaluation_matrix<BinExpr<const float *, const float2_ *, CudaOpSum, float2_>
, double2_, float2_> ") is not allowed
在这种情况下,data_
是一个double2_
对象并且e
是一个float2_
表达式。
我在处理任何int
, float
, double
, int2_
,float2_
或double2_
类型或类时都没有问题data_
。e
当是int
,float
或double
类型的表达式时,我什至没有收到错误消息。唯一的问题出现在e
是 类int2_
或float2_
时double2_
。
有什么帮助吗?谢谢你。
ARNE MERTZ 回答后的工作解决方案
class float2_;
class double2_;
class int2_ {
public:
int x;
int y;
__host__ __device__ int2_() : x(), y() {}
__host__ __device__ inline const int2_& operator=(const int a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const float a) { x = (int)a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const double a) { x = (int)a; y = 0.; return *this; }
__host__ __device__ inline const int2_& operator=(const int2_ a) { x = a.x; y = a.y; return *this; }
__host__ __device__ inline const int2_& operator=(const float2_ a);
__host__ __device__ inline const int2_& operator=(const double2_ a);
};
class float2_ {
public:
float x;
float y;
__host__ __device__ float2_() : x(), y() {}
__host__ __device__ inline const float2_& operator=(const int a) { x = (float)a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const float a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const double a) { x = (float)a; y = 0.; return *this; }
__host__ __device__ inline const float2_& operator=(const int2_ a) { x = (float)a.x; y = (float)a.y; return *this; }
__host__ __device__ inline const float2_& operator=(const float2_ a) { x = a.x; y = a.y; return *this; }
__host__ __device__ inline const float2_& operator=(const double2_ a);
};
class double2_ {
public:
double x;
double y;
__host__ __device__ double2_() : x(), y() {}
__host__ __device__ inline const double2_& operator=(const int a) { x = (double)a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const float a) { x = (double)a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const double a) { x = a; y = 0.; return *this; }
__host__ __device__ inline const double2_& operator=(const int2_ a) { x = (double)a.x; y = (double)a.y;return *this; }
__host__ __device__ inline const double2_& operator=(const float2_ a) { x = (double)a.x; y = (double)a.y;return *this; }
__host__ __device__ inline const double2_& operator=(const double2_ a) { x = a.x; y = a.y; return *this; }
};
__host__ __device__ inline const int2_& int2_::operator=(const float2_ a) { x = (int)a.x; y = (int)a.y; return *this; }
__host__ __device__ inline const int2_& int2_::operator=(const double2_ a) { x = (int)a.x; y = (int)a.y; return *this; }
__host__ __device__ inline const float2_& float2_::operator=(const double2_ a) { x = (float)a.x; y = (float)a.y; return *this; }