reduce.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_BLOCK_REDUCE_DETAIL_HPP
  45. #define OPENCV_CUDEV_BLOCK_REDUCE_DETAIL_HPP
  46. #include "../../common.hpp"
  47. #include "../../util/tuple.hpp"
  48. #include "../../util/type_traits.hpp"
  49. #include "../../warp/warp.hpp"
  50. #include "../../warp/shuffle.hpp"
  51. namespace cv { namespace cudev {
  52. namespace block_reduce_detail
  53. {
  54. // GetType
  55. template <typename T> struct GetType;
  56. template <typename T> struct GetType<T*>
  57. {
  58. typedef T type;
  59. };
  60. template <typename T> struct GetType<volatile T*>
  61. {
  62. typedef T type;
  63. };
  64. template <typename T> struct GetType<T&>
  65. {
  66. typedef T type;
  67. };
  68. // For
  69. template <int I, int N> struct For
  70. {
  71. template <class PointerTuple, class ValTuple>
  72. __device__ static void loadToSmem(const PointerTuple& smem, const ValTuple& val, uint tid)
  73. {
  74. get<I>(smem)[tid] = get<I>(val);
  75. For<I + 1, N>::loadToSmem(smem, val, tid);
  76. }
  77. template <class PointerTuple, class ValTuple>
  78. __device__ static void loadFromSmem(const PointerTuple& smem, const ValTuple& val, uint tid)
  79. {
  80. get<I>(val) = get<I>(smem)[tid];
  81. For<I + 1, N>::loadFromSmem(smem, val, tid);
  82. }
  83. template <class PointerTuple, class ValTuple, class OpTuple>
  84. __device__ static void merge(const PointerTuple& smem, const ValTuple& val, uint tid, uint delta, const OpTuple& op)
  85. {
  86. typename GetType<typename tuple_element<I, PointerTuple>::type>::type reg = get<I>(smem)[tid + delta];
  87. get<I>(smem)[tid] = get<I>(val) = get<I>(op)(get<I>(val), reg);
  88. For<I + 1, N>::merge(smem, val, tid, delta, op);
  89. }
  90. #if CV_CUDEV_ARCH >= 300
  91. template <class ValTuple, class OpTuple>
  92. __device__ static void mergeShfl(const ValTuple& val, uint delta, uint width, const OpTuple& op)
  93. {
  94. typename GetType<typename tuple_element<I, ValTuple>::type>::type reg = shfl_down(get<I>(val), delta, width);
  95. get<I>(val) = get<I>(op)(get<I>(val), reg);
  96. For<I + 1, N>::mergeShfl(val, delta, width, op);
  97. }
  98. #endif
  99. };
  100. template <int N> struct For<N, N>
  101. {
  102. template <class PointerTuple, class ValTuple>
  103. __device__ __forceinline__ static void loadToSmem(const PointerTuple&, const ValTuple&, uint)
  104. {
  105. }
  106. template <class PointerTuple, class ValTuple>
  107. __device__ __forceinline__ static void loadFromSmem(const PointerTuple&, const ValTuple&, uint)
  108. {
  109. }
  110. template <class PointerTuple, class ValTuple, class OpTuple>
  111. __device__ __forceinline__ static void merge(const PointerTuple&, const ValTuple&, uint, uint, const OpTuple&)
  112. {
  113. }
  114. #if CV_CUDEV_ARCH >= 300
  115. template <class ValTuple, class OpTuple>
  116. __device__ __forceinline__ static void mergeShfl(const ValTuple&, uint, uint, const OpTuple&)
  117. {
  118. }
  119. #endif
  120. };
  121. // loadToSmem / loadFromSmem
  122. template <typename T>
  123. __device__ __forceinline__ void loadToSmem(volatile T* smem, T& val, uint tid)
  124. {
  125. smem[tid] = val;
  126. }
  127. template <typename T>
  128. __device__ __forceinline__ void loadFromSmem(volatile T* smem, T& val, uint tid)
  129. {
  130. val = smem[tid];
  131. }
  132. template <typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9,
  133. typename R0, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7, typename R8, typename R9>
  134. __device__ __forceinline__ void loadToSmem(const tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>& smem,
  135. const tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>& val,
  136. uint tid)
  137. {
  138. For<0, tuple_size<tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9> >::value>::loadToSmem(smem, val, tid);
  139. }
  140. template <typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9,
  141. typename R0, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7, typename R8, typename R9>
  142. __device__ __forceinline__ void loadFromSmem(const tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>& smem,
  143. const tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>& val,
  144. uint tid)
  145. {
  146. For<0, tuple_size<tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9> >::value>::loadFromSmem(smem, val, tid);
  147. }
  148. // merge
  149. template <typename T, class Op>
  150. __device__ __forceinline__ void merge(volatile T* smem, T& val, uint tid, uint delta, const Op& op)
  151. {
  152. T reg = smem[tid + delta];
  153. smem[tid] = val = op(val, reg);
  154. }
  155. template <typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9,
  156. typename R0, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7, typename R8, typename R9,
  157. class Op0, class Op1, class Op2, class Op3, class Op4, class Op5, class Op6, class Op7, class Op8, class Op9>
  158. __device__ __forceinline__ void merge(const tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>& smem,
  159. const tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>& val,
  160. uint tid,
  161. uint delta,
  162. const tuple<Op0, Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8, Op9>& op)
  163. {
  164. For<0, tuple_size<tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9> >::value>::merge(smem, val, tid, delta, op);
  165. }
  166. // mergeShfl
  167. #if CV_CUDEV_ARCH >= 300
  168. template <typename T, class Op>
  169. __device__ __forceinline__ void mergeShfl(T& val, uint delta, uint width, const Op& op)
  170. {
  171. T reg = shfl_down(val, delta, width);
  172. val = op(val, reg);
  173. }
  174. template <typename R0, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7, typename R8, typename R9,
  175. class Op0, class Op1, class Op2, class Op3, class Op4, class Op5, class Op6, class Op7, class Op8, class Op9>
  176. __device__ __forceinline__ void mergeShfl(const tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>& val,
  177. uint delta,
  178. uint width,
  179. const tuple<Op0, Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8, Op9>& op)
  180. {
  181. For<0, tuple_size<tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9> >::value>::mergeShfl(val, delta, width, op);
  182. }
  183. #endif
  184. // Generic
  185. template <int N> struct Generic
  186. {
  187. template <typename Pointer, typename Reference, class Op>
  188. __device__ static void reduce(Pointer smem, Reference val, uint tid, Op op)
  189. {
  190. loadToSmem(smem, val, tid);
  191. if (N >= 32)
  192. __syncthreads();
  193. if (N >= 2048)
  194. {
  195. if (tid < 1024)
  196. merge(smem, val, tid, 1024, op);
  197. __syncthreads();
  198. }
  199. if (N >= 1024)
  200. {
  201. if (tid < 512)
  202. merge(smem, val, tid, 512, op);
  203. __syncthreads();
  204. }
  205. if (N >= 512)
  206. {
  207. if (tid < 256)
  208. merge(smem, val, tid, 256, op);
  209. __syncthreads();
  210. }
  211. if (N >= 256)
  212. {
  213. if (tid < 128)
  214. merge(smem, val, tid, 128, op);
  215. __syncthreads();
  216. }
  217. if (N >= 128)
  218. {
  219. if (tid < 64)
  220. merge(smem, val, tid, 64, op);
  221. __syncthreads();
  222. }
  223. if (N >= 64)
  224. {
  225. if (tid < 32)
  226. merge(smem, val, tid, 32, op);
  227. }
  228. if (tid < 16)
  229. {
  230. merge(smem, val, tid, 16, op);
  231. merge(smem, val, tid, 8, op);
  232. merge(smem, val, tid, 4, op);
  233. merge(smem, val, tid, 2, op);
  234. merge(smem, val, tid, 1, op);
  235. }
  236. }
  237. };
  238. // Unroll
  239. template <int I, typename Pointer, typename Reference, class Op> struct Unroll
  240. {
  241. __device__ static void loop(Pointer smem, Reference val, uint tid, Op op)
  242. {
  243. merge(smem, val, tid, I, op);
  244. Unroll<I / 2, Pointer, Reference, Op>::loop(smem, val, tid, op);
  245. }
  246. #if CV_CUDEV_ARCH >= 300
  247. __device__ static void loopShfl(Reference val, Op op, uint N)
  248. {
  249. mergeShfl(val, I, N, op);
  250. Unroll<I / 2, Pointer, Reference, Op>::loopShfl(val, op, N);
  251. }
  252. #endif
  253. };
  254. template <typename Pointer, typename Reference, class Op> struct Unroll<0, Pointer, Reference, Op>
  255. {
  256. __device__ __forceinline__ static void loop(Pointer, Reference, uint, Op)
  257. {
  258. }
  259. #if CV_CUDEV_ARCH >= 300
  260. __device__ __forceinline__ static void loopShfl(Reference, Op, uint)
  261. {
  262. }
  263. #endif
  264. };
  265. // WarpOptimized
  266. template <int N> struct WarpOptimized
  267. {
  268. template <typename Pointer, typename Reference, class Op>
  269. __device__ static void reduce(Pointer smem, Reference val, uint tid, Op op)
  270. {
  271. #if CV_CUDEV_ARCH >= 300
  272. CV_UNUSED(smem);
  273. CV_UNUSED(tid);
  274. Unroll<N / 2, Pointer, Reference, Op>::loopShfl(val, op, N);
  275. #else
  276. loadToSmem(smem, val, tid);
  277. if (tid < N / 2)
  278. Unroll<N / 2, Pointer, Reference, Op>::loop(smem, val, tid, op);
  279. #endif
  280. }
  281. };
  282. // GenericOptimized32
  283. template <int N> struct GenericOptimized32
  284. {
  285. enum { M = N / 32 };
  286. template <typename Pointer, typename Reference, class Op>
  287. __device__ static void reduce(Pointer smem, Reference val, uint tid, Op op)
  288. {
  289. const uint laneId = Warp::laneId();
  290. #if CV_CUDEV_ARCH >= 300
  291. Unroll<16, Pointer, Reference, Op>::loopShfl(val, op, warpSize);
  292. if (laneId == 0)
  293. loadToSmem(smem, val, tid / 32);
  294. #else
  295. loadToSmem(smem, val, tid);
  296. if (laneId < 16)
  297. Unroll<16, Pointer, Reference, Op>::loop(smem, val, tid, op);
  298. __syncthreads();
  299. if (laneId == 0)
  300. loadToSmem(smem, val, tid / 32);
  301. #endif
  302. __syncthreads();
  303. loadFromSmem(smem, val, tid);
  304. if (tid < 32)
  305. {
  306. #if CV_CUDEV_ARCH >= 300
  307. Unroll<M / 2, Pointer, Reference, Op>::loopShfl(val, op, M);
  308. #else
  309. Unroll<M / 2, Pointer, Reference, Op>::loop(smem, val, tid, op);
  310. #endif
  311. }
  312. }
  313. };
  314. template <int N> struct Dispatcher
  315. {
  316. typedef typename SelectIf<
  317. (N <= 32) && IsPowerOf2<N>::value,
  318. WarpOptimized<N>,
  319. typename SelectIf<
  320. (N <= 1024) && IsPowerOf2<N>::value,
  321. GenericOptimized32<N>,
  322. Generic<N>
  323. >::type
  324. >::type reductor;
  325. };
  326. }
  327. }}
  328. #endif