• 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          
  • 相关阅读:
    linux常用命令整理
    总结五大常用算法!
    python数组(列表、元组及字典)
    网络编程---笔记1
    python3与python2的区别 记录一波
    Python学习week3-python数据结构介绍与列表
    Python学习week2-python介绍与pyenv安装
    Python学习week1-linux文件系统与IO重定向
    Python学习week1-计算机基础
    css3文本多行省略
  • 原文地址:https://www.cnblogs.com/gongxijun/p/4462752.html
Copyright © 2020-2023  润新知