Is there a ready-made function in CUDA used for complex array's magnitude square

i get the result of CUFFT_R2C, it is a complex array, i want to square every point’s magnitude then get a float array. any suggestion is expected, please!

A possible method could be similar to this.

#include <cufft.h>
#include <thrust/transform.h>
#include <thrust/device_ptr.h>
#include <thrust/complex.h>

struct my_cplx_sq
  __host__ __device__ float operator()(thrust::complex<float> &d){
    return d.real()*d.real() + d.imag()*d.imag();}

int main(){
  const int N = 256;
  cufftComplex *d;
  float  *r;
  cudaMalloc(&d, N*sizeof(cufftComplex));
  cudaMalloc(&r, N*sizeof(float));
  thrust::device_ptr<thrust::complex<float> > td = thrust::device_pointer_cast((thrust::complex<float> *)d);
  thrust::device_ptr<float> tr = thrust::device_pointer_cast(r);
  thrust::transform(td, td+N, tr, my_cplx_sq() );

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.