reduce_to_vec.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_REDUCE_TO_VEC_HPP
  45. #define OPENCV_CUDEV_GRID_REDUCE_TO_VEC_HPP
  46. #include "../common.hpp"
  47. #include "../util/vec_traits.hpp"
  48. #include "../util/limits.hpp"
  49. #include "../util/saturate_cast.hpp"
  50. #include "../ptr2d/traits.hpp"
  51. #include "../ptr2d/gpumat.hpp"
  52. #include "../ptr2d/mask.hpp"
  53. #include "../functional/functional.hpp"
  54. #include "detail/reduce_to_column.hpp"
  55. #include "detail/reduce_to_row.hpp"
  56. namespace cv { namespace cudev {
  57. //! @addtogroup cudev
  58. //! @{
  59. template <typename T> struct Sum : plus<T>
  60. {
  61. typedef T work_type;
  62. template <typename U> struct rebind
  63. {
  64. typedef Sum<U> other;
  65. };
  66. __device__ __forceinline__ static T initialValue()
  67. {
  68. return VecTraits<T>::all(0);
  69. }
  70. __device__ __forceinline__ static T result(T r, int)
  71. {
  72. return r;
  73. }
  74. };
  75. template <typename T> struct Avg : plus<T>
  76. {
  77. typedef T work_type;
  78. template <typename U> struct rebind
  79. {
  80. typedef Avg<U> other;
  81. };
  82. __device__ __forceinline__ static T initialValue()
  83. {
  84. return VecTraits<T>::all(0);
  85. }
  86. __device__ __forceinline__ static T result(T r, float sz)
  87. {
  88. return saturate_cast<T>(r / sz);
  89. }
  90. };
  91. template <typename T> struct Min : minimum<T>
  92. {
  93. typedef T work_type;
  94. template <typename U> struct rebind
  95. {
  96. typedef Min<U> other;
  97. };
  98. __device__ __forceinline__ static T initialValue()
  99. {
  100. return VecTraits<T>::all(numeric_limits<typename VecTraits<T>::elem_type>::max());
  101. }
  102. __device__ __forceinline__ static T result(T r, int)
  103. {
  104. return r;
  105. }
  106. };
  107. template <typename T> struct Max : maximum<T>
  108. {
  109. typedef T work_type;
  110. template <typename U> struct rebind
  111. {
  112. typedef Max<U> other;
  113. };
  114. __device__ __forceinline__ static T initialValue()
  115. {
  116. return VecTraits<T>::all(-numeric_limits<typename VecTraits<T>::elem_type>::max());
  117. }
  118. __device__ __forceinline__ static T result(T r, int)
  119. {
  120. return r;
  121. }
  122. };
  123. template <class Reductor, class SrcPtr, typename ResType, class MaskPtr>
  124. __host__ void gridReduceToRow(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
  125. {
  126. const int rows = getRows(src);
  127. const int cols = getCols(src);
  128. CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
  129. dst.create(1, cols);
  130. grid_reduce_to_vec_detail::reduceToRow<Reductor>(shrinkPtr(src),
  131. dst[0],
  132. shrinkPtr(mask),
  133. rows, cols,
  134. StreamAccessor::getStream(stream));
  135. }
  136. template <class Reductor, class SrcPtr, typename ResType>
  137. __host__ void gridReduceToRow(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
  138. {
  139. const int rows = getRows(src);
  140. const int cols = getCols(src);
  141. dst.create(1, cols);
  142. grid_reduce_to_vec_detail::reduceToRow<Reductor>(shrinkPtr(src),
  143. dst[0],
  144. WithOutMask(),
  145. rows, cols,
  146. StreamAccessor::getStream(stream));
  147. }
  148. template <class Reductor, class Policy, class SrcPtr, typename ResType, class MaskPtr>
  149. __host__ void gridReduceToColumn_(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
  150. {
  151. const int rows = getRows(src);
  152. const int cols = getCols(src);
  153. CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
  154. cuda::createContinuous(rows, 1, dst.type(), dst);
  155. grid_reduce_to_vec_detail::reduceToColumn<Reductor, Policy>(shrinkPtr(src),
  156. dst[0],
  157. shrinkPtr(mask),
  158. rows, cols,
  159. StreamAccessor::getStream(stream));
  160. }
  161. template <class Reductor, class Policy, class SrcPtr, typename ResType>
  162. __host__ void gridReduceToColumn_(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
  163. {
  164. const int rows = getRows(src);
  165. const int cols = getCols(src);
  166. cuda::createContinuous(rows, 1, dst.type(), dst);
  167. grid_reduce_to_vec_detail::reduceToColumn<Reductor, Policy>(shrinkPtr(src),
  168. dst[0],
  169. WithOutMask(),
  170. rows, cols,
  171. StreamAccessor::getStream(stream));
  172. }
  173. // default policy
  174. struct DefaultReduceToVecPolicy
  175. {
  176. enum {
  177. block_size_x = 32,
  178. block_size_y = 8
  179. };
  180. };
  181. template <class Reductor, class SrcPtr, typename ResType, class MaskPtr>
  182. __host__ void gridReduceToColumn(const SrcPtr& src, GpuMat_<ResType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
  183. {
  184. gridReduceToColumn_<Reductor, DefaultReduceToVecPolicy>(src, dst, mask, stream);
  185. }
  186. template <class Reductor, class SrcPtr, typename ResType>
  187. __host__ void gridReduceToColumn(const SrcPtr& src, GpuMat_<ResType>& dst, Stream& stream = Stream::Null())
  188. {
  189. gridReduceToColumn_<Reductor, DefaultReduceToVecPolicy>(src, dst, stream);
  190. }
  191. //! @}
  192. }}
  193. #endif