1 /*
2 * cl_device.cpp - CL device
3 *
4 * Copyright (c) 2015 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21 #include "cl_device.h"
22 #include "cl_context.h"
23 #if HAVE_LIBDRM
24 #include "intel/cl_intel_context.h"
25 #endif
26
27 namespace XCam {
28
29 SmartPtr<CLDevice> CLDevice::_instance;
30 Mutex CLDevice::_instance_mutex;
31
32 SmartPtr<CLDevice>
instance()33 CLDevice::instance ()
34 {
35 SmartLock locker(_instance_mutex);
36 if (_instance.ptr())
37 return _instance;
38
39 _instance = new CLDevice ();
40 // create default context
41 if (_instance->is_inited() &&
42 !_instance->create_default_context ()) {
43 XCAM_LOG_WARNING ("CL device create default context failed");
44 }
45
46 return _instance;
47 }
48
CLDevice()49 CLDevice::CLDevice()
50 : _platform_id (NULL)
51 , _device_id (NULL)
52 , _inited (false)
53 {
54 if (!init()) {
55 XCAM_LOG_WARNING ("CL device init failed");
56 }
57 XCAM_LOG_DEBUG ("CL device constructed");
58 }
59
~CLDevice()60 CLDevice::~CLDevice ()
61 {
62 XCAM_LOG_DEBUG ("CL device destructed");
63 }
64
65 SmartPtr<CLContext>
get_context()66 CLDevice::get_context ()
67 {
68 //created in CLDevice construction
69 return _default_context;
70 }
71
72 void *
get_extension_function(const char * func_name)73 CLDevice::get_extension_function (const char *func_name)
74 {
75 XCAM_ASSERT (func_name);
76 void *ext_func = NULL;
77
78 #if defined (CL_VERSION_1_2) && (CL_VERSION_1_2 == 1)
79 ext_func = (void *) clGetExtensionFunctionAddressForPlatform (_platform_id, func_name);
80 #else
81 ext_func = (void *) clGetExtensionFunctionAddress (func_name);
82 #endif
83 if (!ext_func)
84 XCAM_LOG_ERROR ("ocl driver get extension function (%s) failed", func_name);
85
86 return ext_func;
87 }
88
89 void
terminate()90 CLDevice::terminate ()
91 {
92 if (_default_context.ptr ()) {
93 _default_context->terminate ();
94 _default_context.release ();
95 }
96 }
97
98 bool
init()99 CLDevice::init ()
100 {
101 cl_platform_id platform_id = NULL;
102 cl_device_id device_id = NULL;
103 cl_uint num_platform = 0;
104 cl_uint num_device = 0;
105 CLDevieInfo device_info;
106
107 if (clGetPlatformIDs (1, &platform_id, &num_platform) != CL_SUCCESS)
108 {
109 XCAM_LOG_WARNING ("get cl platform ID failed");
110 return false;
111 }
112 XCAM_ASSERT (num_platform >= 1);
113
114 if (clGetDeviceIDs (platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &num_device) != CL_SUCCESS)
115 {
116 XCAM_LOG_WARNING ("get cl device ID failed");
117 return false;
118 }
119 XCAM_ASSERT (num_device >= 1);
120
121 // only query first device info
122 if (!query_device_info (device_id, device_info)) {
123 //continue
124 XCAM_LOG_WARNING ("cl get device info failed but continue");
125 } else {
126 XCAM_LOG_INFO (
127 "cl get device info,\n"
128 "\tmax_compute_unit:%" PRIu32
129 "\tmax_work_item_dims:%" PRIu32
130 "\tmax_work_item_sizes:{%" PRIuS ", %" PRIuS ", %" PRIuS "}"
131 "\tmax_work_group_size:%" PRIuS
132 "\timage_pitch_alignment:%" PRIu32,
133 device_info.max_compute_unit,
134 device_info.max_work_item_dims,
135 device_info.max_work_item_sizes[0], device_info.max_work_item_sizes[1], device_info.max_work_item_sizes[2],
136 device_info.max_work_group_size,
137 device_info.image_pitch_alignment);
138 }
139
140 // get platform name string length
141 size_t sz = 0;
142 if (clGetPlatformInfo(platform_id, CL_PLATFORM_NAME, 0, 0, &sz) != CL_SUCCESS)
143 {
144 XCAM_LOG_WARNING ("get cl platform name failed");
145 return false;
146 }
147
148 // get platform name string
149 if (sz >= XCAM_CL_MAX_STR_SIZE) {
150 sz = XCAM_CL_MAX_STR_SIZE - 1;
151 }
152 if (clGetPlatformInfo(platform_id, CL_PLATFORM_NAME, sz, _platform_name, 0) != CL_SUCCESS)
153 {
154 XCAM_LOG_WARNING ("get cl platform name failed");
155 return false;
156 }
157
158 _platform_id = platform_id;
159 _device_id = device_id;
160 _device_info = device_info;
161 _platform_name[sz] = 0;
162 _inited = true;
163 return true;
164 }
165
166 bool
query_device_info(cl_device_id device_id,CLDevieInfo & info)167 CLDevice::query_device_info (cl_device_id device_id, CLDevieInfo &info)
168 {
169 #undef XCAM_CL_GET_DEVICE_INFO
170 #define XCAM_CL_GET_DEVICE_INFO(name, val) \
171 do { \
172 if (clGetDeviceInfo (device_id, name, sizeof (val), &(val), NULL) != CL_SUCCESS) { \
173 XCAM_LOG_WARNING ("cl get device info(%s) failed", #name); \
174 } } while (0)
175
176 XCAM_CL_GET_DEVICE_INFO (CL_DEVICE_MAX_COMPUTE_UNITS, info.max_compute_unit);
177 XCAM_CL_GET_DEVICE_INFO (CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, info.max_work_item_dims);
178 XCAM_CL_GET_DEVICE_INFO (CL_DEVICE_MAX_WORK_ITEM_SIZES, info.max_work_item_sizes);
179 XCAM_CL_GET_DEVICE_INFO (CL_DEVICE_MAX_WORK_GROUP_SIZE, info.max_work_group_size);
180 XCAM_CL_GET_DEVICE_INFO (CL_DEVICE_MAX_WORK_GROUP_SIZE, info.max_work_group_size);
181
182 cl_uint alignment = 0;
183 XCAM_CL_GET_DEVICE_INFO (CL_DEVICE_IMAGE_PITCH_ALIGNMENT, alignment);
184 if (alignment)
185 info.image_pitch_alignment = alignment;
186 else
187 info.image_pitch_alignment = 4;
188 return true;
189 }
190
191 bool
create_default_context()192 CLDevice::create_default_context ()
193 {
194 SmartPtr<CLContext> context;
195
196 #if HAVE_LIBDRM
197 context = new CLIntelContext (_instance);
198 #else
199 context = new CLContext (_instance);
200 #endif
201 if (!context->is_valid())
202 return false;
203
204 // init first cmdqueue
205 if (context->is_valid () && !context->init_cmd_queue (context)) {
206 XCAM_LOG_ERROR ("CL context init cmd queue failed");
207 }
208 _default_context = context;
209 return true;
210 }
211
212 };
213