Distribute Computing Another Slide
Distribute Computing Another Slide
Programming with
Shared Memory
Text Books:
Parallel Programming ‐ Techniques and applications Using Networke
d Workstations and Parallel
Computers (2nd Edition), Barry Wilkinson and Michael Allen, Prenti
ce Hall, 2001.
1
Syllabus Topics
slides8-2
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-3
Bus
Caches
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-4
Shared memory
multiprocessor using a
crossbar switch
Memory modules
Caches
Switch
Bus
Processors
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-5
Using:
• Using heavy weight processes.
• Using threads. Example Pthreads
• Using a completely new programming language for
parallel programming - not popular. Example Ada.
• Using library routines with an existing sequential
programming language.
• Modifying the syntax of an existing sequential
programming language to create a parallel programing
language. Example UPC
• Using an existing sequential programming language
supplemented with compiler directives for
specifying parallelism. Example OpenMP
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-6
FORK-JOIN construct
Main program
FORK
S
p
a
w
n
e
d
FORK
p
r
JOIN o JOIN
c
JOIN e JOIN
s
s
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-8
SPMD model
.
pid = fork(); /* fork */
Code to be executed by both child and parent
if (pid == 0) exit(0); else wait(0)/;* join */
.
.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-9
pid = fork();
if (pid ==
0) {
code to be
executed by
slave
.
} else {
.
Code to be
executed by
parent
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-10
Stack Thread
IP
Threads - shares the same
memory space and global (b) Threads Interrupt routines
Stack Thread
variables between routines. Files
IP
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-11
Pthreads
IEEE Operating System Interface, POSIX, sec. 1003.1
Portable
standard
Executing a Pthread Thread
Main program
thread1
proc1(&arg)
{
pthread_create(&thread1, NULL, proc1, &arg);
eturn(*stat
us);
pthr
ead_join(thread1, *status);
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-12
Detached Threads
It may be that thread are not bothered when a thread it creates
terminates and then a join not needed.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-13
Parameter (attribute)
specifies a detached thread
Main program
pthread_create(); Thread
pthread_create();
Thread
pthread_create();
Thread
Termination
Termination
Termination
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-14
Instruction 1.1
Instruction 1.2
Instruction 2.1
Instruction 1.3
Instruction 2.2
Instruction 2.3
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-16
Compiler/Processor Optimations
Compiler and processor reorder instructions for optimization.
Example
The statements
a = b + 5;
x = y +
4;
x = y + 4;
a = b +
5;
Thread-Safe Routines
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-18
x = x + 1; read x read x
Time
compute x + 1 compute x + 1
write to x write to x
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-19
Shared variable, x
Write Write
Read Read
+1 +1
Process 1 Process 2
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-20
Critical Section
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-21
Locks
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-22
Critical section
lock = 0;
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-23
If a thread reaches a mutex lock and finds it locked, it will wait for
the lock to open. If more than one thread is waiting for the lock to
open when it opens, the system will select one thread to be allowed
to proceed. Only the thread that locks a mutex can unlock it.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-24
Deadlock
Can occur with two processes when one requires a resource held
by the other, and this process requires a resource held by the first
process.
R1 R2 Resource
Two-process deadlock
P1 P2 Process
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-25
R1 R2 Rn-1 Rn
P1 P2 Pn-1 Pn
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-26
Pthreads
Offers one routine that can test whether a lock is actually closed
without blocking the thread:
pthread_mutex_trylock()
Will lock an unlocked mutex and return 0 or will return with EBUSY
if the mutex is already locked – might find a use in overcoming
deadlock.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-27
Semaphores
P operation on semaphore s
Waits until s is greater than zero and then decrements s by one and
allows the process to continue.
V operation on semaphore s
Increments s by one and releases one of the waiting processes (if
any).
Devised by Dijkstra in 1968. Letter P is from the Dutch word passeren, meaning
“to
Slides pass,”
for Parallel and
Programming letter
Techniques V isUsing
& Applications from the
Networked Dutch
Workstations word
& Parallel vrijgeven,
Computers meaning
2nd ed., by B. Wilkinson & M. Allen, “to
2004 release.”)
Pearson Education Inc. All rights
slides8-28
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-29
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-30
Monitor
monitor_proc1()
{
lock(x);
.
monitor body
.
unlock(x);
return;
}
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-31
Condition Variables
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-32
action() counter()
{ {
. .
. .
pthread_mutex_lock(&mutex1); pthread_mutex_lock(&mutex1);
while (c <> 0) c--;
pthread_cond_wait(cond1,mutex1); if (c == 0)
pthread_mutex_unlock(&mutex1); pthread_cond_signal(cond1);
take_action(); pthread_mutex_unlock(&mutex1);
. .
. .
} .
}
shared int x;
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-34
par
Construct
par
{
S1;
S2;
.
.
Sn;
}
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-35
forall
Construct
To start multiple similar processes together:
forall (i = 0; i < n; i++) {
S1;
S2;
.
.
Sm;
}
which generates n processes each consisting of the statements
forming the body of the for loop, S1, S2, …, Sm. Each process
uses a different value of i.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-36
Example
forall (i = 0; i < 5; i++)
a[i] = 0;
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-37
Dependency Analysis
To identify which processes could be executed together.
Example
Can see immediately in the code
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-38
Bernstein's Conditions
Set of conditions sufficient to determine whether two processes can be
executed simultaneously. Given:
Ii is the set of memory locations read (input) by process Pi.
Oj is the set of memory locations written (output) by process Pj.
For two processes P1 and P2 to be executed simultaneously, inputs to
process P1 must not be part of outputs of P2, and inputs of P2 must not
be part of outputs of P1; i.e.,
I 1 O2 =
I 2 O1 =
where is an empty set. Set of outputs of each process must also be
different; i.e.,
O1 O2 =
If the three conditions are all satisfied, the two processes can be
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-39
Example
Suppose the two statements are (in C)
a = x + y;
b = x +
z;
We have
I 1 = (x, y) O1 = (a)
I 2 = (x, z) O2 = (b)
and the conditions
I1
O2 =
I2
are satisfied.
O1 = Hence, the statements a = x + y and b = x + z
can be executed simultaneously.
O1
O2 =
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-40
OpenMP
An accepted standard developed in the late 1990s by a group of
industry specialists.
The compiler directives can specify such things as the par and
forall operations described previously.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-41
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-42
Parallel Directive
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-44
Example
#pragma omp parallel private(x, num_threads)
{
x = omp_get_thread_num();
num_threads = omp_get_num_threads();
a[x] = 10*num_threads;
}
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-45
Established by either:
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-46
Work-Sharing
Note that these constructs do not start a new team of threads. That
done by an enclosing parallelconstruct.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-47
Sections
The construct
#pragma omp sections
{
#pragma omp section
structured_block
#pragma omp section
structured_block
.
.
.
}
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-48
For Loop
#pragma omp for
for_loop
causes the for loop to be divided into parts and parts shared among
threads in the team. The for loop must be of a simple form.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-49
Single
The directive
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-50
with similar effects, i.e. it has the effect of each thread executing
the same for loop.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-51
Master Directive
structured_block
Synchronization Constructs
Critical
The critical directive will only allow one thread execute the
associated structured block. When one or more threads reach the
criticaldirective:
they will wait until no other thread is executing the same critical
section (one with the same name), and then one thread will
proceed to execute the structured block. name is optional.
All critical sections with no name map to one undefined name.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-54
Barrier
When a thread reaches the barrier
it waits until all threads have reached the barrier and then they all
proceed together.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-55
Atomic
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-56
Flush
A synchronization point which causes thread to have a “consistent”
view of certain or all shared variables in memory. All current read
and write operations on the variables allowed to complete and
values written back to memory but any memory operations in the
code after flush are not started, thereby creating a “memory fence”.
Format:
parallel and
criticaldirectives (and combined parallel forand parallel
sections directives), and at the exit of for, sections, and
single (if a nowait clause is not present).
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-57
Ordered
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-58
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-59
Update policy - copies of data in all caches are updated at the time
one copy is altered.
Invalidate policy - when one copy of data is altered, the same data
in any other cache is invalidated (by resetting a valid bit in the
cache). These copies are only updated when the associated
processor makes reference for it.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-60
Main memory
False Sharing
7
6
Different parts of block 5
4
Block
required by different 3
2
1
processors but not 0
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-61
Compiler to alter the layout of the data stored in the main memory,
separating data only altered by one processor into different blocks.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-62
They will execute their critical sections one after the other.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-63
Illustration
Process 1 Process 2 Process 3
Critical section
tcrit waiting
Unlock() waiting
Critical section
Unlock()
tcomp
Critical section
Unlock()
lock()
next critical section
When tcomp < ptcrit, less than p processor will be active
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-64
Sequential Consistency
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-65
Sequential Consistency
Processor (programs)
x = 1; . .
. a = x; .
. . b = x.
y=x . .
+ 3; . .
. . .
. . .
z=x . .
+ y; . .
.
. Any order
x, y, z
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-66
Process P1 Process 2
. .
data = new; .
flag = TRUE; .
. .
. while (flag != TRUE) { };
. data_copy = data;
. .
Program Order
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-68
Compiler Optimizations
The order is not necessarily the same as the order of the
corresponding high level statements in the source program as a
compiler may reorder statements for improved performance. In this
case, the term program order will depend upon context, either the
order in the souce program or the order in the compiled machine
instructions.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-69
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-70
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-71
Process P1 Process 2
. .
new = a * b; .
flag = TRUE; .
data = new; .
. .
. while (flag != TRUE) { };
. data_copy = data;
. .
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-72
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-73
Examples
Alpha processors
Memory barrier (MB) instruction - waits for all previously issued
memory accesses instructions to complete before issuing any new
memory operations.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-74
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-75
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-76
Program Examples
To sum the elements of an array, a[1000]:
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-77
UNIX Processes
Calculation will be divided into two parts, one doing even i and one
doing odd i; i.e.,
Process 1 Process 2
sum1 = 0; sum2 = 0;
for (i = 0; i < 1000; i = i + 2) for (i = 1; i < 1000; i = i + 2)
sum1 = sum1 + a[i]; sum2 = sum2 + a[i];
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-78
sum
Array a[]
addr
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-79
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <stdio.h>
#include <errno.h>
#define array_size 1000 /* no of elements in shared memory */
extern char *shmat();
void P(int *s);
void V(int *s);
int main()
{
int shmid, s, pid; /* shared memory, semaphore, proc id */
char *shm; /*shared mem. addr returned by shmat()*/
int *a, *addr, *sum; /* shared data variables*/
int partial_sum; /* partial sum of each process */
int i;
/* initialize semaphore set */
int init_sem_value = 1;
s = semget(IPC_PRIVATE, 1, (0600 | IPC_CREAT))
if (s == -1) /* if unsuccessful*/
{ perror("semget
"); exit(1);
}
if (semctl(s, 0, SETVAL, init_sem_value) < 0) {
perror("semctl");
exit(1);
}
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-80
/* create segment*/
shmid = shmget(IPC_PRIVATE,(array_size*sizeof(int)+1),
(IPC_CREAT|0600));
if (shmid == -1) {
perror("shmget");
exit(1);
}
/
*
m
a
p
s
e
g
m
e
n
t
t
o
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & p
Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-81
*sum = 0;
for (i = 0; i < array_size; i++) /* load array with numbers */
*(a + i) = i+1;
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-82
/* remove semaphore */
if (semctl(s, 0, IPC_RMID, 1) == -1) {
perror("semctl");
exit(1);
}
/
if (shmctl(shmid, IPC_RMID, NULL) == -1) { *
perror("shmctl");
exit(1); r
e
}
m
} /*
o end of main */
v
e
s
h
a
r
e
d
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-84
SAMPLE OUTPUT
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-85
Pthreads Example
n threads created, each taking numbers from list to add to their
sums. When all numbers taken, threads can add their partial results
to a shared location sum.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-86
global_index sum
Array a[]
addr
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-87
#include <stdio.h>
#include <pthread.h>
#define array_size 1000
#define no_threads 10
/* shared data */
int a[array_size]; /* array of numbers to sum */
int global_index = 0; /* global index */
int sum = 0; /* final result, also used by slaves */
pthread_mutex_t mutex1; /* mutually exclusive lock variable */
void *slave(void *ignored) /* Slave threads */
{
int local_index, partial_sum = 0;
do {
pthread_mutex_lock(&mutex1);/* get next index into the array */
local_index = global_index;/* read current index & save locally*/
global_index++; /* increment global index */
pthread_mutex_unlock(&mutex1);
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-88
main () {
int i;
pthread_t thread[10]; /* threads */
pthread_mutex_init(&mutex1,NULL); /* initialize mutex */
SAMPLE OUTPUT
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-89
Java Example
public class Adder
{
public int[] array;
private int sum = 0;
private int index = 0;
private int number_of_threads = 10;
private int threads_quit;
public Adder()
{
threads_quit = 0;
array = new int[1000];
initializeArray();
startThreads();
}
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-90
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights
slides8-91
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M. Allen, 2004 Pearson Education Inc. All rights