fftw并行处理需要在并行处理之前(for循环)开辟空间、制定plan。以封装逆傅里叶为例,操作步骤:首先封装一个并行处理所需要的结构体
typedef struct plan_thread
{
fftw_complex* input;
fftw_complex* output;
fftw_plan plan;
}planThreads;
.h里面
planThreads* init_omp_threads_ifft(int N);
void fftw_execute_ifft(planThreads* m_vecPlan);//需要归一化处理
void free_destroy(planThreads* m_vecPlan);
.c 使用openMP并行多线程
planThreads* init_omp_threads_ifft(int N)
{
int thread_num = omp_get_max_threads();//获取最大线程
if(!fftw_init_threads())
{
perror("init threads fail\n");
}
planThreads *m_vecPlan;
fftw_plan_with_nthreads(thread_num );//设置线程数
m_vecPlan = (planThreads*)malloc(sizeof(planThreads)*thread_num);
for(int i = 0; i<thread_num;i++)
{
m_vecPlan[i].input = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N);
m_vecPlan[i].output = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N);
m_vecPlan[i].plan = fftw_plan_dft_1d(N,m_vecPlan[i].input,m_vecPlan[i].output,FFTW_BACKWARD,FFTW_ESTIMATE)
}
return m_vecPlan;
}
int curThread = omp_get_thread_num();
fftw_execute(m_vecPlan[curThread ].plan);//需要获得当前的线程,赋值和取值也是如此
void free_destroy(planThreads* m_vecPlan)
{
for(int i = 0;i < opm_get_max_threads();i++)
{
fftw_free(m_vecPlan.input);
fftw_free(m_vecPlan.output);
fftw_destroy_plan(m_vecPlan[i].plan);
}
}