I'm trying to parallelize a loop to speed up calculation. A portion of my code is below...
This is not working. It's giving me a different output every single time, and I really don't know why. When it goes through the loop, it's looking up a static 3D vector T, and then calculating new values and placing them into a different 3D vector called Tn. Then after the loop, I set T = Tn and begin the loop again. I thought this would eliminate any thread racing, but I guess it did not as my final T output is different every single time. If anyone has experience and knows how I can fix this, I'd greatly appreciate it, because that loop within a loop within a loop can end up being billions of calculations depending on how nodes there are, and it would really help to multi-thread that. Thanks!
Code:
int tSteps = 100;
for (int t=1; t<tSteps; t++)
{
#pragma omp parallel for
for (int i=1; i<nodesx-1; i++)
{
dTx = dt * (alpha/(dx*dx) * (T[i+1][j][k] - 2*T[i][j][k] + T[i-1][j][k]));
//cout << "dTx=" << dTx << endl;
for (int j=1; j<nodesy-1; j++)
{
dTy = dt * (alpha/(dy*dy) * (T[i][j+1][k] - 2*T[i][j][k] + T[i][j-1][k]));
for (int k=1; k<nodesz-1; k++)
{
dTz = dt * (alpha/(dz*dz) * (T[i][j][k+1] - 2*T[i][j][k] + T[i][j][k-1]));
Tn[i][j][k] = T[i][j][k] + dTx + dTy + dTz;
}
}
}
T = Tn;
}
Last edited: