Vulkan

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

http://blog.csdn.net/dreampursue/article/details/6278737

Trust 提供了两个vector容器:host_vector 与 device_vector。按照命名规则,host_vector位于主机端,device_vector位于GPU设备端。Trust的vector容器与STL中的容器类似,是通用的容器,可以存储任何数据类型,可以动态调整大小。以下源代码展示如何使用Thrust的vector容器。

  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_vector到host_vector或device_vector到device_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. }  
 

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

 

Thrust命名空间

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

posted on 2012-11-29 10:40  Vulkan  阅读(650)  评论(0编辑  收藏  举报

导航