diff options
-rw-r--r-- | src/api/api_command.cpp | 5 | ||||
-rw-r--r-- | src/api/api_device.cpp | 22 | ||||
-rw-r--r-- | src/api/api_kernel.cpp | 3 | ||||
-rw-r--r-- | src/api/api_program.cpp | 35 | ||||
-rw-r--r-- | src/core/commandqueue.cpp | 6 | ||||
-rw-r--r-- | src/core/context.cpp | 15 | ||||
-rw-r--r-- | src/core/context.h | 1 | ||||
-rw-r--r-- | src/core/cpu/device.cpp | 7 | ||||
-rw-r--r-- | src/core/deviceinterface.h | 14 | ||||
-rw-r--r-- | src/core/events.cpp | 32 | ||||
-rw-r--r-- | src/core/memobject.cpp | 9 | ||||
-rw-r--r-- | src/core/platform.cpp | 28 | ||||
-rw-r--r-- | src/core/program.cpp | 6 | ||||
-rw-r--r-- | src/core/sampler.cpp | 21 |
14 files changed, 125 insertions, 79 deletions
diff --git a/src/api/api_command.cpp b/src/api/api_command.cpp index e9972c6..5486ea4 100644 --- a/src/api/api_command.cpp +++ b/src/api/api_command.cpp @@ -39,11 +39,12 @@ // Command Queue APIs cl_command_queue clCreateCommandQueue(cl_context context, - cl_device_id device, + cl_device_id d_device, cl_command_queue_properties properties, cl_int * errcode_ret) { cl_int default_errcode_ret; + auto device = pobj(d_device); // No errcode_ret ? if (!errcode_ret) @@ -64,7 +65,7 @@ clCreateCommandQueue(cl_context context, *errcode_ret = CL_SUCCESS; Coal::CommandQueue *queue = new Coal::CommandQueue( (Coal::Context *)context, - (Coal::DeviceInterface *)device, + device, properties, errcode_ret); diff --git a/src/api/api_device.cpp b/src/api/api_device.cpp index d8c3790..7bf66a4 100644 --- a/src/api/api_device.cpp +++ b/src/api/api_device.cpp @@ -63,37 +63,41 @@ clGetDeviceIDs(cl_platform_id platform, } cl_int -clGetDeviceInfo(cl_device_id device, +clGetDeviceInfo(cl_device_id d_device, cl_device_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret) { + auto device = pobj(d_device); + if (!device->isA(Coal::Object::T_Device)) return CL_INVALID_DEVICE; - Coal::DeviceInterface *iface = (Coal::DeviceInterface *)device; - return iface->info(param_name, param_value_size, param_value, + return device->info(param_name, param_value_size, param_value, param_value_size_ret); } cl_int -clCreateSubDevices(cl_device_id in_device, +clCreateSubDevices(cl_device_id d_in_device, const cl_device_partition_property * properties, cl_uint num_devices, cl_device_id * out_devices, cl_uint * num_devices_ret) { + auto in_device = pobj(d_in_device); + if (!in_device->isA(Coal::Object::T_Device)) return CL_INVALID_DEVICE; - Coal::DeviceInterface *iface = (Coal::DeviceInterface *)in_device; - return iface->createSubDevices(properties, num_devices, out_devices, num_devices_ret); + return in_device->createSubDevices(properties, num_devices, out_devices, num_devices_ret); } cl_int -clRetainDevice(cl_device_id device) +clRetainDevice(cl_device_id d_device) { + auto device = pobj(d_device); + if (!device->isA(Coal::Object::T_Device)) return CL_INVALID_DEVICE; @@ -103,8 +107,10 @@ clRetainDevice(cl_device_id device) } cl_int -clReleaseDevice(cl_device_id device) +clReleaseDevice(cl_device_id d_device) { + auto device = pobj(d_device); + if (!device->isA(Coal::Object::T_Device)) return CL_INVALID_DEVICE; diff --git a/src/api/api_kernel.cpp b/src/api/api_kernel.cpp index 1ba7068..89f8b01 100644 --- a/src/api/api_kernel.cpp +++ b/src/api/api_kernel.cpp @@ -34,6 +34,7 @@ #include <core/program.h> #include <core/kernel.h> +#include <core/deviceinterface.h> // Kernel Object APIs cl_kernel @@ -229,7 +230,7 @@ clGetKernelWorkGroupInfo(cl_kernel kernel, if (!kernel->isA(Coal::Object::T_Kernel)) return CL_INVALID_KERNEL; - return kernel->workGroupInfo((Coal::DeviceInterface *)device, param_name, + return kernel->workGroupInfo(pobj(device), param_name, param_value_size, param_value, param_value_size_ret); } diff --git a/src/api/api_program.cpp b/src/api/api_program.cpp index 4afd603..8adcde7 100644 --- a/src/api/api_program.cpp +++ b/src/api/api_program.cpp @@ -34,6 +34,7 @@ #include <core/program.h> #include <core/context.h> #include <core/platform.h> +#include <core/deviceinterface.h> #include <cstdlib> @@ -159,18 +160,22 @@ clCreateProgramWithBinary(cl_context context, *errcode_ret = CL_SUCCESS; // Init program + Coal::DeviceInterface **devices = + (Coal::DeviceInterface **)std::malloc(context_num_devices * sizeof(Coal::DeviceInterface *)); + pobj_list(devices, device_list, num_devices); *errcode_ret = program->loadBinaries(binaries, - lengths, binary_status, num_devices, - (Coal::DeviceInterface * const*)device_list); + lengths, binary_status, num_devices, devices); if (*errcode_ret != CL_SUCCESS) { delete program; std::free(context_devices); + std::free(devices); return 0; } std::free(context_devices); + std::free(devices); return (cl_program)program; } @@ -299,8 +304,11 @@ clBuildProgram(cl_program program, if (result == CL_SUCCESS) { // Build program - result = program->build(options, pfn_notify, user_data, num_devices, - (Coal::DeviceInterface * const*)device_list); + Coal::DeviceInterface **devices = + (Coal::DeviceInterface **)std::malloc(num_devices * sizeof(Coal::DeviceInterface *)); + pobj_list(devices, device_list, num_devices); + result = program->build(options, pfn_notify, user_data, num_devices, devices); + free(devices); } if (pfn_notify) @@ -332,9 +340,12 @@ clCompileProgram(cl_program program, result = CL_INVALID_VALUE; } else { - result = program->compile(options, pfn_notify, user_data, num_devices, - (Coal::DeviceInterface * const*)device_list, + Coal::DeviceInterface **devices = + (Coal::DeviceInterface **)std::malloc(num_devices * sizeof(Coal::DeviceInterface *)); + pobj_list(devices, device_list, num_devices); + result = program->compile(options, pfn_notify, user_data, num_devices, devices, num_input_headers, input_headers, header_include_names); + free(devices); } if (pfn_notify) @@ -391,9 +402,13 @@ clLinkProgram(cl_context context, Coal::Program *program = NULL; if (retcode == CL_SUCCESS) { program = new Coal::Program(context); - retcode = program->link(options, pfn_notify, user_data, num_devices, - (Coal::DeviceInterface * const*)device_list, + + Coal::DeviceInterface **devices = + (Coal::DeviceInterface **)std::malloc(num_devices * sizeof(Coal::DeviceInterface *)); + pobj_list(devices, device_list, num_devices); + retcode = program->link(options, pfn_notify, user_data, num_devices, devices, num_input_programs, input_programs); + free(devices); // Note: Unlike clCompileProgram() and clLinkProgram(), which per the 1.2 spec, // must trigger the callback whether the build succeeds or not, here we must have a @@ -453,10 +468,10 @@ clGetProgramBuildInfo(cl_program program, if (!program->isA(Coal::Object::T_Program)) return CL_INVALID_PROGRAM; - if (!device) + if (!device) return CL_INVALID_DEVICE; - return program->buildInfo((Coal::DeviceInterface *)device, param_name, + return program->buildInfo(pobj(device), param_name, param_value_size, param_value, param_value_size_ret); } diff --git a/src/core/commandqueue.cpp b/src/core/commandqueue.cpp index ae5c526..ab1d677 100644 --- a/src/core/commandqueue.cpp +++ b/src/core/commandqueue.cpp @@ -112,9 +112,11 @@ cl_int CommandQueue::info(cl_command_queue_info param_name, break; case CL_QUEUE_DEVICE: - SIMPLE_ASSIGN(cl_device_id, p_device); + { + auto d_device = desc(p_device); + SIMPLE_ASSIGN(cl_device_id, d_device); + } break; - case CL_QUEUE_REFERENCE_COUNT: SIMPLE_ASSIGN(cl_uint, references()); break; diff --git a/src/core/context.cpp b/src/core/context.cpp index e9129ff..cd91ad0 100644 --- a/src/core/context.cpp +++ b/src/core/context.cpp @@ -56,7 +56,7 @@ Context::Context(const cl_context_properties *properties, void *user_data, cl_int *errcode_ret) : Object(Object::T_Context, 0), p_properties(0), p_pfn_notify(pfn_notify), - p_user_data(user_data), p_devices(0), p_num_devices(0), p_props_len(0), + p_user_data(user_data), p_devices(0), p_d_devices(0), p_num_devices(0), p_props_len(0), p_platform(&the_platform) { if (!p_pfn_notify) @@ -131,9 +131,10 @@ Context::Context(const cl_context_properties *properties, // Explore the devices p_devices = (DeviceInterface **)std::malloc(num_devices * sizeof(DeviceInterface *)); + p_d_devices = (cl_device_id *)std::malloc(num_devices * sizeof(cl_device_id)); p_num_devices = num_devices; - if (!p_devices) + if (!p_devices || !p_d_devices) { *errcode_ret = CL_OUT_OF_HOST_MEMORY; return; @@ -141,7 +142,7 @@ Context::Context(const cl_context_properties *properties, for (cl_uint i=0; i<num_devices; ++i) { - cl_device_id device = devices[i]; + auto device = pobj(devices[i]); if (device == 0) { @@ -167,7 +168,8 @@ Context::Context(const cl_context_properties *properties, } // Add the device to the list - p_devices[i] = (DeviceInterface *)device; + p_devices[i] = device; + p_d_devices[i] = devices[i]; } } @@ -178,6 +180,9 @@ Context::~Context() if (p_devices) std::free((void *)p_devices); + + if (p_d_devices) + std::free((void *)p_d_devices); } cl_int Context::info(cl_context_info param_name, @@ -203,7 +208,7 @@ cl_int Context::info(cl_context_info param_name, break; case CL_CONTEXT_DEVICES: - MEM_ASSIGN(p_num_devices * sizeof(DeviceInterface *), p_devices); + MEM_ASSIGN(p_num_devices * sizeof(cl_device_id), p_d_devices); break; case CL_CONTEXT_PROPERTIES: diff --git a/src/core/context.h b/src/core/context.h index 4712d25..f4427a4 100644 --- a/src/core/context.h +++ b/src/core/context.h @@ -92,6 +92,7 @@ class Context : public Object void *p_user_data; DeviceInterface **p_devices; + cl_device_id *p_d_devices; unsigned int p_num_devices, p_props_len; cl_platform_id p_platform; }; diff --git a/src/core/cpu/device.cpp b/src/core/cpu/device.cpp index c170030..38a7e8d 100644 --- a/src/core/cpu/device.cpp +++ b/src/core/cpu/device.cpp @@ -377,7 +377,7 @@ cl_int CPUDevice::createSubDevices( if (out_devices) { for (int i = 0; i < num_new_devices; i++) { new_device = new CPUDevice(this, partition_size); - out_devices[i] = (cl_device_id)new_device; + out_devices[i] = desc(new_device); new_device->setProperties(properties); } } @@ -744,7 +744,10 @@ cl_int CPUDevice::info(cl_device_info param_name, break; case CL_DEVICE_PARENT_DEVICE: - SIMPLE_ASSIGN(cl_device_id, p_parent_device); + { + auto d_device = desc(p_parent_device); + SIMPLE_ASSIGN(cl_device_id, d_device); + } break; case CL_DEVICE_PARTITION_MAX_SUB_DEVICES: if (numCPUs() == 1) { diff --git a/src/core/deviceinterface.h b/src/core/deviceinterface.h index 2db5577..e15994e 100644 --- a/src/core/deviceinterface.h +++ b/src/core/deviceinterface.h @@ -37,12 +37,20 @@ #include <CL/cl.h> #include <string> #include "object.h" +#include "icd.h" /* This pulls in legacy::PassManager when LLVM >= 3.4 */ #include <llvm/PassManager.h> namespace Coal { +class DeviceInterface; +} + +struct _cl_device_id: public Coal::descriptor<Coal::DeviceInterface, _cl_device_id> {}; + +namespace Coal +{ class DeviceBuffer; class DeviceProgram; @@ -53,13 +61,14 @@ class Event; class Program; class Kernel; + /** * \brief Abstraction layer between core Clover objects and the devices * * This interface is used by the core Clover classes to communicate with the * devices, that must reimplement all the functions described here. */ -class DeviceInterface : public Object +class DeviceInterface : public _cl_device_id, public Object { public: DeviceInterface() : Object(Object::T_Device, 0) {} @@ -365,7 +374,4 @@ class DeviceKernel } -struct _cl_device_id : public Coal::DeviceInterface -{}; - #endif diff --git a/src/core/events.cpp b/src/core/events.cpp index 383cd4d..add60d6 100644 --- a/src/core/events.cpp +++ b/src/core/events.cpp @@ -79,13 +79,13 @@ BufferEvent::BufferEvent(CommandQueue *parent, } // Alignment of SubBuffers - DeviceInterface *device = 0; - *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(DeviceInterface *), - &device, 0); + cl_device_id d_device = 0; + *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(cl_device_id), &d_device, 0); if (*errcode_ret != CL_SUCCESS) return; + auto device = pobj(d_device); if (!isSubBufferAligned(buffer, device)) { *errcode_ret = CL_MISALIGNED_SUB_BUFFER_OFFSET; @@ -460,13 +460,12 @@ CopyBufferEvent::CopyBufferEvent(CommandQueue *parent, } // Check alignement of destination - DeviceInterface *device = 0; - *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(DeviceInterface *), - &device, 0); - + cl_device_id d_device = 0; + *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(cl_device_id), &d_device, 0); if (*errcode_ret != CL_SUCCESS) return; + auto device = pobj(d_device); if (!isSubBufferAligned(destination, device)) { *errcode_ret = CL_MISALIGNED_SUB_BUFFER_OFFSET; @@ -629,15 +628,14 @@ NativeKernelEvent::NativeKernelEvent(CommandQueue *parent, } // Check that the device can execute a native kernel - DeviceInterface *device; cl_device_exec_capabilities caps; - - *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(DeviceInterface *), - &device, 0); + cl_device_id d_device = 0; + *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(cl_device_id), &d_device, 0); if (*errcode_ret != CL_SUCCESS) return; + auto device = pobj(d_device); *errcode_ret = device->info(CL_DEVICE_EXECUTION_CAPABILITIES, sizeof(cl_device_exec_capabilities), &caps, 0); @@ -746,17 +744,17 @@ KernelEvent::KernelEvent(CommandQueue *parent, } // Check that the kernel was built for parent's device. - DeviceInterface *device; + cl_device_id d_device = 0; Context *k_ctx, *q_ctx; size_t max_work_group_size; cl_uint max_dims = 0; - *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(DeviceInterface *), - &device, 0); + *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(cl_device_id), &d_device, 0); if (*errcode_ret != CL_SUCCESS) return; + auto device = pobj(d_device); *errcode_ret = parent->info(CL_QUEUE_CONTEXT, sizeof(Context *), &q_ctx, 0); *errcode_ret |= kernel->info(CL_KERNEL_CONTEXT, sizeof(Context *), &k_ctx, 0); *errcode_ret |= device->info(CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), @@ -1241,13 +1239,13 @@ CopyBufferRectEvent::CopyBufferRectEvent(CommandQueue *parent, } // Check alignment of destination (source already checked by BufferEvent) - DeviceInterface *device = 0; - *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(DeviceInterface *), - &device, 0); + cl_device_id d_device = 0; + *errcode_ret = parent->info(CL_QUEUE_DEVICE, sizeof(cl_device_id), &d_device, 0); if (*errcode_ret != CL_SUCCESS) return; + auto device = pobj(d_device); if (!isSubBufferAligned(destination, device)) { *errcode_ret = CL_MISALIGNED_SUB_BUFFER_OFFSET; diff --git a/src/core/memobject.cpp b/src/core/memobject.cpp index 3e2eeb1..1d88fd0 100644 --- a/src/core/memobject.cpp +++ b/src/core/memobject.cpp @@ -131,7 +131,7 @@ MemObject::~MemObject() cl_int MemObject::init() { // Get the device list of the context - DeviceInterface **devices = 0; + cl_device_id *devices = 0; cl_int rs; rs = ((Context *)parent())->info(CL_CONTEXT_NUM_DEVICES, @@ -142,14 +142,13 @@ cl_int MemObject::init() return rs; p_devices_to_allocate = p_num_devices; - devices = (DeviceInterface **)std::malloc(p_num_devices * - sizeof(DeviceInterface *)); + devices = (cl_device_id *)std::malloc(p_num_devices * sizeof(cl_device_id)); if (!devices) return CL_OUT_OF_HOST_MEMORY; rs = ((Context *)parent())->info(CL_CONTEXT_DEVICES, - p_num_devices * sizeof(DeviceInterface *), + p_num_devices * sizeof(cl_device_id), devices, 0); if (rs != CL_SUCCESS) @@ -194,7 +193,7 @@ cl_int MemObject::init() for (unsigned int i=0; i<p_num_devices; ++i) { - DeviceInterface *device = devices[i]; + auto device = pobj(devices[i]); rs = CL_SUCCESS; p_devicebuffers[i] = device->createDeviceBuffer(this, &rs); diff --git a/src/core/platform.cpp b/src/core/platform.cpp index e6f8c03..30ace53 100644 --- a/src/core/platform.cpp +++ b/src/core/platform.cpp @@ -51,6 +51,11 @@ using namespace Coal; +// Ensure that Class Platform remains mutable to the ICD "POD" C structure, as expected +// by the ICD loader +static_assert(std::is_standard_layout<Platform>::value, + "Class Platform must be of C++ standard layout type."); + /****************************************************************************** * begin_file_lock_crit_section ******************************************************************************/ @@ -99,17 +104,19 @@ static int begin_file_lock_crit_section(char* fname) namespace Coal { - Platform::Platform() : dispatch(&dispatch_table) + Platform::Platform(): dispatch(&dispatch_table) { char filename[] = "/var/lock/opencl"; p_lock_fd = begin_file_lock_crit_section(filename); - p_devices.push_back((_cl_device_id*)new Coal::CPUDevice(NULL,0)); + Coal::DeviceInterface * device = new Coal::CPUDevice(NULL,0); + p_devices.push_back(desc(device)); // Driver class only exists for the DSPDevice, so need this guard: #ifndef SHAMROCK_BUILD for (int i = 0; i < Driver::instance()->num_dsps(); i++) - p_devices.push_back((_cl_device_id*)new Coal::DSPDevice(i)); + Coal::DeviceInterface * device = new Coal::DSPDevice(i); + p_devices.push_back(desc(device)); #endif } @@ -119,7 +126,7 @@ namespace Coal close(p_lock_fd); for (int i = 0; i < p_devices.size(); i++) - delete p_devices[i]; + delete pobj(p_devices[i]); } cl_uint Platform::getDevices(cl_device_type device_type, @@ -137,7 +144,9 @@ namespace Coal for (int d = 0; d < p_devices.size(); d++) { cl_device_type type; - p_devices[d]->info(CL_DEVICE_TYPE, sizeof(cl_device_type), &type,0); + auto device = pobj(p_devices[d]); + + device->info(CL_DEVICE_TYPE, sizeof(cl_device_type), &type,0); if (type & device_type) { @@ -193,16 +202,17 @@ namespace Coal break; case CL_PLATFORM_EXTENSIONS: - // TODO add cl_khr_icd when it works #ifdef SHAMROCK_BUILD - STRING_ASSIGN("cl_khr_byte_addressable_store cl_khr_fp64"); + STRING_ASSIGN("cl_khr_byte_addressable_store cl_khr_fp64 cl_khr_icd"); #else - STRING_ASSIGN("cl_khr_byte_addressable_store cl_khr_fp64 cl_ti_msmc_buffers"); + STRING_ASSIGN("cl_khr_byte_addressable_store cl_khr_fp64 cl_ti_msmc_buffers cl_khr_icd"); #endif break; case CL_PLATFORM_ICD_SUFFIX_KHR: -#ifndef SHAMROCK_BUILD +#ifdef SHAMROCK_BUILD + STRING_ASSIGN("Linaro"); +#else STRING_ASSIGN("TI"); #endif break; diff --git a/src/core/program.cpp b/src/core/program.cpp index 60d7a26..ddece95 100644 --- a/src/core/program.cpp +++ b/src/core/program.cpp @@ -929,7 +929,7 @@ cl_int Program::info(cl_program_info param_name, void *value = 0; size_t value_length = 0; llvm::SmallVector<size_t, 4> binary_sizes; - llvm::SmallVector<DeviceInterface *, 4> devices; + llvm::SmallVector<cl_device_id, 4> devices; std::string names; union { @@ -963,11 +963,11 @@ cl_int Program::info(cl_program_info param_name, { const DeviceDependent &dep = p_device_dependent[i]; - devices.push_back(dep.device); + devices.push_back(desc(dep.device)); } value = devices.data(); - value_length = devices.size() * sizeof(DeviceInterface *); + value_length = devices.size() * sizeof(cl_device_id); } else return ((Context *)parent())->info(CL_CONTEXT_DEVICES, diff --git a/src/core/sampler.cpp b/src/core/sampler.cpp index 71fca86..1201327 100644 --- a/src/core/sampler.cpp +++ b/src/core/sampler.cpp @@ -107,7 +107,7 @@ Sampler::Sampler(Context *ctx, unsigned int bitfield) cl_int Sampler::checkImageAvailability() const { cl_uint num_devices; - DeviceInterface **devices; + cl_device_id *d_devices; cl_int rs; rs = ((Context *)parent())->info(CL_CONTEXT_NUM_DEVICES, @@ -117,21 +117,20 @@ cl_int Sampler::checkImageAvailability() const if (rs != CL_SUCCESS) return rs; - devices = (DeviceInterface **)std::malloc(num_devices * - sizeof(DeviceInterface *)); + d_devices = (cl_device_id*)std::malloc(num_devices * sizeof(cl_device_id)); - if (!devices) + if (!d_devices) { return CL_OUT_OF_HOST_MEMORY; } rs = ((Context *)parent())->info(CL_CONTEXT_DEVICES, - num_devices * sizeof(DeviceInterface *), - devices, 0); + num_devices * sizeof(cl_device_id), + d_devices, 0); if (rs != CL_SUCCESS) { - std::free((void *)devices); + std::free((void *)d_devices); return rs; } @@ -139,23 +138,23 @@ cl_int Sampler::checkImageAvailability() const { cl_bool image_support; - rs = devices[i]->info(CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool), + rs = (pobj(d_devices[i]))->info(CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool), &image_support, 0); if (rs != CL_SUCCESS) { - std::free((void *)devices); + std::free((void *)d_devices); return rs; } if (!image_support) { - std::free((void *)devices); + std::free((void *)d_devices); return CL_INVALID_OPERATION; } } - std::free((void *)devices); + std::free((void *)d_devices); return CL_SUCCESS; } |