C++AMP介绍(一)

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



  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值