void * embb_mtapi_alloc_allocate(unsigned int bytes) { void * ptr = embb_alloc(bytes); if (ptr != NULL) { embb_internal__atomic_fetch_and_add_4( &embb_mtapi_alloc_bytes_allocated, sizeof(unsigned int)+bytes); } return ptr; }
int embb_mtapi_network_buffer_initialize( embb_mtapi_network_buffer_t * that, int capacity) { int result = 1; that->position = 0; that->size = 0; that->data = (char*)embb_alloc((size_t)capacity); if (NULL != that->data) { that->capacity = capacity; } else { that->capacity = 0; result = 0; } return result; }
int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, embb_thread_start_t func, void* arg) { pthread_attr_t attr; /* Used to set thread affinities */ int status = pthread_attr_init(&attr); if (status != 0) return EMBB_ERROR; if (core_set != NULL) { #if defined(EMBB_PLATFORM_HAS_GLIB_CPU) || \ defined(EMBB_PLATFORM_HAS_HEADER_CPUSET) assert(embb_core_count_available() < CPU_SETSIZE && "Core sets are only supported up to CPU_SETSIZE processors!"); #ifdef EMBB_PLATFORM_HAS_GLIB_CPU cpu_set_t cpuset; #else cpuset_t cpuset; #endif CPU_ZERO(&cpuset); /* Disable all processors */ for (unsigned int i = 0; i < embb_core_count_available(); i++) { if (embb_core_set_contains(core_set, i)) { CPU_SET(i, &cpuset); } } status = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset); if (status != 0) return EMBB_ERROR; #else embb_log_write("base_c", EMBB_LOG_LEVEL_WARNING, "Could not set thread " "affinity, since no implementation available!\n"); #endif } /* Dynamic allocation of thread arguments. Freed on call of join. */ thread->embb_internal_arg = (embb_internal_thread_arg_t*) embb_alloc(sizeof(embb_internal_thread_arg_t)); thread->embb_internal_arg->func = func; thread->embb_internal_arg->arg = arg; status = pthread_create( &(thread->embb_internal_handle), /* pthread handle */ &attr, /* additional attributes, e.g., affinities */ embb_internal_thread_start, /* thread start function */ (void*)(thread->embb_internal_arg)); /* arguments to thread start func. */ if (status != 0) return EMBB_ERROR; status = pthread_attr_destroy(&attr); if (status != 0) return EMBB_ERROR; return EMBB_SUCCESS; }
int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, embb_thread_start_t func, void *arg) { assert(thread != NULL); thread->embb_internal_arg = (embb_internal_thread_arg_t*) embb_alloc(sizeof(embb_internal_thread_arg_t)); if (thread->embb_internal_arg == NULL) return EMBB_NOMEM; thread->embb_internal_arg->func = func; thread->embb_internal_arg->arg = arg; thread->embb_internal_handle = CreateThread( 0, /* no security */ 0, /* default stack size */ embb_internal_thread_start, /* entry function */ (LPVOID)thread->embb_internal_arg, /* parameters */ 0, /* no creation arguments */ 0); /* no system thread ID */ if (thread->embb_internal_handle == NULL) { return EMBB_ERROR; } if (core_set != NULL) { /* Set thread affinity, if a core set is given */ DWORD_PTR core_mask = 0; DWORD bit_mask = 1; assert(embb_core_count_available() < 64); for (unsigned int i = 0; i < embb_core_count_available(); i++) { if (embb_core_set_contains(core_set, i)) { core_mask |= bit_mask; } bit_mask <<= 1; } if (SetThreadAffinityMask(thread->embb_internal_handle, core_mask) == (DWORD_PTR)NULL) { return EMBB_ERROR; } } return EMBB_SUCCESS; }
mtapi_action_hndl_t mtapi_opencl_action_create( MTAPI_IN mtapi_job_id_t job_id, MTAPI_IN char* kernel_source, MTAPI_IN char* kernel_name, MTAPI_IN mtapi_size_t local_work_size, MTAPI_IN mtapi_size_t element_size, MTAPI_IN void* node_local_data, MTAPI_IN mtapi_size_t node_local_data_size, MTAPI_OUT mtapi_status_t* status) { mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; cl_int err; embb_mtapi_opencl_plugin_t * plugin = &embb_mtapi_opencl_plugin; embb_mtapi_opencl_action_t * action = (embb_mtapi_opencl_action_t*)embb_alloc( sizeof(embb_mtapi_opencl_action_t)); mtapi_action_hndl_t action_hndl = { 0, 0 }; // invalid handle size_t kernel_length = strlen(kernel_source); mtapi_boolean_t free_program_on_error = MTAPI_FALSE; mtapi_boolean_t free_kernel_on_error = MTAPI_FALSE; mtapi_boolean_t free_node_local_data_on_error = MTAPI_FALSE; action->local_work_size = local_work_size; action->element_size = element_size; /* initialization */ action->program = clCreateProgramWithSource(plugin->context, 1, &kernel_source, &kernel_length, &err); if (CL_SUCCESS == err) { free_program_on_error = MTAPI_TRUE; err = clBuildProgram(action->program, 1, &plugin->device_id, NULL, NULL, NULL); } else { err = clGetProgramBuildInfo(action->program, plugin->device_id, CL_PROGRAM_BUILD_LOG, 1024, buffer, NULL); } if (CL_SUCCESS == err) { action->kernel = clCreateKernel(action->program, kernel_name, &err); if (CL_SUCCESS == err) { free_kernel_on_error = MTAPI_TRUE; } } if (0 < node_local_data_size) { action->node_local_data = clCreateBuffer(plugin->context, CL_MEM_READ_ONLY, node_local_data_size, NULL, &err); if (CL_SUCCESS == err) { free_node_local_data_on_error = MTAPI_TRUE; } action->node_local_data_size = (int)node_local_data_size; if (CL_SUCCESS == err) { err = clEnqueueWriteBuffer(plugin->command_queue, action->node_local_data, CL_TRUE, 0, (size_t)action->node_local_data_size, node_local_data, 0, NULL, NULL); } } else { action->node_local_data = NULL; action->node_local_data_size = 0; } if (CL_SUCCESS == err) { err = clSetKernelArg(action->kernel, 4, sizeof(cl_mem), (const void*)&action->node_local_data); } if (CL_SUCCESS == err) { err = clSetKernelArg(action->kernel, 5, sizeof(cl_int), (const void*)&action->node_local_data_size); } if (CL_SUCCESS == err) { action_hndl = mtapi_ext_plugin_action_create( job_id, opencl_task_start, opencl_task_cancel, opencl_action_finalize, action, node_local_data, node_local_data_size, MTAPI_NULL, &local_status); } else { if (free_node_local_data_on_error) { clReleaseMemObject(action->node_local_data); } if (free_kernel_on_error) { clReleaseKernel(action->kernel); } if (free_program_on_error) { clReleaseProgram(action->program); } embb_free(action); } mtapi_status_set(status, local_status); return action_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); }