东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1398|回复: 2
打印 上一主题 下一主题

[C/C++] thrust(unary与binary)中放复杂类到GPU加速

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
跳转到指定楼层
楼主
发表于 2021-7-12 09:21:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式







thrust(unary与binary)中放复杂类到GPU加速


  1. #include <thrust/device_vector.h>
  2. #include <iostream>
  3. #include <string.h>

  4. using namespace std;

  5. //thrust(unary与binary)中放复杂类到GPU加速


  6. struct Teacher
  7. {
  8.   //string name;  // string类型不支持cuda吗? string本身就是stl
  9.   char name[64];  //基础数据类型
  10.   int age;
  11.   

  12.   __host__ __device__
  13.   Teacher() {
  14.     // : name("xxx"),age(10)
  15.     age = 10;
  16.     strcpy(name, "XXX");
  17.     //error: identifier "std::cout" is undefined in device code
  18.     //cout << "无参构造" << endl;
  19.   }
  20.   
  21.   __host__ __device__
  22.   Teacher(const char *pname, int _age) {
  23.     //warning: conversion from a string literal to "char *" is deprecated
  24.     //在所有声明char *的地方都要加上const
  25.     age = _age;
  26.     strcpy(name, pname);   //这个支持gpu操作?
  27.   }

  28.   __host__
  29.   void print_teacher_info(){
  30.     //warning: address of a host variable "std::cout"
  31.     std::cout << "name=" << name << ",age=" << age << std::endl;
  32.   }

  33. };


  34. struct get_age_unary_op
  35.   : public thrust::unary_function< Teacher, int >
  36. {
  37.   //  unary_op; //用于transform 一元操作
  38.   
  39.   __host__ __device__
  40.   int operator()(const Teacher& t) const
  41.   {
  42.     //函数对象:()操作符的重载
  43.     return t.age;
  44.   }
  45. };

  46. struct jjj_plus : public thrust::binary_function<int,int,int>
  47. {
  48.     //自定义二元函数对象   这是基础数据类型的
  49.   __host__ __device__
  50.   int operator()(int a, int b)
  51.   {
  52.     return a+b;
  53.   }
  54. };



  55. int main(){

  56.     int a[6] = {1,2,3,4,5,6};
  57.     thrust::device_vector<int> d1(a,a+6);
  58.     thrust::copy(d1.begin(),d1.end(), std::ostream_iterator<int>(std::cout, "\n"));

  59.     int sum1 = thrust::reduce(d1.begin(),d1.end());
  60.     int sum2 = thrust::reduce(d1.begin(),d1.end(),0,thrust::plus<int>());
  61.     int sum3 = thrust::reduce(d1.begin(),d1.end(),0,jjj_plus());

  62.     std::cout << "求和1=" << sum1 << std::endl;
  63.     std::cout << "求和2=" << sum2 << std::endl;
  64.     std::cout << "求和3=" << sum3 << std::endl;

  65.     //求老师的年龄之和呢?
  66.     Teacher t1("zhang_san", 30), t2("li_si", 33), t3;
  67.     t1.print_teacher_info();
  68.     t2.print_teacher_info();
  69.     t3.print_teacher_info();
  70.     //Teacher t_array[3] = {t1,t2,t3};
  71.     //thrust::device_vector<Teacher> d2(t_array,t_array+3);
  72.     thrust::device_vector<Teacher> teacher_array;
  73.     //thrust::system::system_error'
  74.     //what():  for_each: failed to synchronize: cudaErrorLaunchFailure: unspecified launch failure
  75.     teacher_array.push_back(t1);
  76.     teacher_array.push_back(t2);
  77.     teacher_array.push_back(t3);
  78.     // 1元操作用来 转换 容器里的每个元素  2元操作用来操作相邻的value
  79.     int result_sum = thrust::transform_reduce(teacher_array.begin(), teacher_array.end(), get_age_unary_op(), 0, jjj_plus());
  80.     std::cout << "result_sum=" << result_sum << std::endl;

  81.    
  82.     return 0;
  83. }


  84. /**
  85. for_each: failed to synchronize: cudaErrorLaunchFailure: unspecified launch failure
  86. 已放弃 (核心已转储)


  87. 具体代码:
  88. thrust::device_vector<Teacher> teacher_array(3);

  89. Teacher里有成员变量:
  90. string name;  // string类型不支持cuda吗?

  91. 分析:string是c++的stl

  92. 改用 char name[64];


  93.     string name = "jiangjinju";
  94.     //字符串怎么放到gpu上去?
  95.     thrust::device_vector<string> d1(1);  //会报错
  96. */
复制代码




让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
沙发
 楼主| 发表于 2021-7-12 10:04:58 | 只看该作者
cpu与gpu适配代码,thrust::reduce有点混乱啊

  1. //#include <thrust/transform_reduce.h>
  2. #include <thrust/device_vector.h>
  3. //#include <thrust/pair.h>
  4. #include <thrust/random.h>
  5. //#include <thrust/extrema.h>

  6. // This example shows how to compute a bounding box
  7. // for a set of points in two dimensions.

  8. //cpu与gpu适配代码,thrust::reduce有点混乱啊

  9. struct point2d
  10. {
  11.   float x, y;   //二维坐标点
  12.   
  13.   __host__ __device__
  14.   point2d() : x(0), y(0) {}
  15.   
  16.   __host__ __device__
  17.   point2d(float _x, float _y) : x(_x), y(_y) {}
  18. };

  19. // bounding box type
  20. struct bbox
  21. {
  22.   // construct an empty box
  23.   __host__ __device__
  24.   bbox() {}

  25.   // construct a box from a single point
  26.   __host__ __device__
  27.   bbox(const point2d &point)
  28.     : lower_left(point), upper_right(point)
  29.   {}

  30.   // construct a box from a single point    box1=box2=box3;
  31.   __host__ __device__
  32.   bbox& operator=(const point2d &point)
  33.   {
  34.     lower_left = point;
  35.     upper_right = point;
  36.     return *this;  //为了三连等
  37.   }

  38.   // construct a box from a pair of points
  39.   __host__ __device__
  40.   bbox(const point2d &ll, const point2d &ur)
  41.     : lower_left(ll), upper_right(ur)
  42.   {}

  43.   point2d lower_left, upper_right; //一个box 是由两个点 组成的
  44. };

  45. // reduce a pair of bounding boxes (a,b) to a bounding box containing a and b
  46. struct bbox_reduction : public thrust::binary_function<bbox,bbox,bbox>
  47. {
  48.   //由两个box 变成一个box (能够覆盖掉的最大box)
  49.   __host__ __device__
  50.   bbox operator()(bbox a, bbox b)
  51.   {
  52.     // lower left corner
  53.     point2d ll(thrust::min(a.lower_left.x, b.lower_left.x), thrust::min(a.lower_left.y, b.lower_left.y));
  54.    
  55.     // upper right corner
  56.     point2d ur(thrust::max(a.upper_right.x, b.upper_right.x), thrust::max(a.upper_right.y, b.upper_right.y));
  57.    
  58.     return bbox(ll, ur);
  59.   }
  60. };

  61. int main(void)
  62. {
  63.   const size_t N = 40;
  64.   thrust::default_random_engine rng(6666);
  65.   thrust::uniform_real_distribution<float> u01(0.0f, 1.0f);
  66.   
  67.   // allocate storage for points  很多点的 一个数组
  68.   thrust::device_vector<point2d> points(N);
  69.   
  70.   // generate some random points in the unit square
  71.   for(size_t i = 0; i < points.size(); i++)
  72.   {
  73.       float x = u01(rng);
  74.       float y = u01(rng);
  75.       points[i] = point2d(x,y); // 这里是用copy构造 还是 operator=
  76.   }
  77.   
  78.   // initial bounding box contains first point
  79.   bbox init = bbox(points[0], points[0]);
  80.   
  81.   // binary reduction operation
  82.   bbox_reduction binary_op;
  83.   
  84.   // compute the bounding box for the point set
  85.   // 为啥:bbox_reduction 二元操作 要求是两个bbox啊? 这里是点啊?
  86.   // 虽然初始的值是bbox 但是在点上遍历啊? 第二个如何由点变box呢?  这里感觉混乱?
  87.   bbox result = thrust::reduce(points.begin(), points.end(), init, binary_op);
  88.   
  89.   // print output
  90.   std::cout << "bounding box " << std::fixed;
  91.   std::cout << "(" << result.lower_left.x  << "," << result.lower_left.y  << ") ";
  92.   std::cout << "(" << result.upper_right.x << "," << result.upper_right.y << ")" << std::endl;
  93.   
  94.   return 0;
  95. }
复制代码
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

98

帖子

200

积分

中级会员

Rank: 3Rank: 3

积分
200
板凳
发表于 2021-11-23 19:36:25 | 只看该作者
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-19 14:01 , Processed in 0.170432 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表