I am working with CUDA and C++. On the Linux server where I run my GPU programs some old compilers are installed. I am solving cumbersome differential equations with complex unknowns. In CUDA, there is a structure double2
with members x
and y
, and operator =
is determined, but there are no operators +
,*
, -
etc.
Can I define those operators (both for complex-complex and complex-double arithmetics)? I know how to create my own class with operators, but I don't know how to modify operators for existing class (or structure).
Or it is worth to create my own class? So far I created it like this:
class CComplex{
public:
double x,y;
CComplex() {}
CComplex(double a, double b) : x(a), y(b) {}
};
CComplex operator+ (const CComplex& lhs, const CComplex& rhs){
CComplex temp;
temp.x = lhs.x + rhs.x;
temp.y = lhs.y + rhs.y;
return temp;
}
CComplex operator+ (const CComplex& lhs, const double& rhs){
CComplex temp;
temp.x = lhs.x + rhs;
temp.y = lhs.y;
return temp;
}
CComplex operator* (const CComplex& lhs, const CComplex& rhs){
CComplex temp;
temp.x = lhs.x * rhs.x-lhs.y*rhs.y;
temp.y = lhs.x*rhs.y+lhs.y*rhs.x;
return temp;
}
CComplex operator* (const CComplex& lhs, const double& rhs){
CComplex temp;
temp.x = lhs.x * rhs;
temp.y = lhs.y*rhs;
return temp;
}
CComplex operator- (const CComplex& lhs, const CComplex& rhs){
CComplex temp;
temp.x = lhs.x - rhs.x;
temp.y = lhs.y - rhs.y;
return temp;
}
CComplex operator- (const CComplex& lhs, const double& rhs){
CComplex temp;
temp.x = lhs.x - rhs;
temp.y = lhs.y;
return temp;
}
Not sure what is better and how fast CUDA is working with custom-created classes.