1

I am trying to make a program, where you can set the amount of threads you want, and it will parallelize selection sort algorithm with the given data and amount of threads. I know I should just use another algorithm in this case, but its for educational purposes only. So I run into a problem when parallelizing inner loop in selection sort algorithm some close numbers are left unsorted, but the whole array is sorted apart those few pairs of numbers inside and I cant find out why.

    int* selectionSort(int arr[], int size, int numberOfThreads)
{
    int i, j;
    int me, n, min_idx;
    bool canSwap = false;

#pragma omp parallel num_threads(numberOfThreads) private(i,j,me,n)
    {
        me = omp_get_thread_num();
        n = omp_get_num_threads();

        printf("Hello from %d/%d\n", me, n);


        for (i = 0; i < size - 1; i++) {
            min_idx = i;
            canSwap = true;
#pragma omp barrier

#pragma omp for
            for (j = i + 1; j < size; j++) {
                if (arr[j] < arr[min_idx])
                    min_idx = j;
                //printf("I am %d processing %d,%d\n", me, i, j);

            }

            printf("Min value %d ---- %d \n", arr[min_idx], min_idx);

#pragma omp critical(swap)
            if(canSwap)
            {
                swap(&arr[min_idx], &arr[i]);
                canSwap = false;
            }

#pragma omp barrier

        }
    }

    return arr;
}
  • If you remove the parallelism does it work? – Mgetz Dec 07 '18 at 13:12
  • Yeah, i does work – Edvinas Deksnys Dec 07 '18 at 13:13
  • 1
    So what' that says is you're probably running into a race condition where one thread is seeing stale data. This is why most parallel sorts use algorithms where divide and conquer is an option. – Mgetz Dec 07 '18 at 13:15
  • A reduction clause is mandatory for that inner loop. I'm not about to study reasoning for the enclosing parallel, but it might be good to try simplifying at least as a way of isolating the problem. – tim18 Dec 08 '18 at 05:15

1 Answers1

0

I found out that the problem is that you can't really parallelize this algorithm (well at least in a way I'm doing it), since I'm comparing the arr[j] with arr[min_idx], min_idx value can sometimes get changed in such particular time that other thread will have finished the if (arr[j] < arr[min_idx]) line and right after that another thread would change the min_idx value which would sometimes make just completed if statement not true anymore.

  • Actually it is quite easy to parallelize with a reduction scheme. See [this question](https://stackoverflow.com/questions/53410585/openmp-c-program-run-slower-than-sequential-code/53412396#53412396), for instance. Run the inner loop with thread-private variable, so that each thread has the minimum index of the iterations it processed. Then take the minimum of this small number of minima. – Brice Dec 10 '18 at 08:55