0

我正在定义类int2_,float2_double2_来处理 C++ 和 CUDA 中的复杂算术。我想重载运算符=以混合分配上述类和int,floatdouble类型的对象。

我的实现如下:

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,floatdouble类型的表达式时,我什至没有收到错误消息。唯一的问题出现在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; }
4

1 回答 1

2

好吧,错误说你不能从一个函数调用__host__函数(在这种情况下是你的float2_类的构造__global__函数)。乍一看,这与操作符无关,因为错误消息中没有提到它们。但如果你仔细观察,有data_[i] = e[i]

注意:这里是疯狂的猜测,因为您没有显示所有相关代码:

我猜想e[i]引用表达式的一部分,在这种情况下是 type float2_。您正在将其分配e[i]给 a double2_,并且相应的赋值运算符是您的double2_::__host__ __device__ inline const double2_& operator=(const float2_ a)which - 除了不必要和非常规地返回一个 const 引用之外,它float2_ 按值获取,因此编译器必须e[i]通过复制构造函数进行复制,该复制构造函数似乎是用__host__. 显然,从编译器消息来看,不允许__host____global__函数调用。

一个解决方案是要么声明构造函数,要么__global__op=(const) 引用获取其参数,因此不需要调用复制构造函数。但是,由于它operator=本身是声明__host__的,因此您可能也会因为该调用而得到相同的错误。

__host__我对cuda一无所知,也不知道__global__比错误消息告诉我的更多信息,但希望我能给你一个提示,代码可能有什么问题。

于 2013-04-15T09:43:02.160 回答