pthread PowerPoint Slides

Download Report

Transcript pthread PowerPoint Slides

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!
Outline
• Programming Model
– Threaded Model
– Threads Overview
– pthreads
• Syntax by Example
– Expressing parallelism
– Synchronization
(C) 2005
CS 838
2
Parallel Programming (Review)
• Multiple instruction streams working cooperatively at
the same time
• Components
– Communication
– Synchronization
(C) 2005
CS 838
3
MPI (Review)
• Per-processor private address space
• Communication
– Explicit
– Pass messages
• Synchronization
– Implicit
– Message receive
(C) 2005
CS 838
4
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?
(C) 2005
CS 838
5
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
(C) 2005
CS 838
6
Outline
• Programming Model
– Threaded Model
– Threads Overview
– pthreads
• Syntax by Example
– Expressing parallelism
– Synchronization
(C) 2005
CS 838
7
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
(C) 2005
CS 838
8
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
(C) 2005
CS 838
9
Process vs. Thread
Courtesy of Lawrence Livermore National Lab
http://www.llnl.gov/computing/tutorials/pthreads/
(C) 2005
CS 838
10
Outline
• Programming Model
– Threaded Model
– Threads Overview
– pthreads
• Syntax by Example
– Expressing parallelism
– Synchronization
(C) 2005
CS 838
11
pthreads
•
•
•
•
Posix threads
IEEE POSIX 1003.1c standard
Implemented via a library
Portable to many systems
(C) 2005
CS 838
12
Outline
• Programming Model
– Threaded Model
– Threads Overview
– pthreads
• Syntax by Example
– Expressing parallelism
– Synchronization
(C) 2005
CS 838
13
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) {};
}
(C) 2005
CS 838
14
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
(C) 2005
CS 838
15