前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ GPU && CPU

C++ GPU && CPU

作者头像
Gxjun
发布2018-03-26 16:45:01
6840
发布2018-03-26 16:45:01
举报
文章被收录于专栏:mlml
代码语言:javascript
复制
 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的优势在于单点计算效率高

代码语言:javascript
复制
 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          
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-04-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档