Threads in C: David Chisnall
Threads in C: David Chisnall
Threads in C
David Chisnall
One computer
One process
One thread
One stack
Revision Thread Portability Creating and Joining Threads Parallel Quicksort
Heap Memory
0x00140000
Library Code
0x00100000
0x000fe000
Static Data
0x0000f000
Program Code
0x00000000 0x00001000
Revision Thread Portability Creating and Joining Threads Parallel Quicksort
Thread APIs
Creating Threads
pthread_t thread ;
pthread_create (& thread , NULL , start_function ,
arg ) ;
Combining Threads
void * ret ;
pthread_join ( thread , & ret ) ;
void example ( void ) { printf ( " Stuff goes here \ n " )
; }
void user ( void )
{
// Call example
example () ;
// Store a pointer to example :
void (* funcPtr ) ( void ) = example ;
// Call example via the function pointer
funcPtr () ;
}
Revision Thread Portability Creating and Joining Threads Parallel Quicksort
Thread Overhead
Revision: Quicksort
Serial Quicksort
void quicksort ( int * array , int left , int right )
{
if ( right > left )
{
int pivotIndex = left + ( right - left ) /2;
pivotIndex = partition ( array , left , right ,
pivotIndex ) ;
quicksort ( array , left , pivotIndex -1) ;
quicksort ( array , pivotIndex +1 , right ) ;
}
}
Revision Thread Portability Creating and Joining Threads Parallel Quicksort
Making it Parallel
Recursive Quicksort
void quicksort ( int * array , int left , int right )
{
if ( right > left )
{
int pivotIndex = left + ( right - left ) /2;
pivotIndex = partition ( array , left , right ,
pivotIndex ) ;
struct qsort_starter arg = { array , left ,
pivotIndex -2};
pthread_t thread ;
// Create a new thread for one subsort
pthread_create (& thread , 0 , qsthread , & arg ) ;
quicksort ( array , pivotIndex +1 , right ) ;
// Wait for both to finish
pthread_join ( thread , NULL ) ;
}
}
Revision Thread Portability Creating and Joining Threads Parallel Quicksort
Is this Safe?
struct qsort_starter arg = { array , left ,
pivotIndex -2};
pthread_t thread ;
// Create a new thread for one subsort
pthread_create (& thread , 0 , qsthread , & arg ) ;
Testing Speedup
$ time ./a.out
real 0m30.792s
user 0m30.552s
sys 0m0.222s
Multithreading Performance
35
Wall Clock Time
CPU Time
30
25
20
Seconds
15
10
0
0 2 4 6 8 10 12
Depth
Revision Thread Portability Creating and Joining Threads Parallel Quicksort
Speedup
T1
Sp =
Tp
T1 time for sequential algorithm
Tp time for parallel algorithm with p processors
Sp speedup with p processors
Efficiency
Sp
Ep =
p
Threads p Tp Sp Ep
1 1 30.792 1 1
2 2 24.044 1.28 0.64
4 2 23.780 1.29 0.65
8 2 24.548 1.25 0.63
16 2 18.784 1.63 0.82
Linear speedup: Sp = p, Ep = 1
Superlinear speedup (very rare!) Ep > 1
Revision Thread Portability Creating and Joining Threads Parallel Quicksort
Why Sublinear?
Amdahls Law
Questions?