Reply by alexryu May 3, 20092009-05-03
>On May 1, 10:10=A0pm, "alexryu" <ryu.a...@gmail.com> wrote: >> Here is my code: >> for (j =3D 0; j < NUMP; j++) >> { >> =A0plan =3D fftw_plan_dft_r2c_1d(NUMT,x + j*NUMT, y + j*NUMF,
FFTW_ESTIMA=
>TE); >> =A0fftw_execute(plan);} >> > >As another poster pointed out, you are creating NUMP plans and only >destroying one, hence your leak. > >Here is a simple way not only to fix your leak, but also to get much >better performance: using FFTW's advanced interface, you can perform >all NUMP transforms in a single call, with a single plan. Replace the >above code with: > >int n =3D NUMT; >plan =3D fftw_plan_many_dft_r2c(1, &n, NUMP, > x, NULL, 1, NUMT, > y, NULL, 1, NUMF, > FFTW_ESTIMATE); >fftw_execute(plan); > >See: http://www.fftw.org/doc/Advanced-Real_002ddata-DFTs.html > >NUMP is the number of transforms you want, hence it is passed as the >"howmany" parameter. The transforms are each of rank 1 and length >NUMT, so those are the first two parameters. Each array of length >NUMT is contiguous, so the "stride" parameters are both 1. The offset >between successive transforms is NUMT for the real array and NUMF for >the complex array, so those are passed as the "dist" arguments. The >"nembed" arguments are only used for transforming a portion of a >multidimensional array, so those are both NULL. > >Regards, >Steven G. Johnson >
Thank you both (Steven and Jason) for your help.
Reply by Steven G. Johnson May 2, 20092009-05-02
On May 1, 10:10&#4294967295;pm, "alexryu" <ryu.a...@gmail.com> wrote:
> Here is my code: > for (j = 0; j < NUMP; j++) > { > &#4294967295;plan = fftw_plan_dft_r2c_1d(NUMT,x + j*NUMT, y + j*NUMF, FFTW_ESTIMATE); > &#4294967295;fftw_execute(plan);} >
As another poster pointed out, you are creating NUMP plans and only destroying one, hence your leak. Here is a simple way not only to fix your leak, but also to get much better performance: using FFTW's advanced interface, you can perform all NUMP transforms in a single call, with a single plan. Replace the above code with: int n = NUMT; plan = fftw_plan_many_dft_r2c(1, &n, NUMP, x, NULL, 1, NUMT, y, NULL, 1, NUMF, FFTW_ESTIMATE); fftw_execute(plan); See: http://www.fftw.org/doc/Advanced-Real_002ddata-DFTs.html NUMP is the number of transforms you want, hence it is passed as the "howmany" parameter. The transforms are each of rank 1 and length NUMT, so those are the first two parameters. Each array of length NUMT is contiguous, so the "stride" parameters are both 1. The offset between successive transforms is NUMT for the real array and NUMF for the complex array, so those are passed as the "dist" arguments. The "nembed" arguments are only used for transforming a portion of a multidimensional array, so those are both NULL. Regards, Steven G. Johnson
Reply by May 2, 20092009-05-02
On May 1, 10:10&#4294967295;pm, "alexryu" <ryu.a...@gmail.com> wrote:
> Hello > I have a double array x of length NUMP * NUMT, which is NUMP signals, each > of length NUMT. &#4294967295;I want to take the DFT of each signal, and put it into a > complex double array y of length NUMP * NUMF = NUMP * (NUMT / 2 + 1). > Here is my code: > for (j = 0; j < NUMP; j++) > { > &#4294967295;plan = fftw_plan_dft_r2c_1d(NUMT,x + j*NUMT, y + j*NUMF, FFTW_ESTIMATE); > &#4294967295;fftw_execute(plan);} > > fftw_destroy_plan(plan); > fftw_cleanup(); > > As written, this code has a severe memory leak. &#4294967295;I can get rid of it by > putting the destroy and cleanup statements inside the loop, but that runs > horribly slow. &#4294967295;Does anyone know the best course of action? > Thank you
Look at your code; you're creating NUMP plans, but you're only destroying one of them. The memory allocated for the other NUMP-1 plans won't be reclaimed. If you truly need a different plan for each FFT you're doing (I think it's called the "expert" interface that allows you to apply a plan to input/output arrays other than the ones you specify when creating the plan), then I would recommend creating all of them at one time at startup, using them as needed, then destroying all of the plans when you're done processing. Jason
Reply by alexryu May 1, 20092009-05-01
Hello
I have a double array x of length NUMP * NUMT, which is NUMP signals, each
of length NUMT.  I want to take the DFT of each signal, and put it into a
complex double array y of length NUMP * NUMF = NUMP * (NUMT / 2 + 1).
Here is my code:
for (j = 0; j < NUMP; j++)
{
 plan = fftw_plan_dft_r2c_1d(NUMT,x + j*NUMT, y + j*NUMF, FFTW_ESTIMATE);
 fftw_execute(plan);
}
fftw_destroy_plan(plan); 
fftw_cleanup();

As written, this code has a severe memory leak.  I can get rid of it by
putting the destroy and cleanup statements inside the loop, but that runs
horribly slow.  Does anyone know the best course of action?
Thank you