1

I am doing some image processing and have a nested for loop. I want to implement multiprocessing using OpenMP. The for loop looks like this, where I have added the pragma tags and declared some of the variables private as well.

int a,b,j, idx;
#pragma omp parallel for private(b,j,sumG,sumGI)
    for(a = 0; a < ny; ++a) 
    {
        for(b = 0; b < nx; ++b) 
        {
            idx = a*ny+b;
            if (imMask[idx] == 0) 
            {
                Wshw[idx] = 0;
                continue;
            }

            sumG = 0;
            sumGI = 0;

            for(j = a; j < ny; ++j) 
            {
                sumG += shadowM[j-a];
                sumGI += shadowM[j-a] * imBlurred[nx*j + b];
            }

            Wshw[idx] = sumGI / sumG;
        }
    }

The size of both nx and ny is large and I thought that, using OpenMP, I would get a descent decrease in execution time, instead there is almost no difference. Am I doing something wrong when I implement the multi-threading maybe?

1
  • You need to make idx private as well. Commented Feb 8, 2014 at 9:34

1 Answer 1

2

You have a race conditon in idx. You need to make it private as well.

However, instead you could try something like this.

int a,b,j, idx;
#pragma omp parallel for private(a,b,j,sumG,sumGI)
for(idx=0; idx<ny*nx; ++idx) { 
    if (imMask[idx] == 0) 
    {
        Wshw[idx] = 0;
        continue;
    }

    sumG = 0;
    sumGI = 0;
    a=idx/ny;
    b=idx%ny;
    for(j = a; j < ny; ++j) {
        sumG += shadowM[j-a];
        sumGI += shadowM[j-a] * imBlurred[nx*j + b];
    }

    Wshw[idx] = sumGI / sumG;
}

You might be able to simiply the inner loop as well as a functcion of idx instead a and b.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.