cl_int clGetSupportedImageFormats(cl_context 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) { 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 context, cl_mem_flags flags, size_t size, void * host_ptr, cl_int * errcode_ret) { cl_int dummy_errcode; 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 (cl_mem)buf; }
cl_mem clCreateImage2D(cl_context context, cl_mem_flags flags, const cl_image_format * image_format, size_t image_width, size_t image_height, size_t image_row_pitch, void * host_ptr, cl_int * errcode_ret) { cl_int dummy_errcode; 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::Image2D *image = new Coal::Image2D(context, image_width, image_height, image_row_pitch, image_format, host_ptr, flags, errcode_ret); if (*errcode_ret != CL_SUCCESS || (*errcode_ret = image->init()) != CL_SUCCESS) { delete image; return 0; } return (cl_mem)image; }
cl_int clRetainContext(cl_context context) { if (!context->isA(Coal::Object::T_Context)) return CL_INVALID_CONTEXT; context->reference(); return CL_SUCCESS; }
cl_int clReleaseContext(cl_context context) { if (!context->isA(Coal::Object::T_Context)) return CL_INVALID_CONTEXT; if (context->dereference()) delete context; return CL_SUCCESS; }
cl_int clGetContextInfo(cl_context context, cl_context_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret) { #ifdef DBG_API std::cerr << "clGetContextInfo\n"; #endif if (!context->isA(Coal::Object::T_Context)) return CL_INVALID_CONTEXT; return context->info(param_name, param_value_size, param_value, param_value_size_ret); }
// Program Object APIs cl_program clCreateProgramWithSource(cl_context context, cl_uint count, const char ** strings, const size_t * lengths, cl_int * errcode_ret) { #ifdef DBG_API std::cerr << "clCreateProgramWithSource\n"; #endif cl_int dummy_errcode; if (!errcode_ret) errcode_ret = &dummy_errcode; if (!context->isA(Coal::Object::T_Context)) { *errcode_ret = CL_INVALID_CONTEXT; return 0; } if (!count || !strings) { *errcode_ret = CL_INVALID_VALUE; return 0; } Coal::Program *program = new Coal::Program(context); *errcode_ret = CL_SUCCESS; *errcode_ret = program->loadSources(count, strings, lengths); if (*errcode_ret != CL_SUCCESS) { delete program; return 0; } return (cl_program)program; }
// Command Queue APIs cl_command_queue clCreateCommandQueue(cl_context context, cl_device_id device, cl_command_queue_properties properties, cl_int * errcode_ret) { cl_int default_errcode_ret; // 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( (Coal::Context *)context, (Coal::DeviceInterface *)device, properties, errcode_ret); if (*errcode_ret != CL_SUCCESS) { // Initialization failed, destroy context delete queue; return 0; } return (_cl_command_queue *)queue; }
// Command Queue APIs cl_command_queue clCreateCommandQueue(cl_context context, cl_device_id device, cl_command_queue_properties properties, cl_int * errcode_ret) { #ifdef DBG_API std::cerr << "Entering clCreateCommandQueue\n"; #endif cl_int default_errcode_ret; // No errcode_ret ? if (!errcode_ret) errcode_ret = &default_errcode_ret; #ifdef DBG_API std::cerr << "Check if the device is an object\n"; #endif if (!device->isA(Coal::Object::T_Device)) { std::cerr << "INVALID_DEVICE\n"; *errcode_ret = CL_INVALID_DEVICE; return 0; } if (!context->isA(Coal::Object::T_Context)) { #ifdef DBG_OUTPUT std::cout << "!!! ERROR: INVALID_CONTEXT" << std::endl; #endif *errcode_ret = CL_INVALID_CONTEXT; return 0; } #ifdef DBG_API std::cerr << "Attempt to initialise device\n"; #endif if (!device->init()) { #ifdef DBG_OUTPUT std::cout << "!!!ERROR: Device initialisation failed!\n"; #endif *errcode_ret = CL_DEVICE_NOT_AVAILABLE; return 0; } *errcode_ret = CL_SUCCESS; Coal::CommandQueue *queue = new Coal::CommandQueue( (Coal::Context *)context, (Coal::DeviceInterface *)device, properties, errcode_ret); if (*errcode_ret != CL_SUCCESS) { #ifdef DBG_OUTPUT std::cout << "!!! ERROR: CommandQueue create failed" << std::endl; #endif // Initialization failed, destroy context delete queue; return 0; } return (_cl_command_queue *)queue; }
cl_program clCreateProgramWithBinary(cl_context context, cl_uint num_devices, const cl_device_id * device_list, const size_t * lengths, const unsigned char **binaries, cl_int * binary_status, cl_int * errcode_ret) { #ifdef DBG_API std::cerr << "clCreateProgramWithBinary\n"; #endif cl_int dummy_errcode; if (!errcode_ret) errcode_ret = &dummy_errcode; if (!context->isA(Coal::Object::T_Context)) { *errcode_ret = CL_INVALID_CONTEXT; return 0; } if (!num_devices || !device_list || !lengths || !binaries) { *errcode_ret = CL_INVALID_VALUE; return 0; } // Check the devices for compliance cl_uint context_num_devices = 0; cl_device_id *context_devices; *errcode_ret = context->info(CL_CONTEXT_NUM_DEVICES, sizeof(cl_uint), &context_num_devices, 0); if (*errcode_ret != CL_SUCCESS) return 0; context_devices = (cl_device_id *)std::malloc(context_num_devices * sizeof(cl_device_id)); *errcode_ret = context->info(CL_CONTEXT_DEVICES, context_num_devices * sizeof(cl_device_id), context_devices, 0); if (*errcode_ret != CL_SUCCESS) return 0; for (cl_uint i=0; i<num_devices; ++i) { bool found = false; if (!lengths[i] || !binaries[i]) { if (binary_status) binary_status[i] = CL_INVALID_VALUE; *errcode_ret = CL_INVALID_VALUE; return 0; } for (cl_uint j=0; j<context_num_devices; ++j) { if (device_list[i] == context_devices[j]) { found = true; break; } } if (!found) { *errcode_ret = CL_INVALID_DEVICE; return 0; } } // Create a program Coal::Program *program = new Coal::Program(context); *errcode_ret = CL_SUCCESS; // Init program *errcode_ret = program->loadBinaries(binaries, lengths, binary_status, num_devices, (Coal::DeviceInterface * const*)device_list); if (*errcode_ret != CL_SUCCESS) { delete program; return 0; } return (cl_program)program; }