My OpenMP program blocks on the first "for" loop of the following code, without any apparent reason. I'm simply trying to parallelize a Bubble Sort.
Below is a complete code reproducing the issue :
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <omp.h>
static int N_THREADS;
#define CHUNK_SIZE (size/N_THREADS)
void
parallel_bubble_sort(uint64_t *T, const uint64_t size)
{
register bool swapped;
register uint64_t swap;
register int i,j;
#pragma omp parallel private(swap,i,j)
do {
swapped = false;
#pragma omp for schedule(static) reduction(||:swapped)
for (j=0; j<N_THREADS; j++)
for (i=j*CHUNK_SIZE+1; i<=(j+1)*CHUNK_SIZE-1; i++)
if (T[i-1] > T[i]) {
swap = T[i-1];
T[i-1] = T[i];
T[i] = swap;
swapped = true;
}
#pragma omp for schedule(static) reduction(||:swapped)
for (i=CHUNK_SIZE-1; i<size-CHUNK_SIZE; i+=CHUNK_SIZE)
if (T[i] > T[i+1]) {
swap = T[i];
T[i] = T[i+1];
T[i+1] = swap;
swapped = true;
}
} while(swapped);
}
int main ()
{
uint64_t i;
uint64_t N = 1024;
N_THREADS = omp_get_max_threads();
uint64_t *X = (uint64_t *) malloc(N * sizeof(uint64_t));
for (i = 0 ; i < N ; i++) X[i] = N-i;
parallel_bubble_sort(X, N);
free(X);
}
Some additional context:
- T* is a pointer to an array of type uint64_t
- size is the size of the array
- CHUNK_SIZE is simply size/NUM_THREADS (which is also the default chunk size value OpenMP uses in the static scheduling mode, so I should get the same behavior if I remove this from the clause)
Regarding the logic behind the code:
- In the first loop, I divide my array into chunks, and I propagate bubbles separately without overlap between threads
- In the second loop, I make sure the bubbles propagate at the borders
More details about the issue I have while executing:
- My program is stuck on the first "for" loop. I have localized where the program blocks using #pragma omp single, and a simple print statement.