C++AMP介绍(一)

时间:2022-07-29 13:36:32

C++AMP介绍(一)

最后更新日期:2014-05-02

阅读前提

环境:Windows 8.1 64bit英文版,Visual Studio 2013 Professional Update1英文版。Nvidia QuadroK600 显卡

内容简单介绍

介绍C++ AMP怎样使用加速器(GPU)的并发运行能力。通过两个尽可能简洁的程序,让用户了解到怎样把AMP应用到自己的程序开发其中。

正文

C++AMP (C++ Accelerated Massive Parallelism)利用并行硬件(比如独立图形加速卡)的性能。加速你C++程序的运行速度,C++ AMP编程模型包含支持多维数组。索引。内存传输和平铺,包含数学函数库。你能够使用C++ AMP更广泛的控制CPU同GPU之间数据的传递。

C++ AMP要求你的显卡完整支持DirectX11硬件特性。

在Visual Studio上建立Win32 控制台项目。以下是我第一个C++AMP应用程序源码

#include "stdafx.h"

#include <amp.h>
#include <iostream>
using namespace concurrency; const int size = 5; void CppAmpMethod() {
int aCPP[] = { 1, 2, 3, 4, 5 };
int bCPP[] = { 6, 7, 8, 9, 10 };
int sumCPP[size]; //concurrency::array_view是AMP的数据包装器。可作为智能指针使用,代表了一维或多维数组。
//第一个模板參数是数据类型,第二个模板參数是维度。 //第一个构造參数是数组中元素的数量,第二个构造參数是数组
array_view<const int, 1> a(size, aCPP);
array_view<const int, 1> b(size, bCPP);
array_view<int, 1> sum(size, sumCPP); //调用dsicard_data方法。是为了避免sum包装器中的数据拷贝到GPU
//此方法的调用不能出如今有restrict(amp)约束的上下文(代码段)中
sum.discard_data(); parallel_for_each(
//sum.extent代表计算域,在这上面将会建立线程集合
//由于数组中有5个元素。所以会建立5根线程分别执行
sum.extent,
//Lambda表达式定义在加速器上各个线程将会执行的代码
//restrict(amp)是Microsoft AMP引入的约束符号,要求Lambda执行在GPU上
//默认值是restrict(cpu)约束在CPU上执行。所以不加约束能够在不论什么标准C++编译器中正确编译
//约束还能够是restrict(cpu,amp),没有其他。
//index类用来索引array_view中的元素,index模板參数表示idx的维度
[=](index<1> idx) restrict(amp)
{
//restrict(amp)约束使lambda表达式无法捕获到外面的引用型和指针型变量
//仅仅能使用concurrency::array_view容器,输入输出数据
sum[idx] = a[idx] + b[idx];
}
); // 打印输出结果. 正确的输出应该是 "7, 9, 11, 13, 15".
for (int i = 0; i < size; i++) {
std::cout << sum[i] << "\n";
} //更新sum包装器指向的数据源,即sumCPP中的数据(元素)
sum.synchronize(); // 打印输出结果. 正确的输出应该是 "7, 9, 11, 13, 15".
for (int i = 0; i < size; i++) {
std::cout << sumCPP[i] << "\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
CppAmpMethod(); system("pause"); return 0;
}

第二个C++ AMP程序演示怎样自己编写带restrict(amp)修饰的函数。以及怎样调用它。

#include "stdafx.h"

#include <amp.h>
#include <amp_math.h>
#include <iostream>
using namespace concurrency; const int size = 5; //带restrict(amp)约束的函数仅仅能使用C++标准的子集,称为kernel函数,
//在GPU上执行。仅仅能被带有restrict(amp)约束的上下文(代码段)调用
void AddElementsWithRestrictedFunction(
index<1> idx, array_view<int, 1> sum, array_view<int, 1> a, array_view<int, 1> b) restrict(amp)
{
sum[idx] = a[idx] + b[idx];
} void AddArraysWithFunction() { int aCPP[] = { 1, 2, 3, 4, 5 };
int bCPP[] = { 6, 7, 8, 9, 10 };
int sumCPP[5]; array_view<int, 1> a(5, aCPP);
array_view<int, 1> b(5, bCPP);
array_view<int, 1> sum(5, sumCPP);
sum.discard_data(); parallel_for_each(
sum.extent,
[=](index<1> idx) restrict(amp)
{
//调用restrict(amp)约束的函数
AddElementsWithRestrictedFunction(idx, sum, a, b);
}
); for (int i = 0; i < 5; i++) {
std::cout << sum[i] << "\n";
}
} /*
C++ AMP 带了两个数学库。 在名字空间Concurrency::precise_math的双精度库,也提供单精度数学函数。
在Concurrency::fast_math名字空间的单精度库,仅仅提供单精度数学函数。
能够使用accelerator::supports_double_precision属性推断GPU是否支持双精度库。
这些带restrict(amp)约束的数学函数在<amp_math.h>头文件里声明。
标准C++库<cmath>头文件里声明的数学函数在fast_math和precise_math空间中都能找到。 */
void MathExample() { double numbers[] = { 1.0, 10.0, 60.0, 100.0, 600.0, 1000.0 };
array_view<double, 1> logs(6, numbers); parallel_for_each(
logs.extent,
[=](index<1> idx) restrict(amp) {
logs[idx] = concurrency::fast_math::log10(logs[idx]);
}
); for (int i = 0; i < 6; i++) {
std::cout << logs[i] << "\n";
}
} int _tmain(int argc, _TCHAR* argv[])
{
//測试这里写的带restrict(amp)约束的函数
AddArraysWithFunction(); //測试C++ AMP提供的带restrict(amp)约束的数学函数
MathExample(); system("pause"); return 0;
}

如今你应该已经学会了C++AMP的编程方式,下一篇介绍C++ AMP关于性能优化方面的基本知识。

參考资料

http://msdn.microsoft.com/zh-cn/library/vstudio/hh265136(v=vs.120).aspx

http://blogs.msdn.com/b/nativeconcurrency/archive/2011/09/13/c-amp-in-a-nutshell.aspx

C++ AMP (C++ Accelerated MassiveParallelism)

http://msdn.microsoft.com/zh-cn/library/hh265137.aspx