Simple example

The following example shows a simple ALF application. The host application initializes the ALF runtime, creates a task descriptor and a task associated with that descriptor, adds one work block to the work queue of the task, waits for the task to complete, and finally exits the ALF runtime.

On the accelerator side, the computational kernel prints "Hello World" to stdout.

Source code for the host application

#include <stdio.h>
#include <alf.h>

char* library_name = "alf_hello_world_hybrid_spu64.so";
char* spu_image_name = "alf_hello_world_spu";
char* kernel_name = "comp_kernel";

int main()
{
  alf_handle_t handle;
  alf_task_desc_handle_t task_desc_handle;
  alf_task_handle_t task_handle;
  alf_wb_handle_t wb_handle;

  /* initializes the ALF runtime */
  alf_init(NULL, &handle);
  alf_num_instances_set(handle, 1); /* this is optional, ALF default to use all available accels */

  /* creates the task descriptor */
  alf_task_desc_create(handle, 0, &task_desc_handle);
  alf_task_desc_set_int32(task_desc_handle, ALF_TASK_DESC_MAX_STACK_SIZE, 4096); /* ALF has default stack size */
  alf_task_desc_set_int64(task_desc_handle, ALF_TASK_DESC_ACCEL_IMAGE_REF_L, (unsigned long long)spu_image_name);
  alf_task_desc_set_int64(task_desc_handle, ALF_TASK_DESC_ACCEL_LIBRARY_REF_L, (unsigned long long)library_name);
  alf_task_desc_set_int64(task_desc_handle, ALF_TASK_DESC_ACCEL_KERNEL_REF_L, (unsigned long long)kernel_name);

  /* creates the task */
  alf_task_create(task_desc_handle, NULL, 1, 0, 0, &task_handle);

  /* creates a work block and enqueue it */
  alf_wb_create(task_handle, ALF_WB_SINGLE, 1, &wb_handle);
  alf_wb_enqueue(wb_handle);
  
  /* finalizes the task */
  alf_task_finalize(task_handle);

  /* waits for the task to finish */
  alf_task_wait(task_handle, -1);

  /* exits the ALF runtime */
  alf_exit(handle, ALF_EXIT_POLICY_FORCE, 0);   /* ALF_EXIT_POLICY_WAIT would be nicer for common practices */

  return 0;
}

Source code for the accelerator side

#include <stdio.h>
#include <alf_accel.h>

int comp_kernel(void *p_task_context, 
                void *p_parm_context,          
                void *p_input_buffer, 
                void *p_output_buffer, 
                void *p_inout_buffer,
                unsigned int current_count, 
                unsigned int total_count)
{
  printf("Hello World!\n");
  return 0;
}

ALF_ACCEL_EXPORT_API_LIST_BEGIN
  ALF_ACCEL_EXPORT_API("comp_kernel", comp_kernel);  
  /* P93, "It is recommended to be just the same as the correspondent function identifier" */
ALF_ACCEL_EXPORT_API_LIST_END