reduce.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef OPENCV_CUDA_REDUCE_HPP
  43. #define OPENCV_CUDA_REDUCE_HPP
  44. #ifndef THRUST_DEBUG // eliminate -Wundef warning
  45. #define THRUST_DEBUG 0
  46. #endif
  47. #include <thrust/tuple.h>
  48. #include "detail/reduce.hpp"
  49. #include "detail/reduce_key_val.hpp"
  50. /** @file
  51. * @deprecated Use @ref cudev instead.
  52. */
  53. //! @cond IGNORED
  54. namespace cv { namespace cuda { namespace device
  55. {
  56. template <int N, typename T, class Op>
  57. __device__ __forceinline__ void reduce(volatile T* smem, T& val, unsigned int tid, const Op& op)
  58. {
  59. reduce_detail::Dispatcher<N>::reductor::template reduce<volatile T*, T&, const Op&>(smem, val, tid, op);
  60. }
  61. template <int N,
  62. typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9,
  63. typename R0, typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7, typename R8, typename R9,
  64. class Op0, class Op1, class Op2, class Op3, class Op4, class Op5, class Op6, class Op7, class Op8, class Op9>
  65. __device__ __forceinline__ void reduce(const thrust::tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>& smem,
  66. const thrust::tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>& val,
  67. unsigned int tid,
  68. const thrust::tuple<Op0, Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8, Op9>& op)
  69. {
  70. reduce_detail::Dispatcher<N>::reductor::template reduce<
  71. const thrust::tuple<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>&,
  72. const thrust::tuple<R0, R1, R2, R3, R4, R5, R6, R7, R8, R9>&,
  73. const thrust::tuple<Op0, Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8, Op9>&>(smem, val, tid, op);
  74. }
  75. template <unsigned int N, typename K, typename V, class Cmp>
  76. __device__ __forceinline__ void reduceKeyVal(volatile K* skeys, K& key, volatile V* svals, V& val, unsigned int tid, const Cmp& cmp)
  77. {
  78. reduce_key_val_detail::Dispatcher<N>::reductor::template reduce<volatile K*, K&, volatile V*, V&, const Cmp&>(skeys, key, svals, val, tid, cmp);
  79. }
  80. template <unsigned int N,
  81. typename K,
  82. typename VP0, typename VP1, typename VP2, typename VP3, typename VP4, typename VP5, typename VP6, typename VP7, typename VP8, typename VP9,
  83. typename VR0, typename VR1, typename VR2, typename VR3, typename VR4, typename VR5, typename VR6, typename VR7, typename VR8, typename VR9,
  84. class Cmp>
  85. __device__ __forceinline__ void reduceKeyVal(volatile K* skeys, K& key,
  86. const thrust::tuple<VP0, VP1, VP2, VP3, VP4, VP5, VP6, VP7, VP8, VP9>& svals,
  87. const thrust::tuple<VR0, VR1, VR2, VR3, VR4, VR5, VR6, VR7, VR8, VR9>& val,
  88. unsigned int tid, const Cmp& cmp)
  89. {
  90. reduce_key_val_detail::Dispatcher<N>::reductor::template reduce<volatile K*, K&,
  91. const thrust::tuple<VP0, VP1, VP2, VP3, VP4, VP5, VP6, VP7, VP8, VP9>&,
  92. const thrust::tuple<VR0, VR1, VR2, VR3, VR4, VR5, VR6, VR7, VR8, VR9>&,
  93. const Cmp&>(skeys, key, svals, val, tid, cmp);
  94. }
  95. template <unsigned int N,
  96. typename KP0, typename KP1, typename KP2, typename KP3, typename KP4, typename KP5, typename KP6, typename KP7, typename KP8, typename KP9,
  97. typename KR0, typename KR1, typename KR2, typename KR3, typename KR4, typename KR5, typename KR6, typename KR7, typename KR8, typename KR9,
  98. typename VP0, typename VP1, typename VP2, typename VP3, typename VP4, typename VP5, typename VP6, typename VP7, typename VP8, typename VP9,
  99. typename VR0, typename VR1, typename VR2, typename VR3, typename VR4, typename VR5, typename VR6, typename VR7, typename VR8, typename VR9,
  100. class Cmp0, class Cmp1, class Cmp2, class Cmp3, class Cmp4, class Cmp5, class Cmp6, class Cmp7, class Cmp8, class Cmp9>
  101. __device__ __forceinline__ void reduceKeyVal(const thrust::tuple<KP0, KP1, KP2, KP3, KP4, KP5, KP6, KP7, KP8, KP9>& skeys,
  102. const thrust::tuple<KR0, KR1, KR2, KR3, KR4, KR5, KR6, KR7, KR8, KR9>& key,
  103. const thrust::tuple<VP0, VP1, VP2, VP3, VP4, VP5, VP6, VP7, VP8, VP9>& svals,
  104. const thrust::tuple<VR0, VR1, VR2, VR3, VR4, VR5, VR6, VR7, VR8, VR9>& val,
  105. unsigned int tid,
  106. const thrust::tuple<Cmp0, Cmp1, Cmp2, Cmp3, Cmp4, Cmp5, Cmp6, Cmp7, Cmp8, Cmp9>& cmp)
  107. {
  108. reduce_key_val_detail::Dispatcher<N>::reductor::template reduce<
  109. const thrust::tuple<KP0, KP1, KP2, KP3, KP4, KP5, KP6, KP7, KP8, KP9>&,
  110. const thrust::tuple<KR0, KR1, KR2, KR3, KR4, KR5, KR6, KR7, KR8, KR9>&,
  111. const thrust::tuple<VP0, VP1, VP2, VP3, VP4, VP5, VP6, VP7, VP8, VP9>&,
  112. const thrust::tuple<VR0, VR1, VR2, VR3, VR4, VR5, VR6, VR7, VR8, VR9>&,
  113. const thrust::tuple<Cmp0, Cmp1, Cmp2, Cmp3, Cmp4, Cmp5, Cmp6, Cmp7, Cmp8, Cmp9>&
  114. >(skeys, key, svals, val, tid, cmp);
  115. }
  116. // smem_tuple
  117. template <typename T0>
  118. __device__ __forceinline__
  119. thrust::tuple<volatile T0*>
  120. smem_tuple(T0* t0)
  121. {
  122. return thrust::make_tuple((volatile T0*) t0);
  123. }
  124. template <typename T0, typename T1>
  125. __device__ __forceinline__
  126. thrust::tuple<volatile T0*, volatile T1*>
  127. smem_tuple(T0* t0, T1* t1)
  128. {
  129. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1);
  130. }
  131. template <typename T0, typename T1, typename T2>
  132. __device__ __forceinline__
  133. thrust::tuple<volatile T0*, volatile T1*, volatile T2*>
  134. smem_tuple(T0* t0, T1* t1, T2* t2)
  135. {
  136. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1, (volatile T2*) t2);
  137. }
  138. template <typename T0, typename T1, typename T2, typename T3>
  139. __device__ __forceinline__
  140. thrust::tuple<volatile T0*, volatile T1*, volatile T2*, volatile T3*>
  141. smem_tuple(T0* t0, T1* t1, T2* t2, T3* t3)
  142. {
  143. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1, (volatile T2*) t2, (volatile T3*) t3);
  144. }
  145. template <typename T0, typename T1, typename T2, typename T3, typename T4>
  146. __device__ __forceinline__
  147. thrust::tuple<volatile T0*, volatile T1*, volatile T2*, volatile T3*, volatile T4*>
  148. smem_tuple(T0* t0, T1* t1, T2* t2, T3* t3, T4* t4)
  149. {
  150. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1, (volatile T2*) t2, (volatile T3*) t3, (volatile T4*) t4);
  151. }
  152. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
  153. __device__ __forceinline__
  154. thrust::tuple<volatile T0*, volatile T1*, volatile T2*, volatile T3*, volatile T4*, volatile T5*>
  155. smem_tuple(T0* t0, T1* t1, T2* t2, T3* t3, T4* t4, T5* t5)
  156. {
  157. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1, (volatile T2*) t2, (volatile T3*) t3, (volatile T4*) t4, (volatile T5*) t5);
  158. }
  159. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
  160. __device__ __forceinline__
  161. thrust::tuple<volatile T0*, volatile T1*, volatile T2*, volatile T3*, volatile T4*, volatile T5*, volatile T6*>
  162. smem_tuple(T0* t0, T1* t1, T2* t2, T3* t3, T4* t4, T5* t5, T6* t6)
  163. {
  164. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1, (volatile T2*) t2, (volatile T3*) t3, (volatile T4*) t4, (volatile T5*) t5, (volatile T6*) t6);
  165. }
  166. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
  167. __device__ __forceinline__
  168. thrust::tuple<volatile T0*, volatile T1*, volatile T2*, volatile T3*, volatile T4*, volatile T5*, volatile T6*, volatile T7*>
  169. smem_tuple(T0* t0, T1* t1, T2* t2, T3* t3, T4* t4, T5* t5, T6* t6, T7* t7)
  170. {
  171. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1, (volatile T2*) t2, (volatile T3*) t3, (volatile T4*) t4, (volatile T5*) t5, (volatile T6*) t6, (volatile T7*) t7);
  172. }
  173. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
  174. __device__ __forceinline__
  175. thrust::tuple<volatile T0*, volatile T1*, volatile T2*, volatile T3*, volatile T4*, volatile T5*, volatile T6*, volatile T7*, volatile T8*>
  176. smem_tuple(T0* t0, T1* t1, T2* t2, T3* t3, T4* t4, T5* t5, T6* t6, T7* t7, T8* t8)
  177. {
  178. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1, (volatile T2*) t2, (volatile T3*) t3, (volatile T4*) t4, (volatile T5*) t5, (volatile T6*) t6, (volatile T7*) t7, (volatile T8*) t8);
  179. }
  180. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
  181. __device__ __forceinline__
  182. thrust::tuple<volatile T0*, volatile T1*, volatile T2*, volatile T3*, volatile T4*, volatile T5*, volatile T6*, volatile T7*, volatile T8*, volatile T9*>
  183. smem_tuple(T0* t0, T1* t1, T2* t2, T3* t3, T4* t4, T5* t5, T6* t6, T7* t7, T8* t8, T9* t9)
  184. {
  185. return thrust::make_tuple((volatile T0*) t0, (volatile T1*) t1, (volatile T2*) t2, (volatile T3*) t3, (volatile T4*) t4, (volatile T5*) t5, (volatile T6*) t6, (volatile T7*) t7, (volatile T8*) t8, (volatile T9*) t9);
  186. }
  187. }}}
  188. //! @endcond
  189. #endif // OPENCV_CUDA_REDUCE_HPP