Pthreads High Performance Computing PDF
Document Details
Uploaded by CoherentYtterbium
Instituto Politécnico do Cávado e do Ave
Nuno Lopes
Tags
Summary
These lecture notes cover Pthreads, a library for developing parallel applications using shared memory. The notes include explanations of Pthreads functions, such as creation, joining and mutual exclusion, and an example of an incremental application. They also include exercises on applying Pthreads.
Full Transcript
Pthreads High Performance Computing Master in Applied Artificial Intelligence Nuno Lopes Motivation u Library for developing parallel applications using shared memory. u Assumes a POSIX-compliant operating system as its base. u Library ca...
Pthreads High Performance Computing Master in Applied Artificial Intelligence Nuno Lopes Motivation u Library for developing parallel applications using shared memory. u Assumes a POSIX-compliant operating system as its base. u Library can be embedded in any programming language, usually C. u The use of threads is achieved by invoking functions from the library. Compilation and Execution of Pthreads Programs u Include library headers: u #include u Linker option: u -lpthread u gcc - lpthread hello.c -o hello Pthread API to create and join threads pthread_create( pthread_t* thread_p const pthread_attr_t* attr_p void* (*start_routine)(void*) void* arg_p ); u thread_p: thread object reference, u attr_p: creation attributes, NULL u start_routine: function to execute u arg_p: function argument u Generic function header: void* start_routine(void* args_p); Pthread API to create and join threads pthread_join( pthread_t thread , void** ret_val_p ); Example Incremental Application // global vars long long n; // set to number of iterations long long thread_count; // set to number threads long long sum;// global sum value // thread operation void* Increment(void* rank) { long my_rank = (long) rank; long long my_n = n/thread count; // even division long long my_first_i = my_n * my_rank; long long my_last_i = my_first_i + my_n; printf(“Thread %ld range: %ld to %ld\n”, rank, my_first_i, my_last_i); for( i=my_first_i; i