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 <complex>
     21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     22 #include "tensorflow/core/framework/register_types.h"
     23 #include "tensorflow/core/kernels/cuda_solvers.h"
     24 #include "tensorflow/core/kernels/matrix_band_part_op.h"
     25 #include "tensorflow/core/util/cuda_kernel_helper.h"
     26 
     27 namespace tensorflow {
     28 namespace functor {
     29 typedef Eigen::GpuDevice GPUDevice;
     30 
     31 template <typename Scalar>
     32 __global__ void MatrixBandPartKernel(const int num_threads,
     33                                      const int batch_size, const int m,
     34                                      const int n, const int num_lower_diags,
     35                                      const int num_upper_diags,
     36                                      const Scalar* input_ptr,
     37                                      Scalar* output_ptr) {
     38   CUDA_1D_KERNEL_LOOP(index, num_threads) {
     39     const int col = index % n;
     40     const int row = (index / n) % m;
     41     const int band_start = (num_lower_diags < 0 ? 0 : row - num_lower_diags);
     42     const int band_end = (num_upper_diags < 0 ? n : row + num_upper_diags + 1);
     43     if (col < band_start || col >= band_end) {
     44       output_ptr[index] = Scalar();
     45     } else {
     46       output_ptr[index] = input_ptr[index];
     47     }
     48   }
     49 }
     50 
     51 template <typename Scalar>
     52 struct MatrixBandPartFunctor<GPUDevice, Scalar> {
     53   void operator()(OpKernelContext* context, const GPUDevice& device,
     54                   int num_lower_diags, int num_upper_diags,
     55                   typename TTypes<Scalar, 3>::ConstTensor input,
     56                   typename TTypes<Scalar, 3>::Tensor output) {
     57     const int batch_size = input.dimension(0);
     58     const int m = input.dimension(1);
     59     const int n = input.dimension(2);
     60     CudaLaunchConfig config = GetCudaLaunchConfig(batch_size * m * n, device);
     61     MatrixBandPartKernel<<<config.block_count, config.thread_per_block, 0,
     62                            device.stream()>>>(
     63         config.virtual_thread_count, batch_size, m, n, num_lower_diags,
     64         num_upper_diags, input.data(), output.data());
     65   }
     66 };
     67 
     68 #define DEFINE_GPU_SPEC(T) template struct MatrixBandPartFunctor<GPUDevice, T>;
     69 
     70 TF_CALL_GPU_NUMBER_TYPES(DEFINE_GPU_SPEC);
     71 TF_CALL_bool(DEFINE_GPU_SPEC);
     72 TF_CALL_complex64(DEFINE_GPU_SPEC);
     73 TF_CALL_complex128(DEFINE_GPU_SPEC);
     74 
     75 #undef DEFINE_GPU_SPEC
     76 }  // namespace functor
     77 }  // namespace tensorflow
     78 
     79 #endif  // GOOGLE_CUDA
     80