POSIX threads
Symmetric multi processing, e.g. SMP multi-processor computers, multi-core processors, virtual shared memory computer.
Data layout: A single global memory,. Each thread reads global shared data and writes to a private fraction of global data.
A simplified translation of the following example parallel-for loop is given below.
Grid1 *g = new Grid1(0, n+1);
Grid1IteratorSub it(1, n, g);
DistArray x(g), y(g);
...
float e = 0;
ForEach(int i, it,
x(i) += ( y(i+1) + y(i-1) )*.5;
e += sqr( y(i) ); )
...
|
global declaration:
#include <pthread.h>
float *x, *y;
float vec[8];
int nn, pp;
|
thread code:
void *sub1(void *arg) {
int p = (int)arg;
float e_local = 0;
for (int i=1+(nn*p)/pp; i<1+(nn*(p+1))/pp; ++i) {
x[i] += ( y[i+1] + y[i-1] )*.5;
e_local += y[i] * y[i];
}
vec[p] = e_local;
return (void*) 0;
}
|
main code:
x = new float[n+1];
y = new float[n+1];
...
float e = 0;
int p_threads = 8;
nn = n-1;
pp = p_threads;
pthread_t threads[8];
pthread_attr_t attr;
pthread_attr_init(&attr);
for (int p=0; p<p_threads; ++p)
pthread_create(&threads[p], &attr,
sub1, (void *)p);
for (int p=0; p<p_threads; ++p) {
pthread_join(threads[p], NULL);
e += vec[p];
}
...
delete[] x, y;
|
Removal of global variables requires some code modifications.