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