0332808cbb5bd8bd54f82596a1d6cc44181b3258
[platform/upstream/opencv.git] / doc / tutorials / gpu / gpu-thrust-interop / gpu_thrust_interop.markdown
1 Using a cv::cuda::GpuMat with thrust {#tutorial_gpu_thrust_interop}
2 ===========================================
3
4 Goal
5 ----
6
7 Thrust is an extremely powerful library for various cuda accelerated algorithms.  However thrust is designed
8 to work with vectors and not pitched matricies.  The following tutorial will discuss wrapping cv::cuda::GpuMat's
9 into thrust iterators that can be used with thrust algorithms.
10
11 This tutorial should show you how to:
12 - Wrap a GpuMat into a thrust iterator
13 - Fill a GpuMat with random numbers
14 - Sort a column of a GpuMat in place
15 - Copy values greater than 0 to a new gpu matrix
16 - Use streams with thrust
17
18 Wrapping a GpuMat into a thrust iterator
19 ----
20
21 The following code will produce an iterator for a GpuMat
22
23 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/Thrust_interop.hpp begin_itr
24 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/Thrust_interop.hpp end_itr
25
26 Our goal is to have an iterator that will start at the beginning of the matrix, and increment correctly to access continuous matrix elements.  This is trivial for a continuous row, but how about for a column of a pitched matrix?  To do this we need the iterator to be aware of the matrix dimensions and step.  This information is embedded in the step_functor.
27 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/Thrust_interop.hpp step_functor
28 The step functor takes in an index value and returns the appropriate
29 offset from the beginning of the matrix.  The counting iterator simply increments over the range of pixel elements.  Combined into the transform_iterator we have an iterator that counts from 0 to M*N and correctly
30 increments to account for the pitched memory of a GpuMat.  Unfortunately this does not include any memory location information, for that we need a thrust::device_ptr.  By combining a device pointer with the transform_iterator we can point thrust to the first element of our matrix and have it step accordingly.
31
32 Fill a GpuMat with random numbers
33 ----
34 Now that we have some nice functions for making iterators for thrust, lets use them to do some things OpenCV can't do.  Unfortunately at the time of this writing, OpenCV doesn't have any Gpu random number generation.
35 Thankfully thrust does and it's now trivial to interop between the two.
36 Example taken from http://stackoverflow.com/questions/12614164/generating-a-random-number-vector-between-0-and-1-0-using-thrust
37
38 First we need to write a functor that will produce our random values.
39 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu prg
40
41 This will take in an integer value and output a value between a and b.
42 Now we will populate our matrix with values between 0 and 10 with a thrust transform.
43 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu random
44
45 Sort a column of a GpuMat in place
46 ----
47
48 Lets fill matrix elements with random values and an index.  Afterwards we will sort the random numbers and the indecies.
49 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu sort
50
51 Copy values greater than 0 to a new gpu matrix while using streams
52 ----
53 In this example we're going to see how cv::cuda::Streams can be used with thrust.  Unfortunately this specific example uses functions that must return results to the CPU so it isn't the optimal use of streams.
54
55 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu copy_greater
56
57
58 First we will populate a GPU mat with randomly generated data between -1 and 1 on a stream.
59
60 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu random_gen_stream
61
62 Notice the use of thrust::system::cuda::par.on(...), this creates an execution policy for executing thrust code on a stream.
63 There is a bug in the version of thrust distributed with the cuda toolkit, as of version 7.5 this has not been fixed.  This bug causes code to not execute on streams.
64 The bug can however be fixed by using the newest version of thrust from the git repository. (http://github.com/thrust/thrust.git)
65 Next we will determine how many values are greater than 0 by using thrust::count_if with the following predicate:
66
67 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu pred_greater
68
69 We will use those results to create an output buffer for storing the copied values, we will then use copy_if with the same predicate to populate the output buffer.
70 Lastly we will download the values into a CPU mat for viewing.