The SPE Runtime Management Library (libspe) is the standardized low-level application programming interface (API) that enables application access to the Cell BE SPEs. This library provides an API that is neutral with respect to the underlying operating system and its methods to manage SPEs.
In general, applications do not have control over the physical SPE system resources. The operating system manages these resources. Applications manage and use software constructs called SPE contexts. These SPE contexts are a logical representation of an SPE and are the base object on which libspe operates. The operating system schedules SPE contexts from all running applications onto the physical SPE resources in the system for execution according to the scheduling priorities and policies associated with the runable SPE contexts.
libspe also provides the means for communication and data transfer between PPE threads and SPEs.
Many applications need to use multiple SPEs concurrently. In this case, the application must create at least as many threads as concurrent SPE contexts are required. Each of these threads may run a single SPE context at a time. If N concurrent SPE contexts are needed, it is common to have a main application thread plus N threads dedicated to SPE context execution.
Other schemes are also possible and, depending on the application, potentially more suitable.
For a full list of terms, see Glossary
The following example shows how to load and run a simple SPE executable "hello".
#include <errno.h> #include <stdio.h> #include <stdlib.h> #include "libspe2.h" int main(void) { spe_context_ptr_t ctx; int flags = 0; unsigned int entry = SPE_DEFAULT_ENTRY; void * argp = NULL; void * envp = NULL; spe_program_handle_t * program; spe_stop_info_t stop_info; int rc; program = spe_image_open("hello"); if (!program) { perror("spe_open_image"); return -1; } ctx = spe_context_create(flags, NULL); if (ctx == NULL) { perror("spe_context_create"); return -2; } if (spe_program_load(ctx, program)) { perror("spe_program_load"); return -3; } rc = spe_context_run(ctx, &entry, 0, argp, envp, &stop_info); if (rc < 0) perror("spe_context_run"); spe_context_destroy(ctx); return 0; }
The following simple multi-threaded example shows how an application can run the SPE program "hello" on multiple SPEs concurrently:
#include <stdlib.h> #include <pthread.h> #include "libspe2.h" struct thread_args { struct spe_context * ctx; void * argp; void * envp; }; void * spe_thread(void * arg) { int flags = 0; unsigned int entry = SPE_DEFAULT_ENTRY; spe_program_handle_t * program; struct thread_args * arg_ptr; arg_ptr = (struct thread_args *) arg; program = spe_image_open("hello"); spe_program_load(arg_ptr->ctx, program); spe_context_run(arg_ptr->ctx, &entry, flags, arg_ptr->argp, arg_ptr->envp, NULL); pthread_exit(NULL); } int main() { int thread_id; pthread_t pts; spe_context_ptr_t ctx; struct thread_args t_args; int value = 1; ctx = spe_context_create(0, NULL); t_args.ctx = ctx; t_args.argp = &value; thread_id = pthread_create( &pts, NULL, &spe_thread, &t_args); pthread_join (pts, NULL); spe_context_destroy (ctx); return 0; }