Q: OpenMP nested loop parallelism I m using OpenMP and I have a problem with wrong results. 
Here is the code : 
    #pragma omp parallel shared(L,nthreads,chunk) private(tid,i,j){
        tid = omp_get_thread_num();
        if (tid == 0)
        {
            nthreads = omp_get_num_threads();
            printf("Starting matrix multiple example with %d threads\n",nthreads);
            printf("Initializing matrices...\n");
        }

        #pragma omp for schedule (static, chunk) 
        for( i=0; i<SIZE_A;i++){
            for( j=0; j<SIZE_B;j++){
                if(A[i]==B[j]){
                    if(i==0 || j==0)
                        L[i][j]=1;
                    else
                        L[i][j] = L[i-1][j-1] + 1;
                }
                // or reset the matching score to 0
                else
                    L[i][j]=0;
            }
        }
    }

What do you think, why I am getting wrond result?
What should I change? 
Thanks a lot!

A: You have a loop data dependence:
L[i][j] = L[i-1][j-1] + 1;

Here if interations i and i-1 have been assigned to different threads, then there is no guarantee that the first thread would have finished before the second have started and thus the second thread will read incorrect (still not updated) value of L[i-1][j-1]. You can make the execution ordered by giving the ordered clause to the omp for worksharing directive but that will kill the parallelisation.
Since the dependency is diagonal you can rethink your algorithm to somehow walk L diagonally instead of row-wise.
