0

I have problem to parallel for-loop code in OpenMP, result of parallel for-loop is different with a sequential for-loop. How to make this code parallel with same result as sequential code.

counter = 0;
#pragma omp parallel for
for(i=0; i<L; i++) {
    int sum_found = 0;
    for(j=0; j<M; j++) {
        int found = 0;
        for(k=0; k<N_SUBSET; k++) {
            if (i==0 && unsorted_arr[j*N_SUBSET + k] < intervals[i]) {
                s_prime[counter] = unsorted_arr[j*N_SUBSET + k];
                counter++;
                found++;
            }
            else if (i!=0 && unsorted_arr[j*N_SUBSET + k] >= intervals[i-1] && unsorted_arr[j*N_SUBSET + k] < intervals[i]) {
                s_prime[counter] = unsorted_arr[j*N_SUBSET + k];
                counter++;
                found++;
            }
            else if (i==L-1 && unsorted_arr[j*N_SUBSET + k] >= intervals[i-1]) {
                s_prime[counter] = unsorted_arr[j*N_SUBSET + k];
                counter++;
                found++;
            }
        }
        C[i][j] = found;
        sum_found += found;
    }
    n_intervals_len[i] = sum_found;
}
3
  • Have you tried compiling with thread-sanitizer enabled? Commented Apr 4, 2019 at 12:16
  • I just compile with GCC and OpenMP Commented Apr 4, 2019 at 16:58
  • Try to add -fsanitize=thread. It will tell you if and where you have a data race Commented Apr 4, 2019 at 18:21

1 Answer 1

1

As some of the context of the above code is missing (esp. the variable declarations), it's rather hard to tell what is going wrong. But here are some ideas:

  • the loop counters j and k should be declared private, because otherwise you might have a race condition on them

  • the counter++ and found++ statements are also races for updates from different threads on the same variable. You would either have to use an atomic construct for them or use a proper OpenMP reduction to get rid of the race condition.

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.