7

長いシーケンスx(n)があり、最初の要素だけがゼロと異なるとします。たとえば、とと仮定しています。そのようなシーケンスのFFTWをFFTWで計算したいと思います。これは、 length のシーケンスを持ち、 にゼロをパディングすることと同じです。とは「大きい」可能性があるため、かなりのゼ​​ロ パディングがあります。明示的なゼロパディングを避けて、計算時間を節約できるかどうかを調査しています。K * NNN << KN = 10K = 100000NK * NNK

ケースK = 2

ケースを検討することから始めましょうK = 2。この場合、 の DFT は次のx(n)ように記述できます。

ここに画像の説明を入力

kが偶数、つまりの場合k = 2 * m

ここに画像の説明を入力

つまり、このような DFT の値は、Nではなく長さ のシーケンスの FFT を介して計算できますK * N

kが奇数、つまりの場合k = 2 * m + 1

ここに画像の説明を入力

つまり、このような DFT の値は、長さ のシーケンスの FFT を介して再度計算できますが、 ではNありませんK * N

したがって、結論として、長さの 1 つの FFT を長さの FFT と交換2 * Nでき2ますN

恣意的なケースK

この場合、

ここに画像の説明を入力

書くk = m * K + tと、私たちは持っています

ここに画像の説明を入力

したがって、結論として、長さの 1 つの FFT を長さの FFT と交換K * NできKますN。FFTW には があるのでfftw_plan_many_dft、単一の FFT の場合に対していくらかのゲインがあると期待できます。

それを確認するために、次のコードを設定しました

#include <stdio.h>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <math.h>
#include <fstream>

#include <fftw3.h>

#include "TimingCPU.h"

#define PI_d            3.141592653589793

void main() {

    const int N = 10;
    const int K = 100000;

    fftw_plan plan_zp;

    fftw_complex *h_x = (fftw_complex *)malloc(N     * sizeof(fftw_complex));
    fftw_complex *h_xzp = (fftw_complex *)calloc(N * K, sizeof(fftw_complex));
    fftw_complex *h_xpruning = (fftw_complex *)malloc(N * K * sizeof(fftw_complex));
    fftw_complex *h_xhatpruning = (fftw_complex *)malloc(N * K * sizeof(fftw_complex));
    fftw_complex *h_xhatpruning_temp = (fftw_complex *)malloc(N * K * sizeof(fftw_complex));
    fftw_complex *h_xhat = (fftw_complex *)malloc(N * K * sizeof(fftw_complex));

    // --- Random number generation of the data sequence
    srand(time(NULL));
    for (int k = 0; k < N; k++) {
        h_x[k][0] = (double)rand() / (double)RAND_MAX;
        h_x[k][1] = (double)rand() / (double)RAND_MAX;
    }

    memcpy(h_xzp, h_x, N * sizeof(fftw_complex));

    plan_zp = fftw_plan_dft_1d(N * K, h_xzp, h_xhat, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_plan plan_pruning = fftw_plan_many_dft(1, &N, K, h_xpruning, NULL, 1, N, h_xhatpruning_temp, NULL, 1, N, FFTW_FORWARD, FFTW_ESTIMATE);

    TimingCPU timerCPU;
    timerCPU.StartCounter();
    fftw_execute(plan_zp);
    printf("Stadard %f\n", timerCPU.GetCounter());

    timerCPU.StartCounter();
    double factor = -2. * PI_d / (K * N);
    for (int k = 0; k < K; k++) {
        double arg1 = factor * k;
        for (int n = 0; n < N; n++) {
            double arg = arg1 * n;
            double cosarg = cos(arg);
            double sinarg = sin(arg);
            h_xpruning[k * N + n][0] = h_x[n][0] * cosarg - h_x[n][1] * sinarg;
            h_xpruning[k * N + n][1] = h_x[n][0] * sinarg + h_x[n][1] * cosarg;
        }
    }
    printf("Optimized first step %f\n", timerCPU.GetCounter());

    timerCPU.StartCounter();
    fftw_execute(plan_pruning);
    printf("Optimized second step %f\n", timerCPU.GetCounter());
    timerCPU.StartCounter();
    for (int k = 0; k < K; k++) {
        for (int p = 0; p < N; p++) {
            h_xhatpruning[p * K + k][0] = h_xhatpruning_temp[p + k * N][0];
            h_xhatpruning[p * K + k][1] = h_xhatpruning_temp[p + k * N][1];
        }
    }
    printf("Optimized third step %f\n", timerCPU.GetCounter());

    double rmserror = 0., norm = 0.;
    for (int n = 0; n < N; n++) {
        rmserror = rmserror + (h_xhatpruning[n][0] - h_xhat[n][0]) * (h_xhatpruning[n][0] - h_xhat[n][0]) + (h_xhatpruning[n][1] - h_xhat[n][1]) * (h_xhatpruning[n][1] - h_xhat[n][1]);
        norm = norm + h_xhat[n][0] * h_xhat[n][0] + h_xhat[n][1] * h_xhat[n][1];
    }
    printf("rmserror %f\n", 100. * sqrt(rmserror / norm));

    fftw_destroy_plan(plan_zp);

}

私が開発したアプローチは、次の 3 つのステップで構成されています。

  1. 入力シーケンスを「ひねる」複雑な指数で乗算します。
  2. 実行するfftw_many;
  3. 結果の再編成。

は、入力ポイントfftw_manyで単一の FFTW よりも高速です。K * Nただし、ステップ 1 と 3 は、そのようなゲインを完全に破壊します。ステップ 1 と 3 は、ステップ 2 よりもはるかに軽量であると予想されます。

私の質問は次のとおりです。

  1. ステップ 1 と 3 が、ステップ 2 よりも計算量が多くなる可能性があるのはなぜですか?
  2. ステップ 1 と 3 を改善して、「標準的な」アプローチに対して純利益を得るにはどうすればよいですか?

ヒントをありがとうございました。

編集

私は Visual Studio 2013 で作業しており、リリース モードでコンパイルしています。

4

3 に答える 3

5

より速く実行するためのいくつかのオプション:

  1. シングルスレッドのみを実行していて、複数のコアが利用可能な場合は、マルチスレッドで実行します。

  2. 特に FFT の次元が事前にわかっている場合は、FFTW 知恵ファイルを作成して保存します。を使用FFTW_EXHAUSTIVEし、毎回再計算する代わりに FFTW の知恵を再読み込みします。これは、結果に一貫性を持たせたい場合にも重要です。FFTW は異なる計算された知恵で異なる方法で FFT を計算する可能性があり、知恵の結果が常に同じになるとは限らないため、両方に同じ入力データが与えられた場合、プロセスの実行が異なると異なる結果が生成される可能性があります。

  3. x86 を使用している場合は、64 ビットを実行します。FFTW アルゴリズムは非常にレジスタを集中的に使用するため、64 ビット モードで実行されている x86 CPU は、32 ビット モードで実行されている場合よりもはるかに多くの汎用レジスタを使用できます。

  4. FFTW アルゴリズムはレジスタを大量に使用するため、プリフェッチの使用を防止し、関数の暗黙的なインライン化を防止するコンパイラオプションを使用して FFTW をコンパイルすることで、FFTW のパフォーマンスを向上させることに成功しました。

于 2016-11-16T16:12:06.603 に答える
1

また、Paul R のコメントに従って、コードを改善しました。現在、代替アプローチは、標準 (ゼロが埋め込まれた) アプローチよりも高速です。以下は完全な C++ スクリプトです。手順 1 と 3 については、他の試行済みの解決策にコメントしました。これらの解決策は、コメントされていないものよりも遅いか、同じくらい速いことが示されています。forまた、将来のより単純な CUDA 並列化を考慮して、ネストされていないループに特権を与えました。私はまだ FFTW にマルチスレッドを使用していません。

#include <stdio.h>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <math.h>
#include <fstream>

#include <omp.h>

#include <fftw3.h>

#include "TimingCPU.h"

#define PI_d            3.141592653589793

/******************/
/* STEP #1 ON CPU */
/******************/
void step1CPU(fftw_complex * __restrict h_xpruning, const fftw_complex * __restrict h_x, const int N, const int K) {

//  double factor = -2. * PI_d / (K * N);
//  int n;
//  omp_set_nested(1);
//#pragma omp parallel for private(n) num_threads(4)
//  for (int k = 0; k < K; k++) {
//      double arg1 = factor * k;
//#pragma omp parallel for num_threads(4)
//      for (n = 0; n < N; n++) {
//          double arg = arg1 * n;
//          double cosarg = cos(arg);
//          double sinarg = sin(arg);
//          h_xpruning[k * N + n][0] = h_x[n][0] * cosarg - h_x[n][1] * sinarg;
//          h_xpruning[k * N + n][1] = h_x[n][0] * sinarg + h_x[n][1] * cosarg;
//      }
//  }

    //double factor = -2. * PI_d / (K * N);
    //int k;
    //omp_set_nested(1);
    //#pragma omp parallel for private(k) num_threads(4)
    //for (int n = 0; n < N; n++) {
    //  double arg1 = factor * n;
    //  #pragma omp parallel for num_threads(4)
    //  for (k = 0; k < K; k++) {
    //      double arg = arg1 * k;
    //      double cosarg = cos(arg);
    //      double sinarg = sin(arg);
    //      h_xpruning[k * N + n][0] = h_x[n][0] * cosarg - h_x[n][1] * sinarg;
    //      h_xpruning[k * N + n][1] = h_x[n][0] * sinarg + h_x[n][1] * cosarg;
    //  }
    //}

    //double factor = -2. * PI_d / (K * N);
    //for (int k = 0; k < K; k++) {
    //  double arg1 = factor * k;
    //  for (int n = 0; n < N; n++) {
    //      double arg = arg1 * n;
    //      double cosarg = cos(arg);
    //      double sinarg = sin(arg);
    //      h_xpruning[k * N + n][0] = h_x[n][0] * cosarg - h_x[n][1] * sinarg;
    //      h_xpruning[k * N + n][1] = h_x[n][0] * sinarg + h_x[n][1] * cosarg;
    //  }
    //}

    //double factor = -2. * PI_d / (K * N);
    //for (int n = 0; n < N; n++) {
    //  double arg1 = factor * n;
    //  for (int k = 0; k < K; k++) {
    //      double arg = arg1 * k;
    //      double cosarg = cos(arg);
    //      double sinarg = sin(arg);
    //      h_xpruning[k * N + n][0] = h_x[n][0] * cosarg - h_x[n][1] * sinarg;
    //      h_xpruning[k * N + n][1] = h_x[n][0] * sinarg + h_x[n][1] * cosarg;
    //  }
    //}

    double factor = -2. * PI_d / (K * N);
    #pragma omp parallel for num_threads(8)
    for (int n = 0; n < K * N; n++) {
        int row = n / N;
        int col = n % N;
        double arg = factor * row * col;
        double cosarg = cos(arg);
        double sinarg = sin(arg);
        h_xpruning[n][0] = h_x[col][0] * cosarg - h_x[col][1] * sinarg;
        h_xpruning[n][1] = h_x[col][0] * sinarg + h_x[col][1] * cosarg;
    }
}

/******************/
/* STEP #3 ON CPU */
/******************/
void step3CPU(fftw_complex * __restrict h_xhatpruning, const fftw_complex * __restrict h_xhatpruning_temp, const int N, const int K) {

    //int k;
    //omp_set_nested(1);
    //#pragma omp parallel for private(k) num_threads(4)
    //for (int p = 0; p < N; p++) {
    //  #pragma omp parallel for num_threads(4)
    //  for (k = 0; k < K; k++) {
    //      h_xhatpruning[p * K + k][0] = h_xhatpruning_temp[p + k * N][0];
    //      h_xhatpruning[p * K + k][1] = h_xhatpruning_temp[p + k * N][1];
    //  }
    //} 

    //int p;
    //omp_set_nested(1);
    //#pragma omp parallel for private(p) num_threads(4)
    //for (int k = 0; k < K; k++) {
    //  #pragma omp parallel for num_threads(4)
    //  for (p = 0; p < N; p++) {
    //      h_xhatpruning[p * K + k][0] = h_xhatpruning_temp[p + k * N][0];
    //      h_xhatpruning[p * K + k][1] = h_xhatpruning_temp[p + k * N][1];
    //  }
    //}

    //for (int p = 0; p < N; p++) {
    //  for (int k = 0; k < K; k++) {
    //      h_xhatpruning[p * K + k][0] = h_xhatpruning_temp[p + k * N][0];
    //      h_xhatpruning[p * K + k][1] = h_xhatpruning_temp[p + k * N][1];
    //  }
    //}

    //for (int k = 0; k < K; k++) {
    //  for (int p = 0; p < N; p++) {
    //      h_xhatpruning[p * K + k][0] = h_xhatpruning_temp[p + k * N][0];
    //      h_xhatpruning[p * K + k][1] = h_xhatpruning_temp[p + k * N][1];
    //  }
    //}

    #pragma omp parallel for num_threads(8)
    for (int p = 0; p < K * N; p++) {
        int col = p % N;
        int row = p / K;
        h_xhatpruning[col * K + row][0] = h_xhatpruning_temp[col + row * N][0];
        h_xhatpruning[col * K + row][1] = h_xhatpruning_temp[col + row * N][1];
    }

    //for (int p = 0; p < N; p += 2) {
    //  for (int k = 0; k < K; k++) {
    //      for (int p0 = 0; p0 < 2; p0++) {
    //          h_xhatpruning[(p + p0) * K + k][0] = h_xhatpruning_temp[(p + p0) + k * N][0];
    //          h_xhatpruning[(p + p0) * K + k][1] = h_xhatpruning_temp[(p + p0) + k * N][1];
    //      }
    //  }
    //}

}

/********/
/* MAIN */
/********/
void main() {

    int N = 10;
    int K = 100000;

    // --- CPU memory allocations
    fftw_complex *h_x = (fftw_complex *)malloc(N     * sizeof(fftw_complex));
    fftw_complex *h_xzp = (fftw_complex *)calloc(N * K, sizeof(fftw_complex));
    fftw_complex *h_xpruning = (fftw_complex *)malloc(N * K * sizeof(fftw_complex));
    fftw_complex *h_xhatpruning = (fftw_complex *)malloc(N * K * sizeof(fftw_complex));
    fftw_complex *h_xhatpruning_temp = (fftw_complex *)malloc(N * K * sizeof(fftw_complex));
    fftw_complex *h_xhat = (fftw_complex *)malloc(N * K * sizeof(fftw_complex));
    //double2        *h_xhatGPU = (double2 *)malloc(N * K * sizeof(double2));


    // --- Random number generation of the data sequence on the CPU - moving the data from CPU to GPU
    srand(time(NULL));
    for (int k = 0; k < N; k++) {
        h_x[k][0] = (double)rand() / (double)RAND_MAX;
        h_x[k][1] = (double)rand() / (double)RAND_MAX;
    }
    //gpuErrchk(cudaMemcpy(d_x, h_x, N * sizeof(double2), cudaMemcpyHostToDevice));

    memcpy(h_xzp, h_x, N * sizeof(fftw_complex));

    // --- FFTW and cuFFT plans
    fftw_plan h_plan_zp      = fftw_plan_dft_1d(N * K, h_xzp, h_xhat, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_plan h_plan_pruning = fftw_plan_many_dft(1, &N, K, h_xpruning, NULL, 1, N, h_xhatpruning_temp, NULL, 1, N, FFTW_FORWARD, FFTW_ESTIMATE);

    double totalTimeCPU = 0., totalTimeGPU = 0.;
    double partialTimeCPU, partialTimeGPU;

    /****************************/
    /* STANDARD APPROACH ON CPU */
    /****************************/
    printf("Number of processors available = %i\n", omp_get_num_procs());
    printf("Number of threads              = %i\n", omp_get_max_threads());

    TimingCPU timerCPU;
    timerCPU.StartCounter();
    fftw_execute(h_plan_zp);
    printf("\nStadard on CPU: \t \t %f\n", timerCPU.GetCounter());

    /******************/
    /* STEP #1 ON CPU */
    /******************/
    timerCPU.StartCounter();
    step1CPU(h_xpruning, h_x, N, K);
    partialTimeCPU = timerCPU.GetCounter();
    totalTimeCPU = totalTimeCPU + partialTimeCPU;
    printf("\nOptimized first step CPU: \t %f\n", totalTimeCPU);

    /******************/
    /* STEP #2 ON CPU */
    /******************/
    timerCPU.StartCounter();
    fftw_execute(h_plan_pruning);
    partialTimeCPU = timerCPU.GetCounter();
    totalTimeCPU = totalTimeCPU + partialTimeCPU;
    printf("Optimized second step CPU: \t %f\n", timerCPU.GetCounter());

    /******************/
    /* STEP #3 ON CPU */
    /******************/
    timerCPU.StartCounter();
    step3CPU(h_xhatpruning, h_xhatpruning_temp, N, K);
    partialTimeCPU = timerCPU.GetCounter();
    totalTimeCPU = totalTimeCPU + partialTimeCPU;
    printf("Optimized third step CPU: \t %f\n", partialTimeCPU);

    printf("Total time CPU: \t \t %f\n", totalTimeCPU);

    double rmserror = 0., norm = 0.;
    for (int n = 0; n < N; n++) {
        rmserror = rmserror + (h_xhatpruning[n][0] - h_xhat[n][0]) * (h_xhatpruning[n][0] - h_xhat[n][0]) + (h_xhatpruning[n][1] - h_xhat[n][1]) * (h_xhatpruning[n][1] - h_xhat[n][1]);
        norm = norm + h_xhat[n][0] * h_xhat[n][0] + h_xhat[n][1] * h_xhat[n][1];
    }
    printf("\nrmserror %f\n", 100. * sqrt(rmserror / norm));

    fftw_destroy_plan(h_plan_zp);

}

ケースの場合

N = 10
K = 100000

私のタイミングは次のとおりです

Stadard on CPU:                  23.895417

Optimized first step CPU:        4.472087
Optimized second step CPU:       4.926603
Optimized third step CPU:        2.394958
Total time CPU:                  11.793648
于 2016-11-18T15:39:12.667 に答える