OpenMP C++ Parallel Loop... Thread Racing Problem?

blade52x

2[H]4U
Joined
Sep 9, 2006
Messages
3,191
I'm trying to parallelize a loop to speed up calculation. A portion of my code is below...

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;
    }
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!
 
Last edited:
Some output from within the loop, if it helps at all.

Code:
Thread:0 i:1 T:33.6

Thread:4 i:51 T:33.6

Thread:7 i:87 T:33.6

Thread:3 i:39 T:33.6

Thread:6 i:75 T:33.6

Thread:5 i:63 T:Thread:2 i:27 T:20

20

Thread:1 i:14 T:33.6

Press any key to continue . . . Press any key to continue . . . Press any key to
 continue . . . Press any key to continue . . . Press any key to continue . . .
Press any key to continue . . . Press any key to continue . . . Press any key to
 continue . . .
Threads 0,4,73,6,1 correctly updated to 33.6
Threads 5,2 did not and remained at the initial value of 20.

Code:
Thread:0 i:1 T:33.6

Thread:2 i:27 T:33.6

Thread:6 i:75 T:33.6

Thread:4 i:51 T:20

Thread:1 i:14 T:33.6

Thread:7 i:87 T:Thread:3 i:39 T:20
33.6


Thread:5 i:63 T:33.6

Press any key to continue . . . Press any key to continue . . . Press any key to
 continue . . . Press any key to continue . . . Press any key to continue . . .
Press any key to continue . . . Press any key to continue . . . Press any key to
 continue . . .
Threads 0,2,6,1,7,5 updated to 33.6
Threads 4,7 did not.

So you can see it's kind of random. And I'm not sure why I'm getting some of these non-updated threads right now.
 
Nevermind figured out of my problem.

I was defining variables dTx, dTy, dTz within the loop, and I guess the threads were overwriting the values calculated by the previous thread, and this was somehow causing the lack of updates. But either, two solutions to this... 1) move the calculations of dTx, dTy, dTz into the very final calculation for Tn[j][k] so that whatever is calculated is placed right away where it needs to be or 2) define dTx, dTy, dTz as arrays with length of number of threads, and have each thread only update and read off its own place in the array. I've tested #1 and I know that works for sure.

Anyway, thread closed. Oh the joy of coding. Spend hours one day trying to figure out mistake. Spend 20 minutes the next morning finding stupid mistake and feeling stupid. :eek: But happy the issue is at least solved. :D
 
Back
Top