Home | History | Annotate | Download | only in detail
      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_MINMAXLOC_DETAIL_HPP__
     47 #define __OPENCV_CUDEV_GRID_MINMAXLOC_DETAIL_HPP__
     48 
     49 #include "../../common.hpp"
     50 #include "../../util/vec_traits.hpp"
     51 #include "../../util/type_traits.hpp"
     52 #include "../../util/limits.hpp"
     53 #include "../../block/reduce.hpp"
     54 
     55 namespace cv { namespace cudev {
     56 
     57 namespace grid_minmaxloc_detail
     58 {
     59     template <int BLOCK_SIZE, class SrcPtr, typename ResType, class MaskPtr>
     60     __global__ void minMaxLoc_pass_1(const SrcPtr src, ResType* minVal, ResType* maxVal, int* minLoc, int* maxLoc, const MaskPtr mask, const int rows, const int cols, const int patch_y, const int patch_x)
     61     {
     62         __shared__ ResType sMinVal[BLOCK_SIZE];
     63         __shared__ ResType sMaxVal[BLOCK_SIZE];
     64         __shared__ uint sMinLoc[BLOCK_SIZE];
     65         __shared__ uint sMaxLoc[BLOCK_SIZE];
     66 
     67         const int x0 = blockIdx.x * blockDim.x * patch_x + threadIdx.x;
     68         const int y0 = blockIdx.y * blockDim.y * patch_y + threadIdx.y;
     69 
     70         ResType myMin = numeric_limits<ResType>::max();
     71         ResType myMax = -numeric_limits<ResType>::max();
     72         int myMinLoc = -1;
     73         int myMaxLoc = -1;
     74 
     75         for (int i = 0, y = y0; i < patch_y && y < rows; ++i, y += blockDim.y)
     76         {
     77             for (int j = 0, x = x0; j < patch_x && x < cols; ++j, x += blockDim.x)
     78             {
     79                 if (mask(y, x))
     80                 {
     81                     const ResType srcVal = src(y, x);
     82 
     83                     if (srcVal < myMin)
     84                     {
     85                         myMin = srcVal;
     86                         myMinLoc = y * cols + x;
     87                     }
     88 
     89                     if (srcVal > myMax)
     90                     {
     91                         myMax = srcVal;
     92                         myMaxLoc = y * cols + x;
     93                     }
     94                 }
     95             }
     96         }
     97 
     98         const int tid = threadIdx.y * blockDim.x + threadIdx.x;
     99 
    100         blockReduceKeyVal<BLOCK_SIZE>(smem_tuple(sMinVal, sMaxVal), tie(myMin, myMax),
    101                                       smem_tuple(sMinLoc, sMaxLoc), tie(myMinLoc, myMaxLoc),
    102                                       tid,
    103                                       make_tuple(less<ResType>(), greater<ResType>()));
    104 
    105         const int bid = blockIdx.y * gridDim.x + blockIdx.x;
    106 
    107         if (tid == 0)
    108         {
    109             minVal[bid] = myMin;
    110             maxVal[bid] = myMax;
    111             minLoc[bid] = myMinLoc;
    112             maxLoc[bid] = myMaxLoc;
    113         }
    114     }
    115 
    116     template <int BLOCK_SIZE, typename T>
    117     __global__ void minMaxLoc_pass_2(T* minMal, T* maxVal, int* minLoc, int* maxLoc, int count)
    118     {
    119         __shared__ T sMinVal[BLOCK_SIZE];
    120         __shared__ T sMaxVal[BLOCK_SIZE];
    121         __shared__ int sMinLoc[BLOCK_SIZE];
    122         __shared__ int sMaxLoc[BLOCK_SIZE];
    123 
    124         const int idx = ::min(threadIdx.x, count - 1);
    125 
    126         T myMin = minMal[idx];
    127         T myMax = maxVal[idx];
    128         int myMinLoc = minLoc[idx];
    129         int myMaxLoc = maxLoc[idx];
    130 
    131         blockReduceKeyVal<BLOCK_SIZE>(smem_tuple(sMinVal, sMaxVal), tie(myMin, myMax),
    132                                       smem_tuple(sMinLoc, sMaxLoc), tie(myMinLoc, myMaxLoc),
    133                                       threadIdx.x,
    134                                       make_tuple(less<T>(), greater<T>()));
    135 
    136         if (threadIdx.x == 0)
    137         {
    138             minMal[0] = myMin;
    139             maxVal[0] = myMax;
    140             minLoc[0] = myMinLoc;
    141             maxLoc[0] = myMaxLoc;
    142         }
    143     }
    144 
    145     template <class Policy>
    146     void getLaunchCfg(int rows, int cols, dim3& block, dim3& grid)
    147     {
    148         block = dim3(Policy::block_size_x, Policy::block_size_y);
    149         grid = dim3(divUp(cols, block.x * Policy::patch_size_x), divUp(rows, block.y * Policy::patch_size_y));
    150 
    151         grid.x = ::min(grid.x, block.x);
    152         grid.y = ::min(grid.y, block.y);
    153     }
    154 
    155     template <class Policy, class SrcPtr, typename ResType, class MaskPtr>
    156     __host__ void minMaxLoc(const SrcPtr& src, ResType* minVal, ResType* maxVal, int* minLoc, int* maxLoc, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
    157     {
    158         dim3 block, grid;
    159         getLaunchCfg<Policy>(rows, cols, block, grid);
    160 
    161         const int patch_x = divUp(divUp(cols, grid.x), block.x);
    162         const int patch_y = divUp(divUp(rows, grid.y), block.y);
    163 
    164         minMaxLoc_pass_1<Policy::block_size_x * Policy::block_size_y><<<grid, block, 0, stream>>>(src, minVal, maxVal, minLoc, maxLoc, mask, rows, cols, patch_y, patch_x);
    165         CV_CUDEV_SAFE_CALL( cudaGetLastError() );
    166 
    167         minMaxLoc_pass_2<Policy::block_size_x * Policy::block_size_y><<<1, Policy::block_size_x * Policy::block_size_y, 0, stream>>>(minVal, maxVal, minLoc, maxLoc, grid.x * grid.y);
    168         CV_CUDEV_SAFE_CALL( cudaGetLastError() );
    169 
    170         if (stream == 0)
    171             CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
    172     }
    173 }
    174 
    175 }}
    176 
    177 #endif
    178