warp.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_DEVICE_WARP_HPP
  43. #define OPENCV_CUDA_DEVICE_WARP_HPP
  44. /** @file
  45. * @deprecated Use @ref cudev instead.
  46. */
  47. //! @cond IGNORED
  48. namespace cv { namespace cuda { namespace device
  49. {
  50. struct Warp
  51. {
  52. enum
  53. {
  54. LOG_WARP_SIZE = 5,
  55. WARP_SIZE = 1 << LOG_WARP_SIZE,
  56. STRIDE = WARP_SIZE
  57. };
  58. /** \brief Returns the warp lane ID of the calling thread. */
  59. static __device__ __forceinline__ unsigned int laneId()
  60. {
  61. unsigned int ret;
  62. asm("mov.u32 %0, %%laneid;" : "=r"(ret) );
  63. return ret;
  64. }
  65. template<typename It, typename T>
  66. static __device__ __forceinline__ void fill(It beg, It end, const T& value)
  67. {
  68. for(It t = beg + laneId(); t < end; t += STRIDE)
  69. *t = value;
  70. }
  71. template<typename InIt, typename OutIt>
  72. static __device__ __forceinline__ OutIt copy(InIt beg, InIt end, OutIt out)
  73. {
  74. for(InIt t = beg + laneId(); t < end; t += STRIDE, out += STRIDE)
  75. *out = *t;
  76. return out;
  77. }
  78. template<typename InIt, typename OutIt, class UnOp>
  79. static __device__ __forceinline__ OutIt transform(InIt beg, InIt end, OutIt out, UnOp op)
  80. {
  81. for(InIt t = beg + laneId(); t < end; t += STRIDE, out += STRIDE)
  82. *out = op(*t);
  83. return out;
  84. }
  85. template<typename InIt1, typename InIt2, typename OutIt, class BinOp>
  86. static __device__ __forceinline__ OutIt transform(InIt1 beg1, InIt1 end1, InIt2 beg2, OutIt out, BinOp op)
  87. {
  88. unsigned int lane = laneId();
  89. InIt1 t1 = beg1 + lane;
  90. InIt2 t2 = beg2 + lane;
  91. for(; t1 < end1; t1 += STRIDE, t2 += STRIDE, out += STRIDE)
  92. *out = op(*t1, *t2);
  93. return out;
  94. }
  95. template <class T, class BinOp>
  96. static __device__ __forceinline__ T reduce(volatile T *ptr, BinOp op)
  97. {
  98. const unsigned int lane = laneId();
  99. if (lane < 16)
  100. {
  101. T partial = ptr[lane];
  102. ptr[lane] = partial = op(partial, ptr[lane + 16]);
  103. ptr[lane] = partial = op(partial, ptr[lane + 8]);
  104. ptr[lane] = partial = op(partial, ptr[lane + 4]);
  105. ptr[lane] = partial = op(partial, ptr[lane + 2]);
  106. ptr[lane] = partial = op(partial, ptr[lane + 1]);
  107. }
  108. return *ptr;
  109. }
  110. template<typename OutIt, typename T>
  111. static __device__ __forceinline__ void yota(OutIt beg, OutIt end, T value)
  112. {
  113. unsigned int lane = laneId();
  114. value += lane;
  115. for(OutIt t = beg + lane; t < end; t += STRIDE, value += STRIDE)
  116. *t = value;
  117. }
  118. };
  119. }}} // namespace cv { namespace cuda { namespace cudev
  120. //! @endcond
  121. #endif /* OPENCV_CUDA_DEVICE_WARP_HPP */