2

I want to compute 3D FFT using Intel MKL of an array which has about 300×200×200 elements. This 3D array is stored as a 1D array of type double in a columnwise fashion:

for( int k = 0; k < nk; k++ ) // Loop through the height.
    for( int j = 0; j < nj; j++ ) // Loop through the rows.
        for( int i = 0; i < ni; i++ ) // Loop through the columns.
        {
            ijk = i + ni * j + ni * nj * k;
            my3Darray[ ijk ] = 1.0;
        }

I want to perform not-in-place FFT on the input array and prevent it from getting modified (I need to use it later in my code) and then do the backward computation in-place. I also want to have zero padding.

My questions are:

  1. How can I perform the zero-padding?
  2. How should I deal with the size of the arrays used by FFT functions when zero padding is included in the computation?
  3. How can I take out the zero padded results and get the actual result?

Here is my attempt to the problem, I would be absolutely thankful for any comment, suggestion, or hint.

#include <stdio.h>
#include "mkl.h"

int max(int a, int b, int c)
{
     int m = a;
     (m < b) && (m = b); 
     (m < c) && (m = c); 
     return m;
}

void FFT3D_R2C( // Real to Complex 3D FFT.
    double *in, int nRowsIn , int nColsIn , int nHeightsIn ,
    double *out )
{    
    int n  = max( nRowsIn , nColsIn , nHeightsIn  );
    // Round up to the next highest power of 2.
    unsigned int N = (unsigned int) n; // compute the next highest power of 2 of 32-bit n.
    N--;
    N |= N >> 1;
    N |= N >> 2;
    N |= N >> 4;
    N |= N >> 8;
    N |= N >> 16;
    N++;

    /* Strides describe data layout in real and conjugate-even domain. */
    MKL_LONG rs[4], cs[4];

    // DFTI descriptor.
    DFTI_DESCRIPTOR_HANDLE fft_desc = 0;

    // Variables needed for out-of-place computations.
    MKL_Complex16 *in_fft  = new MKL_Complex16 [ N*N*N ];
    MKL_Complex16 *out_fft = new MKL_Complex16 [ N*N*N ];
    double *out_ZeroPadded = new double [ N*N*N ];

    /* Compute strides */
    rs[3] = 1;           cs[3] = 1;
    rs[2] = (N/2+1)*2;   cs[2] = (N/2+1);
    rs[1] = N*(N/2+1)*2; cs[1] = N*(N/2+1);
    rs[0] = 0;           cs[0] = 0;

    // Create DFTI descriptor.
    MKL_LONG sizes[] = { N, N, N };
    DftiCreateDescriptor( &fft_desc, DFTI_DOUBLE, DFTI_REAL, 3, sizes );

    // Configure DFTI descriptor.
    DftiSetValue( fft_desc, DFTI_CONJUGATE_EVEN_STORAGE, DFTI_COMPLEX_COMPLEX );
    DftiSetValue( fft_desc, DFTI_PLACEMENT, DFTI_NOT_INPLACE  ); // Out-of-place transformation.
    DftiSetValue( fft_desc, DFTI_INPUT_STRIDES  , rs  );
    DftiSetValue( fft_desc, DFTI_OUTPUT_STRIDES , cs  );

    DftiCommitDescriptor( fft_desc );
    DftiComputeForward  ( fft_desc, in , in_fft  );

    // Change strides to compute backward transform.
    DftiSetValue        ( fft_desc, DFTI_INPUT_STRIDES , cs);
    DftiSetValue        ( fft_desc, DFTI_OUTPUT_STRIDES, rs);
    DftiCommitDescriptor( fft_desc );
    DftiComputeBackward ( fft_desc, out_fft, out_ZeroPadded );

    // Printing the zero padded 3D FFT result.
    for( long long i = 0; i < (long long)N*N*N; i++ )
        printf("%f\n", out_ZeroPadded[i] );

    /* I don't know how to take out the zero padded results and 
       save the actual result in the variable named "out" */

    DftiFreeDescriptor  ( &fft_desc );

    delete[] in_fft;
    delete[] out_ZeroPadded ;
}

int main()
{
    int n = 10;

    double *a    = new double [n*n*n]; // This array is real.
    double *afft = new double [n*n*n]; 

    // Fill the array with some 'real' numbers.
    for( int i = 0; i < n*n*n; i++ )
        a[ i ] = 1.0;

    // Calculate FFT.
    FFT3D_R2C( a, n, n, n, afft );

    printf("FFT results:\n");
    for( int i = 0; i < n*n*n; i++ )
        printf( "%15.8f\n", afft[i] );

    delete[] a;
    delete[] afft;

    return 0;
}
afp_2008
  • 1,940
  • 1
  • 19
  • 46

1 Answers1

0

just few hints:

  1. Power of 2 size

    • I don't like the way you are computing the size
    • so let Nx,Ny,Nz be the size of input matrix
    • and nx,ny,nz size of the padded matrix

      for (nx=1;nx<Nx;nx<<=1);
      for (ny=1;ny<Ny;ny<<=1);
      for (nz=1;nz<Nz;nz<<=1);
      
    • now zero pad by memset to zero first and then copy the matrix lines

    • padding to N^3 instead of nx*ny*nz can result in big slowdowns
    • if nx,ny,nz are not close to each other
  2. output is complex

    • if I get it right a is input real matrix
    • and afft the output complex matrix
    • so why not allocate the space for it correctly?
    • double *afft = new double [2*nx*ny*nz];
    • complex number is real+imaginary part so 2 values per number
    • that goes also for the final print of result
    • and some "\r\n" after lines would be good for viewing
  3. 3D DFFT

    • I do not use nor know your DFFT library
    • I use mine own, but anyway 3D DFFT can be done by 1D DFFT
    • if you do it by the lines ... see this 2D DFCT by 1D DFFT
    • in 3D is the same but you need to add one pass and different normalization constant
    • this way you can have single line buffer double lin[2*max(nx,ny,nz)];
    • and make the zero padding on the run (so no need to have bigger matrix in memory)...
    • but that involves coping the lines on each 1D DFFT ...
Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380