Thrust快速入門教程(二)——Vector的使用


Trust 提供了兩個vector容器:host_vector device_vector。按照命名規則,host_vector位於主機端,device_vector位於GPU設備端。Trustvector容器與STL中的容器類似,是通用的容器,可以存儲任何數據類型,可以動態調整大小。以下源代碼展示如何使用Thrustvector容器。

  1. # include <thrust / host_vector .h>   
  2. # include <thrust / device_vector .h>   
  3. # include <iostream >   
  4. int main ( void )  
  5. {  
  6. // H has storage for 4 integers   
  7. thrust :: host_vector <int > H (4);  
  8. // initialize individual elements   
  9. H [0] = 14;  
  10. H [1] = 20;  
  11. H [2] = 38;  
  12. H [3] = 46;  
  13. // H. size () returns the size of vector H   
  14. std :: cout << "H has size " << H. size () << std :: endl ;  
  15. // print contents of H   
  16. for ( int i = 0; i < H. size (); i ++)  
  17. std :: cout << "H[" << i << "] = " << H[i] << std :: endl ;  
  18. // resize H   
  19. H. resize (2) ;  
  20. std :: cout << "H now has size " << H. size () << std :: endl ;  
  21. // Copy host_vector H to device_vector D   
  22. thrust :: device_vector <int > D = H;  
  23. // elements of D can be modified   
  24. D [0] = 99;  
  25. D [1] = 88;  
  26. // print contents of D   
  27. for ( int i = 0; i < D. size (); i ++)  
  28. std :: cout << "D[" << i << "] = " << D[i] << std :: endl ;  
  29. // H and D are automatically deleted when the function returns   
  30. return 0;  
  31. }  
 

 

個例子所示,運算符”=”可以用來復制host_vector

device_vector(反之亦然)。 運算符”=”也可以用來復制host_vectorhost_vectordevice_vectordevice_vector同樣device_vector訪問單個元素可以使用准的括號表示法。但是,由於每次訪問需要調cudaMemcpy應謹慎使用。下面我將看看一些更有效的技

初始化所有向量的元素特定、或從一個vector向另一個拷特定是非常常用的技術Thrust提供了一些方法可以完成些種操作。

  1. # include <thrust / host_vector .h>   
  2. # include <thrust / device_vector .h>   
  3. # include <thrust / copy .h>   
  4. # include <thrust / fill .h>   
  5. # include <thrust / sequence .h>   
  6. # include <iostream >   
  7. int main ( void )  
  8. {  
  9. // initialize all ten integers of a device_vector to 1   
  10. thrust :: device_vector <int > D(10 , 1);  
  11. // set the first seven elements of a vector to 9   
  12. thrust :: fill (D. begin () , D. begin () + 7, 9);  
  13. // initialize a host_vector with the first five elements of D   
  14. thrust :: host_vector <int > H(D. begin () , D. begin () + 5);  
  15. // set the elements of H to 0, 1, 2, 3, ...   
  16. thrust :: sequence (H. begin () , H. end ());  
  17. // copy all of H back to the beginning of D   
  18. thrust :: copy (H. begin () , H. end () , D. begin ());  
  19. // print D   
  20. for ( int i = 0; i < D. size (); i ++)  
  21. std :: cout << "D[" << i << "] = " << D[i] << std :: endl ;  
  22. return 0;  
  23. }  
 

 

里我看到了fillcopysequence的使用方法。copy函數可以用來拷主機端或者設備端的數據到另外一個vector。與STL中的似,fill用於簡單的向一段元素賦特定值。sequence可以用來生成等差數列。

 

Thrust命名空間

你可能會注意到在我們的例子中使用了thrust::host_vector thrust::copy的字段。其中thrust::告訴編譯器在thrust命名空間中查找函數與類。命名空間是一個很好的方式避免命名重復。例如,thrust::copy就可以與STL中的std::copy區別開來。C++的命名空間允許我們使用這兩個copy函數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM