赞
踩
感觉AMP代码很好懂
#include <amp.h>
using namespace concurrency;
extern "C" __declspec ( dllexport ) void _stdcall square_array(float* arr, int n)
{
// Create a view over the data on the CPU
array_view<float,1> dataView(n, &arr[0]);
// Run code on the GPU
parallel_for_each(dataView.extent, [=] (index<1> idx) restrict(amp)
{
dataView[idx] = dataView[idx] * dataView[idx];
});
// Copy data from GPU to CPU
dataView.synchronize();
}

parallel_for_each 类似 C#的 Parallel.ForEach
第12行的lambda 更是c#里面天天用的,看到以后很激动
dataView.extend 貌似是需要同时启动的算子数目
[=] 是 [dataView] 的缩写,
在c#里面lambda是没有“[]”这个的, []表示要进入lambda函数闭包内的变量。
c#是自动分析的,而vc++11需要手工列出传入的变量。
array_view(AMP引用数组) 传值
array 传引用(AMP数组) (传引用加&)
restrict(amp) 是限定这个函数进行语法检查的一个标记,括号里可以写amp 或cpu
如果是amp (GPU)则,函数体里面的内容只能是c++11的一个子集,有些写法是不允许的。具体msdn上有描述
函数体不解释。至于Idx可以理解为并行算子的编号。
dataView.synchronize();
将GPU上的数据写回CPU
很简单真的,几乎没有一行不明白。
===============================================================================
- [DllImport("Win32Project1", CallingConvention = CallingConvention.StdCall)]
- extern unsafe static void square_array(float* array, int length);
-
- unsafe void Do()
- {
- // Allocate an array
- float[] arr = this.textBox1.Text
- .Split(' ')
- .Select(v=> Convert.ToSingle(v) ).ToArray();
-
- // Square the array elements using C++ AMP
- fixed (float* arrPt = &arr[0])
- {
- square_array(arrPt, arr.Length);
- }
-
-
- this.textBox2.Text = string.Join(" ", arr);
- }

读取textbox的字符串转换为 浮点数组,然后调用amp函数,然后输出到另外一个textBox中。
http://files.cnblogs.com/xzbrillia/%E5%AD%A6%E4%B9%A0amp1.rar
五、参考地址
http://blogs.msdn.com/b/pfxteam/archive/2011/09/21/10214538.aspxCopyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。