|
1 | 1 | #include "Thrust_interop.hpp"
|
| 2 | +#include <opencv2/core/cuda_stream_accessor.hpp> |
2 | 3 |
|
3 | 4 | #include <thrust/transform.h>
|
4 | 5 | #include <thrust/random.h>
|
5 | 6 | #include <thrust/sort.h>
|
| 7 | +#include <thrust/system/cuda/execution_policy.h> |
6 | 8 | struct prg
|
7 | 9 | {
|
8 | 10 | float a, b;
|
9 |
| - |
| 11 | + |
10 | 12 | __host__ __device__
|
11 | 13 | prg(float _a = 0.f, float _b = 1.f) : a(_a), b(_b) {};
|
12 | 14 |
|
@@ -39,6 +41,25 @@ template<typename T> struct pred_eq
|
39 | 41 | return val.val[channel] == value;
|
40 | 42 | return false;
|
41 | 43 | }
|
| 44 | + |
| 45 | + __host__ __device__ bool operator()( const thrust::tuple<T, T, T>& val) |
| 46 | + { |
| 47 | + if (channel == 0) |
| 48 | + return thrust::get<0>(val) == value; |
| 49 | + if (channel == 1) |
| 50 | + return thrust::get<1>(val) == value; |
| 51 | + if (channel == 2) |
| 52 | + return thrust::get<2>(val) == value; |
| 53 | + } |
| 54 | +}; |
| 55 | +template<typename T> struct pred_greater |
| 56 | +{ |
| 57 | + T value; |
| 58 | + __host__ __device__ pred_greater(T value_) : value(value_){} |
| 59 | + __host__ __device__ bool operator()(const T& val) const |
| 60 | + { |
| 61 | + return val > value; |
| 62 | + } |
42 | 63 | };
|
43 | 64 |
|
44 | 65 |
|
@@ -80,9 +101,18 @@ int main(void)
|
80 | 101 | auto count = thrust::count(GpuMatBeginItr<int>(d_value), GpuMatEndItr<int>(d_value), 15);
|
81 | 102 | std::cout << count << std::endl;
|
82 | 103 | }
|
83 |
| - |
| 104 | + // Randomly fill an array then copy only values greater than 0. Perform these tasks on a stream. |
| 105 | + { |
| 106 | + cv::cuda::GpuMat d_value(1, 100, CV_32F); |
| 107 | + auto valueBegin = GpuMatBeginItr<float>(d_value); |
| 108 | + auto valueEnd = GpuMatEndItr<float>(d_value); |
| 109 | + cv::cuda::Stream stream; |
| 110 | + thrust::transform(thrust::system::cuda::par.on(cv::cuda::StreamAccessor::getStream(stream)), thrust::make_counting_iterator(0), thrust::make_counting_iterator(d_value.cols), valueBegin, prg(-1, 1)); |
| 111 | + int count = thrust::count_if(thrust::system::cuda::par.on(cv::cuda::StreamAccessor::getStream(stream)), valueBegin, valueEnd, pred_greater<float>(0.0)); |
| 112 | + cv::cuda::GpuMat d_valueGreater(1, count, CV_32F); |
| 113 | + thrust::copy_if(thrust::system::cuda::par.on(cv::cuda::StreamAccessor::getStream(stream)), valueBegin, valueEnd, GpuMatBeginItr<float>(d_valueGreater), pred_greater<float>(0.0)); |
| 114 | + cv::Mat h_greater(d_valueGreater); |
| 115 | + } |
84 | 116 |
|
85 |
| - |
86 |
| - |
87 | 117 | return 0;
|
88 | 118 | }
|
0 commit comments