AMP (GPU parallel computing, C #, VC ++ 11) Learning (1)

Source: Internet
Author: User

I feel that the amp code is very understandable.

 

I. VC ++ 11 code
   1: #include "stdafx.h"

   2: #include <amp.h>

   3:  

   4: using namespace concurrency;

   5:  

   6: extern "C" __declspec ( dllexport ) void _stdcall square_array(float* arr, int n)

   7: {

   8:     // Create a view over the data on the CPU

   9:     array_view<float,1> dataView(n, &arr[0]);

  10:  

  11:     // Run code on the GPU

  12:     parallel_for_each(dataView.extent, [=] (index<1> idx) restrict(amp)

  13:     {

  14:         dataView[idx] = dataView[idx] * dataView[idx];

  15:     });

  16:  

  17:     // Copy data from GPU to CPU

  18:     dataView.synchronize();

  19: }

 

Concurrency is the same as the thread security data structure name of C. So it is easy to understand

Parallel_for_each is similar to parallel. foreach of C #.

Lambda on the fifth line is used every day in C #. I am very excited to see it.

 

Dataview. Extend seems to be the number of operators to be started at the same time

[=] Is the abbreviation of [dataview,

 

In C #, Lambda does not include "[]". [] indicates the variable to enter the closure of the lambda function.

C # is automatically analyzed, and VC ++ 11 needs to manually list input variables.

 

Array_view (Amp reference array)

Array upload reference (Amp array) (upload reference plus &)

 

Restrict (AMP) is a mark that limits this function to perform a syntax check. You can write the amp or CPU in the brackets.

If it is an amp (GPU), the content in the function body can only be a subset of C ++ 11, and some writing methods are not allowed. Specific descriptions on msdn

 

The function body is not interpreted. Idx can be interpreted as the number of parallel operators.

Dataview. Synchronize ();

Write GPU data back to cpu

 

Very simple, really, almost no line does not understand.

 

========================================================== ========================================================

II. C # Call Code
   1: [DllImport("Win32Project1", CallingConvention = CallingConvention.StdCall)]

   2: extern unsafe static void square_array(float* array, int length);

   3:  

   4: unsafe void Do()

   5: {

   6:     // Allocate an array

   7:     float[] arr = this.textBox1.Text

   8:         .Split(' ')

   9:         .Select(v=> Convert.ToSingle(v) ).ToArray();

  10:  

  11:     // Square the array elements using C++ AMP

  12:     fixed (float* arrPt = &arr[0])

  13:     {

  14:         square_array(arrPt, arr.Length);

  15:     }

  16:  

  17:  

  18:     this.textBox2.Text = string.Join(" ", arr);

  19: }

Convert the string that reads the textbox to a floating point group, call the amp function, and output it to another textbox.

 

Iii. Running Effect

 

 

4. code files

Http://files.cnblogs.com/xzbrillia/%E5%AD%A6%E4%B9%A0amp1.rar

 

V. Reference address

 

Http://blogs.msdn.com/ B /pfxteam/archive/2011/09/21/10214538.aspx

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.