Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
09d392f0
Commit
09d392f0
authored
Sep 15, 2015
by
Dan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added thrust tutorial.
parent
a80e0cf8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
4 deletions
+34
-4
main.cu
samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu
+34
-4
No files found.
samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu
View file @
09d392f0
#include "Thrust_interop.hpp"
#include <opencv2/core/cuda_stream_accessor.hpp>
#include <thrust/transform.h>
#include <thrust/random.h>
#include <thrust/sort.h>
#include <thrust/system/cuda/execution_policy.h>
struct prg
{
float a, b;
__host__ __device__
prg(float _a = 0.f, float _b = 1.f) : a(_a), b(_b) {};
...
...
@@ -39,6 +41,25 @@ template<typename T> struct pred_eq
return val.val[channel] == value;
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);
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;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment