Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2016 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 #if GOOGLE_CUDA
     17 
     18 #define EIGEN_USE_GPU
     19 
     20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     21 #include "tensorflow/core/kernels/inplace_ops_functor.h"
     22 #include "tensorflow/core/util/cuda_kernel_helper.h"
     23 
     24 namespace tensorflow {
     25 namespace functor {
     26 
     27 typedef Eigen::GpuDevice Device;
     28 
     29 template <typename T>
     30 __global__ void DoParallelConcatOpKernel(int nthreads, const int64 rows,
     31                                          const int64 cols, int32 loc,
     32                                          const T* src, T* dst) {
     33   CUDA_1D_KERNEL_LOOP(idx, nthreads) {
     34     int64 c = idx % cols;
     35     int64 r = (loc % rows + rows) % rows;  // Guard index range.
     36     T* p = dst + r * cols + c;
     37     const T* q = src + idx;
     38     *p = ldg(q);
     39   }
     40 }
     41 
     42 template <typename T>
     43 Status DoParallelConcatUpdate(const Device& d, const Tensor& value, int32 loc,
     44                               Tensor* output) {
     45   const int64 nelem = value.NumElements();
     46   CudaLaunchConfig cfg = GetCudaLaunchConfig(nelem, d);
     47   auto Toutput = output->flat_outer_dims<T>();
     48   const int64 nrows = Toutput.dimension(0);
     49   const int64 ncols = Toutput.dimension(1);
     50   const T* src = value.flat<T>().data();
     51   T* dst = output->flat<T>().data();
     52   DoParallelConcatOpKernel<T>
     53       <<<cfg.block_count, cfg.thread_per_block, 0, d.stream()>>>(
     54           cfg.virtual_thread_count, nrows, ncols, loc, src, dst);
     55   return Status::OK();
     56 }
     57 
     58 template <>
     59 Status DoParallelConcat(const Device& d, const Tensor& value, int32 loc,
     60                         Tensor* output) {
     61   CHECK_EQ(value.dtype(), output->dtype());
     62   switch (value.dtype()) {
     63 #define CASE(type)                                              \
     64   case DataTypeToEnum<type>::value:                             \
     65     return DoParallelConcatUpdate<type>(d, value, loc, output); \
     66     break;
     67 
     68     CASE(float)
     69     CASE(double)
     70     CASE(Eigen::half)
     71 // Using TF_CALL_GPU_NUMBER_TYPES(CASE) results in the compiler complaining
     72 // that CASE is not defined...hence the above construction
     73 #undef CASE
     74     default:
     75       return errors::InvalidArgument("Unsupported data type: ", value.dtype());
     76   }
     77   return Status::OK();
     78 }
     79 
     80 }  // end namespace functor
     81 }  // namespace tensorflow
     82 #endif  // GOOGLE_CUDA
     83