void mtapi_action_set_attribute( MTAPI_IN mtapi_action_hndl_t action, MTAPI_IN mtapi_uint_t attribute_num, MTAPI_IN void* attribute, MTAPI_IN mtapi_size_t attribute_size, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; embb_mtapi_log_trace("mtapi_action_set_attribute() called\n"); if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); if (embb_mtapi_action_pool_is_handle_valid(node->action_pool, action)) { embb_mtapi_action_t* local_action = embb_mtapi_action_pool_get_storage_for_handle( node->action_pool, action); mtapi_actionattr_set( &local_action->attributes, attribute_num, attribute, attribute_size, &local_status); } else { local_status = MTAPI_ERR_ACTION_INVALID; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); }
static void opencl_action_finalize( MTAPI_IN mtapi_action_hndl_t action, MTAPI_OUT mtapi_status_t* status ) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; cl_int err; EMBB_UNUSED_IN_RELEASE(err); if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t * node = embb_mtapi_node_get_instance(); if (embb_mtapi_action_pool_is_handle_valid(node->action_pool, action)) { embb_mtapi_action_t * local_action = embb_mtapi_action_pool_get_storage_for_handle( node->action_pool, action); embb_mtapi_opencl_action_t * opencl_action = (embb_mtapi_opencl_action_t *)local_action->plugin_data; if (NULL != opencl_action->node_local_data) { err = clReleaseMemObject(opencl_action->node_local_data); assert(CL_SUCCESS == err); } err = clReleaseKernel(opencl_action->kernel); assert(CL_SUCCESS == err); err = clReleaseProgram(opencl_action->program); assert(CL_SUCCESS == err); embb_free(opencl_action); local_status = MTAPI_SUCCESS; } } mtapi_status_set(status, local_status); }
void mtapi_action_enable( MTAPI_IN mtapi_action_hndl_t action, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; embb_mtapi_log_trace("mtapi_action_enable() called\n"); if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); if (embb_mtapi_action_pool_is_handle_valid(node->action_pool, action)) { embb_mtapi_action_t* local_action = embb_mtapi_action_pool_get_storage_for_handle( node->action_pool, action); local_action->enabled = MTAPI_TRUE; local_status = MTAPI_SUCCESS; } else { local_status = MTAPI_ERR_ACTION_INVALID; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); }
void mtapi_group_set_attribute( MTAPI_IN mtapi_group_hndl_t group, MTAPI_IN mtapi_uint_t attribute_num, MTAPI_OUT void* attribute, MTAPI_IN mtapi_size_t attribute_size, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); embb_mtapi_group_t* local_group; embb_mtapi_log_trace("mtapi_group_set_attribute() called\n"); if (embb_mtapi_node_is_initialized()) { if (embb_mtapi_group_pool_is_handle_valid(node->group_pool, group)) { local_group = embb_mtapi_group_pool_get_storage_for_handle( node->group_pool, group); mtapi_groupattr_set(&local_group->attributes, attribute_num, attribute, attribute_size, &local_status); } else { local_status = MTAPI_ERR_GROUP_INVALID; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); }
void mtapi_group_delete( MTAPI_IN mtapi_group_hndl_t group, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); if (embb_mtapi_group_pool_is_handle_valid(node->group_pool, group)) { embb_mtapi_group_t* local_group = embb_mtapi_group_pool_get_storage_for_handle( node->group_pool, group); if (local_group->deleted) { local_status = MTAPI_ERR_GROUP_INVALID; } else { embb_mtapi_group_finalize(local_group); embb_mtapi_group_pool_deallocate(node->group_pool, local_group); local_status = MTAPI_SUCCESS; } } else { local_status = MTAPI_ERR_GROUP_INVALID; } } else { local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); }
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); } } }
mtapi_boolean_t mtapi_affinity_get( MTAPI_OUT mtapi_affinity_t* mask, MTAPI_IN mtapi_uint_t core_num, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; mtapi_boolean_t affinity = MTAPI_FALSE; embb_mtapi_node_t * node = embb_mtapi_node_get_instance(); embb_mtapi_log_trace("mtapi_affinity_get() called\n"); if (embb_mtapi_node_is_initialized()) { if (MTAPI_NULL != mask) { if (core_num < node->attributes.num_cores) { affinity = embb_bitset_is_set(mask, core_num) ? MTAPI_TRUE : MTAPI_FALSE; local_status = MTAPI_SUCCESS; } else { local_status = MTAPI_ERR_CORE_NUM; } } else { local_status = MTAPI_ERR_PARAMETER; } } else { local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); return affinity; }
void mtapi_affinity_init( MTAPI_OUT mtapi_affinity_t* mask, MTAPI_IN mtapi_boolean_t affinity, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; embb_mtapi_node_t * node = embb_mtapi_node_get_instance(); embb_mtapi_log_trace("mtapi_affinity_init() called\n"); if (embb_mtapi_node_is_initialized()) { if (MTAPI_NULL != mask) { embb_bitset_clear_all(mask); if (affinity) { embb_bitset_set_n(mask, node->attributes.num_cores); } local_status = MTAPI_SUCCESS; } else { local_status = MTAPI_ERR_PARAMETER; } } else { local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); }
void mtapi_action_get_attribute( MTAPI_IN mtapi_action_hndl_t action, MTAPI_IN mtapi_uint_t attribute_num, MTAPI_OUT void* attribute, MTAPI_IN mtapi_size_t attribute_size, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; embb_mtapi_log_trace("mtapi_action_get_attribute() called\n"); if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); if (embb_mtapi_action_pool_is_handle_valid(node->action_pool, action)) { embb_mtapi_action_t* local_action = embb_mtapi_action_pool_get_storage_for_handle( node->action_pool, action); if (MTAPI_NULL == attribute) { local_status = MTAPI_ERR_PARAMETER; } else { switch (attribute_num) { case MTAPI_ACTION_GLOBAL: local_status = embb_mtapi_attr_get_mtapi_boolean_t( &local_action->attributes.global, attribute, attribute_size); break; case MTAPI_ACTION_AFFINITY: local_status = embb_mtapi_attr_get_mtapi_affinity_t( &local_action->attributes.affinity, attribute, attribute_size); break; case MTAPI_ACTION_DOMAIN_SHARED: local_status = embb_mtapi_attr_get_mtapi_boolean_t( &local_action->attributes.domain_shared, attribute, attribute_size); break; default: /* attribute unknown */ local_status = MTAPI_ERR_ATTR_NUM; break; } } } else { local_status = MTAPI_ERR_ACTION_INVALID; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); }
void mtapi_group_get_attribute( MTAPI_IN mtapi_group_hndl_t group, MTAPI_IN mtapi_uint_t attribute_num, MTAPI_OUT void* attribute, MTAPI_IN mtapi_size_t attribute_size, MTAPI_OUT mtapi_status_t* status ) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; EMBB_UNUSED(attribute_num); EMBB_UNUSED(attribute_size); embb_mtapi_log_trace("mtapi_group_get_attribute() called\n"); if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); if (embb_mtapi_group_pool_is_handle_valid(node->group_pool, group)) { /* the following is not needed for now, since there are no attributes embb_mtapi_group_t* local_group = embb_mtapi_group_pool_get_storage_for_handle( node->group_pool, group); */ if (MTAPI_NULL == attribute) { local_status = MTAPI_ERR_PARAMETER; } else { /* switch is not needed for now, since there are no attributes switch (attribute_num) { default: */ local_status = MTAPI_ERR_ATTR_NUM; /* break; }*/ } } else { local_status = MTAPI_ERR_GROUP_INVALID; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); }
mtapi_group_hndl_t mtapi_group_create( MTAPI_IN mtapi_group_id_t group_id, MTAPI_IN mtapi_group_attributes_t* attributes, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); mtapi_group_hndl_t group_hndl = { 0, EMBB_MTAPI_IDPOOL_INVALID_ID }; embb_mtapi_group_t* group = NULL; embb_mtapi_log_trace("mtapi_group_create() called\n"); if (embb_mtapi_node_is_initialized()) { group = embb_mtapi_group_pool_allocate(node->group_pool); if (MTAPI_NULL != group) { embb_mtapi_group_initialize_with_node(group, node); group->group_id = group_id; if (MTAPI_NULL != attributes) { group->attributes = *attributes; local_status = MTAPI_SUCCESS; } else { mtapi_groupattr_init(&group->attributes, &local_status); } if (MTAPI_SUCCESS == local_status) { group_hndl = group->handle; } else { embb_mtapi_group_finalize(group); embb_mtapi_group_pool_deallocate(node->group_pool, group); } } else { local_status = MTAPI_ERR_GROUP_LIMIT; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); return group_hndl; }
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); }
void mtapi_action_disable( MTAPI_IN mtapi_action_hndl_t action, MTAPI_IN mtapi_timeout_t timeout, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; embb_mtapi_log_trace("mtapi_action_disable() called\n"); if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); if (embb_mtapi_action_pool_is_handle_valid(node->action_pool, action)) { embb_mtapi_action_t* local_action = embb_mtapi_action_pool_get_storage_for_handle( node->action_pool, action); local_action->enabled = MTAPI_FALSE; embb_mtapi_thread_context_t * context = NULL; embb_duration_t wait_duration; embb_time_t end_time; if (MTAPI_INFINITE < timeout) { embb_duration_set_milliseconds( &wait_duration, (unsigned long long)timeout); embb_time_in(&end_time, &wait_duration); } /* cancel all tasks */ embb_mtapi_scheduler_process_tasks( node->scheduler, embb_mtapi_action_disable_visitor, local_action); /* find out on which thread we are */ context = embb_mtapi_scheduler_get_current_thread_context( node->scheduler); local_status = MTAPI_SUCCESS; while (embb_atomic_load_int(&local_action->num_tasks)) { if (MTAPI_INFINITE < timeout) { embb_time_t current_time; embb_time_now(¤t_time); if (embb_time_compare(¤t_time, &end_time) > 0) { /* timeout! */ local_status = MTAPI_TIMEOUT; break; } } /* do other work if applicable */ embb_mtapi_scheduler_execute_task_or_yield( node->scheduler, node, context); } } else { local_status = MTAPI_ERR_ACTION_INVALID; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); }
mtapi_action_hndl_t mtapi_action_create( MTAPI_IN mtapi_job_id_t job_id, MTAPI_IN mtapi_action_function_t action_function, MTAPI_IN void* node_local_data, MTAPI_IN mtapi_size_t node_local_data_size, MTAPI_IN mtapi_action_attributes_t* attributes, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; mtapi_action_hndl_t action_handle = { 0, EMBB_MTAPI_IDPOOL_INVALID_ID }; embb_mtapi_log_trace("mtapi_action_create() called\n"); if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); /* check if job is valid */ if (embb_mtapi_job_is_id_valid(node, job_id)) { embb_mtapi_job_t* job = embb_mtapi_job_get_storage_for_id(node, job_id); embb_mtapi_action_t* new_action = embb_mtapi_action_pool_allocate(node->action_pool); if (MTAPI_NULL != new_action) { new_action->domain_id = node->domain_id; new_action->node_id = node->node_id; new_action->job_id = job_id; new_action->node_local_data = node_local_data; new_action->node_local_data_size = node_local_data_size; new_action->enabled = MTAPI_TRUE; new_action->is_plugin_action = MTAPI_FALSE; embb_atomic_store_int(&new_action->num_tasks, 0); new_action->action_function = action_function; /* set defaults if no attributes were given */ if (MTAPI_NULL != attributes) { new_action->attributes = *attributes; local_status = MTAPI_SUCCESS; } else { /* use the default */ mtapi_actionattr_init(&new_action->attributes, &local_status); } /* check if affinity is sane */ if (0 == new_action->attributes.affinity) { local_status = MTAPI_ERR_PARAMETER; } if (MTAPI_SUCCESS == local_status) { action_handle = new_action->handle; embb_mtapi_job_add_action(job, new_action); } else { embb_mtapi_action_pool_deallocate(node->action_pool, new_action); } } else { /* no more space left in action pool */ local_status = MTAPI_ERR_ACTION_LIMIT; } } else { local_status = MTAPI_ERR_JOB_INVALID; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); return action_handle; }
void mtapi_group_wait_any( MTAPI_IN mtapi_group_hndl_t group, MTAPI_OUT void** result, MTAPI_IN mtapi_timeout_t timeout, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; embb_mtapi_log_trace("mtapi_group_wait_any() called\n"); if (embb_mtapi_node_is_initialized()) { embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); if (embb_mtapi_group_pool_is_handle_valid(node->group_pool, group)) { embb_mtapi_group_t* local_group = embb_mtapi_group_pool_get_storage_for_handle( node->group_pool, group); embb_mtapi_task_t* local_task; /* are there any tasks left? */ if (0 == embb_atomic_load_int(&local_group->num_tasks)) { /* group becomes invalid, so delete it */ mtapi_group_delete(group, &local_status); local_status = MTAPI_GROUP_COMPLETED; } else { embb_mtapi_thread_context_t * context = NULL; embb_duration_t wait_duration; embb_time_t end_time; if (MTAPI_INFINITE < timeout) { embb_duration_set_milliseconds( &wait_duration, (unsigned long long)timeout); embb_time_in(&end_time, &wait_duration); } /* find out on which thread we are */ context = embb_mtapi_scheduler_get_current_thread_context( node->scheduler); /* wait for any task to arrive */ local_status = MTAPI_SUCCESS; local_task = embb_mtapi_task_queue_pop(&local_group->queue); while (MTAPI_NULL == local_task) { if (MTAPI_INFINITE < timeout) { embb_time_t current_time; embb_time_now(¤t_time); if (embb_time_compare(¤t_time, &end_time) > 0) { /* timeout! */ local_status = MTAPI_TIMEOUT; break; } } /* do other work if applicable */ embb_mtapi_scheduler_execute_task_or_yield( node->scheduler, node, context); /* try to pop a task from the group queue */ local_task = embb_mtapi_task_queue_pop(&local_group->queue); } /* was there a timeout, or is there a result? */ if (MTAPI_NULL != local_task) { /* store result */ if (MTAPI_NULL != result) { *result = local_task->result_buffer; } /* return error code set by the task */ local_status = local_task->error_code; /* delete task */ embb_mtapi_task_delete(local_task, node->task_pool); embb_atomic_fetch_and_add_int(&local_group->num_tasks, -1); } } } else { local_status = MTAPI_ERR_GROUP_INVALID; } } else { embb_mtapi_log_error("mtapi not initialized\n"); local_status = MTAPI_ERR_NODE_NOTINIT; } mtapi_status_set(status, local_status); embb_mtapi_log_trace("mtapi_group_wait_any() returns\n"); }