2015-06-07 61 views
0

我剛剛開始學習有關Parallel.For()在c#中,我有一個功能,做一些事情,我不知道如何調整它爲並行。 任何人都可以向我展示這種類似案例嗎?在c#中調整並行的函數#

更新:

for (int i = 0; i < (samples32array.Length); i += 35) //Delimiter Samples Length = SampleRate*DelimiterTimeLength,(i.e: 44100*0.2=8820 samples) 
{ 
    if ((i + (int)DelimiterSamplesCount - 1) > samples32array.Length) 
    { 
        break; 
    } 
    double power = MyGoertzel.GoertzelFilter(samples32array, i, i + (int)DelimiterSamplesCount - 1, coeffDelimiter); 
    if (power > max_power) 
    { 
     max_power = power; 
     max_power_index = i; 
    } 
} 

更新:這是我的tryings但事情錯了......

private int delimiter_search_Parallel(float[] samples32array, int DelimiterSamplesCount, double DelimiterFreq) 
    { 
     double normalizedfreqDelimiter = DelimiterFreq/44100; 
     double coeffDelimiter = 2 * Math.Cos(2 * Math.PI * normalizedfreqDelimiter); 
     double max_power = 0; 
     int max_power_index = 0; 
     List<double> powerParallelList = new List<double>(); 
     List<int> powerParallelListIndex = new List<int>(); 
     Parallel.For(0, samples32array.Length, (i, loopState) => 
      { 
       if ((i + (int)DelimiterSamplesCount - 1) > samples32array.Length) 
       { 
        //TODO -> Genereate a Windows with ERROR Exception 
        loopState.Stop(); 
        return; 
       } 
       else 
       { 
        double power = MyGoertzel.GoertzelFilter(samples32array, i, i + (int)DelimiterSamplesCount - 1, coeffDelimiter); 
        powerParallelList.Add(power); 
        powerParallelListIndex.Add(i); 
       } 
      } 
      ); 
     max_power = powerParallelList.Max(); 
     max_power_index = powerParallelListIndex[powerParallelList.IndexOf(max_power)]; 
     return max_power_index; 
    } 
+3

你不能並行化。每次迭代都需要先前迭代的答案。 – SimpleVar

+0

謝謝。現在明白了。 – axcelenator

+0

你好@YoryeNathan,我更新了代碼。對不起,我放錯了一個 – axcelenator

回答

1

您應該使用Parallel.For重載,它允許您擁有線程本地狀態,您可以在其中保持當前的最大值。

private int delimiter_search_Parallel(float[] samples32array,int DelimiterSamplesCount,double DelimiterFreq) { 
    if(samples32array.Length%35<DelimiterSamplesCount-1) { 
     //TODO -> Genereate a Windows with ERROR Exception 
    } 
    double normalizedfreqDelimiter=DelimiterFreq/44100; 
    double coeffDelimiter=2*Math.Cos(2*Math.PI*normalizedfreqDelimiter); 
    object lockObject=new object(); 
    Tuple<double,int> result=null; 
    Parallel.For(0,(samples32array.Length-(DelimiterSamplesCount-1))/35+1,() => (Tuple<double,int>)null,(i,loopState,max) => { 
     i*=35; 
     double power=MyGoertzel.GoertzelFilter(samples32array,i,i+(int)DelimiterSamplesCount-1,coeffDelimiter); 
     return max==null||power>max.Item1?Tuple.Create(power,i):max; 
    },max => { 
     if(max!=null) { 
      lock(lockObject) { 
       if(result==null||max.Item1>result.Item1) { 
        result=max; 
       } 
      } 
     } 
    }); 
    return result.Item2; 
} 
1

你的目的,我建議使用一個ConcurrentDictionary<int,double>dictPower類似於以下樣品(編號:https://msdn.microsoft.com/en-us/library/dd287191%28v=vs.110%29.aspx

// dictionary with the desired concurrencyLevel and initialCapacity 
ConcurrentDictionary<int, double> cd = new ConcurrentDictionary<int, double>(concurrencyLevel, initialCapacity); 

(其中併發級別通常等於CPU內核的數量),並相應地修改代碼:代替

powerParallelList.Add(power); 
powerParallelListIndex.Add(i); 

使用:

dictPower.Add(i,power) 

在該Parallel.For循環完成時,剛計算ItemsMax()dictPower(根據您的要求),或根據需要應用任何其他功能。

希望這可能有所幫助。