I assume that you are concerned with the time taken by the entire code inside the while
loop, not just by the time taken by the loop beginning for(int i = 1; i < MAX-1; i++)
.
This operation
if(iterationCounter != 0)
{
memcpy(oldTemperature, newTemperature, sizeof(oldTemperature));
}
is unnecessary and, for large arrays, may be enough to kill performance. Instead of maintaining 2 arrays, old
and new
, maintain one 3D array with two planes. Create two integer variables, let's call them old
and new
, and set them to 0
and 1
initially. Replace
newTemperature[i][j] = ((oldTemperature[i-1][j] + oldTemperature[i+1][j] + oldTemperature[i][j-1] + oldTemperature[i][j+1]) / 4);
by
temperature[new][i][j] =
(temperature[old][i-1][j] +
temperature[old][i+1][j] +
temperature[old][i][j-1] +
temperature[old][i][j+1])/4;
and, at the end of the update swap the values of old
and new
so that the updates go the other way round. I'll leave it to you to determine whether old/new
should be the first index into your array or the last. This approach eliminates the need to move (large amounts of) data around in memory.
Another possible cause of serious slowdown, or failure to accelerate, is covered in this SO question and answer. Whenever I see arrays with sizes of 2^n
I suspect cache issues.
No comments:
Post a Comment