Add next and previous navigation links to all tutorials
[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 @prev_tutorial{tutorial_gpu_basics_similarity}
5
6 Goal
7 ----
8
9 Thrust is an extremely powerful library for various cuda accelerated algorithms.  However thrust is designed
10 to work with vectors and not pitched matricies.  The following tutorial will discuss wrapping cv::cuda::GpuMat's
11 into thrust iterators that can be used with thrust algorithms.
12
13 This tutorial should show you how to:
14 - Wrap a GpuMat into a thrust iterator
15 - Fill a GpuMat with random numbers
16 - Sort a column of a GpuMat in place
17 - Copy values greater than 0 to a new gpu matrix
18 - Use streams with thrust
19
20 Wrapping a GpuMat into a thrust iterator
21 ----
22
23 The following code will produce an iterator for a GpuMat
24
25 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/Thrust_interop.hpp begin_itr
26 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/Thrust_interop.hpp end_itr
27
28 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.
29 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/Thrust_interop.hpp step_functor
30 The step functor takes in an index value and returns the appropriate
31 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
32 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.
33
34 Fill a GpuMat with random numbers
35 ----
36 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.
37 Thankfully thrust does and it's now trivial to interop between the two.
38 Example taken from http://stackoverflow.com/questions/12614164/generating-a-random-number-vector-between-0-and-1-0-using-thrust
39
40 First we need to write a functor that will produce our random values.
41 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu prg
42
43 This will take in an integer value and output a value between a and b.
44 Now we will populate our matrix with values between 0 and 10 with a thrust transform.
45 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu random
46
47 Sort a column of a GpuMat in place
48 ----
49
50 Lets fill matrix elements with random values and an index.  Afterwards we will sort the random numbers and the indecies.
51 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu sort
52
53 Copy values greater than 0 to a new gpu matrix while using streams
54 ----
55 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.
56
57 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu copy_greater
58
59
60 First we will populate a GPU mat with randomly generated data between -1 and 1 on a stream.
61
62 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu random_gen_stream
63
64 Notice the use of thrust::system::cuda::par.on(...), this creates an execution policy for executing thrust code on a stream.
65 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.
66 The bug can however be fixed by using the newest version of thrust from the git repository. (http://github.com/thrust/thrust.git)
67 Next we will determine how many values are greater than 0 by using thrust::count_if with the following predicate:
68
69 @snippet samples/cpp/tutorial_code/gpu/gpu-thrust-interop/main.cu pred_greater
70
71 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.
72 Lastly we will download the values into a CPU mat for viewing.