123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- /*M///////////////////////////////////////////////////////////////////////////////////////
- //
- // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
- //
- // By downloading, copying, installing or using the software you agree to this license.
- // If you do not agree to this license, do not download, install,
- // copy or use the software.
- //
- //
- // License Agreement
- // For Open Source Computer Vision Library
- //
- // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
- // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
- // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
- // Third party copyrights are property of their respective owners.
- //
- // Redistribution and use in source and binary forms, with or without modification,
- // are permitted provided that the following conditions are met:
- //
- // * Redistribution's of source code must retain the above copyright notice,
- // this list of conditions and the following disclaimer.
- //
- // * Redistribution's in binary form must reproduce the above copyright notice,
- // this list of conditions and the following disclaimer in the documentation
- // and/or other materials provided with the distribution.
- //
- // * The name of the copyright holders may not be used to endorse or promote products
- // derived from this software without specific prior written permission.
- //
- // This software is provided by the copyright holders and contributors "as is" and
- // any express or implied warranties, including, but not limited to, the implied
- // warranties of merchantability and fitness for a particular purpose are disclaimed.
- // In no event shall the Intel Corporation or contributors be liable for any direct,
- // indirect, incidental, special, exemplary, or consequential damages
- // (including, but not limited to, procurement of substitute goods or services;
- // loss of use, data, or profits; or business interruption) however caused
- // and on any theory of liability, whether in contract, strict liability,
- // or tort (including negligence or otherwise) arising in any way out of
- // the use of this software, even if advised of the possibility of such damage.
- //
- //M*/
- #pragma once
- #ifndef OPENCV_CUDEV_WARP_SHUFFLE_HPP
- #define OPENCV_CUDEV_WARP_SHUFFLE_HPP
- #include "../common.hpp"
- #include "../util/vec_traits.hpp"
- #include "../block/block.hpp"
- #include "warp.hpp"
- namespace cv { namespace cudev {
- //! @addtogroup cudev
- //! @{
- #if CV_CUDEV_ARCH >= 300
- #if __CUDACC_VER_MAJOR__ >= 9
- # define __shfl(x, y, z) __shfl_sync(0xFFFFFFFFU, x, y, z)
- # define __shfl_xor(x, y, z) __shfl_xor_sync(0xFFFFFFFFU, x, y, z)
- //# define __shfl_up(x, y, z) __shfl_up_sync(0xFFFFFFFFU, x, y, z)
- # define __shfl_down(x, y, z) __shfl_down_sync(0xFFFFFFFFU, x, y, z)
- #endif
- // shfl
- __device__ __forceinline__ uchar shfl(uchar val, int srcLane, int width = warpSize)
- {
- return (uchar) __shfl((int) val, srcLane, width);
- }
- __device__ __forceinline__ schar shfl(schar val, int srcLane, int width = warpSize)
- {
- return (schar) __shfl((int) val, srcLane, width);
- }
- __device__ __forceinline__ ushort shfl(ushort val, int srcLane, int width = warpSize)
- {
- return (ushort) __shfl((int) val, srcLane, width);
- }
- __device__ __forceinline__ short shfl(short val, int srcLane, int width = warpSize)
- {
- return (short) __shfl((int) val, srcLane, width);
- }
- __device__ __forceinline__ int shfl(int val, int srcLane, int width = warpSize)
- {
- return __shfl(val, srcLane, width);
- }
- __device__ __forceinline__ uint shfl(uint val, int srcLane, int width = warpSize)
- {
- return (uint) __shfl((int) val, srcLane, width);
- }
- __device__ __forceinline__ float shfl(float val, int srcLane, int width = warpSize)
- {
- return __shfl(val, srcLane, width);
- }
- __device__ double shfl(double val, int srcLane, int width = warpSize)
- {
- int lo = __double2loint(val);
- int hi = __double2hiint(val);
- lo = __shfl(lo, srcLane, width);
- hi = __shfl(hi, srcLane, width);
- return __hiloint2double(hi, lo);
- }
- #define CV_CUDEV_SHFL_VEC_INST(input_type) \
- __device__ __forceinline__ input_type ## 1 shfl(const input_type ## 1 & val, int srcLane, int width = warpSize) \
- { \
- return VecTraits<input_type ## 1>::make( \
- shfl(val.x, srcLane, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 2 shfl(const input_type ## 2 & val, int srcLane, int width = warpSize) \
- { \
- return VecTraits<input_type ## 2>::make( \
- shfl(val.x, srcLane, width), \
- shfl(val.y, srcLane, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 3 shfl(const input_type ## 3 & val, int srcLane, int width = warpSize) \
- { \
- return VecTraits<input_type ## 3>::make( \
- shfl(val.x, srcLane, width), \
- shfl(val.y, srcLane, width), \
- shfl(val.z, srcLane, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 4 shfl(const input_type ## 4 & val, int srcLane, int width = warpSize) \
- { \
- return VecTraits<input_type ## 4>::make( \
- shfl(val.x, srcLane, width), \
- shfl(val.y, srcLane, width), \
- shfl(val.z, srcLane, width), \
- shfl(val.w, srcLane, width) \
- ); \
- }
- CV_CUDEV_SHFL_VEC_INST(uchar)
- CV_CUDEV_SHFL_VEC_INST(char)
- CV_CUDEV_SHFL_VEC_INST(ushort)
- CV_CUDEV_SHFL_VEC_INST(short)
- CV_CUDEV_SHFL_VEC_INST(uint)
- CV_CUDEV_SHFL_VEC_INST(int)
- CV_CUDEV_SHFL_VEC_INST(float)
- CV_CUDEV_SHFL_VEC_INST(double)
- #undef CV_CUDEV_SHFL_VEC_INST
- // shfl_up
- #if __CUDACC_VER_MAJOR__ >= 9
- template <typename T>
- __device__ __forceinline__ T shfl_up_sync(uint mask, T val, uint delta, int width = warpSize)
- {
- return (T) __shfl_up_sync(mask, val, delta, width);
- }
- #else
- __device__ __forceinline__ uchar shfl_up(uchar val, uint delta, int width = warpSize)
- {
- return (uchar) __shfl_up((int) val, delta, width);
- }
- __device__ __forceinline__ schar shfl_up(schar val, uint delta, int width = warpSize)
- {
- return (schar) __shfl_up((int) val, delta, width);
- }
- __device__ __forceinline__ ushort shfl_up(ushort val, uint delta, int width = warpSize)
- {
- return (ushort) __shfl_up((int) val, delta, width);
- }
- __device__ __forceinline__ short shfl_up(short val, uint delta, int width = warpSize)
- {
- return (short) __shfl_up((int) val, delta, width);
- }
- __device__ __forceinline__ int shfl_up(int val, uint delta, int width = warpSize)
- {
- return __shfl_up(val, delta, width);
- }
- __device__ __forceinline__ uint shfl_up(uint val, uint delta, int width = warpSize)
- {
- return (uint) __shfl_up((int) val, delta, width);
- }
- __device__ __forceinline__ float shfl_up(float val, uint delta, int width = warpSize)
- {
- return __shfl_up(val, delta, width);
- }
- __device__ double shfl_up(double val, uint delta, int width = warpSize)
- {
- int lo = __double2loint(val);
- int hi = __double2hiint(val);
- lo = __shfl_up(lo, delta, width);
- hi = __shfl_up(hi, delta, width);
- return __hiloint2double(hi, lo);
- }
- __device__ __forceinline__ unsigned long long shfl_up(unsigned long long val, uint delta, int width = warpSize)
- {
- return __shfl_up(val, delta, width);
- }
- #define CV_CUDEV_SHFL_UP_VEC_INST(input_type) \
- __device__ __forceinline__ input_type ## 1 shfl_up(const input_type ## 1 & val, uint delta, int width = warpSize) \
- { \
- return VecTraits<input_type ## 1>::make( \
- shfl_up(val.x, delta, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 2 shfl_up(const input_type ## 2 & val, uint delta, int width = warpSize) \
- { \
- return VecTraits<input_type ## 2>::make( \
- shfl_up(val.x, delta, width), \
- shfl_up(val.y, delta, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 3 shfl_up(const input_type ## 3 & val, uint delta, int width = warpSize) \
- { \
- return VecTraits<input_type ## 3>::make( \
- shfl_up(val.x, delta, width), \
- shfl_up(val.y, delta, width), \
- shfl_up(val.z, delta, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 4 shfl_up(const input_type ## 4 & val, uint delta, int width = warpSize) \
- { \
- return VecTraits<input_type ## 4>::make( \
- shfl_up(val.x, delta, width), \
- shfl_up(val.y, delta, width), \
- shfl_up(val.z, delta, width), \
- shfl_up(val.w, delta, width) \
- ); \
- }
- CV_CUDEV_SHFL_UP_VEC_INST(uchar)
- CV_CUDEV_SHFL_UP_VEC_INST(char)
- CV_CUDEV_SHFL_UP_VEC_INST(ushort)
- CV_CUDEV_SHFL_UP_VEC_INST(short)
- CV_CUDEV_SHFL_UP_VEC_INST(uint)
- CV_CUDEV_SHFL_UP_VEC_INST(int)
- CV_CUDEV_SHFL_UP_VEC_INST(float)
- CV_CUDEV_SHFL_UP_VEC_INST(double)
- #undef CV_CUDEV_SHFL_UP_VEC_INST
- #endif
- template <typename T>
- __device__ __forceinline__ T compatible_shfl_up(T val, uint delta, int width = warpSize)
- {
- #if __CUDACC_VER_MAJOR__ < 9
- return shfl_up(val, delta, width);
- #else // __CUDACC_VER_MAJOR__ < 9
- #if CV_CUDEV_ARCH >= 700
- return shfl_up_sync(0xFFFFFFFFU, val, delta, width);
- #else
- const int block_size = Block::blockSize();
- const int residual = block_size & (warpSize - 1);
- if (0 == residual)
- return shfl_up_sync(0xFFFFFFFFU, val, delta, width);
- else
- {
- const int n_warps = divUp(block_size, warpSize);
- const int warp_id = Warp::warpId();
- if (warp_id < n_warps - 1)
- return shfl_up_sync(0xFFFFFFFFU, val, delta, width);
- else
- {
- // We are at the last threads of a block whose number of threads
- // is not a multiple of the warp size
- uint mask = (1LU << residual) - 1;
- return shfl_up_sync(mask, val, delta, width);
- }
- }
- #endif
- #endif // __CUDACC_VER_MAJOR__ < 9
- }
- // shfl_down
- __device__ __forceinline__ uchar shfl_down(uchar val, uint delta, int width = warpSize)
- {
- return (uchar) __shfl_down((int) val, delta, width);
- }
- __device__ __forceinline__ schar shfl_down(schar val, uint delta, int width = warpSize)
- {
- return (schar) __shfl_down((int) val, delta, width);
- }
- __device__ __forceinline__ ushort shfl_down(ushort val, uint delta, int width = warpSize)
- {
- return (ushort) __shfl_down((int) val, delta, width);
- }
- __device__ __forceinline__ short shfl_down(short val, uint delta, int width = warpSize)
- {
- return (short) __shfl_down((int) val, delta, width);
- }
- __device__ __forceinline__ int shfl_down(int val, uint delta, int width = warpSize)
- {
- return __shfl_down(val, delta, width);
- }
- __device__ __forceinline__ uint shfl_down(uint val, uint delta, int width = warpSize)
- {
- return (uint) __shfl_down((int) val, delta, width);
- }
- __device__ __forceinline__ float shfl_down(float val, uint delta, int width = warpSize)
- {
- return __shfl_down(val, delta, width);
- }
- __device__ double shfl_down(double val, uint delta, int width = warpSize)
- {
- int lo = __double2loint(val);
- int hi = __double2hiint(val);
- lo = __shfl_down(lo, delta, width);
- hi = __shfl_down(hi, delta, width);
- return __hiloint2double(hi, lo);
- }
- #define CV_CUDEV_SHFL_DOWN_VEC_INST(input_type) \
- __device__ __forceinline__ input_type ## 1 shfl_down(const input_type ## 1 & val, uint delta, int width = warpSize) \
- { \
- return VecTraits<input_type ## 1>::make( \
- shfl_down(val.x, delta, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 2 shfl_down(const input_type ## 2 & val, uint delta, int width = warpSize) \
- { \
- return VecTraits<input_type ## 2>::make( \
- shfl_down(val.x, delta, width), \
- shfl_down(val.y, delta, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 3 shfl_down(const input_type ## 3 & val, uint delta, int width = warpSize) \
- { \
- return VecTraits<input_type ## 3>::make( \
- shfl_down(val.x, delta, width), \
- shfl_down(val.y, delta, width), \
- shfl_down(val.z, delta, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 4 shfl_down(const input_type ## 4 & val, uint delta, int width = warpSize) \
- { \
- return VecTraits<input_type ## 4>::make( \
- shfl_down(val.x, delta, width), \
- shfl_down(val.y, delta, width), \
- shfl_down(val.z, delta, width), \
- shfl_down(val.w, delta, width) \
- ); \
- }
- CV_CUDEV_SHFL_DOWN_VEC_INST(uchar)
- CV_CUDEV_SHFL_DOWN_VEC_INST(char)
- CV_CUDEV_SHFL_DOWN_VEC_INST(ushort)
- CV_CUDEV_SHFL_DOWN_VEC_INST(short)
- CV_CUDEV_SHFL_DOWN_VEC_INST(uint)
- CV_CUDEV_SHFL_DOWN_VEC_INST(int)
- CV_CUDEV_SHFL_DOWN_VEC_INST(float)
- CV_CUDEV_SHFL_DOWN_VEC_INST(double)
- #undef CV_CUDEV_SHFL_DOWN_VEC_INST
- // shfl_xor
- __device__ __forceinline__ uchar shfl_xor(uchar val, int laneMask, int width = warpSize)
- {
- return (uchar) __shfl_xor((int) val, laneMask, width);
- }
- __device__ __forceinline__ schar shfl_xor(schar val, int laneMask, int width = warpSize)
- {
- return (schar) __shfl_xor((int) val, laneMask, width);
- }
- __device__ __forceinline__ ushort shfl_xor(ushort val, int laneMask, int width = warpSize)
- {
- return (ushort) __shfl_xor((int) val, laneMask, width);
- }
- __device__ __forceinline__ short shfl_xor(short val, int laneMask, int width = warpSize)
- {
- return (short) __shfl_xor((int) val, laneMask, width);
- }
- __device__ __forceinline__ int shfl_xor(int val, int laneMask, int width = warpSize)
- {
- return __shfl_xor(val, laneMask, width);
- }
- __device__ __forceinline__ uint shfl_xor(uint val, int laneMask, int width = warpSize)
- {
- return (uint) __shfl_xor((int) val, laneMask, width);
- }
- __device__ __forceinline__ float shfl_xor(float val, int laneMask, int width = warpSize)
- {
- return __shfl_xor(val, laneMask, width);
- }
- __device__ double shfl_xor(double val, int laneMask, int width = warpSize)
- {
- int lo = __double2loint(val);
- int hi = __double2hiint(val);
- lo = __shfl_xor(lo, laneMask, width);
- hi = __shfl_xor(hi, laneMask, width);
- return __hiloint2double(hi, lo);
- }
- #define CV_CUDEV_SHFL_XOR_VEC_INST(input_type) \
- __device__ __forceinline__ input_type ## 1 shfl_xor(const input_type ## 1 & val, int laneMask, int width = warpSize) \
- { \
- return VecTraits<input_type ## 1>::make( \
- shfl_xor(val.x, laneMask, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 2 shfl_xor(const input_type ## 2 & val, int laneMask, int width = warpSize) \
- { \
- return VecTraits<input_type ## 2>::make( \
- shfl_xor(val.x, laneMask, width), \
- shfl_xor(val.y, laneMask, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 3 shfl_xor(const input_type ## 3 & val, int laneMask, int width = warpSize) \
- { \
- return VecTraits<input_type ## 3>::make( \
- shfl_xor(val.x, laneMask, width), \
- shfl_xor(val.y, laneMask, width), \
- shfl_xor(val.z, laneMask, width) \
- ); \
- } \
- __device__ __forceinline__ input_type ## 4 shfl_xor(const input_type ## 4 & val, int laneMask, int width = warpSize) \
- { \
- return VecTraits<input_type ## 4>::make( \
- shfl_xor(val.x, laneMask, width), \
- shfl_xor(val.y, laneMask, width), \
- shfl_xor(val.z, laneMask, width), \
- shfl_xor(val.w, laneMask, width) \
- ); \
- }
- CV_CUDEV_SHFL_XOR_VEC_INST(uchar)
- CV_CUDEV_SHFL_XOR_VEC_INST(char)
- CV_CUDEV_SHFL_XOR_VEC_INST(ushort)
- CV_CUDEV_SHFL_XOR_VEC_INST(short)
- CV_CUDEV_SHFL_XOR_VEC_INST(uint)
- CV_CUDEV_SHFL_XOR_VEC_INST(int)
- CV_CUDEV_SHFL_XOR_VEC_INST(float)
- CV_CUDEV_SHFL_XOR_VEC_INST(double)
- #undef CV_CUDEV_SHFL_XOR_VEC_INST
- #undef __shfl
- #undef __shfl_xor
- #undef __shfl_up
- #undef __shfl_down
- #endif // CV_CUDEV_ARCH >= 300
- //! @}
- }}
- #endif
|