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);
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 virtual ~Buffer() { 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
53 // Writes data to a buffer. Data should point to a region that
54 // has exact size in bytes as size_in_bytes(constructor parameter).
55 template <typename T>
56 absl::Status WriteData(CLCommandQueue* queue, const absl::Span<T> data);
57
58 // Reads data from Buffer into CPU memory.
59 template <typename T>
60 absl::Status ReadData(CLCommandQueue* queue, std::vector<T>* result) const;
61
62 absl::Status GetGPUResources(const GPUObjectDescriptor* obj_ptr,
63 GPUResourcesWithValue* resources) const override;
64
65 absl::Status CreateFromBufferDescriptor(const BufferDescriptor& desc,
66 CLContext* context);
67
68 private:
69 void Release();
70
71 cl_mem buffer_ = nullptr;
72 size_t size_;
73 };
74
75 absl::Status CreateReadOnlyBuffer(size_t size_in_bytes, CLContext* context,
76 Buffer* result);
77
78 absl::Status CreateReadOnlyBuffer(size_t size_in_bytes, const void* data,
79 CLContext* context, Buffer* result);
80
81 absl::Status CreateReadWriteBuffer(size_t size_in_bytes, CLContext* context,
82 Buffer* result);
83
84 template <typename T>
WriteData(CLCommandQueue * queue,const absl::Span<T> data)85 absl::Status Buffer::WriteData(CLCommandQueue* queue,
86 const absl::Span<T> data) {
87 if (size_ != sizeof(T) * data.size()) {
88 return absl::InvalidArgumentError(
89 "absl::Span<T> data size is different from buffer allocated size.");
90 }
91 RETURN_IF_ERROR(queue->EnqueueWriteBuffer(buffer_, size_, data.data()));
92 return absl::OkStatus();
93 }
94
95 template <typename T>
ReadData(CLCommandQueue * queue,std::vector<T> * result)96 absl::Status Buffer::ReadData(CLCommandQueue* queue,
97 std::vector<T>* result) const {
98 if (size_ % sizeof(T) != 0) {
99 return absl::UnknownError("Wrong element size(typename T is not correct?");
100 }
101
102 const int elements_count = size_ / sizeof(T);
103 result->resize(elements_count);
104
105 return queue->EnqueueReadBuffer(buffer_, size_, result->data());
106 }
107
108 } // namespace cl
109 } // namespace gpu
110 } // namespace tflite
111
112 #endif // TENSORFLOW_LITE_DELEGATES_GPU_CL_BUFFER_H_
113