Đang chuẩn bị liên kết để tải về tài liệu:
Parallel Programming: for Multicore and Cluster Systems- P28

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Parallel Programming: for Multicore and Cluster Systems- P28: 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 | 262 6 Thread Programming include pthread.h typedef struct int size row column double MA 8 MB 8 MC 8 matrix_type_t void thread_mult void w matrix_type_t work matrix-type_t w int i row work- row column work- column work - MC row column 0 for i 0 i work- size i work- MC row column work- MA row i work- MB i column return NULL int main int row column size 8 i double MA 8 8 MB 8 8 MC 8 8 matrix_type_t work pthread_t thread 8 8 for row 0 row size row for column 0 column size column work matrix_type_t malloc sizeof matrix_type_t work- size size work- row row work- column column work- MA MA work- MB MB work- MC MC pthread_create thread column row 8 NULL thread_mult void work for i 0 i size size i pthread_join thread i NULL Fig. 6.1 Pthreads program for the multiplication of two matrices MA and MB. A separate thread is created for each element of the output matrix MC. A separate data structure work is provided for each of the threads created been set into a detached state calling pthread_join for this thread returns the error value EINVAL. Example We give a first example for a Pthreads program Fig. 6.1 shows a program fragment for the multiplication of two matrices see also 126 . The matrices MA and MB to be multiplied have a fixed size of eight rows and eight columns. For each of the elements of the result matrix MC a separate thread is created. The IDs of these threads are stored in the array thread. Each thread obtains a separate data structure of type matrix_type_t which contains pointers to the input matrices MA and MB the output matrix MC and the row and column position of the entry of MC to be computed by the corresponding thread. Each thread executes the same thread function thread_mult which computes the scalar product of one row of MA and one column of MB. After creating a new thread for each of the 64 elements 6.1 Programming with Pthreads 263 of MC to be computed the main thread waits for the termination of each of these threads using pthread_join . The program .