1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_CORE_UTIL_CUDA_KERNEL_HELPER_H_
17 #define TENSORFLOW_CORE_UTIL_CUDA_KERNEL_HELPER_H_
18
19 #if GOOGLE_CUDA
20
21 #include "tensorflow/core/util/cuda_device_functions.h"
22 #include "tensorflow/core/util/cuda_launch_config.h"
23
24 // Deprecated, use 'for(int i : CudaGridRangeX(n))' instead.
25 #define CUDA_1D_KERNEL_LOOP(i, n) \
26 for (int i : ::tensorflow::CudaGridRangeX<int>(n))
27 // Deprecated, use 'for(int i : CudaGridRange?(n))' instead.
28 #define CUDA_AXIS_KERNEL_LOOP(i, n, axis) \
29 for (int i : ::tensorflow::CudaGridRange##axis<int>(n))
30
31 namespace tensorflow {
CudaLdg(const tensorflow::bfloat16 * address)32 __host__ __device__ inline tensorflow::bfloat16 CudaLdg(
33 const tensorflow::bfloat16* address) {
34 tensorflow::bfloat16 return_value;
35 return_value.value = CudaLdg(reinterpret_cast<const uint16_t*>(address));
36 return return_value;
37 }
38
39 template <typename T>
ldg(const T * ptr)40 __host__ __device__ inline T ldg(const T* ptr) {
41 return CudaLdg(ptr);
42 }
43
44 template <typename T>
tf_min(const T & x,const T & y)45 __host__ __device__ inline const T& tf_min(const T& x, const T& y) {
46 return x < y ? x : y;
47 }
48
49 template <typename T>
tf_max(const T & x,const T & y)50 __host__ __device__ inline const T& tf_max(const T& x, const T& y) {
51 return x < y ? y : x;
52 }
53
54 // Overloads of the above functions for float and double.
tf_min(float x,float y)55 __host__ __device__ inline float tf_min(float x, float y) {
56 return fminf(x, y);
57 }
tf_min(double x,double y)58 __host__ __device__ inline double tf_min(double x, double y) {
59 return fmin(x, y);
60 }
tf_max(float x,float y)61 __host__ __device__ inline float tf_max(float x, float y) {
62 return fmaxf(x, y);
63 }
tf_max(double x,double y)64 __host__ __device__ inline double tf_max(double x, double y) {
65 return fmax(x, y);
66 }
67
68 __device__ inline Eigen::half CudaShuffleSync(unsigned mask, Eigen::half value,
69 int src_lane,
70 int width = warpSize) {
71 return Eigen::half(
72 CudaShuffleSync(mask, static_cast<uint16>(value), src_lane, width));
73 }
74
75 __device__ EIGEN_ALWAYS_INLINE Eigen::half CudaShuffleUpSync(
76 unsigned mask, Eigen::half value, int delta, int width = warpSize) {
77 return Eigen::half(
78 CudaShuffleUpSync(mask, static_cast<uint16>(value), delta, width));
79 }
80
81 __device__ EIGEN_ALWAYS_INLINE Eigen::half CudaShuffleDownSync(
82 unsigned mask, Eigen::half value, int delta, int width = warpSize) {
83 return Eigen::half(
84 CudaShuffleDownSync(mask, static_cast<uint16>(value), delta, width));
85 }
86
87 __device__ EIGEN_ALWAYS_INLINE Eigen::half CudaShuffleXorSync(
88 unsigned mask, Eigen::half value, int lane_mask, int width = warpSize) {
89 return Eigen::half(
90 CudaShuffleXorSync(mask, static_cast<uint16>(value), lane_mask, width));
91 }
92
93 namespace cuda_helper {
94 template <typename IntType>
upper_bound(IntType * first,IntType count,IntType val)95 __device__ IntType upper_bound(IntType* first, IntType count, IntType val) {
96 IntType* orig = first;
97 IntType* it = nullptr;
98 IntType step = 0;
99 while (count > 0) {
100 it = first;
101 step = count / 2;
102 it += step;
103 if (!(val < *it)) {
104 first = ++it;
105 count -= step + 1;
106 } else {
107 count = step;
108 }
109 }
110
111 return first - orig;
112 }
113 } // namespace cuda_helper
114 } // namespace tensorflow
115
116 #endif // GOOGLE_CUDA
117 #endif // TENSORFLOW_CORE_UTIL_CUDA_KERNEL_HELPER_H_
118