C++ GPU && CPU

 

        

 1 #include <amp.h>
 2 
 3 #include <iostream>
 4 
 5 #include <winbase.h>    //操作系统的底层文件
 6 
 7  
 8 
 9   using namespace concurrency;
10 
11   using namespace std;
12 
13  
14 
15 void main () {
16 
17  
18 
19  
20 
21       int a [] = {1 ,2, 3,4 ,5, 6,7 ,8, 9,10 };
22 
23       array_view<int >av( 10,a );  //GPU计算结构,av存储到GPU显存,根据数组初始化
24 
25       // restrict 定向到GPU
26 
27       // 直接操作AV,(index<1>)操作每一个元素
28 
29  
30 
31       parallel_for_each(av .extent, [=](index< 1>idx )restrict (amp) {av [idx] += 1; });
32 
33      
34 
35       for ( int i = 0; i < 10; i++) {
36 
37            std::cout << av[i ] << endl;
38 
39       }
40 
41  
42 
43       cin.get ();
44 
45  
46 
47 }
48 
49  

 

 

对于GPU和CPU的各自优势:

 

   GPu优势在于多点计算效率高,并发计算

 

   CPU的优势在于单点计算效率高

 

 
 1 #include <amp.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <functional>
 5  
 6 using namespace   concurrency;
 7 using namespace std;
 8  
 9   template < typename T >
10   class my {
11   public :
12     //重载()函数
13       void  operator () (const T & a ) const {
14             std::cout << a << end ;
15       }
16   };
17  
18 int main () {
19       int str [] = { 'a','b' ,'c', 'd','e' ,'f', 'g','h' ,'i', 'j','k' ,'l' };
20  
21       array_view<int > ss (12 , str );
22       parallel_for_each(ss .extent, [=](index< 1>idx )restrict (amp) {
23            ss[idx ] += 1;
24       }
25       );
26       for ( unsigned int i = 0 ; i < 12; i++) {
27            std::cout << static_cast <char>( ss[i ]);
28       }
29       cin.get ();
30       return 0 ;
31 }
32          

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值