FFTW3-就地复数ftf并行化一维速度慢

时间:2019-02-20 15:14:28

标签: openmp fftw icc

所以我正在并行化1D FFT。作为第一个任务,我在具有16个内核的Intel®Xeon®CPU E5-2620 v3 @ 2.40GHz 上对FFTW3库进行了基准测试。我只是用OpenMP作为我的线程库做了一个基本的一维复数FFT。我使用以下命令在ICC上进行了编译:

icc -Wall -Werror
-I/.../mkl/include -I/apps/intel/linux/mkl/include/fftw  
fftw3_dft.c   
-L/.../intel/linux/mkl/.../intel64 -lmkl_rt 
-L/.../intel/.../linux/mkl/../compiler/lib/intel64 
-L/apps/intel/.../clinux/mkl/../tbb/lib/intel64/gcc4.4 
-liomp5 -lm -lpthread -ldl 
 -o fftw3_dft.out

我计算了不同问题大小的加速指标。我无法解释这个情节

  • 对于介于2 ^ 21和2 ^ 24之间的问题,为什么使用2个处理器没有加速?(即使有4,8和16个线程有加速)
  • >
  • 当问题大小大于2 ^ 27时,为什么加速突然增加?

Speedup vs problem size graph

代码

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>

#include "fftw3.h"
#include "mkl.h"

/* Compute (K*L)%M accurately */
static double moda(int K, int L, int M)
{
    return (double)(((long long)K * L) % M);
}

/* Initialize array x[N] with harmonic H */
static void init(fftw_complex *x, int N, int H)
{
    double TWOPI = 6.2831853071795864769, phase;
    int n;

    for (n = 0; n < N; n++)
    {
         phase  = moda(n,H,N) / N;
         x[n][0] = cos( TWOPI * phase ) / N;
        x[n][1] = sin( TWOPI * phase ) / N;
    }
}

int main(int argc, char *argv[]) {

    if(argc < 3) {
         printf("Error : give args\n");
        return 0;
     }


int N = atoi(argv[1]);
int p = atoi(argv[2]);

int H =  -N/2;
fftw_plan forward_plan = 0, backward_plan = 0;
fftw_complex *x = 0;
int status = 0;

fftw_init_threads();
fftw_plan_with_nthreads(p);

x  = fftw_malloc(sizeof(fftw_complex)*N);

forward_plan = fftw_plan_dft(1, &N, x, x, FFTW_FORWARD, FFTW_ESTIMATE);

init(x, N, H);

double start_time = dsecnd();

/*--------------ALG STARTS HERE --------------------------*/
fftw_execute(forward_plan);
/*--------------ALG ENDS HERE --------------------------*/

double end_time = dsecnd();

printf(LI", %d, %lf\n", N, p, end_time - start_time);
fftw_cleanup_threads()
fftw_destroy_plan(forward_plan);
fftw_free(x);

}

1 个答案:

答案 0 :(得分:1)

即使在n = 2 ^ 14的情况下,我也确实获得了2个内核的加速,此后一直保持在1.5以上。记住要多次运行代码,并丢弃用于旋转的第一部分。现代内核需要一些时间才能完全进入速度。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>

#include "fftw3.h"
#include "omp.h"

/* Compute (K*L)%M accurately */
static double moda(int K, int L, int M)
{
    return (double)(((long long)K * L) % M);
}

/* Initialize array x[N] with harmonic H */
static void init(fftw_complex *x, int N, int H)
{
    double TWOPI = 6.2831853071795864769, phase;
    int n;

    for (n = 0; n < N; n++)
    {
         phase  = moda(n,H,N) / N;
         x[n][0] = cos( TWOPI * phase ) / N;
        x[n][1] = sin( TWOPI * phase ) / N;
    }
}

int main(int argc, char *argv[]) {

    if(argc < 2) {
         printf("Error : give args\n");
        return 0;
     }


int max_pow = atoi(argv[1]);
int p = 1;
#pragma omp parallel
{
#pragma omp single
{
  p = omp_get_num_threads();
}
}
printf("%i\n", p);

fftw_plan forward_plan = 0;
fftw_complex *x = 0;

fftw_init_threads();
fftw_plan_with_nthreads(p);

for(int iter=1;iter<=2;iter++){
  //throw away the first round, a couple of seconds is enough
  for(int pw=12;pw<=max_pow;pw++){
    int N = pow(2, pw);
    int H =  -N/2;

    x  = fftw_malloc(sizeof(fftw_complex)*N);

    forward_plan = fftw_plan_dft(1, &N, x, x, FFTW_FORWARD, FFTW_MEASURE);

    init(x, N, H);

    double start_time = omp_get_wtime();

    /*--------------ALG STARTS HERE --------------------------*/
    for(int i=1;i<=5;i++){fftw_execute(forward_plan);}
    /*--------------ALG ENDS HERE --------------------------*/

    double end_time = omp_get_wtime();

    printf("%i %lf\n", pw, (end_time - start_time)/5);
    fftw_destroy_plan(forward_plan);
    fftw_free(x);
  }
}

return 0;
}

> gfortran -fopenmp fftw1d.c -lfftw3 -lfftw3_omp
> OMP_NUM_THREADS=2 ./a.out 24

在使用-O3和不使用import json with open('scrapercomment.json', 'r') as src: parsed_json = json.load(src) for comment in parsed_json: f = open(comment['author']['id'], 'w') f.write(str(comment)) f.close() 的情况下,结果都是相同的。

enter image description here

在四核Intel(R)Core(TM)i7-3770 CPU @ 3.40GHz上进行了测试