Skip to main content

Fun with ALF, Part 2: Converting I/O data

Examples show you how to use ALF's task context buffer as a large lookup table to convert 16-bit input to 8-bit output data

Kane Scarlett, Editor, Multicore acceleration, IBM Japan
Kane Scarlett
Kane Scarlett is a technology journalist/analyst with 20 years in the business, working for such publishers as National Geographic, Population Reference Bureau, Miller Freeman, and IDG, and managing, editing, and writing for such august journals as JavaWorld, LinuxWorld, and of course, developerWorks.

Summary:  In this Cell Broadband Engine™ (Cell/B.E.) series, learn how to use the Accelerated Library Framework (ALF) task context buffer as a large lookup table to convert the 16-bit input data to 8-bit output data. The "ALF for Cell/B.E. Programmer's Guide and API Reference, Version 3.0" (see Resources) is the source for the content.

View more content in this series

Date:  25 Mar 2008
Level:  Introductory
Activity:  2257 views

Introduction

In this article, you'll see how to use the task context buffer as a large lookup table to convert the 16-bit input data to 8-bit output data. The lookup table has 65536 entries defined:

For all -32768 <= in <32768
          / 0,        in < -4096 
Table(n)=| in/32      -4096 <=in<4096 
          \ 255,      in >= 4096

More fun with ALF

Look for more in the Fun with ALF series:

  • The first one in this series, "Fun with ALF, Part 1: Adding large matrices together" (developerWorks, March 2008) shows you how to use ALF to add two large matrices together (with an example for host data partitioning and for accelerator data partitioning).
  • In "Min-max finder," discover how you can use the task context to keep the partial computing results for each task instance and then combine these partial results into the final result.
  • In "Multiple vector dot products," uncover how to use the bundled work block distribution with the task context to handle situations where the work block cannot hold the partitioned data because of a local memory size limit.
  • In "Overlapped I/O buffer," take a look at using overlapped I/O buffers to do matrix addition.
  • In "Task dependency," check out a simple simulation in which you can use task dependency in a two-stage pipeline application.

And watch for similar Fun with series addressing DaCS, BLAS, and other technologies to make your Cell/B.E. programming easier.

In the next section, you'll see the stripped-down code list. The routines of less interest have been removed to allow you to focus on the key features. Because the host code already initialized the task context buffer (the lookup table) and because the table is used as read-only data, you do not need the context setup and context merge functions on the accelerator side.

You can use this sample as a template to build a more complicated application.

Exploring data structures shared by the host and accelerator

The following code segment shows the data structures that the host and accelerators share. The my_task_context_t data structure contains the lookup table. The my_wb_parms_t data structure represents the parameter and context data for each work block.

/* ---------------------------------------------- */ 
/* data structures shared by host and accelerator */ 
/* ---------------------------------------------- */ 
typedef struct _my_task_context_t 
{
    alf_data_byte_t table[65536]; 
} my_task_context_t; 

typedef struct _my_wb_parms_t 
{
    alf_data_uint32_t num_data; /* number of data in this WB */ 
} my_wb_parms_t;


Setting up the task descriptor

The following code segment shows how to set up the task descriptor for this application. The task context-related information is indicated in bold font in the code.

alf_task_desc_create(alf_handle, 0, &task_desc_handle;); 
  /* set up the task descriptor ... ... */ 
  /* the computing kernel name */ 
  alf_task_desc_set_int64(task_desc_handle, 
     ALF_TASK_DESC_ACCEL_KERNEL_REF_L, "comp_kernel");

  /* the task context buffer size */ 
  alf_task_desc_set_int32(task_desc_handle, 
     ALF_TASK_DESC_TSK_CTX_SIZE, sizeof(my_task_context_t));

  /* the work block parm buffer size */ 
  alf_task_desc_set_int32(task_desc_handle, 
     ALF_TASK_DESC_WB_PARM_CTX_BUF_SIZE, sizeof(my_wb_parms_t)); 

  /* the input buffer size */ 
  alf_task_desc_set_int32(task_desc_handle, 
     ALF_TASK_DESC_WB_IN_BUF_SIZE, 
     PART_SIZE*sizeof(alf_data_int16_t)); 

  /* the output buffer size */ 
  alf_task_desc_set_int32(task_desc_handle, 
     ALF_TASK_DESC_WB_OUT_BUF_SIZE, 
     PART_SIZE*sizeof(alf_data_byte_t)); 

  /* the task context entry */ 
  alf_task_desc_ctx_entry_add(task_desc_handle, ALF_DATA_BYTE, 
    sizeof(my_task_context_t)/sizeof(alf_data_byte_t));


Setting up the work block

The following code segment shows the code to create a work block.

/* creating wb and adding param & io buffer */ 
for (i = 0; i < NUM_DATA; i += PART_SIZE) 
{
   alf_wb_create(task_handle, ALF_WB_SINGLE, 0, &wb_handle); 
   alf_wb_dtl_begin(wb_handle, ALF_BUF_IN, 0); /* input */ 
   alf_wb_dtl_entry_add(wb_handle, pcm16_in+i, PART_SIZE, ALF_DATA_INT16); 
   alf_wb_dtl_end(wb_handle); 
   alf_wb_dtl_begin(wb_handle, ALF_BUF_OUT, 0); /* output */ 
   alf_wb_dtl_entry_add(wb_handle, pcm8_out+i,PART_SIZE, ALF_DATA_BYTE); 
   alf_wb_dtl_end(wb_handle); 
   wb_parm.num_data = PART_SIZE; 
   alf_wb_parm_add(wb_handle, (void *)&wb_parm, /* wb parm */ 
   sizeof(wb_parm)/sizeof(unsigned int), ALF_DATA_INT32, 0); 
   alf_wb_enqueue(wb_handle); 
}


Creating the accelerator code

The following code is the accelerator side code. The section of the code that modifies the task context is marked in bold font.

/* ---------------------------------------------- */ 
/* the accelerator side code */ 
/* ---------------------------------------------- */ 
/* the computation kernel function */ 
int comp_kernel(void *p_task_context, void *p_parm_ctx_buffer, 
                    void *p_input_buffer, void *p_output_buffer, 
                    void *p_inout_buffer, 
                    unsigned int current_count, unsigned int total_count) 
{ 
    my_task_context_t *p_ctx = (my_task_context_t *) p_task_context; 
    my_wb_parms_t *p_parm = (my_wb_parms_t *) p_parm_ctx_buffer; 

    alf_data_int16_t *in = (alf_data_int16_t *)p_input_buffer; 
    alf_data_byte_t *out = (alf_data_byte_t *)p_output_buffer; 
    unsigned int size = p_parm->num_data; 
    unsigned int i; 

    // it is just a simple table lookup 
    for(i=0;i<size;i++) 
    { 
       out[i] = p_ctx->table[(unsigned short)in[i]];
    } 
    return 0; 
}


Conclusion

You're finished. It was not difficult to use the ALF task context buffer as a large lookup table to convert the 16-bit input data to 8-bit output data


Resources

Learn

Get products and technologies

Discuss

About the author

Kane Scarlett

Kane Scarlett is a technology journalist/analyst with 20 years in the business, working for such publishers as National Geographic, Population Reference Bureau, Miller Freeman, and IDG, and managing, editing, and writing for such august journals as JavaWorld, LinuxWorld, and of course, developerWorks.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Multicore acceleration
ArticleID=297090
ArticleTitle=Fun with ALF, Part 2: Converting I/O data
publish-date=03252008
author1-email=kane@us.ibm.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers