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_CL_KERNEL_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_CL_KERNEL_H_ 18 19 #include <string> 20 21 #include "tensorflow/lite/delegates/gpu/cl/cl_context.h" 22 #include "tensorflow/lite/delegates/gpu/cl/cl_device.h" 23 #include "tensorflow/lite/delegates/gpu/cl/cl_program.h" 24 #include "tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h" 25 #include "tensorflow/lite/delegates/gpu/common/kernel_info.h" 26 #include "tensorflow/lite/delegates/gpu/common/status.h" 27 28 namespace tflite { 29 namespace gpu { 30 namespace cl { 31 32 // Arguments binding to CLKernel can be manual or automatic 33 // In manual you specify binding index explicitly 34 // In automatic binding, index auto-incremented with every binding call 35 // Also, if you use automatic mode you must call ResetBindingCounter 36 // before parameters binding 37 class CLKernel { 38 public: CLKernel()39 CLKernel() {} 40 41 // Move only 42 CLKernel(CLKernel&& kernel); 43 CLKernel& operator=(CLKernel&& kernel); 44 CLKernel(const CLKernel&) = delete; 45 CLKernel& operator=(const CLKernel&) = delete; 46 47 ~CLKernel(); 48 kernel()49 cl_kernel kernel() const { return kernel_; } 50 51 absl::Status CreateFromProgram(const CLProgram& program, 52 const std::string& function_name); 53 54 absl::Status SetMemory(int index, cl_mem memory); 55 absl::Status SetMemoryAuto(cl_mem memory); 56 template <typename T> SetBytes(int index,const T & value)57 absl::Status SetBytes(int index, const T& value) const { 58 return SetBytes(index, static_cast<const void*>(&value), sizeof(T)); 59 } 60 template <typename T> SetBytesAuto(const T & value)61 absl::Status SetBytesAuto(const T& value) { 62 return SetBytesAuto(static_cast<const void*>(&value), sizeof(T)); 63 } 64 GetBindingCounter()65 int GetBindingCounter() const { return binding_counter_; } ResetBindingCounter()66 void ResetBindingCounter() { binding_counter_ = 0; } 67 68 // Do not use this function 69 // workaround for Mali memory leak 70 absl::Status ReInit() const; 71 72 KernelInfo info_; 73 74 private: 75 void Release(); 76 absl::Status SetBytes(int index, const void* ptr, int length) const; 77 absl::Status SetBytesAuto(const void* ptr, int length); 78 79 int binding_counter_ = -1; 80 81 std::string function_name_; 82 // reference to program from which kernel was created 83 cl_program program_ = nullptr; 84 cl_kernel kernel_ = nullptr; 85 }; 86 87 } // namespace cl 88 } // namespace gpu 89 } // namespace tflite 90 91 #endif // TENSORFLOW_LITE_DELEGATES_GPU_CL_CL_KERNEL_H_ 92