Professional Documents
Culture Documents
Objectives
At the end of this module you should be able to:
Describe the shared-memory model of parallel
programming
Describe the differences between the fork/join
model and the general threads model
Demonstrate how to implement domain and
functional decompositions using threads
Decide whether a variable in a multithreaded
program should be shared or private
CPU
CPU
Private
Memory
Private
Memory
Private
Memory
Shared Memory
Methodology
Study problem, sequential program, or code segment
Look for opportunities for parallelism
Use threads to express parallelism
What Is a Process?
A program in some state of execution
Code
Data
Logical address space
What Is a Thread?
A unit of control within a process Carver and Tai
Main thread executes programs main function
Main thread may create other threads to execute
other functions
Threads have own program counter, copy of CPU
registers, and stack of activation records
Threads share processs data, code, address space,
and other resources
Threads have lower overhead than processes
8
11
12
Join
Fork
Join
Other Threads
Fork
Master
Thread
13
Sequential code
for {
Parallel code
}
Sequential code
14
15
General model
More flexible
Better support for functional decompositions
16
Incremental Parallelization
Sequential program a special case of threaded
program
Utility of Threads
Threads are flexible enough to implement
Domain decomposition
Functional decomposition
Pipelining
18
Thread 2
f()
f()
Shared
Memory
Thread 1
f()
19
Thread 1
Shared
Memory
h()
20
f()
g()
Thread 1
Thread 2
f()
g()
Data
set 3
Data
set 2
Input
Data sets
5, 6, ...
21
Data
set 1
Output
Shared Memory
Private
Variables
Shared
Variables
Private
Variables
Thread
22
Domain Decomposition
Sequential Code:
int a[1000], i;
for (i = 0; i < 1000; i++) a[i] = foo(i);
23
Domain Decomposition
Sequential Code:
int a[1000], i;
for (i = 0; i < 1000; i++) a[i] = foo(i);
Thread 0:
24
Domain Decomposition
Sequential Code:
int a[1000], i;
for (i = 0; i < 1000; i++) a[i] = foo(i);
Thread 0:
Shared
Functional Decomposition
int e;
main () {
int x[10], j, k, m;
}
26
return a;
a = e / x[k];
return a;
Functional Decomposition
int e;
main () {
int x[10], j, k, m;
}
j = f(x, k);
27
m = g(x, k);
Thread 0
return a;
Thread 1
a = e / x[k];
return a;
Functional Decomposition
volatile int e; Static variable: Shared
main () {
int x[10], j, k, m;
}
j = f(x, k);
28
m = g(x, k);
Thread 0
return a;
Thread 1
a = e / x[k];
return a;
Functional Decomposition
volatile int e;
main () {
int x[10], j, k, m;
}
j = f(x, k);
29
m = g(x, k);
Thread 0
return a;
Thread 1
a = e / x[k];
return a;
Functional Decomposition
volatile int e;
main () {
int x[10], j, k, m;
j = f(x, k); m = g(x. k);
}
Functions local variables: Private
int f(int *x, int k)
{
int a;
a = e * x[k] * x[k];
}
int g(int *x, int k)
{
int a;
k = k-1;
}
30
Thread 0
return a;
Thread 1
a = e / x[k];
return a;
Private variables
Loop index variables
Run-time stack of functions invoked by thread
31
CPU
CPU
Private
Memory
Private
Memory
Private
Memory
Shared Memory
32
Thread
Private
Variables
Private
Variables
Thread
Private
Variables
Shared Variables
33
References
Jim Beveridge and Robert Wiener, Multithreading Applications in
Win32, Addison-Wesley (1997).
34
35