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应用程序源代码

[cpp]  view plain  copy
  1. #include "stdafx.h"  
  2.   
  3. #include <amp.h>  
  4. #include <iostream>  
  5. using namespace concurrency;  
  6.   
  7. const int size = 5;  
  8.   
  9. void CppAmpMethod() {  
  10.     int aCPP[] = { 1, 2, 3, 4, 5 };  
  11.     int bCPP[] = { 6, 7, 8, 9, 10 };  
  12.     int sumCPP[size];  
  13.   
  14.     //concurrency::array_view是AMP的数据包装器,可作为智能指针使用,代表了一维或多维数组。  
  15.     //第一个模板参数是数据类型,第二个模板参数是维度。  
  16.     //第一个构造参数是数组中元素的数量,第二个构造参数是数组  
  17.     array_view<const int, 1> a(size, aCPP);  
  18.     array_view<const int, 1> b(size, bCPP);  
  19.     array_view<int, 1> sum(size, sumCPP);  
  20.   
  21.     //调用dsicard_data方法,是为了避免sum包装器中的数据复制到GPU  
  22.     //此方法的调用不能出现在有restrict(amp)约束的上下文(代码段)中  
  23.     sum.discard_data();  
  24.   
  25.     parallel_for_each(  
  26.         //sum.extent代表计算域,在这上面将会建立线程集合  
  27.         //因为数组中有5个元素,所以会建立5根线程分别运行  
  28.         sum.extent,  
  29.         //Lambda表达式定义在加速器上各个线程将会运行的代码  
  30.         //restrict(amp)是Microsoft AMP引入的约束符号,要求Lambda运行在GPU上  
  31.         //默认值是restrict(cpu)约束在CPU上运行,所以不加约束可以在任何标准C++编译器中正确编译  
  32.         //约束还可以是restrict(cpu,amp),没有其它。  
  33.         //index类用来索引array_view中的元素,index模板参数表示idx的维度  
  34.         [=](index<1> idx) restrict(amp)  
  35.     {  
  36.         //restrict(amp)约束使lambda表达式无法捕获到外面的引用型和指针型变量  
  37.         //只能使用concurrency::array_view容器,输入输出数据  
  38.         sum[idx] = a[idx] + b[idx];  
  39.     }  
  40.     );  
  41.   
  42.     // 打印输出结果. 正确的输出应该是 "7, 9, 11, 13, 15".  
  43.     for (int i = 0; i < size; i++) {  
  44.         std::cout << sum[i] << "\n";  
  45.     }  
  46.   
  47.     //更新sum包装器指向的数据源,即sumCPP中的数据(元素)  
  48.     sum.synchronize();  
  49.   
  50.     // 打印输出结果. 正确的输出应该是 "7, 9, 11, 13, 15".  
  51.     for (int i = 0; i < size; i++) {  
  52.         std::cout << sumCPP[i] << "\n";  
  53.     }  
  54. }  
  55. int _tmain(int argc, _TCHAR* argv[])  
  56. {  
  57.     CppAmpMethod();  
  58.   
  59.     system("pause");  
  60.   
  61.     return 0;  
  62. }  

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


[cpp]  view plain  copy
  1. #include "stdafx.h"  
  2.   
  3. #include <amp.h>  
  4. #include <amp_math.h>  
  5. #include <iostream>  
  6. using namespace concurrency;  
  7.   
  8. const int size = 5;  
  9.   
  10. //带restrict(amp)约束的函数只能使用C++标准的子集,称为kernel函数,  
  11. //在GPU上运行,只能被带有restrict(amp)约束的上下文(代码段)调用  
  12. void AddElementsWithRestrictedFunction(  
  13.     index<1> idx, array_view<int, 1> sum, array_view<int, 1> a, array_view<int, 1> b) restrict(amp)  
  14. {  
  15.     sum[idx] = a[idx] + b[idx];  
  16. }  
  17.   
  18.   
  19. void AddArraysWithFunction() {  
  20.   
  21.     int aCPP[] = { 1, 2, 3, 4, 5 };  
  22.     int bCPP[] = { 6, 7, 8, 9, 10 };  
  23.     int sumCPP[5];  
  24.   
  25.     array_view<int, 1> a(5, aCPP);  
  26.     array_view<int, 1> b(5, bCPP);  
  27.     array_view<int, 1> sum(5, sumCPP);  
  28.     sum.discard_data();  
  29.   
  30.     parallel_for_each(  
  31.         sum.extent,  
  32.         [=](index<1> idx) restrict(amp)  
  33.     {  
  34.         //调用restrict(amp)约束的函数  
  35.         AddElementsWithRestrictedFunction(idx, sum, a, b);  
  36.     }  
  37.     );  
  38.   
  39.     for (int i = 0; i < 5; i++) {  
  40.         std::cout << sum[i] << "\n";  
  41.     }  
  42. }  
  43.   
  44. /* 
  45. C++ AMP 带了两个数学库, 在名字空间Concurrency::precise_math的双精度库,也提供单精度数学函数。 
  46. 在Concurrency::fast_math名字空间的单精度库,只提供单精度数学函数。 
  47. 可以使用accelerator::supports_double_precision属性判断GPU是否支持双精度库。 
  48. 这些带restrict(amp)约束的数学函数在<amp_math.h>头文件中声明。 
  49. 标准C++库<cmath>头文件中声明的数学函数在fast_math和precise_math空间中都能找到。 
  50. */  
  51. void MathExample() {  
  52.   
  53.     double numbers[] = { 1.0, 10.0, 60.0, 100.0, 600.0, 1000.0 };  
  54.     array_view<double, 1> logs(6, numbers);  
  55.   
  56.     parallel_for_each(  
  57.         logs.extent,  
  58.         [=](index<1> idx) restrict(amp) {  
  59.         logs[idx] = concurrency::fast_math::log10(logs[idx]);  
  60.     }  
  61.     );  
  62.   
  63.     for (int i = 0; i < 6; i++) {  
  64.         std::cout << logs[i] << "\n";  
  65.     }  
  66. }  
  67.   
  68. int _tmain(int argc, _TCHAR* argv[])  
  69. {  
  70.     //测试这里写的带restrict(amp)约束的函数  
  71.     AddArraysWithFunction();  
  72.   
  73.     //测试C++ AMP提供的带restrict(amp)约束的数学函数  
  74.     MathExample();  
  75.   
  76.     system("pause");  
  77.   
  78.     return 0;  
  79. }  

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值