博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thrust快速入门教程(二)——Vector的使用
阅读量:4326 次
发布时间:2019-06-06

本文共 4321 字,大约阅读时间需要 14 分钟。

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

[cpp]
  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提供了一些方法可以完成些种操作。

[cpp]
  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函数。

转载于:https://www.cnblogs.com/carekee/articles/2409501.html

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_6、SpringBoot2.xHTTP请求配置讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_10、常用json框架介绍和Jackson返回结果处理...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_11、SpringBoot2.x目录文件结构讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_15、SpringBoot2.x配置文件讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_13、jar包方式运行web项目文件上传和访问...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_17、SpringBootTest单元测试实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_14、SpringBoot2.x使用Dev-tool热部署...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_16、注解配置文件自动映射到属性和实体类实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_18、SpringBoot测试进阶高级篇之MockMvc讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_36、SpringBoot整合mybatis之事务处理实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_38、源码编译安装Redis4.x...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_33、SpringBoot2.x整合Mybatis3.x注解实战...
查看>>