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. }  
# include <thrust / host_vector .h> # include <thrust / device_vector .h> # include <iostream > int main ( void ) { // H has storage for 4 integers thrust :: host_vector <int > H (4); // initialize individual elements H [0] = 14; H [1] = 20; H [2] = 38; H [3] = 46; // H. size () returns the size of vector H std :: cout << "H has size " << H. size () << std :: endl ; // print contents of H for ( int i = 0; i < H. size (); i ++) std :: cout << "H[" << i << "] = " << H[i] << std :: endl ; // resize H H. resize (2) ; std :: cout << "H now has size " << H. size () << std :: endl ; // Copy host_vector H to device_vector D thrust :: device_vector <int > D = H; // elements of D can be modified D [0] = 99; D [1] = 88; // print contents of D for ( int i = 0; i < D. size (); i ++) std :: cout << "D[" << i << "] = " << D[i] << std :: endl ; // H and D are automatically deleted when the function returns return 0; } 

 

个例子所示,运算符”=”可以用来复制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. }  
# include <thrust / host_vector .h> # include <thrust / device_vector .h> # include <thrust / copy .h> # include <thrust / fill .h> # include <thrust / sequence .h> # include <iostream > int main ( void ) { // initialize all ten integers of a device_vector to 1 thrust :: device_vector <int > D(10 , 1); // set the first seven elements of a vector to 9 thrust :: fill (D. begin () , D. begin () + 7, 9); // initialize a host_vector with the first five elements of D thrust :: host_vector <int > H(D. begin () , D. begin () + 5); // set the elements of H to 0, 1, 2, 3, ... thrust :: sequence (H. begin () , H. end ()); // copy all of H back to the beginning of D thrust :: copy (H. begin () , H. end () , D. begin ()); // print D for ( int i = 0; i < D. size (); i ++) std :: cout << "D[" << i << "] = " << D[i] << std :: endl ; return 0; } 

 

里我看到了fillcopysequence的使用方法。copy函数可以用来拷主机端或者设备端的数据到另外一个vector。与STL中的似,fill用于简单的向一段元素赋特定值。sequence可以用来生成等差数列。

 

Thrust命名空间

你可能会注意到在我们的例子中使用了thrust::host_vector thrust::copy的字段。其中thrust::告诉编译器在thrust命名空间中查找函数与类。命名空间是一个很好的方式避免命名重复。例如,thrust::copy就可以与STL中的std::copy区别开来。C++的命名空间允许我们使用这两个copy函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值