Boost.Thread
A C++ thread library, part of the Boost C++ library. Available for POSIX threads, special implementation for Linux and Windows.
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 <boost/thread/thread.hpp>
using namespace boost;
|
thread code:
struct sub1 {
float *x, *y, *e;
int i0, i1;
sub1(float *xx, float *yy, float *ee, int ii0, int ii1) :
x(xx), y(yy), e(ee), i0(ii0), i1(ii1) {}
void operator()() {
float e_local = 0;
for (int i=i0; i<i1; ++i) {
x[i] += ( y[i+1] + y[i-1] )*.5;
e_local += y[i] * y[i];
}
*e = e_local;
}
};
|
main code:
float *x = new float[n+1];
float *y = new float[n+1];
float e = 0;
...
float e_local[NUM_THREADS];
thread_group grp;
for (int i=0; i<NUM_THREADS; ++i) {
sub1 s(x, y, &e_local[i],
1+((n-1)*i)/NUM_THREADS, 1+((n-1)*(i+1))/NUM_THREADS);
grp.create_thread(s);
}
grp.join_all();
for (int i=0; i<NUM_THREADS; ++i)
e += e_local[i];
...
delete[] x, y;
|