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
|
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)); |
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);
}
|
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;
}
|
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
Learn
- Use an
RSS
feed to request notification for the upcoming articles in this series. (Find out more about RSS feeds of developerWorks content.)
- Refer to Accelerated Library Framework for Cell Broadband Engine ProgrammerâÂÂs Guide and API Reference for the source material from which this article was extracted.
- Check out other articles in this Fun
with ALF series.
- Take a look at these other ALF-related
quick-read guides:
- "Introducing ALF."
- "10 major ALF concepts."
- "Programming with ALF: Basic ALF application structure."
- "Programming with ALF: Double buffering."
- "Programming with ALF: Handling ALF constraints."
- "Programming with ALF: Optimizing ALF applications."
- "ALF and hybrid x86."
- To learn more on Cell/B.E. programming, try the
developerWorks series:
- "Programming high-performance applications on the Cell/B.E. processor"
- "PS3 fab-to-lab"
- "The little broadband engine that could"
- Refer to the Cell
Broadband Engine documentation section of the IBM Semiconductor Solutions Technical Library for a wealth of downloadable manuals,
specifications, and more.
- Sign up for the developerWorks newsletter
and get the latest developer news and Cell/B.E. happenings delivered to your inbox each week.
Check Power Architecture® when you sign up to receive Cell/B.E. news in your newsletter.
Get products and technologies
- Find all Cell/B.E.-related articles, discussion forums, downloads,
and more at the IBM developerWorks Cell
Broadband Engine resource center: your definitive resource for all
things Cell/B.E.
- Contact IBM about custom
Cell/B.E.-based or custom-processor based solutions.
- Get your copy of the
IBM SDK for Multicore Acceleration 3.0
or browse through the exhaustive
library of Cell/B.E. documentation.
Discuss
- Participate in the discussion forum.
- Check out the Cell Broadband
Engine Architecture forum to get your technical questions about the processor answered.
Juicy problems and answers from the forums are rounded up periodically and highlighted
in the "Forum watch" blog series.
- Go to the Power Architecture blog for news, downloads,
instructional resources, and event notifications for Cell/B.E. and other Power Architecture-related technologies. You can find
the popular "Forum watch" blog series (Q&A roundup), the "FixIt" technology updates, and the Infobomb quick-read
technology introductions.

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)




