c++complex-numbersarrayfire

Arrayfire array from complex host data


I'm having a problem initializing an arrayfire array from host data.

The following code will not link for me:

constexp int mNumEigenInfos = 100;
std::complex<float> mEigenVectors[mNumEigenInfos][6];
af::array mEigenVectorsArray = af::array((dim_t)6,(dim_t)(mNumEigenInfos),reinterpret_cast<float2*>(mEigenVectors));

Giving me an error of:

undefined reference to `af::array::array<float2>(long long, long long, float2 const*, af_source)'

Now if i change the reinterpret_cast from float2* to float*:

constexp int mNumEigenInfos = 100;
std::complex<float> mEigenVectors[mNumEigenInfos][6];
af::array mEigenVectorsArray = af::array((dim_t)6,(dim_t)(mNumEigenInfos),reinterpret_cast<float*>(mEigenVectors));

It links fine. From reading online, i thought i was supposed to treat complex data as a cuComplex (which casting to float2 or cuComplex gives the exact same error since they are the same thing).

I feel like i'm making a stupid mistake here but can't seem to figure it out.

How am i supposed to initialize an arrayfire array from std::complex host data?

Thank you for your Help


Solution

  • This is kind of similar to the question af::array::device doesn't work with complex arrays please have a look - the only difference is the direction in which complex data is passed.

    The following should work fine as std::complex should be ABI compatible with af::cfloat.

    af::array mEigenVectorsArray =
        af::array((dim_t)6, (dim_t)(mNumEigenInfos),
                  reinterpret_cast<af::cfloat>(mEigenVectors));
    

    ArrayFire API won't have a symbol for function with float2 type, however it will have the symbol for function with type af::cfloat. Even cuFloatComplext or float2 should also be ABI compatible with af::cfloat I think. So, reinterpret-cast to af::cfloat is what should be done.