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 #include "tensorflow/lite/delegates/gpu/gl/command_queue.h"
17
18 #include <memory>
19
20 #include "absl/memory/memory.h"
21 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h"
22 #include "tensorflow/lite/delegates/gpu/common/status.h"
23 #include "tensorflow/lite/delegates/gpu/common/types.h"
24 #include "tensorflow/lite/delegates/gpu/gl/gl_call.h"
25 #include "tensorflow/lite/delegates/gpu/gl/gl_sync.h"
26 #include "tensorflow/lite/delegates/gpu/gl/portable_gl31.h"
27
28 namespace tflite {
29 namespace gpu {
30 namespace gl {
31 namespace {
32
33 class DefaultCommandQueue : public CommandQueue {
34 public:
Dispatch(const GlProgram & program,const uint3 & workgroups)35 absl::Status Dispatch(const GlProgram& program,
36 const uint3& workgroups) override {
37 RETURN_IF_ERROR(program.Dispatch(workgroups));
38 return TFLITE_GPU_CALL_GL(glMemoryBarrier, GL_ALL_BARRIER_BITS);
39 }
40
WaitForCompletion()41 absl::Status WaitForCompletion() override {
42 // TODO(akulik): Maybe let the user choose which wait method to use.
43 return GlActiveSyncWait();
44 }
45
Flush()46 absl::Status Flush() override { return absl::OkStatus(); }
47 };
48
49 // On Adreno do flush periodically as this affects performance. Command queue
50 // needs to be manually managed to ensure that accumulated work goes to GPU as
51 // fast as it can.
52 //
53 // Also, on older Adreno devices glFlush is required after every memory barrier
54 // to avoid hitting GPU driver bug.
55 class AdrenoCommandQueue : public DefaultCommandQueue {
56 public:
AdrenoCommandQueue(int flush_every_n)57 explicit AdrenoCommandQueue(int flush_every_n)
58 : flush_every_n_(flush_every_n) {}
59
Dispatch(const GlProgram & program,const uint3 & workgroups)60 absl::Status Dispatch(const GlProgram& program,
61 const uint3& workgroups) final {
62 RETURN_IF_ERROR(DefaultCommandQueue::Dispatch(program, workgroups));
63 if ((++program_counter_ % flush_every_n_) == 0) {
64 glFlush();
65 }
66 return absl::OkStatus();
67 }
68
WaitForCompletion()69 absl::Status WaitForCompletion() override {
70 program_counter_ = 0;
71 return DefaultCommandQueue::WaitForCompletion();
72 }
73
Flush()74 absl::Status Flush() final {
75 // Flush exactly once after the last dispatch.
76 if (program_counter_ != 0) {
77 program_counter_ = 0;
78 glFlush();
79 }
80 return absl::OkStatus();
81 }
82
83 private:
84 const int flush_every_n_;
85 int program_counter_ = 0;
86 };
87
88 } // namespace
89
NewCommandQueue(const GpuInfo & gpu_info)90 std::unique_ptr<CommandQueue> NewCommandQueue(const GpuInfo& gpu_info) {
91 if (gpu_info.IsAdreno()) {
92 int flush_every_n = 1;
93 // On Adreno 630 and Adreno 505 there is up to 2x performance boost when
94 // glFlush happens not so often.
95 if (gpu_info.adreno_info.adreno_gpu == AdrenoGpu::kAdreno630 ||
96 gpu_info.adreno_info.adreno_gpu == AdrenoGpu::kAdreno505) {
97 flush_every_n = 10;
98 }
99 return std::make_unique<AdrenoCommandQueue>(flush_every_n);
100 }
101 return std::make_unique<DefaultCommandQueue>();
102 }
103
104 } // namespace gl
105 } // namespace gpu
106 } // namespace tflite
107