static void CL_API_CALL opencl_task_complete( cl_event ev, cl_int status, void * data) { EMBB_UNUSED(ev); EMBB_UNUSED(status); cl_int err; EMBB_UNUSED_IN_RELEASE(err); embb_mtapi_opencl_task_t * opencl_task = (embb_mtapi_opencl_task_t*)data; if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t * node = embb_mtapi_node_get_instance(); if (embb_mtapi_task_pool_is_handle_valid( node->task_pool, opencl_task->task)) { embb_mtapi_task_t * local_task = embb_mtapi_task_pool_get_storage_for_handle( node->task_pool, opencl_task->task); err = clWaitForEvents(1, &opencl_task->kernel_finish_event); assert(CL_SUCCESS == err); if (NULL != opencl_task->result_buffer) { err = clReleaseMemObject(opencl_task->result_buffer); assert(CL_SUCCESS == err); } if (NULL != opencl_task->arguments) { err = clReleaseMemObject(opencl_task->arguments); assert(CL_SUCCESS == err); } embb_mtapi_task_set_state(local_task, MTAPI_TASK_COMPLETED); } } }
static mtapi_boolean_t embb_mtapi_action_disable_visitor( embb_mtapi_task_t * task, void * user_data) { embb_mtapi_action_t * action = (embb_mtapi_action_t*)user_data; mtapi_boolean_t result = MTAPI_FALSE; assert(MTAPI_NULL != action); assert(MTAPI_NULL != task); if ( task->action.id == action->handle.id && task->action.tag == action->handle.tag) { /* task is scheduled and needs to be cancelled */ embb_mtapi_task_set_state(task, MTAPI_TASK_CANCELLED); task->error_code = MTAPI_ERR_ACTION_DISABLED; result = MTAPI_TRUE; } return result; }
static void opencl_task_start( MTAPI_IN mtapi_task_hndl_t task, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; cl_int err; if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t * node = embb_mtapi_node_get_instance(); if (embb_mtapi_task_pool_is_handle_valid(node->task_pool, task)) { embb_mtapi_task_t * local_task = embb_mtapi_task_pool_get_storage_for_handle(node->task_pool, task); if (embb_mtapi_action_pool_is_handle_valid( node->action_pool, local_task->action)) { embb_mtapi_action_t * local_action = embb_mtapi_action_pool_get_storage_for_handle( node->action_pool, local_task->action); embb_mtapi_opencl_plugin_t * plugin = &embb_mtapi_opencl_plugin; embb_mtapi_opencl_action_t * opencl_action = (embb_mtapi_opencl_action_t*)local_action->plugin_data; embb_mtapi_opencl_task_t * opencl_task = (embb_mtapi_opencl_task_t*)embb_alloc( sizeof(embb_mtapi_opencl_task_t)); size_t elements = local_task->result_size / opencl_action->element_size; size_t global_work_size; if (0 == elements) elements = 1; global_work_size = round_up(opencl_action->local_work_size, elements); opencl_task->task = task; opencl_task->arguments_size = (int)local_task->arguments_size; if (0 < local_task->arguments_size) { opencl_task->arguments = clCreateBuffer(plugin->context, CL_MEM_READ_ONLY, local_task->arguments_size, NULL, &err); } else { opencl_task->arguments = NULL; } opencl_task->result_buffer_size = (int)local_task->result_size; if (0 < local_task->result_size) { opencl_task->result_buffer = clCreateBuffer(plugin->context, CL_MEM_WRITE_ONLY, local_task->result_size, NULL, &err); } else { opencl_task->result_buffer = NULL; } err = clSetKernelArg(opencl_action->kernel, 0, sizeof(cl_mem), (const void*)&opencl_task->arguments); err |= clSetKernelArg(opencl_action->kernel, 1, sizeof(cl_int), (const void*)&opencl_task->arguments_size); err |= clSetKernelArg(opencl_action->kernel, 2, sizeof(cl_mem), (const void*)&opencl_task->result_buffer); err |= clSetKernelArg(opencl_action->kernel, 3, sizeof(cl_int), (const void*)&opencl_task->result_buffer_size); err |= clEnqueueWriteBuffer(plugin->command_queue, opencl_task->arguments, CL_FALSE, 0, (size_t)opencl_task->arguments_size, local_task->arguments, 0, NULL, NULL); if (CL_SUCCESS == err) { embb_mtapi_task_set_state(local_task, MTAPI_TASK_RUNNING); err |= clEnqueueNDRangeKernel(plugin->command_queue, opencl_action->kernel, 1, NULL, &global_work_size, &opencl_action->local_work_size, 0, NULL, NULL); err |= clEnqueueReadBuffer(plugin->command_queue, opencl_task->result_buffer, CL_FALSE, 0, (size_t)opencl_task->result_buffer_size, local_task->result_buffer, 0, NULL, &opencl_task->kernel_finish_event); err |= clSetEventCallback(opencl_task->kernel_finish_event, CL_COMPLETE, opencl_task_complete, opencl_task); } err |= clFlush(plugin->command_queue); if (CL_SUCCESS != err) { embb_mtapi_task_set_state(local_task, MTAPI_TASK_ERROR); local_status = MTAPI_ERR_ACTION_FAILED; } else { local_status = MTAPI_SUCCESS; } } } } mtapi_status_set(status, local_status); }