TAILIEUCHUNG - Parallel Programming: for Multicore and Cluster Systems- P30

Parallel Programming: for Multicore and Cluster Systems- P30: Innovations in hardware architecture, like hyper-threading or multicore processors, mean that parallel computing resources are available for inexpensive desktop computers. In only a few years, many standard software products will be based on concepts of parallel programming implemented on such hardware, and the range of applications will be much broader than that of scientific computing, up to now the main application area for parallel computing | 282 6 Thread Programming int pipe_send stage_t nstage long data parameter target stage and data element to be processed pthread_mutex_lock nstage- m while nstage- data_ready pthread_cond_wait nstage- ready nstage- m nstage- data data nstage- data_ready 1 pthread_cond_signal nstage- avail pthread_mutex_unlock nstage- m void pipe_stage void arg stage stage_t arg long result-data pthread_mutex_lock stage- m for while stage- data_ready wait for data pthread_cond_wait stage- avail stage- m process data element and forward to next stage result-data stage- data 1 compute result data element pipe_send stage- next result-data stage- datarready 0 processing finished pthread_cond_signal stage- ready pthread_mutex_unlock stage- m this unlock will never be reached Fig. Implementation of a pipeline part 2 functions to forward data elements to a pipeline stage and thread functions for the pipeline stages The function pipe_send shown in Fig. is used to send a data element to a stage of the pipeline. This function is used to send a data element to the first stage of the pipeline and it is also used to pass a data element to the next stage of the pipeline after the computation of a stage has been completed. The stage receiving the data element is identified by the parameter nstage. Before inserting the data element the mutex variable m of the receiving stage is locked to ensure that only one thread at a time is accessing the stage. A data element can be written into the receiving stage only if the computation of the previous data element in this stage has been finished. This is indicated by the condition 0. If this is not the case the sending thread is blocked on the condition variable ready of the receiving stage. If the receiving stage is ready to receive the data element the sending thread writes the element into the stage and wakes up the thread of the receiving stage if it is blocked on the condition variable avail. Programming with Pthreads 283 .