1 /******************************************************************************
2 * Copyright (c) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
30 #include <cstdlib>
31 #include <cassert>
32 using std::size_t;
34 #include <iostream>
36 #include "ocl_device.h"
37 #include "ocl_util.h"
38 #include "trace.h"
39 #include "../dsp/ocl_wrapper.dsp_h"
41 using namespace tidl;
43 static const char* error2string(cl_int err);
44 static void errorCheck(cl_int ret, int line);
46 Device::Device(cl_device_type t, const DeviceIds& ids):
47 device_type_m(t), device_ids_m(ids)
48 {
49 TRACE::print("\tOCL Device: %s created\n",
50 device_type_m == CL_DEVICE_TYPE_ACCELERATOR ? "DSP" :
51 device_type_m == CL_DEVICE_TYPE_CUSTOM ? "EVE" : "Unknown");
53 for (int i = 0; i < MAX_DEVICES; i++)
54 queue_m[i] = nullptr;
56 }
58 DspDevice::DspDevice(const DeviceIds& ids, const std::string &binary_filename):
59 Device(CL_DEVICE_TYPE_ACCELERATOR, ids)
60 {
61 cl_uint num_devices_found;
62 cl_device_id device_ids[MAX_DEVICES];
64 cl_int errcode = clGetDeviceIDs(0, // platform
65 device_type_m, // device_type
66 MAX_DEVICES, // num_entries
67 device_ids, // devices
68 &num_devices_found); // num_devices
69 errorCheck(errcode, __LINE__);
71 if (num_devices_found != 1)
72 throw Exception("OpenCL DSP device not found",
73 __FILE__, __FUNCTION__, __LINE__);
75 cl_int num_compute_units;
76 errcode = clGetDeviceInfo(device_ids[0],
77 CL_DEVICE_MAX_COMPUTE_UNITS,
78 sizeof(num_compute_units),
79 &num_compute_units,
80 nullptr);
82 if (num_compute_units == 1)
83 {
84 context_m = clCreateContextFromType(0, // properties
85 device_type_m, // device_type
86 0, // pfn_notify
87 0, // user_data
88 &errcode);
89 errorCheck(errcode, __LINE__);
91 // Queue 0 on device 0
92 queue_m[0] = clCreateCommandQueue(context_m,
93 device_ids[0],
94 0,
95 &errcode);
96 errorCheck(errcode, __LINE__);
97 BuildProgramFromBinary(binary_filename, device_ids, 1);
98 }
99 else
100 {
101 const cl_uint NUM_SUB_DEVICES = 2;
103 // Create 2 sub-device's, each consisting of a C66x DSP
104 cl_device_partition_property properties[3] =
105 { CL_DEVICE_PARTITION_EQUALLY, 1, 0 };
107 // Query the number of sub-devices that can be created
108 cl_uint n_sub_devices = 0;
109 errcode = clCreateSubDevices(device_ids[0], // in_device
110 properties, // properties
111 0, // num_devices
112 NULL, // out_devices
113 &n_sub_devices); // num_devices_ret
114 errorCheck(errcode, __LINE__);
116 assert(n_sub_devices == NUM_SUB_DEVICES);
118 // Create the sub-devices
119 cl_device_id sub_devices[NUM_SUB_DEVICES] = {0, 0};
120 errcode = clCreateSubDevices(device_ids[0], // in_device
121 properties, // properties
122 n_sub_devices, // num_devices
123 sub_devices, // out_devices
124 nullptr); // num_devices_ret
125 errorCheck(errcode, __LINE__);
127 // Create a context containing the sub-devices
128 context_m = clCreateContext(NULL, // properties
129 NUM_SUB_DEVICES, // num_devices
130 sub_devices, // devices
131 NULL, // pfn_notify
132 NULL, // user_data
133 &errcode); // errcode_ret
134 errorCheck(errcode, __LINE__);
136 // Create queues to each sub-device
137 for (auto id : device_ids_m)
138 {
139 int index = static_cast<int>(id);
140 queue_m[index] = clCreateCommandQueue(context_m,
141 sub_devices[index],
142 0,
143 &errcode);
144 errorCheck(errcode, __LINE__);
145 }
147 BuildProgramFromBinary(binary_filename, sub_devices, NUM_SUB_DEVICES);
148 }
150 errcode = clGetDeviceInfo(device_ids[0],
151 CL_DEVICE_MAX_CLOCK_FREQUENCY,
152 sizeof(freq_in_mhz_m),
153 &freq_in_mhz_m,
154 nullptr);
155 errorCheck(errcode, __LINE__);
156 }
159 EveDevice::EveDevice(const DeviceIds& ids, const std::string &kernel_names):
160 Device(CL_DEVICE_TYPE_CUSTOM, ids)
161 {
162 cl_uint num_devices_found;
163 cl_device_id all_device_ids[MAX_DEVICES];
165 // Find all the OpenCL devices available of the given type
166 cl_int errcode = clGetDeviceIDs(0, // platform
167 device_type_m, // device_type
168 MAX_DEVICES, // num_entries
169 all_device_ids, // devices
170 &num_devices_found); // num_devices
171 errorCheck(errcode, __LINE__);
173 assert (num_devices_found >= device_ids_m.size());
175 context_m = clCreateContextFromType(0, // properties
176 device_type_m, // device_type
177 0, // pfn_notify
178 0, // user_data
179 &errcode);
180 errorCheck(errcode, __LINE__);
183 // Create command queues to OpenCL devices specified by the
184 // device_ids_m set.
185 for (auto id : device_ids_m)
186 {
187 int index = static_cast<int>(id);
188 queue_m[index] = clCreateCommandQueue(context_m,
189 all_device_ids[index],
190 0,
191 &errcode);
192 errorCheck(errcode, __LINE__);
193 }
195 BuildProgramFromBinary(kernel_names, all_device_ids, device_ids_m.size());
197 errcode = clGetDeviceInfo(all_device_ids[0],
198 CL_DEVICE_MAX_CLOCK_FREQUENCY,
199 sizeof(freq_in_mhz_m),
200 &freq_in_mhz_m,
201 nullptr);
202 errorCheck(errcode, __LINE__);
203 }
206 bool DspDevice::BuildProgramFromBinary(const std::string &BFN,
207 cl_device_id device_ids[],
208 int num_devices)
209 {
210 size_t bin_len = ocl_wrapper_dsp_bin_len;
212 assert (bin_len != 0);
214 // Casting to make ocl_read_binary work with clCreateProgramWithBinary
215 const unsigned char *bin_arrc = reinterpret_cast <const unsigned char *>
216 (ocl_wrapper_dsp_bin);
218 size_t lengths[num_devices];
219 for (int i=0; i < num_devices; i++) lengths[i] = bin_len;
221 const unsigned char* binaries[num_devices];
222 for (int i=0; i < num_devices; i++) binaries[i] = bin_arrc;
224 cl_int err;
225 program_m = clCreateProgramWithBinary(context_m,
226 num_devices,
227 device_ids, // device_list
228 lengths,
229 binaries,
230 0, // binary_status
231 &err);
232 errorCheck(err, __LINE__);
234 const char *options = "";
235 err = clBuildProgram(program_m, num_devices, device_ids, options, 0, 0);
236 errorCheck(err, __LINE__);
238 return true;
239 }
241 bool EveDevice::BuildProgramFromBinary(const std::string& kernel_names,
242 cl_device_id device_ids[],
243 int num_devices)
244 {
245 cl_int err;
246 cl_device_id executor_device_ids[MAX_DEVICES];
248 int i = 0;
249 for (auto id : device_ids_m)
250 executor_device_ids[i++] = device_ids[static_cast<int>(id)];
252 program_m = clCreateProgramWithBuiltInKernels(context_m,
253 num_devices,
254 executor_device_ids, // device_list
255 kernel_names.c_str(),
256 &err);
257 errorCheck(err, __LINE__);
259 return true;
260 }
262 Kernel::Kernel(Device* device, const std::string& name,
263 const KernelArgs& args, uint8_t device_index):
264 name_m(name), device_m(device), device_index_m(device_index),
265 is_running_m(false)
266 {
267 TRACE::print("Creating kernel %s\n", name.c_str());
268 cl_int err;
269 kernel_m = clCreateKernel(device_m->program_m, name_m.c_str(), &err);
270 errorCheck(err, __LINE__);
272 int arg_index = 0;
273 for (const auto& arg : args)
274 {
275 if (!arg.isLocal())
276 {
277 if (arg.kind() == DeviceArgInfo::Kind::BUFFER)
278 {
279 cl_mem buffer = device_m->CreateBuffer(arg);
281 clSetKernelArg(kernel_m, arg_index, sizeof(cl_mem), &buffer);
282 TRACE::print(" Arg[%d]: %p\n", arg_index, buffer);
284 buffers_m.push_back(buffer);
285 }
286 else if (arg.kind() == DeviceArgInfo::Kind::SCALAR)
287 {
288 clSetKernelArg(kernel_m, arg_index, arg.size(), arg.ptr());
289 TRACE::print(" Arg[%d]: %p\n", arg_index, arg.ptr());
290 }
291 else
292 {
293 assert ("DeviceArgInfo kind not supported");
294 }
295 }
296 else
297 {
298 clSetKernelArg(kernel_m, arg_index, arg.size(), NULL);
299 TRACE::print(" Arg[%d]: local, %d\n", arg_index, arg.size());
300 }
301 arg_index++;
303 }
304 }
306 Kernel& Kernel::RunAsync()
307 {
308 // Execute kernel
309 TRACE::print("\tKernel: device %d executing %s\n", device_index_m,
310 name_m.c_str());
311 cl_int ret = clEnqueueTask(device_m->queue_m[device_index_m],
312 kernel_m, 0, 0, &event_m);
313 errorCheck(ret, __LINE__);
314 is_running_m = true;
316 return *this;
317 }
320 bool Kernel::Wait()
321 {
322 // Wait called without a corresponding RunAsync
323 if (!is_running_m)
324 return false;
326 TRACE::print("\tKernel: waiting...\n");
327 cl_int ret = clWaitForEvents(1, &event_m);
328 errorCheck(ret, __LINE__);
329 ret = clReleaseEvent(event_m);
330 errorCheck(ret, __LINE__);
331 TRACE::print("\tKernel: finished execution\n");
333 is_running_m = false;
334 return true;
335 }
337 Kernel::~Kernel()
338 {
339 for (auto b : buffers_m)
340 device_m->ReleaseBuffer(b);
342 clReleaseKernel(kernel_m);
343 }
345 cl_mem Device::CreateBuffer(const DeviceArgInfo &Arg)
346 {
347 size_t size = Arg.size();
348 void *host_ptr = Arg.ptr();
350 if (host_ptr == nullptr)
351 {
352 TRACE::print("\tOCL Create B:%p\n", nullptr);
353 return nullptr;
354 }
356 bool hostPtrInCMEM = __is_in_malloced_region(host_ptr);
358 // Conservative till we have sufficient information.
359 cl_mem_flags flag = CL_MEM_READ_WRITE;
361 if (hostPtrInCMEM) flag |= (cl_mem_flags)CL_MEM_USE_HOST_PTR;
362 else flag |= (cl_mem_flags)CL_MEM_COPY_HOST_PTR;
364 cl_int errcode;
365 cl_mem buffer = clCreateBuffer(context_m,
366 flag,
367 size,
368 host_ptr,
369 &errcode);
370 errorCheck(errcode, __LINE__);
372 TRACE::print("\tOCL Create B:%p\n", buffer);
374 return buffer;
375 }
377 void Device::ReleaseBuffer(cl_mem M)
378 {
379 TRACE::print("\tOCL Release B:%p\n", M);
380 clReleaseMemObject(M);
381 }
383 /// Release resources associated with an OpenCL device
384 Device::~Device()
385 {
386 TRACE::print("\tOCL Device: deleted\n");
387 for (unsigned int i = 0; i < device_ids_m.size(); i++)
388 {
389 clFinish(queue_m[i]);
390 clReleaseCommandQueue (queue_m[i]);
391 }
393 clReleaseProgram (program_m);
394 clReleaseContext (context_m);
395 }
397 void errorCheck(cl_int ret, int line)
398 {
399 if (ret != CL_SUCCESS)
400 {
401 std::cerr << "ERROR: [ Line: " << line << "] " << error2string(ret) << std::endl;
402 exit(ret);
403 }
404 }
406 /// Convert OpenCL error codes to a string
407 const char* error2string(cl_int err)
408 {
409 switch(err)
410 {
411 case 0: return "CL_SUCCESS";
412 case -1: return "CL_DEVICE_NOT_FOUND";
413 case -2: return "CL_DEVICE_NOT_AVAILABLE";
414 case -3: return "CL_COMPILER_NOT_AVAILABLE";
415 case -4: return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
416 case -5: return "CL_OUT_OF_RESOURCES";
417 case -6: return "CL_OUT_OF_HOST_MEMORY";
418 case -7: return "CL_PROFILING_INFO_NOT_AVAILABLE";
419 case -8: return "CL_MEM_COPY_OVERLAP";
420 case -9: return "CL_IMAGE_FORMAT_MISMATCH";
421 case -10: return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
422 case -11: return "CL_BUILD_PROGRAM_FAILURE";
423 case -12: return "CL_MAP_FAILURE";
425 case -30: return "CL_INVALID_VALUE";
426 case -31: return "CL_INVALID_DEVICE_TYPE";
427 case -32: return "CL_INVALID_PLATFORM";
428 case -33: return "CL_INVALID_DEVICE";
429 case -34: return "CL_INVALID_CONTEXT";
430 case -35: return "CL_INVALID_QUEUE_PROPERTIES";
431 case -36: return "CL_INVALID_COMMAND_QUEUE";
432 case -37: return "CL_INVALID_HOST_PTR";
433 case -38: return "CL_INVALID_MEM_OBJECT";
434 case -39: return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
435 case -40: return "CL_INVALID_IMAGE_SIZE";
436 case -41: return "CL_INVALID_SAMPLER";
437 case -42: return "CL_INVALID_BINARY";
438 case -43: return "CL_INVALID_BUILD_OPTIONS";
439 case -44: return "CL_INVALID_PROGRAM";
440 case -45: return "CL_INVALID_PROGRAM_EXECUTABLE";
441 case -46: return "CL_INVALID_KERNEL_NAME";
442 case -47: return "CL_INVALID_KERNEL_DEFINITION";
443 case -48: return "CL_INVALID_KERNEL";
444 case -49: return "CL_INVALID_ARG_INDEX";
445 case -50: return "CL_INVALID_ARG_VALUE";
446 case -51: return "CL_INVALID_ARG_SIZE";
447 case -52: return "CL_INVALID_KERNEL_ARGS";
448 case -53: return "CL_INVALID_WORK_DIMENSION";
449 case -54: return "CL_INVALID_WORK_GROUP_SIZE";
450 case -55: return "CL_INVALID_WORK_ITEM_SIZE";
451 case -56: return "CL_INVALID_GLOBAL_OFFSET";
452 case -57: return "CL_INVALID_EVENT_WAIT_LIST";
453 case -58: return "CL_INVALID_EVENT";
454 case -59: return "CL_INVALID_OPERATION";
455 case -60: return "CL_INVALID_GL_OBJECT";
456 case -61: return "CL_INVALID_BUFFER_SIZE";
457 case -62: return "CL_INVALID_MIP_LEVEL";
458 case -63: return "CL_INVALID_GLOBAL_WORK_SIZE";
459 default: return "Unknown OpenCL error";
460 }
461 }
463 Device::Ptr Device::Create(DeviceType core_type, const DeviceIds& ids,
464 const std::string& name)
465 {
466 Device::Ptr p(nullptr);
467 if (core_type == DeviceType::DSP)
468 p.reset(new DspDevice(ids, name));
469 else if (core_type == DeviceType::EVE)
470 p.reset(new EveDevice(ids, name));
472 return p;
473 }
475 static bool PlatformIsAM57()
476 {
477 cl_platform_id id;
478 cl_int err;
480 err = clGetPlatformIDs(1, &id, nullptr);
481 if (err != CL_SUCCESS) return false;
483 // Check if the device name is AM57
484 size_t length;
485 err = clGetPlatformInfo(id, CL_PLATFORM_NAME, 0, nullptr, &length);
486 if (err != CL_SUCCESS) return false;
488 std::unique_ptr<char> name(new char[length]);
490 err = clGetPlatformInfo(id, CL_PLATFORM_NAME, length, name.get(), nullptr);
491 if (err != CL_SUCCESS) return false;
493 std::string platform_name(name.get());
495 if (platform_name.find("AM57") == std::string::npos)
496 return false;
498 return true;
499 }
501 // TI DL is supported on AM57x - EVE or C66x devices
502 uint32_t Device::GetNumDevices(DeviceType device_type)
503 {
504 if (!PlatformIsAM57()) return 0;
506 // Convert DeviceType to OpenCL device type
507 cl_device_type t = (device_type == DeviceType::EVE) ?
508 CL_DEVICE_TYPE_CUSTOM :
509 CL_DEVICE_TYPE_ACCELERATOR;
511 // Find all the OpenCL devices available
512 cl_uint num_devices_found;
513 cl_device_id all_device_ids[MAX_DEVICES];
515 cl_int errcode = clGetDeviceIDs(0, // platform
516 t, // device_type
517 MAX_DEVICES, // num_entries
518 all_device_ids, // devices
519 &num_devices_found); // num_devices
522 if (errcode != CL_SUCCESS) return 0;
523 if (num_devices_found == 0) return 0;
525 // DSP, return the number of compute units since we maintain a
526 // queue to each compute unit (i.e. C66x DSP)
527 if (t == CL_DEVICE_TYPE_ACCELERATOR)
528 {
529 cl_int num_compute_units;
530 errcode = clGetDeviceInfo(all_device_ids[0],
531 CL_DEVICE_MAX_COMPUTE_UNITS,
532 sizeof(num_compute_units),
533 &num_compute_units,
534 nullptr);
535 if (errcode != CL_SUCCESS)
536 return 0;
538 return num_compute_units;
539 }
541 // EVE, return the number of devices since each EVE is a device
542 return num_devices_found;
543 }