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 #ifndef TENSORFLOW_CORE_KERNELS_CUDA_DEVICE_ARRAY_H_
     16 #define TENSORFLOW_CORE_KERNELS_CUDA_DEVICE_ARRAY_H_
     17 
     18 #if GOOGLE_CUDA
     19 
     20 #include "tensorflow/core/common_runtime/gpu/gpu_event_mgr.h"
     21 #include "tensorflow/core/framework/op_kernel.h"
     22 #include "tensorflow/core/kernels/cuda_device_array_gpu.h"
     23 
     24 namespace tensorflow {
     25 
     26 // Create an array of value on the host, to be sent to kernel using
     27 // CudaDeviceArrayStruct.
     28 //
     29 // Usage:
     30 //   int size = ...;
     31 //   CudaDeviceArrayOnHost ptrs(context, size);
     32 //   OP_REQUIRES_OK(ptrs.Init());
     33 //   for (int i = 0; i < size; ++i) {
     34 //     ptrs.Set(i, ...);
     35 //   }
     36 //   OP_REQUIRES_OK(ptrs.Finalize());
     37 //   launchKernel(..., ptrs.data, ...);
     38 //
     39 // ValueType must be memcopyable.
     40 template <typename ValueType, int MaxInlineValues = 8>
     41 class CudaDeviceArrayOnHost {
     42  public:
     43   CudaDeviceArrayOnHost(OpKernelContext* context, int32 size)
     44       : context_(context),
     45         total_bytes_(static_cast<int64>(size) * sizeof(ValueType)) {
     46     data_.size = size;
     47   }
     48 
     49   Status Init() {
     50     if (inlined()) {
     51       values_ = data_.inline_values;
     52       return Status::OK();
     53     }
     54 
     55     // Out-of-line: allocate data that will be memcopied.
     56     AllocatorAttributes attr;
     57     attr.set_on_host(true);
     58     attr.set_gpu_compatible(true);
     59     TF_RETURN_IF_ERROR(
     60         context_->allocate_temp(DT_INT8, TensorShape{total_bytes_},
     61                                 &out_of_line_values_on_host_, attr));
     62     values_ = reinterpret_cast<ValueType*>(
     63         out_of_line_values_on_host_.flat<int8>().data());
     64     return Status::OK();
     65   }
     66 
     67   void Set(int index, ValueType val) {
     68     DCHECK(values_);  // ensure Init was called.
     69     DCHECK_LT(index, data_.size);
     70     *(values_ + index) = val;
     71   }
     72 
     73   Status Finalize() {
     74     if (inlined()) {
     75       return Status::OK();
     76     }
     77 
     78     // Out-of-line - copy pointers to device.
     79     auto stream = context_->op_device_context()->stream();
     80     TensorReference tensor_ref(out_of_line_values_on_host_);
     81     TF_RETURN_IF_ERROR(context_->allocate_temp(
     82         DT_INT8, TensorShape{total_bytes_}, &out_of_line_values_on_gpu_));
     83     perftools::gputools::DeviceMemoryBase output_values_base{
     84         out_of_line_values_on_gpu_.flat<int8>().data(),
     85         static_cast<uint64>(total_bytes_)};
     86     stream->ThenMemcpy(&output_values_base,
     87                        out_of_line_values_on_host_.flat<int8>().data(),
     88                        total_bytes_);
     89     context_->device()->tensorflow_gpu_device_info()->event_mgr->ThenExecute(
     90         stream, [tensor_ref]() { tensor_ref.Unref(); });
     91     data_.out_of_line_values = reinterpret_cast<ValueType*>(
     92         out_of_line_values_on_gpu_.flat<int8>().data());
     93     return Status::OK();
     94   }
     95 
     96   const CudaDeviceArrayStruct<ValueType, MaxInlineValues>& data() const {
     97     // Ensure Finalize is called.
     98     DCHECK(inlined() || out_of_line_values_on_gpu_.IsInitialized());
     99     return data_;
    100   }
    101 
    102  private:
    103   bool inlined() const { return data_.size <= MaxInlineValues; }
    104 
    105   OpKernelContext* const context_;
    106   const int64 total_bytes_;  // total size of all pointers.
    107   ValueType* values_ = nullptr;
    108   CudaDeviceArrayStruct<ValueType, MaxInlineValues> data_;
    109 
    110   Tensor out_of_line_values_on_host_;
    111   Tensor out_of_line_values_on_gpu_;
    112 
    113   TF_DISALLOW_COPY_AND_ASSIGN(CudaDeviceArrayOnHost);
    114 };
    115 
    116 }  // namespace tensorflow
    117 
    118 #endif  // GOOGLE_CUDA
    119 
    120 #endif  // TENSORFLOW_CORE_KERNELS_CUDA_DEVICE_ARRAY_H_
    121