veda
veda

Reputation: 6614

openMp: need to parallelize a loop having data dependency problem

I wanted to know whether there is a way to parallelize a "for loop" having data dependency problem using OpenMP.

here is the for loop which I like to parallelize

int k = 0;
int A[100][100];
for(i = 0; i < 100; i++) {
    for(j = 0; j < 100; j++) {
           A[i][j] = k;
           k++;
    }
}

Do anyone have a solution for this...

Upvotes: 1

Views: 1071

Answers (2)

Blair Holloway
Blair Holloway

Reputation: 16519

The problem, at first glance, is that k is not being declared as a derived value; it is a value external to the loop, which could be confusing the compiler.

You could rewrite your loop to explicitly specify k as a function of i and j, and retain the same behaviour:

int A[100][100];
for(i = 0; i < 100; i++) {
    for(j = 0; j < 100; j++) {
           const int k = i * 100 + j;
           A[i][j] = k;
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502656

You don't really have a data dependency problem there, because k can be computed from i and j:

int A[100][100];
for(i = 0; i < 100; i++) {
    for(j = 0; j < 100; j++) {
        A[i][j] = i * 100 + j;
    }
}

If you can give a more realistic example of what you're trying to do, we may be able to help you more.

Upvotes: 4

Related Questions