1

I am trying to copy complex number array from host to device using ArrayFire framework:

std::complex<float> hostArray[131072];
array deviceArray (131072, hostArray); 

But it gives compilation error due to data type incompatibilities. What am I doing wrong?

I can copy the real and imaginary parts separately into device in order to create complex numbers inside gpu memory but it is costly and I also dont't know how to construct complex number from two numbers in ArrayFire framework.

I would be grateful if someone can help me with ArrayFire framework in this matter.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
uahakan
  • 576
  • 1
  • 6
  • 23

1 Answers1

3

ArrayFire uses cuComplex(cfloat in ArrayFire 2.0RC) to store complex numbers. cuComplex is defined as a float2 internally which is a struct with two elements.

std::complex should have the same structure. You might be able to perform a reinterpret_cast to change the type of the variable without moving the data to a different data structure. On my machine(Linux Mint with g++ 4.7.1) I was able to create an ArrayFire array from a std::complex using the following code:

int count = 10;
std::complex<float> host_complex[count];
for(int i = 0; i < count; i++) {
    std::real(host_complex[i]) = i;
    std::imag(host_complex[i]) = i*2;
}
array af_complex(count, reinterpret_cast<cuComplex*>(host_complex));
print(af_complex);

Output:

af_complex =
       0.0000 +       0.0000i
       1.0000 +       2.0000i
       2.0000 +       4.0000i
       3.0000 +       6.0000i
       4.0000 +       8.0000i
       5.0000 +      10.0000i
       6.0000 +      12.0000i
       7.0000 +      14.0000i
       8.0000 +      16.0000i
       9.0000 +      18.0000i

Caveat

As far as I can tell the C++ standard does not specify the size or data layout of the std::complex type therefore this approach might not be portable. If you want a portable solution, I would suggest storing your complex data in a struct like a float2/cfloat to avoid compiler related issues.

Umar

Umar Arshad
  • 970
  • 1
  • 9
  • 22