C # GPU general computing technology

Source: Internet
Author: User

GPU's parallel computing capability is higher than the CPU, so recently there are also a lot of projects using GPU appear in our field of view, on InfoQ saw this article about Accelerator-V2, it is a research project of Microsoft Research Institute. It needs to be registered before it can be downloaded. I feel that it is a good first step in accessing general GPU computing, So I downloaded it back.

 

In the installation package, there are several example programs, such as the famous Life game. However, the Life game is a little more complex than the one I just got started with GPU computing. To simplify the process, just perform some simple calculations and find that DX9Target. if the return parameter of ToArray is an int array, an "unsupported operation" exception may occur. Think too, the video card is indeed excellent in floating point operations.

 

Originally, I thought that GPU computing is only available in DirectX 11, but the Accelerator supports DirectX 9. If you want DirectX 11 to support a higher computing capability and a simpler method.

 

To compare the speed of CPU and GPU, we also wrote a. net 4 parallel computing program. Because DX9Target does not support int, float is also used for the array here, as shown below:

 

 

Code
 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 =<

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.