0

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.

Mikhail Genkin
  • 3,247
  • 4
  • 27
  • 47
  • 1
    `thrust::complex` probably has much of what you need, and all of it can be used in ordinary CUDA C/C++ code. You can also take a look in `cuComplex.h` (in `/usr/local/cuda/include`) for some definitions that may be useful. – Robert Crovella Sep 16 '16 at 21:23
  • 1
    A stand-alone function (not a method) `Double2 operator+ (const Double2& left, const Double2 &right);` should do the trick but I don't have time to test it now. If someone want's to follow thru (and it works) feel free to take credit (grin). Consider the techniques to define operator << for output streams and arbitrary types. This is the same problem. – Dale Wilson Sep 16 '16 at 21:25
  • 1
    I would recommend using `thrust::complex`, but [here](http://pastebin.com/xxP94mES) is a worked example of extending the structs/definitions in `cuComplex.h` to add the `+` operator. – Robert Crovella Sep 16 '16 at 21:34
  • Since I don't need any Thurst reduction algorithms, I think it's not worth to use it – Mikhail Genkin Sep 16 '16 at 21:36
  • 1
    You don't have to use algorithms or any other part of thrust in order to make use of `thrust::complex` in your CUDA C/C++ code. [Here](http://pastebin.com/dhMice4c) is my worked example from above reworked to use `thrust::complex`. – Robert Crovella Sep 16 '16 at 21:44
  • Thanks, your link doesn't open from my lab wi-fi. I'll take a look at home. – Mikhail Genkin Sep 16 '16 at 22:03

0 Answers0