Vasiliy Stavenko
Vasiliy Stavenko

Reputation: 1214

Fftw3 library and plans reuse

I'm about to use fftw3 library in my very certain task.

I have a heavy load packets stream with variable frame size, which is produced like that:

while(thereIsStillData){
   copyDataToInputArray();
   createFFTWPlan();
   performExecution();
   destroyPlan();
}

Since creating plans is rather expensive, I want to modify my code to something like this:

while(thereIsStillData){
  if(inputArraySizeDiffers()) destroyOldAndCreateNewPlan();
  copyDataToInputArray(); // e.g. `memcpy` or `std::copy`;
  performExecution();
}

Can I do this? I mean, does plan contain some important information based on data such, that plan created for one array with size N, when executed will give incorrect results for the other array of same size N.

Upvotes: 2

Views: 2032

Answers (1)

John Bollinger
John Bollinger

Reputation: 180296

The fftw_execute() function does not modify the plan presented to it, and can be called multiple times with the same plan. Note, however, that the plan contains pointers to the input and output arrays, so if copyDataToInputArray() involves creating a different input (or output) array then you cannot afterwards use the old plan in fftw_execute() to transform the new data.

FFTW does, however, have a set of "New-array Execute Functions" that could help here, supposing that the new arrays satisfy some additional similarity criteria with respect to the old (see linked docs for details).

The docs do recommend:

If you are tempted to use the new-array execute interface because you want to transform a known bunch of arrays of the same size, you should probably go use the advanced interface instead

but that's talking about transforming multiple arrays that are all in memory simultaneously, and arranged in a regular manner.

Note, too, that if your variable frame size is not too variable -- that is, if it is always one of a relatively small number of choices -- then you could consider keeping a separate plan in memory for each frame size instead of recomputing a plan every time one frame's size differs from the previous one's.

Upvotes: 4

Related Questions