2016-08-24 68 views
1

我有一個c應用程序處理用於Radix4計算的浮點表。現在,我希望它以多核系統上運行它被分成兩個線程,首先,這是功能結構:將c函數拆分爲線程

void radix4(float* x, float* y, N) 
{ 
    for (l = 1; l <= PMAX; l++) 
    { 
     n = pow_4[l];//LUT for power of 4 
     for (c =0; c < n; c=c+4) 
     { 
     //Loading some parameters from a look-up table 
     n2 =LUT_n2[l][c]; 
     N2 =LUT_n2[l][c+1]; 
     N2_2=LUT_n2[l][c+2]; 
     N2_3=LUT_n2[l][c+3]; 
     factor = TWIDDLE_LIMIT/(range*4); 

      while ((k < range) && (range > 7)) 
      {//loading data from input tables 
      //Computing butterflies 
      //Loading twiddles 
      //Computing final values 
      //Store result in the same table 
      } 
      while ((k<range) && (range<=7)) 
      { 
      //loading data from input tables 
      //Computing butterflies 
      //Loading twiddles 
      //Computing final values 
      //Store result in the same table 
      } 
     } 
    } 
} 

的while循環展開。 現在我想了解的是如何知道哪些部分可以拆分成線程,並且可以給出一些關於如何去做的提示,因爲我正在閱讀很多讓我有點困惑的東西。

回答

0

我認爲你可以創建兩個工作的功能。我認爲k和範圍的值已經在程序中定義了。 使用升壓/線程。

約翰

#include <boost/thread.hpp> 

    void task1() 
    { 
     while ((k < range) && (range > 7)) 
     { 
      //loading data from input tables 
      //Computing butterflies 
      //Loading twiddles 
      //Computing final values 
      //Store result in the same table 
     } 
    } 

    void task2() 
    { 
     while ((k<range) && (range<=7)) 
     { 
      //loading data from input tables 
      //Computing butterflies 
      //Loading twiddles 
      //Computing final values 
      //Store result in the same table 
     } 
    } 

void radix4(float* x, float* y, N) 
{ 
    for (l = 1; l <= PMAX; l++) 
    { 
     n = pow_4[l];//LUT for power of 4 
     for (c =0; c < n; c=c+4) 
     { 
     //Loading some parameters from a look-up table 
     n2 =LUT_n2[l][c]; 
     N2 =LUT_n2[l][c+1]; 
     N2_2=LUT_n2[l][c+2]; 
     N2_3=LUT_n2[l][c+3]; 
     factor = TWIDDLE_LIMIT/(range*4); 

     thread thread_1 = thread(task1); 
     thread thread_2 = thread(task2); 

     // join 
     thread_2.join(); 
     thread_1.join(); 

    } 
} 
+1

問題是針對C而不是C++。 – sendaran

+0

改爲使用pthreads庫,工作原理相同 – baliman

+0

1)這不是C! 2)純代碼答案3)爲什麼兩個函數? 4)不,它不「工作相同」!不同的語言! – Olaf

1

它看起來像你想優化的FFT程序。您可能希望查看線程池,因爲您將在應用程序的整個生命週期中計算多個FFT。看看FFTW的網站,看看他們的API是如何構建的。

而對於你的問題的答案,你可以將你的問題「拆分」爲N線程(讓我們選N = 2)。那麼你需要做的是基本上去交錯(即使在陣列的上半部分,而在底部爲N = 2)。並運行另一個例程來計算這些數組的子集的FFT(其中的N)。然後,您可以使用標識/對稱屬性重新放置數組,並重新交錯數組。

希望這會有所幫助。