Skip to content

Commit 09d392f

Browse files
author
Dan
committed
Added thrust tutorial.
1 parent a80e0cf commit 09d392f

File tree

1 file changed

+34
-4
lines changed
  • samples/cpp/tutorial_code/gpu/gpu-thrust-interop

1 file changed

+34
-4
lines changed

samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "Thrust_interop.hpp"
2+
#include <opencv2/core/cuda_stream_accessor.hpp>
23

34
#include <thrust/transform.h>
45
#include <thrust/random.h>
56
#include <thrust/sort.h>
7+
#include <thrust/system/cuda/execution_policy.h>
68
struct prg
79
{
810
float a, b;
9-
11+
1012
__host__ __device__
1113
prg(float _a = 0.f, float _b = 1.f) : a(_a), b(_b) {};
1214

@@ -39,6 +41,25 @@ template<typename T> struct pred_eq
3941
return val.val[channel] == value;
4042
return false;
4143
}
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+
}
4263
};
4364

4465

@@ -80,9 +101,18 @@ int main(void)
80101
auto count = thrust::count(GpuMatBeginItr<int>(d_value), GpuMatEndItr<int>(d_value), 15);
81102
std::cout << count << std::endl;
82103
}
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+
}
84116

85-
86-
87117
return 0;
88118
}

0 commit comments

Comments
 (0)