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