fftw split example crashes

China☆狼群 提交于 2020-01-06 16:08:03

问题


I'm trying to use fftw (3.3.4) on split arrays. I took an example from a C/MEX guide (PDF: http://www.researchgate.net/publictopics.PublicPostFileLoader.html?id=551a2e59cf57d7620c8b463b&key=3a91499f-ec8f-41f4-8e87-fff8bf00e54a).

When the program is run it crashes (segfault). Running valgrind shows memory violations.

What am I doing wrong here?

#include <vector>
#include <fftw3.h>

void DivideArray(double *Data, int NumEl, double Divisor)
{
  int n;
  for(n = 0; n < NumEl; n++)
    Data[n] /= Divisor;
}

void FFTNDSplit(int NumDims, const int N[], double *XReal, double *XImag, double *YReal, double *YImag, int Sign)
{
  fftw_plan Plan;
  fftw_iodim Dim[NumDims];
  int k, NumEl;
  for(k = 0, NumEl = 1; k < NumDims; k++)
  {
    Dim[NumDims-k-1].n = N[k];
    Dim[NumDims-k-1].is = Dim[NumDims-k-1].os = (k == 0) ? 1 : (N[k-1] * Dim[NumDims-k].is);
    NumEl *= N[k];
  }
  if(!(Plan = fftw_plan_guru_split_dft(NumDims, Dim, 0, NULL,
    XReal, XImag, YReal, YImag, FFTW_ESTIMATE))) {
    printf("Failed creating FFTW plan.\n");
    return;
  }

  if(Sign == -1)
    fftw_execute_split_dft(Plan, XReal, XImag, YReal, YImag);
  else
  {
    fftw_execute_split_dft(Plan, XImag, XReal, YImag, YReal);
    DivideArray(YReal, NumEl, NumEl);
    DivideArray(YImag, NumEl, NumEl);
  }

  fftw_destroy_plan(Plan);
  return; 
}

int main()
{
  int W = 201, H = 201;
  std::vector<double> xr(W*H);
  std::vector<double> xi(W*H);
  std::vector<double> yr(W*H);
  std::vector<double> yi(W*H);

  for (int i = 0; i < W*H; i++) {
    xr[i] = xi[i] = yr[i] = yi[i] = 5.0;
  }

  int dims[2] = { W, H };
  FFTNDSplit(2, dims, &xr[0], &xi[0], &yr[0], &yi[0], 1);
  return 0;
}

valgrind's first error output:

==20663== Invalid write of size 8
==20663==    at 0x4E4A0EB: fftw_cpy2d (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4E4A36C: dotile_buf (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4E4F4CA: fftw_tile2d (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4E4F4CA: fftw_tile2d (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4E4A51F: fftw_cpy2d_tiledbuf (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4E8E651: copy (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4E877C6: apply (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4E518F8: apply (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x400CDF: FFTNDSplit(int, int const*, double*, double*, double*, double*, int) (in /.../fftw3_split_test)
==20663==    by 0x400FDA: main (in /.../fftw3_split_test)
==20663==  Address 0x5e56b80 is 0 bytes inside a block of size 32 free'd
==20663==    at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==20663==    by 0x4E4F368: fftw_tensor_destroy2 (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4E562BB: fftw_mkproblem_dft_d (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x4F1B4CB: fftw_plan_guru_split_dft (in /usr/local/lib/libfftw3.so.3.4.4)
==20663==    by 0x400C7D: FFTNDSplit(int, int const*, double*, double*, double*, double*, int) (in /.../fftw3_split_test)
==20663==    by 0x400FDA: main (in /.../fftw3_split_test)

回答1:


The problem is that you are planning on array (Real, Imag) but you are executing with (Imag, Real), in an attempt to use the same plan for the forward and inverse transforms. This is not allowed, as per manual Section 4.6:

  • For split arrays, the separations between the real and imaginary parts, 'ii-ri' and 'io-ro', are the same as they were for the input and output arrays when the plan was created. (This condition is automatically satisfied for interleaved arrays.)

Creating a separate plan for the inverse will solve the problem.



来源:https://stackoverflow.com/questions/31099349/fftw-split-example-crashes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!