设为首页 加入收藏

TOP

C++AMP介绍(一)
2015-07-20 17:30:59 来源: 作者: 【 】 浏览:2
Tags: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 
  
   
#include 
   
     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
    
      a(size, aCPP); array_view
     
       b(size, bCPP); array_view
      
        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 
  
   
#include 
   
     #include 
    
      using namespace concurrency; const int size = 5; //带restrict(amp)约束的函数只能使用C++标准的子集,称为kernel函数, //在GPU上运行,只能被带有restrict(amp)约束的上下文(代码段)调用 void AddElementsWithRestrictedFunction( index<1> idx, array_view
     
       sum, array_view
      
        a, array_view
       
         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
        
          a(5, aCPP); array_view
         
           b(5, bCPP); array_view
          
            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)约束的数学函数在
           
            头文件中声明。 标准C++库
            
             头文件中声明的数学函数在fast_math和precise_math空间中都能找到。 */ void MathExample() { double numbers[] = { 1.0, 10.0, 60.0, 100.0, 600.0, 1000.0 }; array_view
             
               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



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 2425-Hiking Trip(BFS+优先队.. 下一篇C++_系列自学课程_第_11_课_类型..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++ Lambda表达式保 (2025-12-26 05:49:45)
·C++ Lambda表达式的 (2025-12-26 05:49:42)
·深入浅出 C++ Lambda (2025-12-26 05:49:40)
·C语言指针从入门到基 (2025-12-26 05:21:36)
·【C语言指针初阶】C (2025-12-26 05:21:33)