標籤:
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