1 /* Copyright 2019 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 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_CL_BUFFER_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_BUFFER_H_
18
19 #include "absl/strings/str_cat.h"
20 #include "absl/types/span.h"
21 #include "tensorflow/lite/delegates/gpu/cl/cl_command_queue.h"
22 #include "tensorflow/lite/delegates/gpu/cl/cl_context.h"
23 #include "tensorflow/lite/delegates/gpu/cl/gpu_object.h"
24 #include "tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h"
25 #include "tensorflow/lite/delegates/gpu/cl/util.h"
26 #include "tensorflow/lite/delegates/gpu/common/status.h"
27 #include "tensorflow/lite/delegates/gpu/common/task/buffer_desc.h"
28
29 namespace tflite {
30 namespace gpu {
31 namespace cl {
32
33 // Buffer represent linear GPU data storage with arbitrary data format.
34 // Buffer is moveable but not copyable.
35 class Buffer : public GPUObject {
36 public:
Buffer()37 Buffer() {} // just for using Buffer as a class members
38 Buffer(cl_mem buffer, size_t size_in_bytes, bool is_sub_buffer = false);
39
40 // Move only
41 Buffer(Buffer&& buffer);
42 Buffer& operator=(Buffer&& buffer);
43 Buffer(const Buffer&) = delete;
44 Buffer& operator=(const Buffer&) = delete;
45
~Buffer()46 ~Buffer() override { Release(); }
47
48 // for profiling and memory statistics
GetMemorySizeInBytes()49 uint64_t GetMemorySizeInBytes() const { return size_; }
50
GetMemoryPtr()51 cl_mem GetMemoryPtr() const { return buffer_; }
52
IsSubBuffer()53 bool IsSubBuffer() const { return is_sub_buffer_; }
54
55 // Writes data to a buffer. Data should point to a region that
56 // has exact size in bytes as size_in_bytes(constructor parameter).
57 template <typename T>
58 absl::Status WriteData(CLCommandQueue* queue, const absl::Span<T> data);
59
60 // Reads data from Buffer into CPU memory.
61 template <typename T>
62 absl::Status ReadData(CLCommandQueue* queue, std::vector<T>* result) const;
63
64 absl::Status GetGPUResources(const GPUObjectDescriptor* obj_ptr,
65 GPUResourcesWithValue* resources) const override;
66
67 absl::Status CreateFromBufferDescriptor(const BufferDescriptor& desc,
68 CLContext* context);
69
70 private:
71 void Release();
72
73 cl_mem buffer_ = nullptr;
74 size_t size_ = 0;
75 bool is_sub_buffer_ = false;
76 };
77
78 absl::Status CreateReadOnlyBuffer(size_t size_in_bytes, CLContext* context,
79 Buffer* result);
80
81 absl::Status CreateReadOnlyBuffer(size_t size_in_bytes, const void* data,
82 CLContext* context, Buffer* result);
83
84 absl::Status CreateReadWriteBuffer(size_t size_in_bytes, CLContext* context,
85 Buffer* result);
86
87 absl::Status CreateReadWriteSubBuffer(const Buffer& parent,
88 size_t origin_in_bytes,
89 size_t size_in_bytes, CLContext* context,
90 Buffer* result);
91
92 template <typename T>
WriteData(CLCommandQueue * queue,const absl::Span<T> data)93 absl::Status Buffer::WriteData(CLCommandQueue* queue,
94 const absl::Span<T> data) {
95 if (size_ != sizeof(T) * data.size()) {
96 return absl::InvalidArgumentError(
97 "absl::Span<T> data size is different from buffer allocated size.");
98 }
99 RETURN_IF_ERROR(queue->EnqueueWriteBuffer(buffer_, size_, data.data()));
100 return absl::OkStatus();
101 }
102
103 template <typename T>
ReadData(CLCommandQueue * queue,std::vector<T> * result)104 absl::Status Buffer::ReadData(CLCommandQueue* queue,
105 std::vector<T>* result) const {
106 if (size_ % sizeof(T) != 0) {
107 return absl::UnknownError("Wrong element size(typename T is not correct?");
108 }
109
110 const int elements_count = size_ / sizeof(T);
111 result->resize(elements_count);
112
113 return queue->EnqueueReadBuffer(buffer_, size_, result->data());
114 }
115
116 } // namespace cl
117 } // namespace gpu
118 } // namespace tflite
119
120 #endif // TENSORFLOW_LITE_DELEGATES_GPU_CL_BUFFER_H_
121