クラスint2_
、float2_
、およびdouble2_
を定義して、C++ および CUDA で複雑な算術演算を処理します。=
上記のクラスのオブジェクトとint
, float
and型の混合代入の演算子をオーバーロードしたいと考えてい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; }