1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 #pragma once
45 
46 #ifndef __OPENCV_CUDEV_GRID_REDUCE_TO_ROW_DETAIL_HPP__
47 #define __OPENCV_CUDEV_GRID_REDUCE_TO_ROW_DETAIL_HPP__
48 
49 #include "../../common.hpp"
50 #include "../../util/saturate_cast.hpp"
51 #include "../../block/reduce.hpp"
52 
53 namespace cv { namespace cudev {
54 
55 namespace grid_reduce_to_vec_detail
56 {
57     template <class Reductor, int BLOCK_SIZE_X, int BLOCK_SIZE_Y, class SrcPtr, typename ResType, class MaskPtr>
reduceToRow(const SrcPtr src,ResType * dst,const MaskPtr mask,const int rows,const int cols)58     __global__ void reduceToRow(const SrcPtr src, ResType* dst, const MaskPtr mask, const int rows, const int cols)
59     {
60         typedef typename Reductor::work_type work_type;
61 
62         __shared__ work_type smem[BLOCK_SIZE_X * BLOCK_SIZE_Y];
63 
64         const int x = blockIdx.x * BLOCK_SIZE_X + threadIdx.x;
65 
66         work_type myVal = Reductor::initialValue();
67 
68         Reductor op;
69 
70         if (x < cols)
71         {
72             for (int y = threadIdx.y; y < rows; y += BLOCK_SIZE_Y)
73             {
74                 if (mask(y, x))
75                 {
76                     myVal = op(myVal, saturate_cast<work_type>(src(y, x)));
77                 }
78             }
79         }
80 
81         smem[threadIdx.x * BLOCK_SIZE_Y + threadIdx.y] = myVal;
82 
83         __syncthreads();
84 
85         volatile work_type* srow = smem + threadIdx.y * BLOCK_SIZE_X;
86 
87         myVal = srow[threadIdx.x];
88         blockReduce<BLOCK_SIZE_X>(srow, myVal, threadIdx.x, op);
89 
90         if (threadIdx.x == 0)
91             srow[0] = myVal;
92 
93         __syncthreads();
94 
95         if (threadIdx.y == 0 && x < cols)
96             dst[x] = saturate_cast<ResType>(Reductor::result(smem[threadIdx.x * BLOCK_SIZE_X], rows));
97     }
98 
99     template <class Reductor, class SrcPtr, typename ResType, class MaskPtr>
reduceToRow(const SrcPtr & src,ResType * dst,const MaskPtr & mask,int rows,int cols,cudaStream_t stream)100     __host__ void reduceToRow(const SrcPtr& src, ResType* dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
101     {
102         const int BLOCK_SIZE_X = 16;
103         const int BLOCK_SIZE_Y = 16;
104 
105         const dim3 block(BLOCK_SIZE_X, BLOCK_SIZE_Y);
106         const dim3 grid(divUp(cols, block.x));
107 
108         reduceToRow<Reductor, BLOCK_SIZE_X, BLOCK_SIZE_Y><<<grid, block, 0, stream>>>(src, dst, mask, rows, cols);
109         CV_CUDEV_SAFE_CALL( cudaGetLastError() );
110 
111         if (stream == 0)
112             CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
113     }
114 }
115 
116 }}
117 
118 #endif
119