/* print a JSON object to the FILE stream */ static void pobj(FILE *fp, mj_t *obj, int depth) { unsigned i; char *s; if (obj == NULL) { (void) fprintf(stderr, "No object found\n"); return; } for (i = 0 ; i < (unsigned)depth ; i++) { p(fp, " ", NULL); } switch(obj->type) { case MJ_NULL: case MJ_FALSE: case MJ_TRUE: p(fp, (obj->type == MJ_NULL) ? "null" : (obj->type == MJ_FALSE) ? "false" : "true", NULL); break; case MJ_NUMBER: p(fp, obj->value.s, NULL); break; case MJ_STRING: if ((i = mj_asprint(&s, obj, MJ_HUMAN)) > 2) { (void) fprintf(fp, "%.*s", (int)i - 2, &s[1]); free(s); } break; case MJ_ARRAY: for (i = 0 ; i < obj->c ; i++) { pobj(fp, &obj->value.v[i], depth + 1); if (i < obj->c - 1) { (void) fprintf(fp, ", "); } } (void) fprintf(fp, "\n"); break; case MJ_OBJECT: for (i = 0 ; i < obj->c ; i += 2) { pobj(fp, &obj->value.v[i], depth + 1); p(fp, ": ", NULL); pobj(fp, &obj->value.v[i + 1], 0); if (i < obj->c - 1) { p(fp, ", ", NULL); } } p(fp, "\n", NULL); break; default: break; } }
Coal::DSPDevice * getDspDevice() { static Coal::DSPDevice *dspdevice = NULL; if (dspdevice == NULL) { cl_device_id* devices; cl_uint num_devices = 0; cl_int errcode; errcode = clGetDeviceIDs((cl_platform_id) &the_platform::Instance(), CL_DEVICE_TYPE_ACCELERATOR, 0, NULL, &num_devices); if (!num_devices) return NULL; devices = (cl_device_id*) malloc(num_devices * sizeof(cl_device_id)); if (!devices) return NULL; errcode = clGetDeviceIDs((cl_platform_id) &the_platform::Instance(), CL_DEVICE_TYPE_ACCELERATOR, num_devices, devices, 0); if (errcode != CL_SUCCESS) { free (devices); return NULL; } dspdevice = (Coal::DSPDevice *)pobj(devices[0]); free(devices); } return dspdevice; }
cl_int clGetSupportedImageFormats(cl_context d_context, cl_mem_flags flags, cl_mem_object_type image_type, cl_uint num_entries, cl_image_format * image_formats, cl_uint * num_image_formats) { auto context = pobj(d_context); if (!context->isA(Coal::Object::T_Context)) return CL_INVALID_CONTEXT; (void) flags; (void) image_type; if (!num_entries && image_formats) return CL_INVALID_VALUE; if (image_formats) { std::memcpy(image_formats, supported_formats, MIN(num_entries * sizeof(cl_image_format), sizeof(supported_formats))); } if (num_image_formats) *num_image_formats = sizeof(supported_formats) / sizeof(cl_image_format); return CL_SUCCESS; }
// Memory Object APIs cl_mem clCreateBuffer(cl_context d_context, cl_mem_flags flags, size_t size, void * host_ptr, cl_int * errcode_ret) { cl_int dummy_errcode; auto context = pobj(d_context); if (!errcode_ret) errcode_ret = &dummy_errcode; if (!context->isA(Coal::Object::T_Context)) { *errcode_ret = CL_INVALID_CONTEXT; return 0; } *errcode_ret = CL_SUCCESS; Coal::Buffer *buf = new Coal::Buffer(context, size, host_ptr, flags, errcode_ret); if (*errcode_ret != CL_SUCCESS || (*errcode_ret = buf->init()) != CL_SUCCESS) { delete buf; return 0; } return desc(buf); }
// Sampler APIs cl_sampler clCreateSampler(cl_context d_context, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode, cl_int * errcode_ret) { cl_int dummy_errcode; auto context = pobj(d_context); if (!errcode_ret) errcode_ret = &dummy_errcode; if (!context->isA(Coal::Object::T_Context)) { *errcode_ret = CL_INVALID_CONTEXT; return 0; } *errcode_ret = CL_SUCCESS; Coal::Sampler *sampler = new Coal::Sampler(context, normalized_coords, addressing_mode, filter_mode, errcode_ret); if (*errcode_ret != CL_SUCCESS) { delete sampler; return 0; } return desc(sampler); }
cl_uint Platform::getDevices(cl_device_type device_type, cl_uint num_entries, cl_device_id * devices) { cl_uint device_number = 0; if (device_type == CL_DEVICE_TYPE_DEFAULT) #ifdef SHAMROCK_BUILD device_type = CL_DEVICE_TYPE_CPU; #else device_type = CL_DEVICE_TYPE_ACCELERATOR; #endif for (int d = 0; d < p_devices.size(); d++) { cl_device_type type; auto device = pobj(p_devices[d]); device->info(CL_DEVICE_TYPE, sizeof(cl_device_type), &type,0); if (type & device_type) { if (devices && device_number < num_entries) devices[device_number++] = p_devices[d]; else device_number++; } } return device_number; }
cl_int clReleaseKernel(cl_kernel d_kernel) { auto kernel = pobj(d_kernel); if (!kernel->isA(Coal::Object::T_Kernel)) return CL_INVALID_KERNEL; if (kernel->dereference()) { Coal::Program *p =(Coal::Program *)kernel->parent(); for (size_t i=0; i < p->kernelList.size(); i++) { if (p->kernelList[i]->p_name.compare(kernel->p_name) == 0) { p->kernelReleasedList.push_back(p->kernelList[i]); p->kernelList.erase(p->kernelList.begin() + i); // BUG: TAG // For some odd reason when we delete this, we're corrupting then inside of some kernel objects //delete kernel; } } } return CL_SUCCESS; }
Platform::~Platform() { flock(p_lock_fd, LOCK_UN); close(p_lock_fd); for (int i = 0; i < p_devices.size(); i++) delete pobj(p_devices[i]); }
cl_int clGetKernelWorkGroupInfo(cl_kernel d_kernel, cl_device_id d_device, cl_kernel_work_group_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret) { auto device = pobj(d_device); auto kernel = pobj(d_kernel); if (!kernel->isA(Coal::Object::T_Kernel)) return CL_INVALID_KERNEL; return kernel->workGroupInfo(device, param_name, param_value_size, param_value, param_value_size_ret); }
cl_mem clCreateSubBuffer(cl_mem d_buffer, cl_mem_flags flags, cl_buffer_create_type buffer_create_type, const void * buffer_create_info, cl_int * errcode_ret) { cl_int dummy_errcode; // code below seems to be expecting a Coal::Buffer *, so convert to such: Coal::Buffer * buffer = (Coal::Buffer *)pobj(d_buffer); if (!errcode_ret) errcode_ret = &dummy_errcode; if (!buffer->isA(Coal::Object::T_MemObject)) { *errcode_ret = CL_INVALID_MEM_OBJECT; return 0; } Coal::MemObject *memobject = (Coal::MemObject *)buffer; cl_buffer_region *region = (cl_buffer_region *)buffer_create_info; // NOTE: Is it right ? Couldn't we create SubBuffers of images ? if (memobject->type() != Coal::MemObject::Buffer) { *errcode_ret = CL_INVALID_MEM_OBJECT; return 0; } if (buffer_create_type != CL_BUFFER_CREATE_TYPE_REGION) { *errcode_ret = CL_INVALID_VALUE; return 0; } if (!buffer_create_info) { *errcode_ret = CL_INVALID_VALUE; return 0; } *errcode_ret = CL_SUCCESS; Coal::SubBuffer *buf = new Coal::SubBuffer(buffer, region->origin, region->size, flags, errcode_ret); if (*errcode_ret != CL_SUCCESS || (*errcode_ret = buf->init()) != CL_SUCCESS) { delete buf; return 0; } return desc(buf); }
// Command Queue APIs cl_command_queue clCreateCommandQueue(cl_context d_context, cl_device_id d_device, cl_command_queue_properties properties, cl_int * errcode_ret) { cl_int default_errcode_ret; auto device = pobj(d_device); auto context = pobj(d_context); // No errcode_ret ? if (!errcode_ret) errcode_ret = &default_errcode_ret; if (!device->isA(Coal::Object::T_Device)) { *errcode_ret = CL_INVALID_DEVICE; return 0; } if (!context->isA(Coal::Object::T_Context)) { *errcode_ret = CL_INVALID_CONTEXT; return 0; } *errcode_ret = CL_SUCCESS; Coal::CommandQueue *queue = new Coal::CommandQueue( context, device, properties, errcode_ret); if (*errcode_ret != CL_SUCCESS) { // Initialization failed, destroy context delete queue; return 0; } return desc(queue); }
cl_int clRetainKernel(cl_kernel d_kernel) { auto kernel = pobj(d_kernel); if (!kernel->isA(Coal::Object::T_Kernel)) return CL_INVALID_KERNEL; kernel->reference(); return CL_SUCCESS; }
cl_int clRetainMemObject(cl_mem d_memobj) { auto memobj = pobj(d_memobj); if (!memobj->isA(Coal::Object::T_MemObject)) return CL_INVALID_MEM_OBJECT; memobj->reference(); return CL_SUCCESS; }
cl_int clRetainDevice(cl_device_id d_device) { auto device = pobj(d_device); if (!device->isA(Coal::Object::T_Device)) return CL_INVALID_DEVICE; device->reference(); return CL_SUCCESS; }
cl_int clRetainSampler(cl_sampler d_sampler) { auto sampler = pobj(d_sampler); if (!sampler->isA(Coal::Object::T_Sampler)) return CL_INVALID_SAMPLER; sampler->reference(); return CL_SUCCESS; }
cl_int clRetainCommandQueue(cl_command_queue d_command_queue) { auto command_queue = pobj(d_command_queue); if (!command_queue->isA(Coal::Object::T_CommandQueue)) return CL_INVALID_COMMAND_QUEUE; command_queue->reference(); return CL_SUCCESS; }
cl_int clSetKernelArg(cl_kernel d_kernel, cl_uint arg_indx, size_t arg_size, const void * arg_value) { auto kernel = pobj(d_kernel); if (!kernel->isA(Coal::Object::T_Kernel)) return CL_INVALID_KERNEL; return kernel->setArg(arg_indx, arg_size, arg_value); }
cl_int clSetCommandQueueProperty(cl_command_queue d_command_queue, cl_command_queue_properties properties, cl_bool enable, cl_command_queue_properties * old_properties) { auto command_queue = pobj(d_command_queue); if (!command_queue->isA(Coal::Object::T_CommandQueue)) return CL_INVALID_COMMAND_QUEUE; return command_queue->setProperty(properties, enable, old_properties); }
cl_int clReleaseSampler(cl_sampler d_sampler) { auto sampler = pobj(d_sampler); if (!sampler->isA(Coal::Object::T_Sampler)) return CL_INVALID_SAMPLER; if (sampler->dereference()) delete sampler; return CL_SUCCESS; }
cl_int clReleaseDevice(cl_device_id d_device) { auto device = pobj(d_device); if (!device->isA(Coal::Object::T_Device)) return CL_INVALID_DEVICE; if (device->dereference()) delete device; return CL_SUCCESS; }
cl_int clReleaseMemObject(cl_mem d_memobj) { auto memobj = pobj(d_memobj); if (!memobj->isA(Coal::Object::T_MemObject)) return CL_INVALID_MEM_OBJECT; if (memobj->dereference()) delete memobj; return CL_SUCCESS; }
ObjectBase::~ObjectBase() { // remove the reference in the parent PObjectBase parent = m_parent.lock(); if (parent) { PObjectBase pobj(GetThis()); parent->RemoveChild(pobj); } LogDebug(wxT("delete ObjectBase")); }
cl_int 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; return in_device->createSubDevices(properties, num_devices, out_devices, num_devices_ret); }
ObjectBase::~ObjectBase() { s_instances--; // eliminamos la referencia en el padre shared_ptr<ObjectBase> parent = m_parent.lock(); if (parent) { shared_ptr<ObjectBase> pobj(GetThis()); parent->RemoveChild(pobj); } Debug::Print(wxT("delete ObjectBase (%d)"),s_instances); }
cl_int clGetSamplerInfo(cl_sampler d_sampler, cl_sampler_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret) { auto sampler = pobj(d_sampler); if (!sampler->isA(Coal::Object::T_Sampler)) return CL_INVALID_SAMPLER; return sampler->info(param_name, param_value_size, param_value, param_value_size_ret); }
cl_int clSetMemObjectDestructorCallback(cl_mem d_memobj, void (CL_CALLBACK *pfn_notify)(cl_mem memobj, void *user_data), void * user_data) { auto memobj = pobj(d_memobj); if (!memobj->isA(Coal::Object::T_MemObject)) return CL_INVALID_MEM_OBJECT; memobj->setDestructorCallback(pfn_notify, user_data); return CL_SUCCESS; }
cl_int clGetMemObjectInfo(cl_mem d_memobj, cl_mem_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret) { auto memobj = pobj(d_memobj); if (!memobj->isA(Coal::Object::T_MemObject)) return CL_INVALID_MEM_OBJECT; return memobj->info(param_name, param_value_size, param_value, param_value_size_ret); }
cl_int clGetKernelInfo(cl_kernel d_kernel, cl_kernel_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret) { auto kernel = pobj(d_kernel); if (!kernel->isA(Coal::Object::T_Kernel)) return CL_INVALID_KERNEL; return kernel->info(param_name, param_value_size, param_value, param_value_size_ret); }
cl_int clGetCommandQueueInfo(cl_command_queue d_command_queue, cl_command_queue_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret) { auto command_queue = pobj(d_command_queue); if (!command_queue->isA(Coal::Object::T_CommandQueue)) return CL_INVALID_COMMAND_QUEUE; return command_queue->info(param_name, param_value_size, param_value, param_value_size_ret); }
cl_int clReleaseCommandQueue(cl_command_queue d_command_queue) { auto command_queue = pobj(d_command_queue); if (!command_queue->isA(Coal::Object::T_CommandQueue)) return CL_INVALID_COMMAND_QUEUE; command_queue->flush(); if (command_queue->dereference()) delete command_queue; return CL_SUCCESS; }