C++ GPU && CPU

來源:互聯網
上載者:User

標籤:

 

        

 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 定向到GPU26 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          

 

C++ GPU && CPU

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.