Programming with Shared Memory: Nguyễn Quang Hùng
Programming with Shared Memory: Nguyễn Quang Hùng
Shared Memory
Nguyễn Quang Hùng
Outline
Introduction
Shared memory multiprocessors
Constructs for specifying parallelism
Creating concurrent processes
Threads
Sharing data
Creating shared data
Accessing shared data
Language constructs for parallelism
Dependency analysis
Shared data in systems with caches
Examples
Pthreads example
Exercises
Introduction
This section focuses on programming on shared
memory system (e.g SMP architecture).
Programming mainly discusses on:
Multi-processes: Unix/Linux fork(), wait()…
Multithreads: IEEE Pthreads, Java Thread…
Multiprocessor system
Multiprocessor systems: two types
Shared memory multiprocessor.
Message-passing multicomputer.
In “Parallel programming:Techniques & applications using networked
workstations & parallel computing” book.
Shared memory multiprocessor:
SMP-based architecture: IBM RS/6000, Big BLUE/Gene
supercomputer, etc.
Cache
........ ........
Source: www.ibm.com
Several alternatives for
programming shared memory
multiprocessors
Using library routines with an existing sequential programming
language.
Multiprocesses programming:
fork(), execv()…
Multithread programming:
IEEE Pthreads library
Java Thread. http://java.sun.com
FORK
FORK
JOIN
JOIN
JOIN
JOIN
UNIX System Calls
No join routine - use exit() and wait()
SPMD model
..
pid = fork(); /* fork */
Code to be executed by both child and parent
if (pid == 0) exit(0); else wait(0); /* join */
...
UNIX System Calls (2)
SPMD model: master-workers model.
1. …
2. pid = fork();
3. if (pid == 0) {
4. Code to be executed by slave process
5. } else {
6. Code to be executed by master process
7. }
8. if (pid == 0) exit(0); else wait(0);
9. ...
Process vs thread
Process
- Completely separate
program with its
own variables,
stack, and memory
allocation.
Threads
– Share the same
memory space
and global
variables between
routines
IEEE Pthreads (1)
IEEE Portable Operating System Interface,
POSIX, sec. 1003.1 standard
Executing a Pthread thread
Main program Thread1
proc1( &arg )
pthread_create( &thread, NULL, proc1, &arg ); {
….
return( *status );
Pthread_join( thread1, *status); }
The pthread_create() function
#include <pthread.h>
int pthread_create(
pthread_t *threadid,
pthread_attr_t * attr,
void * (*start_routine)(void *),
void * arg);
Thread
Termination
pthread_create()
Thread
Termination
Termination
The pthread_detach() function
#include <pthread.h>
int pthread_detach(pthread_t threadid);
write write
read read
+1 +1
Process 1 Process 2
Critical section
A mechanism for ensuring that only one process
accesses a particular resource at a time is to establish
sections of code involving the resource as so-called
critical sections and arrange that only one such
critical section is executed at a time
Lock = 0; lock = 1;
Critical section
Lock = 0;
Pthreads lock functions
Pthreads implements lock by mutally exclusive
lock variables (mutex variables).
pthread_mutex_t mutex1;
pthread_mutex_init( &mutex1, NULL );
Only 1 thread
…….. can enter the
critical section
pthread_mutex_lock ( &mutex1 ); code or wait
global_index
sum
Array a[ ]
…………………………………………..
addr
par {
proc1();
proc2();
…
}
Forall Construct
Keywords: forall or parfor forall (i = 0 ; i < N; i++ ) {
To start multiple similar S1;
processes together: which S2;
generates n processes each
consisting of the statements …..
forming the body of the for loop, Sm;
S1, S2, …, Sm. Each process
uses a different value of i. }
Example:
forall (i = 0; i < 5; i++)
a[i] = 0;