| | #include <thrust/transform_reduce.h> |
| | #include <thrust/device_vector.h> |
| | #include <thrust/pair.h> |
| | #include <thrust/random.h> |
| | #include <thrust/extrema.h> |
| |
|
| | |
| | |
| |
|
| | struct point2d |
| | { |
| | float x, y; |
| | |
| | __host__ __device__ |
| | point2d() : x(0), y(0) {} |
| | |
| | __host__ __device__ |
| | point2d(float _x, float _y) : x(_x), y(_y) {} |
| | }; |
| |
|
| | |
| | struct bbox |
| | { |
| | |
| | __host__ __device__ |
| | bbox() {} |
| |
|
| | |
| | __host__ __device__ |
| | bbox(const point2d &point) |
| | : lower_left(point), upper_right(point) |
| | {} |
| |
|
| | |
| | __host__ __device__ |
| | bbox& operator=(const point2d &point) |
| | { |
| | lower_left = point; |
| | upper_right = point; |
| | return *this; |
| | } |
| |
|
| | |
| | __host__ __device__ |
| | bbox(const point2d &ll, const point2d &ur) |
| | : lower_left(ll), upper_right(ur) |
| | {} |
| |
|
| | point2d lower_left, upper_right; |
| | }; |
| |
|
| | |
| | struct bbox_reduction : public thrust::binary_function<bbox,bbox,bbox> |
| | { |
| | __host__ __device__ |
| | bbox operator()(bbox a, bbox b) |
| | { |
| | |
| | point2d ll(thrust::min(a.lower_left.x, b.lower_left.x), thrust::min(a.lower_left.y, b.lower_left.y)); |
| | |
| | |
| | point2d ur(thrust::max(a.upper_right.x, b.upper_right.x), thrust::max(a.upper_right.y, b.upper_right.y)); |
| | |
| | return bbox(ll, ur); |
| | } |
| | }; |
| |
|
| | int main(void) |
| | { |
| | const size_t N = 40; |
| | thrust::default_random_engine rng; |
| | thrust::uniform_real_distribution<float> u01(0.0f, 1.0f); |
| | |
| | |
| | thrust::device_vector<point2d> points(N); |
| | |
| | |
| | for(size_t i = 0; i < N; i++) |
| | { |
| | float x = u01(rng); |
| | float y = u01(rng); |
| | points[i] = point2d(x,y); |
| | } |
| | |
| | |
| | bbox init = bbox(points[0], points[0]); |
| | |
| | |
| | bbox_reduction binary_op; |
| | |
| | |
| | bbox result = thrust::reduce(points.begin(), points.end(), init, binary_op); |
| | |
| | |
| | std::cout << "bounding box " << std::fixed; |
| | std::cout << "(" << result.lower_left.x << "," << result.lower_left.y << ") "; |
| | std::cout << "(" << result.upper_right.x << "," << result.upper_right.y << ")" << std::endl; |
| | |
| | return 0; |
| | } |
| |
|