reduce.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_WARP_REDUCE_DETAIL_HPP
  45. #define OPENCV_CUDEV_WARP_REDUCE_DETAIL_HPP
  46. #include "../../common.hpp"
  47. #include "../../util/tuple.hpp"
  48. #include "../../warp/shuffle.hpp"
  49. namespace cv { namespace cudev {
  50. namespace warp_reduce_detail
  51. {
  52. // GetType
  53. template <typename T> struct GetType;
  54. template <typename T> struct GetType<T*>
  55. {
  56. typedef T type;
  57. };
  58. template <typename T> struct GetType<volatile T*>
  59. {
  60. typedef T type;
  61. };
  62. template <typename T> struct GetType<T&>
  63. {
  64. typedef T type;
  65. };
  66. // For
  67. template <int I, int N> struct For
  68. {
  69. template <class PointerTuple, class ValTuple>
  70. __device__ static void loadToSmem(const PointerTuple& smem, const ValTuple& val, uint tid)
  71. {
  72. get<I>(smem)[tid] = get<I>(val);
  73. For<I + 1, N>::loadToSmem(smem, val, tid);
  74. }
  75. template <class PointerTuple, class ValTuple, class OpTuple>
  76. __device__ static void merge(const PointerTuple& smem, const ValTuple& val, uint tid, uint delta, const OpTuple& op)
  77. {
  78. typename GetType<typename tuple_element<I, PointerTuple>::type>::type reg = get<I>(smem)[tid + delta];
  79. get<I>(smem)[tid] = get<I>(val) = get<I>(op)(get<I>(val), reg);
  80. For<I + 1, N>::merge(smem, val, tid, delta, op);
  81. }
  82. #if CV_CUDEV_ARCH >= 300
  83. template <class ValTuple, class OpTuple>
  84. __device__ static void mergeShfl(const ValTuple& val, uint delta, uint width, const OpTuple& op)
  85. {
  86. typename GetType<typename tuple_element<I, ValTuple>::type>::type reg = shfl_down(get<I>(val), delta, width);
  87. get<I>(val) = get<I>(op)(get<I>(val), reg);
  88. For<I + 1, N>::mergeShfl(val, delta, width, op);
  89. }
  90. #endif
  91. };
  92. template <int N> struct For<N, N>
  93. {
  94. template <class PointerTuple, class ValTuple>
  95. __device__ __forceinline__ static void loadToSmem(const PointerTuple&, const ValTuple&, uint)
  96. {
  97. }
  98. template <class PointerTuple, class ValTuple, class OpTuple>
  99. __device__ __forceinline__ static void merge(const PointerTuple&, const ValTuple&, uint, uint, const OpTuple&)
  100. {
  101. }
  102. #if CV_CUDEV_ARCH >= 300
  103. template <class ValTuple, class OpTuple>
  104. __device__ __forceinline__ static void mergeShfl(const ValTuple&, uint, uint, const OpTuple&)
  105. {
  106. }
  107. #endif
  108. };
  109. // loadToSmem
  110. template <typename T>
  111. __device__ __forceinline__ void loadToSmem(volatile T* smem, T& val, uint tid)
  112. {
  113. smem[tid] = val;
  114. }
  115. template <typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9,
  116. typename R0, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7, typename R8, typename R9>
  117. __device__ __forceinline__ void loadToSmem(const tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>& smem,
  118. const tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>& val,
  119. uint tid)
  120. {
  121. For<0, tuple_size<tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9> >::value>::loadToSmem(smem, val, tid);
  122. }
  123. // merge
  124. template <typename T, class Op>
  125. __device__ __forceinline__ void merge(volatile T* smem, T& val, uint tid, uint delta, const Op& op)
  126. {
  127. T reg = smem[tid + delta];
  128. smem[tid] = val = op(val, reg);
  129. }
  130. template <typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9,
  131. typename R0, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7, typename R8, typename R9,
  132. class Op0, class Op1, class Op2, class Op3, class Op4, class Op5, class Op6, class Op7, class Op8, class Op9>
  133. __device__ __forceinline__ void merge(const tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>& smem,
  134. const tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>& val,
  135. uint tid,
  136. uint delta,
  137. const tuple<Op0, Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8, Op9>& op)
  138. {
  139. For<0, tuple_size<tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9> >::value>::merge(smem, val, tid, delta, op);
  140. }
  141. // mergeShfl
  142. #if CV_CUDEV_ARCH >= 300
  143. template <typename T, class Op>
  144. __device__ __forceinline__ void mergeShfl(T& val, uint delta, uint width, const Op& op)
  145. {
  146. T reg = shfl_down(val, delta, width);
  147. val = op(val, reg);
  148. }
  149. template <typename R0, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7, typename R8, typename R9,
  150. class Op0, class Op1, class Op2, class Op3, class Op4, class Op5, class Op6, class Op7, class Op8, class Op9>
  151. __device__ __forceinline__ void mergeShfl(const tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>& val,
  152. uint delta,
  153. uint width,
  154. const tuple<Op0, Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8, Op9>& op)
  155. {
  156. For<0, tuple_size<tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9> >::value>::mergeShfl(val, delta, width, op);
  157. }
  158. #endif
  159. // WarpReductor
  160. struct WarpReductor
  161. {
  162. template <typename Pointer, typename Reference, class Op>
  163. __device__ static void reduce(Pointer smem, Reference val, uint tid, Op op)
  164. {
  165. #if CV_CUDEV_ARCH >= 300
  166. CV_UNUSED(smem);
  167. CV_UNUSED(tid);
  168. mergeShfl(val, 16, 32, op);
  169. mergeShfl(val, 8, 32, op);
  170. mergeShfl(val, 4, 32, op);
  171. mergeShfl(val, 2, 32, op);
  172. mergeShfl(val, 1, 32, op);
  173. #else
  174. loadToSmem(smem, val, tid);
  175. if (tid < 16)
  176. {
  177. merge(smem, val, tid, 16, op);
  178. merge(smem, val, tid, 8, op);
  179. merge(smem, val, tid, 4, op);
  180. merge(smem, val, tid, 2, op);
  181. merge(smem, val, tid, 1, op);
  182. }
  183. #endif
  184. }
  185. };
  186. }
  187. }}
  188. #endif