程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 初探C# GPU通用計算技術

初探C# GPU通用計算技術

編輯:C#入門知識

GPU 的並行計算能力高於 CPU,所以最近也有很多利用 GPU 的項目出現在我們的視野中,在 InfoQ 上看到這篇介紹 Accelerator-V2 的文章,它是微軟研究院的研究項目,需要注冊後才能下載,感覺作為我接觸 GPU 通用運算的第一步還不錯,於是去下載了回來。

 

在安裝包裡,包含了幾個例子程序,比如著名的 Life 游戲,不過,Life 游戲,相對於剛接觸 GPU 運算的我,還是稍顯復雜了。於是簡化一下,只是進行一些簡單的計算,發現,DX9Target.ToArray 如果返回參數是 int 數組的話,則會爆出“未支持的操作”的異常,想想也對,顯卡確實是精於浮點運算的。

 

本來,我以為,GPU 運算是 DirectX 11 才有的功能,但是 Accelerator 支持的卻是 DirectX 9,想來 DirectX 11 支持的運算能力更高、方式更簡單吧。

 

為了簡單比較一下 CPU 和 GPU 的速度,也寫了一個 .net 4 的並行運算的程序,因為 DX9Target 不支持 int,所以這裡的數組也用 float,如下:

 

 

\\代碼
private const int GridSize = 1024;
private float[] _map;

public Form1()
{
InitializeComponent();
_map = new float[GridSize * GridSize];
for (int y = 0; y < GridSize; y++)
{
for (int x = 0; x < GridSize; x++)
{
_map[x * GridSize + y] = x * y;
}
}
Render();
}

private void Start_Click(object sender, EventArgs e)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
_map = _map.AsParallel().Select(p => p * p * p / 4 + 194).ToArray();
var time = stopwatch.ElapsedMilliseconds;
this.Text = time.ToString();
Render();
}

private void Render()
{
var workingBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

for (int y = 0; y < pictureBox1.Height; y++)
{
for (int x = 0; x < pictureBox1.Width; x++)
{
workingBitmap.SetPixel(x, y, Color.FromArgb(-0x1000000 | (int)_map[x * 2 * GridSize + y * 2]));
}
}
pictureBox1.Image =<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved