偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

淺嘗.NET 4并行計(jì)算 效率已大幅提升

開(kāi)發(fā) 后端
.NET 4中關(guān)于并行計(jì)算方面的提升,讓人激動(dòng)。畢竟這是代表未來(lái)開(kāi)發(fā)技術(shù)發(fā)展的一個(gè)方向。本文也將為大家介紹這方面的內(nèi)容。

隨著Visual Studio 2010正式版的發(fā)布,我們已經(jīng)可以用上.NET 4的所有功能。那么對(duì)于并行計(jì)算的嘗試,是本文的重點(diǎn)。51CTO向您推薦《F#函數(shù)式編程語(yǔ)言》,以便于您全方位了解.NET 4中不同的部分。

我們都知道CPU的性能至關(guān)重要,但主頻已經(jīng)越來(lái)越難以提升,縱向發(fā)展受限的情況下,橫向發(fā)展成為必然——核心數(shù)開(kāi)始越來(lái)越多。然而多核心的利用、并行計(jì)算一直是編程中的難題,大的不說(shuō),就說(shuō)代碼的編寫(xiě),程序員大多都有過(guò)痛苦的經(jīng)歷:多線程的程序代碼量大,編寫(xiě)復(fù)雜,容易出錯(cuò),并且實(shí)際運(yùn)行效率是否理想也較難保證。

為改善這種狀況,.NET 4.0中引入了 TPL(任務(wù)并行庫(kù)),關(guān)于TPL,MSDN的簡(jiǎn)介是:

任務(wù)并行庫(kù) (TPL) 的設(shè)計(jì)是為了能更簡(jiǎn)單地編寫(xiě)可自動(dòng)使用多處理器的托管代碼。使用該庫(kù),您可以非常方便地用現(xiàn)有序列代碼表達(dá)潛在并行性,這樣序列代碼中公開(kāi)的并行任務(wù)將會(huì)在所有可用的處理器上同時(shí)運(yùn)行。通常這會(huì)大大提高速度。

簡(jiǎn)而言之,TPL提供了一系列的類庫(kù),可以使編寫(xiě)并行運(yùn)算的代碼更簡(jiǎn)單和方便。

說(shuō)起來(lái)很簡(jiǎn)單,我們來(lái)看點(diǎn)例子:

  1. void ThreadpoolMatrixMult(int size, double[,] m1, double[,] m2,   
  2.      double[,] result)  
  3.  {  
  4.    int N = size;                             
  5. int P = 2 * Environment.ProcessorCount; // assume twice the procs for   
  6.                                        // good work distribution  
  7.    int Chunk = N / P;                  // size of a work chunk  
  8.    AutoResetEvent signal = new AutoResetEvent(false);   
  9.    int counter = P;                      // use a counter to reduce   
  10.                                         // kernel transitions      
  11.   for (int c = 0; c < P; c++) {         // for each chunk  
  12.     ThreadPool.QueueUserWorkItem(delegate(Object o)  
  13.     {  
  14.       int lc = (int)o;  
  15.   for (int i = lc * Chunk;           // iterate through a work chunk  
  16.      i < (lc + 1 == P ? N : (lc + 1) * Chunk); // respect upper   
  17.                                                // bound  
  18.            i++) {  
  19.         // original inner loop body  
  20.         for (int j = 0; j < size; j++) {  
  21.           result[i, j] = 0;  
  22.           for (int k = 0; k < size; k++) {  
  23.             result[i, j] += m1[i, k] * m2[k, j];  
  24.           }  
  25.         }  
  26.       }  
  27.    if (Interlocked.Decrement(ref counter) == 0) { // use efficient   
  28.                                                 // interlocked   
  29.                                                // instructions        
  30.         signal.Set();  // and kernel transition only when done  
  31.       }  
  32.     }, c);   
  33.   }  
  34.   signal.WaitOne();  

很眼熟但同時(shí)看著也很心煩的代碼吧。在換用TPL后,上面的代碼就可以簡(jiǎn)化為:

  1. void ParMatrixMult(int size, double[,] m1, double[,] m2, double[,] result)  
  2.  {  
  3.    Parallel.For( 0, size, delegate(int i) {  
  4.      for (int j = 0; j < size; j++) {  
  5.       result[i, j] = 0;  
  6.        for (int k = 0; k < size; k++) {  
  7.          result[i, j] += m1[i, k] * m2[k, j];  
  8.        }  
  9.      }  
  10.   });  

舒服多了吧?具體的內(nèi)容請(qǐng)見(jiàn)MSDN的文章 優(yōu)化多核計(jì)算機(jī)的托管代碼。

裝好正式版的VS2010以后,寫(xiě)了段代碼來(lái)測(cè)試下,TPL究竟好不好用。

代碼很簡(jiǎn)單,拿一條字符串和一堆字符串里的每一條分別用LevenshteinDistance算法做字符串相似程度比對(duì)。先用傳統(tǒng)的順序執(zhí)行的代碼跑一遍,記錄下時(shí)間;再換用TPL的并行代碼跑一遍,記錄下時(shí)間。然后比對(duì)兩次運(yùn)行的時(shí)間差異。

  1. using System;  
  2.   using System.Collections.Generic;  
  3.   using System.Linq;  
  4.   using System.Text;  
  5.   using System.Threading.Tasks;  
  6.   using System.Diagnostics;  
  7.     
  8.   namespace ParallelLevenshteinDistance  
  9.   {  
  10.      class Program  
  11.      {  
  12.          static void Main(string[] args)  
  13.          {  
  14.              Stopwatch sw;  
  15.    
  16.              int length;  
  17.              int count;  
  18.              string[] strlist;  
  19.              int[] steps;  
  20.              string comparestring;  
  21.    
  22.              Console.WriteLine("Input string lenth:");  
  23.              length = int.Parse(Console.ReadLine());  
  24.    
  25.              Console.WriteLine("Input string list count:");  
  26.              count = int.Parse(Console.ReadLine());  
  27.    
  28.              comparestring = GenerateRandomString(length);  
  29.              strlist = new string[count];  
  30.              steps = new int[count];  
  31.    
  32.              // prepare string[] for comparison  
  33.              Parallel.For(0, count, delegate(int i)  
  34.              {  
  35.                  strlist[i] = GenerateRandomString(length);  
  36.              });  
  37.    
  38.              Console.WriteLine("{0}Computing...{0}", Environment.NewLine);  
  39.    
  40.              // sequential comparison  
  41.              sw = Stopwatch.StartNew();  
  42.              for (int i = 0; i < count; i++)  
  43.              {  
  44.                  steps[i] = LevenshteinDistance(comparestring, strlist[i]);  
  45.              }  
  46.              sw.Stop();  
  47.              Console.WriteLine("[Sequential] Elapsed:");  
  48.              Console.WriteLine(sw.Elapsed.ToString());  
  49.    
  50.              // parallel comparison  
  51.              sw = Stopwatch.StartNew();  
  52.              Parallel.For(0, count, delegate(int i)  
  53.              {  
  54.                  steps[i] = LevenshteinDistance(comparestring, strlist[i]);  
  55.              });  
  56.              sw.Stop();  
  57.              Console.WriteLine("[Parallel] Elapsed:");  
  58.              Console.WriteLine(sw.Elapsed.ToString());  
  59.                            
  60.              Console.ReadLine();  
  61.          }  
  62.    
  63.          private static string GenerateRandomString(int length)  
  64.          {  
  65.              Random r = new Random((int)DateTime.Now.Ticks);  
  66.              StringBuilder sb = new StringBuilder(length);  
  67.              for (int i = 0; i < length; i++)  
  68.              {  
  69.                 int c = r.Next(97, 123);  
  70.                  sb.Append(Char.ConvertFromUtf32(c));  
  71.              }  
  72.              return sb.ToString();  
  73.          }  
  74.    
  75.          private static int LevenshteinDistance(string str1, string str2)  
  76.          {  
  77.              int[,] scratchDistanceMatrix = new int[str1.Length + 1, str2.Length + 1];  
  78.              // distance matrix contains one extra row and column for the seed values              
  79.              for (int i = 0; i <= str1.Length; i++) scratchDistanceMatrix[i, 0] = i;  
  80.              for (int j = 0; j <= str2.Length; j++) scratchDistanceMatrix[0, j] = j;  
  81.    
  82.              for (int i = 1; i <= str1.Length; i++)  
  83.              {  
  84.                  int str1Index = i - 1;  
  85.                  for (int j = 1; j <= str2.Length; j++)  
  86.                  {  
  87.                      int str2Index = j - 1;  
  88.                      var cost = (str1[str1Index] == str2[str2Index]) ? 0 : 1;  
  89.    
  90.                      int deletion = (i == 0) ? 1 : scratchDistanceMatrix[i - 1, j] + 1;  
  91.                      int insertion = (j == 0) ? 1 : scratchDistanceMatrix[i, j - 1] + 1;  
  92.                int substitution = (i == 0 || j == 0) ? cost : scratchDistanceMatrix[i - 1, j - 1] + cost;  
  93.    
  94.                      scratchDistanceMatrix[i, j] = Math.Min(Math.Min(deletion, insertion), substitution);  
  95.    
  96.                      // Check for Transposition  
  97.   if (i > 1 && j > 1 && (str1[str1Index] == str2[str2Index - 1]) && (str1[str1Index - 1] == str2[str2Index]))  
  98.                      {  
  99. scratchDistanceMatrix[i, j] = Math.Min(scratchDistanceMatrix[i, j], scratchDistanceMatrix[i - 2, j - 2] + cost);  
  100.                     }  
  101.                 }  
  102.             }  
  103.  
  104.             // Levenshtein distance is the bottom right element  
  105.             return scratchDistanceMatrix[str1.Length, str2.Length];  
  106.         }  
  107.  
  108.     }  
  109. }  

這里只用了最簡(jiǎn)單的 Parallel.For 方法,代碼很簡(jiǎn)單和隨意,但是看看效果還是可以的。

測(cè)試機(jī)找了不少,喜歡硬件的朋友興許也能找到你感興趣的:P

Intel Core i7 920 (4物理核心8邏輯核心,2.66G) + DDR3 1600 @ 7-7-7-24

AMD Athlon II X4 630 (4物理核心,2.8G) + DDR3 1600 @ 8-8-8-24

AMD Athlon II X2 240 (2物理核心,2.8G) + DDR2 667

Intel Core E5300 (2物理核心,2.33G) + DDR2 667

Intel Atom N270 (1物理核心2邏輯核心,1.6G) + DDR2 667

還在VM workstation里跑過(guò),分別VM了XP和WIN7,都跑在上述i7的機(jī)器里,各自分配了2個(gè)核心。

程序設(shè)置每個(gè)字符串長(zhǎng)1000個(gè)字符,共1000條字符串。

每個(gè)機(jī)器上程序都跑了3遍,取平均成績(jī),得到下表:

CPU Core Time_Sequential(s) Time_Parallel(s) S/P(%)
Intel Core i7 920 4 Cores, 8 Threads, 2.6G 55.132634 14.645687 376.44%
AMD AthlonII X4 630 4 Cores, 4 Threads, 2.8G 58.10592 17.152494 338.76%
AMD AthlonII X2 240 2 Cores, 2 Threads, 2.8G 66.159735 32.293972 204.87%
Intel E5300 2 Cores, 2 Threads, 2.3G 70.827157 38.50654 183.94%
Intel Atom N270 1 Cores, 2 Threads, 1.6G 208.47852 157.27869 132.55%
VMWin7(2 logic core) 2 Cores, 2 Threads 56.965068 33.069084 172.26%
VMXP(2 logic core) 2 Cores, 2 Threads 59.799399 35.35805 169.13%

可見(jiàn),在多核心處理器上,并行計(jì)算的執(zhí)行速度都得到了大幅提升,即便是在單核心超線程出2個(gè)邏輯核的Atom N270上亦縮短了32.55%的運(yùn)行時(shí)間。在A240上并行計(jì)算的效率竟然是順序計(jì)算的204.87% ?!而同樣是4核心,i7 920在超線程的幫助下,并行執(zhí)行效率提升明顯高過(guò)A630。最后VM里的測(cè)試,是否也可以在某種程度上佐證在多核心的調(diào)度上,Win7要強(qiáng)過(guò)XP呢(純猜測(cè))?順帶可以看到,同樣是i7的硬件環(huán)境,單線程宿主OS(Win7)里執(zhí)行花費(fèi)55.133秒,VM(Win7)里56.965秒,速度上有約3%的差異。

另外,針對(duì)性能較強(qiáng)的i7處理器,加大程序中的2個(gè)變量后再做測(cè)試,并行執(zhí)行的效率比得到了進(jìn)一步的提升。應(yīng)該是因?yàn)閯?chuàng)建/管理/銷毀多線程的開(kāi)銷被進(jìn)一步的攤平的緣故。例如在每字符串2000個(gè)字符,共2000條字符串的情況下,順序執(zhí)行和并行執(zhí)行的時(shí)間分別是07:20.9679066和01:47.7059225,消耗時(shí)間比達(dá)到了409.42%。

 來(lái)幾張截圖:

 

 

運(yùn)行結(jié)果1

運(yùn)行結(jié)果

從截圖中可以發(fā)現(xiàn),這段測(cè)試程序在順序執(zhí)行的部分,內(nèi)存占用相對(duì)平穩(wěn),CPU則大部分核心處在比較空閑的狀態(tài)。到了并行執(zhí)行部分,所有核心都如預(yù)期般被調(diào)動(dòng)起來(lái),同時(shí)內(nèi)存占用開(kāi)始出現(xiàn)明顯波動(dòng)。附圖是在每字符串2000個(gè)字符,共2000條字符串的情況下得到的。

 這里只是非常局部和簡(jiǎn)單的一個(gè)測(cè)試,目的是希望能帶來(lái)一個(gè)直觀的體驗(yàn)。微軟已經(jīng)提供了一組不錯(cuò)的例子可供參考 Samples for Parallel Programming with the .NET Framework 4  

原文標(biāo)題:小試 .NET 4.0 之 并行計(jì)算

鏈接:http://www.cnblogs.com/Elvin/archive/2010/04/20/1716258.html

【編輯推薦】

  1. 使用ASP.NET 4的自動(dòng)啟動(dòng)特性
  2. 詳解.NET 4.0并行計(jì)算支持歷史
  3. 詳讀.NET 4.0環(huán)境配置
  4. 詳解.NET 4.0中異常處理方面的新特性
  5. 三方面詮釋.NET 4.0的新特性
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2023-10-30 08:57:19

.Net開(kāi)發(fā)并行計(jì)算

2009-12-18 09:38:27

.NET 4.0并行計(jì)

2012-12-18 15:33:44

遞歸數(shù)據(jù)并行計(jì)算

2010-03-22 14:45:40

云計(jì)算

2009-06-28 22:55:00

SAN惠普存儲(chǔ)

2021-06-01 05:51:37

云計(jì)算并行計(jì)算分布式計(jì)算

2011-04-20 17:15:21

并行計(jì)算

2011-04-21 09:13:14

并行計(jì)算

2012-08-17 09:32:52

Python

2022-03-21 15:24:27

ThingWorxDPMPTC

2022-11-30 12:41:03

戴爾

2010-03-19 17:23:45

云計(jì)算

2014-04-24 10:25:15

2019-04-18 09:15:05

DaskPython計(jì)算

2014-01-21 11:16:59

MPI并行計(jì)算

2021-04-26 18:27:39

Vue3開(kāi)發(fā)運(yùn)行

2010-03-11 15:23:44

Visual Stud

2010-06-10 08:37:04

并行計(jì)算

2017-04-24 12:07:44

Spark大數(shù)據(jù)并行計(jì)算

2010-06-02 08:53:51

.NET 4并行編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)