Added thrust tutorial.

This commit is contained in:
Dan 2015-09-15 12:17:30 -04:00
parent a80e0cf8bd
commit 09d392f09d

View File

@ -1,8 +1,10 @@
#include "Thrust_interop.hpp" #include "Thrust_interop.hpp"
#include <opencv2/core/cuda_stream_accessor.hpp>
#include <thrust/transform.h> #include <thrust/transform.h>
#include <thrust/random.h> #include <thrust/random.h>
#include <thrust/sort.h> #include <thrust/sort.h>
#include <thrust/system/cuda/execution_policy.h>
struct prg struct prg
{ {
float a, b; float a, b;
@ -39,6 +41,25 @@ template<typename T> struct pred_eq
return val.val[channel] == value; return val.val[channel] == value;
return false; return false;
} }
__host__ __device__ bool operator()( const thrust::tuple<T, T, T>& val)
{
if (channel == 0)
return thrust::get<0>(val) == value;
if (channel == 1)
return thrust::get<1>(val) == value;
if (channel == 2)
return thrust::get<2>(val) == value;
}
};
template<typename T> struct pred_greater
{
T value;
__host__ __device__ pred_greater(T value_) : value(value_){}
__host__ __device__ bool operator()(const T& val) const
{
return val > value;
}
}; };
@ -80,9 +101,18 @@ int main(void)
auto count = thrust::count(GpuMatBeginItr<int>(d_value), GpuMatEndItr<int>(d_value), 15); auto count = thrust::count(GpuMatBeginItr<int>(d_value), GpuMatEndItr<int>(d_value), 15);
std::cout << count << std::endl; std::cout << count << std::endl;
} }
// Randomly fill an array then copy only values greater than 0. Perform these tasks on a stream.
{
cv::cuda::GpuMat d_value(1, 100, CV_32F);
auto valueBegin = GpuMatBeginItr<float>(d_value);
auto valueEnd = GpuMatEndItr<float>(d_value);
cv::cuda::Stream stream;
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));
int count = thrust::count_if(thrust::system::cuda::par.on(cv::cuda::StreamAccessor::getStream(stream)), valueBegin, valueEnd, pred_greater<float>(0.0));
cv::cuda::GpuMat d_valueGreater(1, count, CV_32F);
thrust::copy_if(thrust::system::cuda::par.on(cv::cuda::StreamAccessor::getStream(stream)), valueBegin, valueEnd, GpuMatBeginItr<float>(d_valueGreater), pred_greater<float>(0.0));
cv::Mat h_greater(d_valueGreater);
}
return 0; return 0;
} }