Consider a container for two-dimensional complex-valued arrays
#include <vector>
#include <array>
struct Array2D {
typedef std::array<double, 2> complex;
int X, Y;
std::vector<complex> values;
};
and a function that uses FFTW to compute the Fourier transform:
#include <fftw3.h>
void FourierTransform(const Array2D& source, Array2D *dest) {
fftw_plan p = fftw_plan_dft_2d(source.X, source.Y, (fftw_complex*)source.values.data(), (fftw_complex*)dest->values.data(), FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(p);
fftw_destroy_plan(p);
}
This is not an optimal implementation because it creates and destroys a plan every time the Fourier transform is computed, but it is convenient and easy to use. However, creating and destroying FFTW plans is not thread safe (see here), so this function should not be called simultaneously from different threads.
The problem:
- I have many different arrays in my application of which I need to calculate Fourier transforms and
- there are several function calls in between the parallelization of the main loop with
#pragma omp parallel for
and the places in the code where I need to calculate the Fourier transform.
Initializing all of these arrays along with the fftw plans before #pragma omp parallel for
and then passing them as arguments to the subsequent functions would make the code a lot less legible. Is there another way that would allow me to encapsulate and hide the FFTW function calls in a thread-safe way?
reinterpret_cast
ingstd::array<double, 2> *
tofftw_complex*
, which is invalid. Why aren't you usingstd::complex<double>
or_Complex
? – Caleth