Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references.

Similar presentations


Presentation on theme: "CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references."— Presentation transcript:

1 CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references from Lawrence Livermore National Laboratory as well as CS 757 notes created by Mark Hill and Min Xu Thanks!

2 2 (C) 2005 CS 838 Outline Programming Model –Threaded Model –Threads Overview –pthreads Syntax by Example –Expressing parallelism –Synchronization

3 3 (C) 2005 CS 838 Parallel Programming (Review) Multiple instruction streams working cooperatively at the same time Components –Communication –Synchronization

4 4 (C) 2005 CS 838 MPI (Review) Per-processor private address space Communication –Explicit –Pass messages Synchronization –Implicit –Message receive

5 5 (C) 2005 CS 838 Contrast with pthreads Shared memory: single, shared address space Communication –Implicit –A write to a shared address by any thread is immediately visible to all threads Synchronization –Explicit –Why?

6 6 (C) 2005 CS 838 Races Example –Initial value of a = 0 –T1: a = 1 –T2: a = 2 –T3: printf(“%d”, a); –Output: ??? Strategies to combat (details soon) –Locks (mutexes) –Condition variables (CVs) –Barriers

7 7 (C) 2005 CS 838 Outline Programming Model –Threaded Model –Threads Overview –pthreads Syntax by Example –Expressing parallelism –Synchronization

8 8 (C) 2005 CS 838 Thread Stream of instructions that OS can schedule to run independently Think of procedure that runs independently from / concurrently with main program Lets you run many procedures (even many incarnations of the same one) at the same time Each one has its own, independent control flow

9 9 (C) 2005 CS 838 System View Unix process –Created by OS with a fair amount of overhead –Contains info about program resources & execution state »pid, gid, uid, environment, working dir, instructions, registers, stack, heap, file descriptor, signal actions, shared libraries, IPC tools… Threads –Multiple can belong to the same Unix process –All share process resources –Lightweight »Only duplicate enough state to run independently: Stack pointer, registers, scheduling properties, pending & blocked signals, thread-specific data

10 10 (C) 2005 CS 838 Process vs. Thread Courtesy of Lawrence Livermore National Lab http://www.llnl.gov/computing/tutorials/pthreads/

11 11 (C) 2005 CS 838 Outline Programming Model –Threaded Model –Threads Overview –pthreads Syntax by Example –Expressing parallelism –Synchronization

12 12 (C) 2005 CS 838 pthreads Posix threads IEEE POSIX 1003.1c standard Implemented via a library Portable to many systems

13 13 (C) 2005 CS 838 Outline Programming Model –Threaded Model –Threads Overview –pthreads Syntax by Example –Expressing parallelism –Synchronization

14 14 (C) 2005 CS 838 Barrier With Sense Reversal BARRIER(bar_name, p) { /* toggle private state */ local_sense = !(local_sense); LOCK(bar_name.lock); bar_name.counter++; UNLOCK(bar_name.lock); if (bar_name.counter == p) { bar_name.counter = 0; bar_name.flag = local_sense; } else { /* busy wait */ while(bar_name.flag != local_sense) {}; }

15 15 (C) 2005 CS 838 Summary pthreads is a library for threaded programming Write programs with pthreads –#include –man pthreads for a list of functions »Thread creation »Synchronization Lock / mutex Condition variable Barrier »Thread termination –Compile: cc –mt –lpthread program.c Review example in http://www.cs.wisc.edu/~markhill/cs838/Fall2005/handouts/ eg_pthread.tar.gz


Download ppt "CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references."

Similar presentations


Ads by Google