July 2007
Intermediate to advanced
332 pages
10h 4m
English
Here is a simple seismic wave simulation (wave propagation) based on parallel_for and blocked_range. The key parts of this example are shown here; the entire code is available for download.
The main program steps through the simulation of a seismic wave in a core loop that sets the impulse from the source of the disturbance, does the two tough computations of stress updates and velocities, and finally cleans up the edges of the simulation.
First we’ll look at the stress algorithm, in Example 11-2 and Example 11-3. The first shows the serial version of the algorithm, and the second shows an equivalent parallel version.
Example 11-2. Seismic serial: Update stress
static void SerialUpdateStress() {
drawing_area drawing(0, 0, UniverseWidth, UniverseHeight);
for( int i=1; i<UniverseHeight-1; ++i ) {
color_t* c = ColorMap[Material[i]];
drawing.set_pos(1, i);
#pragma ivdep
for( int j=1; j<UniverseWidth-1; ++j ) {
S[i][j] += (V[i][j+1]-V[i][j]);
T[i][j] += (V[i+1][j]-V[i][j]);
int index = (int)(V[i][j]*(ColorMapSize/2)) + ColorMapSize/2;
if( index<0 ) index = 0;
if( index>=ColorMapSize ) index = ColorMapSize-1;
drawing.put_pixel(c[index]);
}
}
}Example 11-3. Seismic parallel: Update stress
struct UpdateStressBody { void operator()( const tbb::blocked_range<int>& range ) const { drawing_area drawing(0, range.begin(), UniverseWidth, range.end()-range.begin()); int i_end = range.end(); for( int y = 0, i=range.begin(); i!=i_end; ++i,y++ ) { color_t* c = ColorMap[Material[i]]; drawing.set_pos(1, ...