shuffle.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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_SHUFFLE_HPP
  45. #define OPENCV_CUDEV_WARP_SHUFFLE_HPP
  46. #include "../common.hpp"
  47. #include "../util/vec_traits.hpp"
  48. #include "../block/block.hpp"
  49. #include "warp.hpp"
  50. namespace cv { namespace cudev {
  51. //! @addtogroup cudev
  52. //! @{
  53. #if CV_CUDEV_ARCH >= 300
  54. #if __CUDACC_VER_MAJOR__ >= 9
  55. # define __shfl(x, y, z) __shfl_sync(0xFFFFFFFFU, x, y, z)
  56. # define __shfl_xor(x, y, z) __shfl_xor_sync(0xFFFFFFFFU, x, y, z)
  57. //# define __shfl_up(x, y, z) __shfl_up_sync(0xFFFFFFFFU, x, y, z)
  58. # define __shfl_down(x, y, z) __shfl_down_sync(0xFFFFFFFFU, x, y, z)
  59. #endif
  60. // shfl
  61. __device__ __forceinline__ uchar shfl(uchar val, int srcLane, int width = warpSize)
  62. {
  63. return (uchar) __shfl((int) val, srcLane, width);
  64. }
  65. __device__ __forceinline__ schar shfl(schar val, int srcLane, int width = warpSize)
  66. {
  67. return (schar) __shfl((int) val, srcLane, width);
  68. }
  69. __device__ __forceinline__ ushort shfl(ushort val, int srcLane, int width = warpSize)
  70. {
  71. return (ushort) __shfl((int) val, srcLane, width);
  72. }
  73. __device__ __forceinline__ short shfl(short val, int srcLane, int width = warpSize)
  74. {
  75. return (short) __shfl((int) val, srcLane, width);
  76. }
  77. __device__ __forceinline__ int shfl(int val, int srcLane, int width = warpSize)
  78. {
  79. return __shfl(val, srcLane, width);
  80. }
  81. __device__ __forceinline__ uint shfl(uint val, int srcLane, int width = warpSize)
  82. {
  83. return (uint) __shfl((int) val, srcLane, width);
  84. }
  85. __device__ __forceinline__ float shfl(float val, int srcLane, int width = warpSize)
  86. {
  87. return __shfl(val, srcLane, width);
  88. }
  89. __device__ double shfl(double val, int srcLane, int width = warpSize)
  90. {
  91. int lo = __double2loint(val);
  92. int hi = __double2hiint(val);
  93. lo = __shfl(lo, srcLane, width);
  94. hi = __shfl(hi, srcLane, width);
  95. return __hiloint2double(hi, lo);
  96. }
  97. #define CV_CUDEV_SHFL_VEC_INST(input_type) \
  98. __device__ __forceinline__ input_type ## 1 shfl(const input_type ## 1 & val, int srcLane, int width = warpSize) \
  99. { \
  100. return VecTraits<input_type ## 1>::make( \
  101. shfl(val.x, srcLane, width) \
  102. ); \
  103. } \
  104. __device__ __forceinline__ input_type ## 2 shfl(const input_type ## 2 & val, int srcLane, int width = warpSize) \
  105. { \
  106. return VecTraits<input_type ## 2>::make( \
  107. shfl(val.x, srcLane, width), \
  108. shfl(val.y, srcLane, width) \
  109. ); \
  110. } \
  111. __device__ __forceinline__ input_type ## 3 shfl(const input_type ## 3 & val, int srcLane, int width = warpSize) \
  112. { \
  113. return VecTraits<input_type ## 3>::make( \
  114. shfl(val.x, srcLane, width), \
  115. shfl(val.y, srcLane, width), \
  116. shfl(val.z, srcLane, width) \
  117. ); \
  118. } \
  119. __device__ __forceinline__ input_type ## 4 shfl(const input_type ## 4 & val, int srcLane, int width = warpSize) \
  120. { \
  121. return VecTraits<input_type ## 4>::make( \
  122. shfl(val.x, srcLane, width), \
  123. shfl(val.y, srcLane, width), \
  124. shfl(val.z, srcLane, width), \
  125. shfl(val.w, srcLane, width) \
  126. ); \
  127. }
  128. CV_CUDEV_SHFL_VEC_INST(uchar)
  129. CV_CUDEV_SHFL_VEC_INST(char)
  130. CV_CUDEV_SHFL_VEC_INST(ushort)
  131. CV_CUDEV_SHFL_VEC_INST(short)
  132. CV_CUDEV_SHFL_VEC_INST(uint)
  133. CV_CUDEV_SHFL_VEC_INST(int)
  134. CV_CUDEV_SHFL_VEC_INST(float)
  135. CV_CUDEV_SHFL_VEC_INST(double)
  136. #undef CV_CUDEV_SHFL_VEC_INST
  137. // shfl_up
  138. #if __CUDACC_VER_MAJOR__ >= 9
  139. template <typename T>
  140. __device__ __forceinline__ T shfl_up_sync(uint mask, T val, uint delta, int width = warpSize)
  141. {
  142. return (T) __shfl_up_sync(mask, val, delta, width);
  143. }
  144. #else
  145. __device__ __forceinline__ uchar shfl_up(uchar val, uint delta, int width = warpSize)
  146. {
  147. return (uchar) __shfl_up((int) val, delta, width);
  148. }
  149. __device__ __forceinline__ schar shfl_up(schar val, uint delta, int width = warpSize)
  150. {
  151. return (schar) __shfl_up((int) val, delta, width);
  152. }
  153. __device__ __forceinline__ ushort shfl_up(ushort val, uint delta, int width = warpSize)
  154. {
  155. return (ushort) __shfl_up((int) val, delta, width);
  156. }
  157. __device__ __forceinline__ short shfl_up(short val, uint delta, int width = warpSize)
  158. {
  159. return (short) __shfl_up((int) val, delta, width);
  160. }
  161. __device__ __forceinline__ int shfl_up(int val, uint delta, int width = warpSize)
  162. {
  163. return __shfl_up(val, delta, width);
  164. }
  165. __device__ __forceinline__ uint shfl_up(uint val, uint delta, int width = warpSize)
  166. {
  167. return (uint) __shfl_up((int) val, delta, width);
  168. }
  169. __device__ __forceinline__ float shfl_up(float val, uint delta, int width = warpSize)
  170. {
  171. return __shfl_up(val, delta, width);
  172. }
  173. __device__ double shfl_up(double val, uint delta, int width = warpSize)
  174. {
  175. int lo = __double2loint(val);
  176. int hi = __double2hiint(val);
  177. lo = __shfl_up(lo, delta, width);
  178. hi = __shfl_up(hi, delta, width);
  179. return __hiloint2double(hi, lo);
  180. }
  181. __device__ __forceinline__ unsigned long long shfl_up(unsigned long long val, uint delta, int width = warpSize)
  182. {
  183. return __shfl_up(val, delta, width);
  184. }
  185. #define CV_CUDEV_SHFL_UP_VEC_INST(input_type) \
  186. __device__ __forceinline__ input_type ## 1 shfl_up(const input_type ## 1 & val, uint delta, int width = warpSize) \
  187. { \
  188. return VecTraits<input_type ## 1>::make( \
  189. shfl_up(val.x, delta, width) \
  190. ); \
  191. } \
  192. __device__ __forceinline__ input_type ## 2 shfl_up(const input_type ## 2 & val, uint delta, int width = warpSize) \
  193. { \
  194. return VecTraits<input_type ## 2>::make( \
  195. shfl_up(val.x, delta, width), \
  196. shfl_up(val.y, delta, width) \
  197. ); \
  198. } \
  199. __device__ __forceinline__ input_type ## 3 shfl_up(const input_type ## 3 & val, uint delta, int width = warpSize) \
  200. { \
  201. return VecTraits<input_type ## 3>::make( \
  202. shfl_up(val.x, delta, width), \
  203. shfl_up(val.y, delta, width), \
  204. shfl_up(val.z, delta, width) \
  205. ); \
  206. } \
  207. __device__ __forceinline__ input_type ## 4 shfl_up(const input_type ## 4 & val, uint delta, int width = warpSize) \
  208. { \
  209. return VecTraits<input_type ## 4>::make( \
  210. shfl_up(val.x, delta, width), \
  211. shfl_up(val.y, delta, width), \
  212. shfl_up(val.z, delta, width), \
  213. shfl_up(val.w, delta, width) \
  214. ); \
  215. }
  216. CV_CUDEV_SHFL_UP_VEC_INST(uchar)
  217. CV_CUDEV_SHFL_UP_VEC_INST(char)
  218. CV_CUDEV_SHFL_UP_VEC_INST(ushort)
  219. CV_CUDEV_SHFL_UP_VEC_INST(short)
  220. CV_CUDEV_SHFL_UP_VEC_INST(uint)
  221. CV_CUDEV_SHFL_UP_VEC_INST(int)
  222. CV_CUDEV_SHFL_UP_VEC_INST(float)
  223. CV_CUDEV_SHFL_UP_VEC_INST(double)
  224. #undef CV_CUDEV_SHFL_UP_VEC_INST
  225. #endif
  226. template <typename T>
  227. __device__ __forceinline__ T compatible_shfl_up(T val, uint delta, int width = warpSize)
  228. {
  229. #if __CUDACC_VER_MAJOR__ < 9
  230. return shfl_up(val, delta, width);
  231. #else // __CUDACC_VER_MAJOR__ < 9
  232. #if CV_CUDEV_ARCH >= 700
  233. return shfl_up_sync(0xFFFFFFFFU, val, delta, width);
  234. #else
  235. const int block_size = Block::blockSize();
  236. const int residual = block_size & (warpSize - 1);
  237. if (0 == residual)
  238. return shfl_up_sync(0xFFFFFFFFU, val, delta, width);
  239. else
  240. {
  241. const int n_warps = divUp(block_size, warpSize);
  242. const int warp_id = Warp::warpId();
  243. if (warp_id < n_warps - 1)
  244. return shfl_up_sync(0xFFFFFFFFU, val, delta, width);
  245. else
  246. {
  247. // We are at the last threads of a block whose number of threads
  248. // is not a multiple of the warp size
  249. uint mask = (1LU << residual) - 1;
  250. return shfl_up_sync(mask, val, delta, width);
  251. }
  252. }
  253. #endif
  254. #endif // __CUDACC_VER_MAJOR__ < 9
  255. }
  256. // shfl_down
  257. __device__ __forceinline__ uchar shfl_down(uchar val, uint delta, int width = warpSize)
  258. {
  259. return (uchar) __shfl_down((int) val, delta, width);
  260. }
  261. __device__ __forceinline__ schar shfl_down(schar val, uint delta, int width = warpSize)
  262. {
  263. return (schar) __shfl_down((int) val, delta, width);
  264. }
  265. __device__ __forceinline__ ushort shfl_down(ushort val, uint delta, int width = warpSize)
  266. {
  267. return (ushort) __shfl_down((int) val, delta, width);
  268. }
  269. __device__ __forceinline__ short shfl_down(short val, uint delta, int width = warpSize)
  270. {
  271. return (short) __shfl_down((int) val, delta, width);
  272. }
  273. __device__ __forceinline__ int shfl_down(int val, uint delta, int width = warpSize)
  274. {
  275. return __shfl_down(val, delta, width);
  276. }
  277. __device__ __forceinline__ uint shfl_down(uint val, uint delta, int width = warpSize)
  278. {
  279. return (uint) __shfl_down((int) val, delta, width);
  280. }
  281. __device__ __forceinline__ float shfl_down(float val, uint delta, int width = warpSize)
  282. {
  283. return __shfl_down(val, delta, width);
  284. }
  285. __device__ double shfl_down(double val, uint delta, int width = warpSize)
  286. {
  287. int lo = __double2loint(val);
  288. int hi = __double2hiint(val);
  289. lo = __shfl_down(lo, delta, width);
  290. hi = __shfl_down(hi, delta, width);
  291. return __hiloint2double(hi, lo);
  292. }
  293. #define CV_CUDEV_SHFL_DOWN_VEC_INST(input_type) \
  294. __device__ __forceinline__ input_type ## 1 shfl_down(const input_type ## 1 & val, uint delta, int width = warpSize) \
  295. { \
  296. return VecTraits<input_type ## 1>::make( \
  297. shfl_down(val.x, delta, width) \
  298. ); \
  299. } \
  300. __device__ __forceinline__ input_type ## 2 shfl_down(const input_type ## 2 & val, uint delta, int width = warpSize) \
  301. { \
  302. return VecTraits<input_type ## 2>::make( \
  303. shfl_down(val.x, delta, width), \
  304. shfl_down(val.y, delta, width) \
  305. ); \
  306. } \
  307. __device__ __forceinline__ input_type ## 3 shfl_down(const input_type ## 3 & val, uint delta, int width = warpSize) \
  308. { \
  309. return VecTraits<input_type ## 3>::make( \
  310. shfl_down(val.x, delta, width), \
  311. shfl_down(val.y, delta, width), \
  312. shfl_down(val.z, delta, width) \
  313. ); \
  314. } \
  315. __device__ __forceinline__ input_type ## 4 shfl_down(const input_type ## 4 & val, uint delta, int width = warpSize) \
  316. { \
  317. return VecTraits<input_type ## 4>::make( \
  318. shfl_down(val.x, delta, width), \
  319. shfl_down(val.y, delta, width), \
  320. shfl_down(val.z, delta, width), \
  321. shfl_down(val.w, delta, width) \
  322. ); \
  323. }
  324. CV_CUDEV_SHFL_DOWN_VEC_INST(uchar)
  325. CV_CUDEV_SHFL_DOWN_VEC_INST(char)
  326. CV_CUDEV_SHFL_DOWN_VEC_INST(ushort)
  327. CV_CUDEV_SHFL_DOWN_VEC_INST(short)
  328. CV_CUDEV_SHFL_DOWN_VEC_INST(uint)
  329. CV_CUDEV_SHFL_DOWN_VEC_INST(int)
  330. CV_CUDEV_SHFL_DOWN_VEC_INST(float)
  331. CV_CUDEV_SHFL_DOWN_VEC_INST(double)
  332. #undef CV_CUDEV_SHFL_DOWN_VEC_INST
  333. // shfl_xor
  334. __device__ __forceinline__ uchar shfl_xor(uchar val, int laneMask, int width = warpSize)
  335. {
  336. return (uchar) __shfl_xor((int) val, laneMask, width);
  337. }
  338. __device__ __forceinline__ schar shfl_xor(schar val, int laneMask, int width = warpSize)
  339. {
  340. return (schar) __shfl_xor((int) val, laneMask, width);
  341. }
  342. __device__ __forceinline__ ushort shfl_xor(ushort val, int laneMask, int width = warpSize)
  343. {
  344. return (ushort) __shfl_xor((int) val, laneMask, width);
  345. }
  346. __device__ __forceinline__ short shfl_xor(short val, int laneMask, int width = warpSize)
  347. {
  348. return (short) __shfl_xor((int) val, laneMask, width);
  349. }
  350. __device__ __forceinline__ int shfl_xor(int val, int laneMask, int width = warpSize)
  351. {
  352. return __shfl_xor(val, laneMask, width);
  353. }
  354. __device__ __forceinline__ uint shfl_xor(uint val, int laneMask, int width = warpSize)
  355. {
  356. return (uint) __shfl_xor((int) val, laneMask, width);
  357. }
  358. __device__ __forceinline__ float shfl_xor(float val, int laneMask, int width = warpSize)
  359. {
  360. return __shfl_xor(val, laneMask, width);
  361. }
  362. __device__ double shfl_xor(double val, int laneMask, int width = warpSize)
  363. {
  364. int lo = __double2loint(val);
  365. int hi = __double2hiint(val);
  366. lo = __shfl_xor(lo, laneMask, width);
  367. hi = __shfl_xor(hi, laneMask, width);
  368. return __hiloint2double(hi, lo);
  369. }
  370. #define CV_CUDEV_SHFL_XOR_VEC_INST(input_type) \
  371. __device__ __forceinline__ input_type ## 1 shfl_xor(const input_type ## 1 & val, int laneMask, int width = warpSize) \
  372. { \
  373. return VecTraits<input_type ## 1>::make( \
  374. shfl_xor(val.x, laneMask, width) \
  375. ); \
  376. } \
  377. __device__ __forceinline__ input_type ## 2 shfl_xor(const input_type ## 2 & val, int laneMask, int width = warpSize) \
  378. { \
  379. return VecTraits<input_type ## 2>::make( \
  380. shfl_xor(val.x, laneMask, width), \
  381. shfl_xor(val.y, laneMask, width) \
  382. ); \
  383. } \
  384. __device__ __forceinline__ input_type ## 3 shfl_xor(const input_type ## 3 & val, int laneMask, int width = warpSize) \
  385. { \
  386. return VecTraits<input_type ## 3>::make( \
  387. shfl_xor(val.x, laneMask, width), \
  388. shfl_xor(val.y, laneMask, width), \
  389. shfl_xor(val.z, laneMask, width) \
  390. ); \
  391. } \
  392. __device__ __forceinline__ input_type ## 4 shfl_xor(const input_type ## 4 & val, int laneMask, int width = warpSize) \
  393. { \
  394. return VecTraits<input_type ## 4>::make( \
  395. shfl_xor(val.x, laneMask, width), \
  396. shfl_xor(val.y, laneMask, width), \
  397. shfl_xor(val.z, laneMask, width), \
  398. shfl_xor(val.w, laneMask, width) \
  399. ); \
  400. }
  401. CV_CUDEV_SHFL_XOR_VEC_INST(uchar)
  402. CV_CUDEV_SHFL_XOR_VEC_INST(char)
  403. CV_CUDEV_SHFL_XOR_VEC_INST(ushort)
  404. CV_CUDEV_SHFL_XOR_VEC_INST(short)
  405. CV_CUDEV_SHFL_XOR_VEC_INST(uint)
  406. CV_CUDEV_SHFL_XOR_VEC_INST(int)
  407. CV_CUDEV_SHFL_XOR_VEC_INST(float)
  408. CV_CUDEV_SHFL_XOR_VEC_INST(double)
  409. #undef CV_CUDEV_SHFL_XOR_VEC_INST
  410. #undef __shfl
  411. #undef __shfl_xor
  412. #undef __shfl_up
  413. #undef __shfl_down
  414. #endif // CV_CUDEV_ARCH >= 300
  415. //! @}
  416. }}
  417. #endif