问题
I need to use FFTW with different arithmetic precisions and multithreaded plans.
I need to setup multithreading for all precisions? Like this:
fftwf_init_threads();
fftwf_plan_with_nthreads(nthreads);
fftw_init_threads();
fftw_plan_with_nthreads(nthreads);
fftwl_init_threads();
fftwl_plan_with_nthreads(nthreads);
Or writing this in my start routine
fftw_init_threads();
fftw_plan_with_nthreads(nthreads);
will be enough?
回答1:
The fftw libraries for different precisions are completely independent from one another. Hence, you need to setup multithreading for all precisions by calling the corresponding function of fftw:
int nbthreads=2;
fftw_init_threads();
fftw_plan_with_nthreads(nbthreads);
fftwf_init_threads();
fftwf_plan_with_nthreads(nbthreads);
fftwq_init_threads();
fftwq_plan_with_nthreads(nbthreads);
...
fftw_cleanup_threads();
fftwf_cleanup_threads();
fftwq_cleanup_threads();
Here is a sample code to convinve you. It can be compiled by gcc main.c -o main -lfftw3_threads -lfftw3 -lfftw3f_threads -lfftw3f -lfftw3q_threads -lfftw3q -lm -lpthread -Wall
on Unix, once the corrponding libraries are built.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
int main ( ){
int n = 40000000;
int nf = n*2;
int nq =n/32;
fftw_complex *in;
fftw_complex *in2;
fftw_complex *out;
fftw_plan plan_backward;
fftw_plan plan_forward;
fftwf_complex *inf;
fftwf_complex *in2f;
fftwf_complex *outf;
fftwf_plan plan_backwardf;
fftwf_plan plan_forwardf;
fftwq_complex *inq;
fftwq_complex *in2q;
fftwq_complex *outq;
fftwq_plan plan_backwardq;
fftwq_plan plan_forwardq;
//thread parameters
int nbthreads=2;
fftw_init_threads();
fftw_plan_with_nthreads(nbthreads);
fftwf_init_threads();
fftwf_plan_with_nthreads(nbthreads);
fftwq_init_threads();
fftwq_plan_with_nthreads(nbthreads);
in = fftw_malloc ( sizeof ( fftw_complex ) * n );
out = fftw_malloc ( sizeof ( fftw_complex ) * n );
inf = fftwf_malloc ( sizeof ( fftwf_complex ) * nf );
outf = fftwf_malloc ( sizeof ( fftwf_complex ) * nf );
inq = fftwq_malloc ( sizeof ( fftwq_complex ) * nq );
outq = fftwq_malloc ( sizeof ( fftwq_complex ) * nq);
// forward fft
plan_forward = fftw_plan_dft_1d ( n, in, out, FFTW_FORWARD, FFTW_ESTIMATE );
fftw_execute ( plan_forward );
plan_forwardf = fftwf_plan_dft_1d ( nf, inf, outf, FFTW_FORWARD, FFTW_ESTIMATE );
fftwf_execute ( plan_forwardf );
plan_forwardq = fftwq_plan_dft_1d ( nq, inq, outq, FFTW_FORWARD, FFTW_ESTIMATE );
fftwq_execute ( plan_forwardq );
// backward fft
in2 = fftw_malloc ( sizeof ( fftw_complex ) * n );
plan_backward = fftw_plan_dft_1d ( n, out, in2, FFTW_BACKWARD, FFTW_ESTIMATE );
fftw_execute ( plan_backward );
in2f = fftwf_malloc ( sizeof ( fftwf_complex ) * nf );
plan_backwardf = fftwf_plan_dft_1d ( nf, outf, in2f, FFTW_BACKWARD, FFTW_ESTIMATE );
fftwf_execute ( plan_backwardf );
in2q = fftwq_malloc ( sizeof ( fftwq_complex ) * nq );
plan_backwardq = fftwq_plan_dft_1d ( nq, outq, in2q, FFTW_BACKWARD, FFTW_ESTIMATE );
fftwq_execute ( plan_backwardq);
fftw_cleanup_threads();
fftw_destroy_plan ( plan_forward );
fftw_destroy_plan ( plan_backward );
fftw_free ( in );
fftw_free ( in2 );
fftw_free ( out );
fftwf_cleanup_threads();
fftwf_destroy_plan ( plan_forwardf );
fftwf_destroy_plan ( plan_backwardf );
fftwf_free ( inf );
fftwf_free ( in2f );
fftwf_free ( outf );
fftwq_cleanup_threads();
fftwq_destroy_plan ( plan_forwardq);
fftwq_destroy_plan ( plan_backwardq );
fftwq_free ( inq );
fftwq_free ( in2q );
fftwq_free ( outq );
return 0;
}
You can comment fftwq_init_threads(); fftwq_plan_with_nthreads(nbthreads);
and fftwq_cleanup_threads();
and monitor cpu usage to check that fftw will not use multithreading for quad precision in this case.
To build fftw3 for different precisions using intel simd sse2, do:
./configure --enable-threads --enable-shared --enable-sse2
make
make install
./configure --enable-threads --enable-shared --enable-sse2 --enable-float
make
make install
./configure --enable-threads --enable-shared --enable-quad-precision
make
make install
There is no sse2 support for quad precision.
来源:https://stackoverflow.com/questions/36790241/how-to-setup-fftw-with-threads-and-different-precisions