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