/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #if GOOGLE_CUDA #define EIGEN_USE_GPU #include "tensorflow/core/kernels/eye_functor.h" #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" #include "tensorflow/core/framework/tensor_types.h" #include "tensorflow/core/platform/types.h" #include "tensorflow/core/util/cuda_kernel_helper.h" namespace tensorflow { namespace functor { typedef Eigen::GpuDevice GPUDevice; template __global__ void EyeKernel(int num_threads, int batch_size, int m, int n, Scalar* output_ptr) { const Scalar one = Scalar(1); const Scalar zero = Scalar(0); CUDA_1D_KERNEL_LOOP(index, num_threads) { // TODO(rmlarsen): Benchmark to see if it's just as fast to use mod (%), // since it's easier to read. const int global_row = index / n; const int col = index - global_row * n; const int batch = global_row / m; const int row = global_row - batch * m; output_ptr[index] = col == row ? one : zero; } } template struct EyeFunctor { void operator()(const GPUDevice& device, typename TTypes::Tensor matrix_batch) { const int batch_size = matrix_batch.dimension(0); const int m = matrix_batch.dimension(1); const int n = matrix_batch.dimension(2); CudaLaunchConfig config = GetCudaLaunchConfig(batch_size * m * n, device); EyeKernel<<>>(config.virtual_thread_count, batch_size, m, n, matrix_batch.data()); } }; template struct EyeFunctor; template struct EyeFunctor; template struct EyeFunctor; template struct EyeFunctor; } // namespace functor } // namespace tensorflow #endif // GOOGLE_CUDA