ABUL_SALASA
ABUL_SALASA

Reputation: 49

Problem parallelizing nested inter-dependent loops for arrays assignments

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.

Upvotes: 1

Views: 72

Answers (0)

Related Questions