minmaxloc.hpp 6.9 KB

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