cs 838: pervasive parallelism introduction to pthreads copyright 2005 mark d. hill university of...
TRANSCRIPT
![Page 1: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/1.jpg)
CS 838: Pervasive Parallelism
Introduction to pthreads
Copyright 2005 Mark D. HillUniversity of Wisconsin-Madison
Slides are derived from online references fromLawrence Livermore National Laboratory as well as CS
757 notes created by Mark Hill and Min XuThanks!
![Page 2: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/2.jpg)
CS 838 2(C) 2005
Outline
• Programming Model– Threaded Model
– Threads Overview
– pthreads
• Syntax by Example– Expressing parallelism
– Synchronization
![Page 3: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/3.jpg)
CS 838 3(C) 2005
Parallel Programming (Review)
• Multiple instruction streams working cooperatively at the same time
• Components– Communication
– Synchronization
![Page 4: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/4.jpg)
CS 838 4(C) 2005
MPI (Review)
• Per-processor private address space
• Communication– Explicit
– Pass messages
• Synchronization– Implicit
– Message receive
![Page 5: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/5.jpg)
CS 838 5(C) 2005
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?
![Page 6: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/6.jpg)
CS 838 6(C) 2005
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
![Page 7: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/7.jpg)
CS 838 7(C) 2005
Outline
• Programming Model– Threaded Model
– Threads Overview
– pthreads
• Syntax by Example– Expressing parallelism
– Synchronization
![Page 8: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/8.jpg)
CS 838 8(C) 2005
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
![Page 9: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/9.jpg)
CS 838 9(C) 2005
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
![Page 10: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/10.jpg)
CS 838 10(C) 2005
Process vs. Thread
Courtesy of Lawrence Livermore National Lab http://www.llnl.gov/computing/tutorials/pthreads/
![Page 11: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/11.jpg)
CS 838 11(C) 2005
Outline
• Programming Model– Threaded Model
– Threads Overview
– pthreads
• Syntax by Example– Expressing parallelism
– Synchronization
![Page 12: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/12.jpg)
CS 838 12(C) 2005
pthreads
• Posix threads
• IEEE POSIX 1003.1c standard
• Implemented via a library
• Portable to many systems
![Page 13: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/13.jpg)
CS 838 13(C) 2005
Outline
• Programming Model– Threaded Model– Threads Overview– pthreads
• Syntax by Example– Expressing parallelism– Synchronization
![Page 14: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/14.jpg)
CS 838 14(C) 2005
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) {};}
![Page 15: CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references](https://reader036.vdocuments.us/reader036/viewer/2022082818/5697bfc41a28abf838ca5eeb/html5/thumbnails/15.jpg)
CS 838 15(C) 2005
Summary
• pthreads is a library for threaded programming
• Write programs with pthreads– #include <pthread.h>– 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