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_DEVICE_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_CL_DEVICE_H_
18
19 #include <string>
20 #include <vector>
21
22 #include "tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h"
23 #include "tensorflow/lite/delegates/gpu/cl/util.h"
24 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h"
25 #include "tensorflow/lite/delegates/gpu/common/status.h"
26 #include "tensorflow/lite/delegates/gpu/common/types.h"
27
28 namespace tflite {
29 namespace gpu {
30 namespace cl {
31
32 // A wrapper around opencl device id
33 class CLDevice {
34 public:
35 CLDevice() = default;
36 CLDevice(cl_device_id id, cl_platform_id platform_id);
37
38 CLDevice(CLDevice&& device);
39 CLDevice& operator=(CLDevice&& device);
40 CLDevice(const CLDevice&);
41 CLDevice& operator=(const CLDevice&);
42
~CLDevice()43 ~CLDevice() {}
44
id()45 cl_device_id id() const { return id_; }
platform()46 cl_platform_id platform() const { return platform_id_; }
47 std::string GetPlatformVersion() const;
48
49 // To track bug on some Adreno. b/131099086
50 void DisableOneLayerTextureArray();
51
GetInfo()52 const GpuInfo& GetInfo() const { return info_; }
53 // We update device info during context creation, so as supported texture
54 // formats can be requested from context only.
55 mutable GpuInfo info_;
56
57 private:
58 cl_device_id id_ = nullptr;
59 cl_platform_id platform_id_ = nullptr;
60 };
61
62 absl::Status CreateDefaultGPUDevice(CLDevice* result);
63
64 template <typename T>
GetDeviceInfo(cl_device_id id,cl_device_info info)65 T GetDeviceInfo(cl_device_id id, cl_device_info info) {
66 T result;
67 cl_int error = clGetDeviceInfo(id, info, sizeof(T), &result, nullptr);
68 if (error != CL_SUCCESS) {
69 return -1;
70 }
71 return result;
72 }
73
74 template <typename T>
GetDeviceInfo(cl_device_id id,cl_device_info info,T * result)75 absl::Status GetDeviceInfo(cl_device_id id, cl_device_info info, T* result) {
76 cl_int error = clGetDeviceInfo(id, info, sizeof(T), result, nullptr);
77 if (error != CL_SUCCESS) {
78 return absl::InvalidArgumentError(CLErrorCodeToString(error));
79 }
80 return absl::OkStatus();
81 }
82
83 } // namespace cl
84 } // namespace gpu
85 } // namespace tflite
86
87 #endif // TENSORFLOW_LITE_DELEGATES_GPU_CL_CL_DEVICE_H_
88