1

I am currently debugging my code, where I use the CUDA FFT routines.

I have something like this (please see comments for my thoughts on what I do):

#include <cufft.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuComplex.h>

void foo(double* real, double* imag, size_t size)
{
    cufftHandle plan;
    cufftDoubleComplex* inputData;
    cufftDoubleReal* outputReal;

    //Allocation of arrays:
    size_t allocSizeInput = sizeof(cufftDoubleComplex) * size;
    size_t allocSizeOutput = sizeof(cufftDoubleReal) * (size - 1) * 2;

    cudaMalloc((void**)&outputReal, allocSizeOutput);
    cudaMalloc((void**)&inputData, allocSizeInput);

    //Now I put the data in the arrays real and imag into input data by 
    //interleaving it
    cudaMemcpy2D(static_cast<void*>(inputData),
            2 * sizeof (double),
            static_cast<const void*>(real),
            sizeof(double),
            sizeof(double),
            size,
            cudaMemcpyHostToDevice);

    cudaMemcpy2D(static_cast<void*>(inputData) + sizeof(double),
            2 * sizeof (double),
            static_cast<const void*>(imag),
            sizeof(double),
            sizeof(double),
            size,
            cudaMemcpyHostToDevice);

    //I checked inputData at this point and it does indeed look like i expect it to.

    //Now I create the plan
    cufftPlan1d(&plan, size, CUFFT_Z2D, 1);

    //Now I execute the plan
    cufftExecZ2D(plan, inputData, outputReal);

    //Now I wait for device sync
    cudaDeviceSynchronize();

    //Now I fetch up the data from device
    double* outDbl = new double[(size-1)*2]
    cudaMemcpy(static_cast<void*>(outDbl),
            static_cast<void*>(outputReal),
            allocSizeOutput,
            cudaMemcpyDeviceToHost);

    //Here I am doing other fancy stuff which is not important
}

So the problem I have now is, that the results in outDbl are not what I expect them to be. For example, if I give the following values into this function:

real = [0 -5.567702511594111 -5.595068807897317 -5.595068807897317 -5.567702511594111]

imag = [0 9.678604224870535 2.280007038673738 -2.280007038673738 -9.678604224870535]

I expect to get:

result = [-4.46511 -3.09563 -0.29805 2.51837 5.34042]

But i get something completely different.

What do I do wrong? Did I misunderstand the FFT function? Is it not basically the inverse FFT from complex to real? Is there a problem in my data copy routines?

I must admit I am a bit lost on this one.

FreddyKay
  • 275
  • 1
  • 4
  • 13

1 Answers1

1

Yeah.. sorry. I found the answer on stackoverflow after I asked the question.

See here

Basically: The cuda fft is not normalized so I have to divide the values I get by the number of elements to get the normalized values.

Community
  • 1
  • 1
FreddyKay
  • 275
  • 1
  • 4
  • 13