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