1

I am learning omp and came across nested loops involving arrays assignments etc. with details:

  1. Loop indices are inter-dependent
  2. Arrays a, b, and c have been initialized
  3. I am using Visual Studio 2022 (apparently with limited features of omp due to old version)

The following code seems working fine though but I was wondering if there is a way we could accomplish the task without using the schedule clause.

#pragma omp parallel
{
    int temp = 0;
    
    #pragma omp for
        for(int i = 0 ; i < 10; i++)
            for(int j = i + 1; j < 10; j++)
            {
                if(a[i] > a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;

                    temp = b[i];
                    b[i] = b[j];
                    b[j] = temp;

                    temp = c[i];
                    c[i] = c[j];
                    c[j] = temp;
                }
            }
    
} 

I tried avoiding schedule by introducing critical clause around the assignments but that didn't work. Can we in any way use a reduction clause here?

Moreover, if anyone could guide best practices to follow when dealing with arrays specially when these are depending on previous/next values (e.g, A[i] = A[i+1] - delta;).

The results came out correct when using schedule clause but I want to avoid it and bring results with reduced execution cost.

7
  • Some algorithms are inherently sequential. The only way to get around that is to use a different algorithm. Commented Oct 31, 2024 at 19:17
  • FYI: in C++ you should be using std::swap for swapping things. Commented Oct 31, 2024 at 19:19
  • As noted by another user in the staging ground, the race condition is independent of the scheduling. Even if you got correct results once (or multiple times) the race is there and can effect the results of future runs. Commented Oct 31, 2024 at 19:24
  • It looks like you are trying to implement bubble-sort. It's hardly worth parallelizing sorting for 10 elements, but if you want, just use std::sort(std::execution::par, .... Commented Oct 31, 2024 at 22:06
  • 2
    @paleonix Oh, I see. Too bad. There are nice things in standard C++ to learn too. auto zip = std::views::zip(a, b, c); std::sort(std::execution::par, zip.begin(), zip.end()); - done Commented Oct 31, 2024 at 22:16

0

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.