2

I have an array of 100 elements that needs to be sorted with insertion sort using OpenMP. When I parallelize my sort it does not give correct values. Can some one help me

void insertionSort(int a[])
{
    int i, j, k;
    #pragma omp parallel for private(i)
    for(i = 0; i < 100; i++)
    {
            k = a[i];
            for (j = i; j > 0 && a[j-1] > k; j--)
                    #pragma omp critical
                    a[j] = a[j-1];
                    a[j] = k;
    }
}
1

2 Answers 2

3

Variables "j" and "k" need to be private on the parallel region. Otherwise you have a data race condition.

Sign up to request clarification or add additional context in comments.

6 Comments

@ejd I get the following output when I make 'j' and 'k' private
(2), (2), (3), (4), (4), (4), (5), (5), (6), (7), (8), (9), (10), (10), (11), (15), (29), (34), (45), (54), (63), (66), (77), (99), (1100), (1254), (1300), (4322), (5), (5), (6), (6), (7), (7), (8), (8), (9), (19), (22), (24), (27), (32), (33), (34), (34), (39), (42), (43), (44), (45), (45), (48), (54), (54), (54), (54), (54), (55), (58), (64), (65), (66), (76), (79), (89), (92), (95), (98), (100), (344), (667), (2999), (5454), (5454), (9377), (8), (6), (6), (23), (25), (25), (32), (33), (43), (43), (43), (45), (45), (52), (54), (63), (64), (75), (435), (665), (776), (954), (2323), (9444),
I was just commenting on data scoping. You need to look at the critical region.
@ejd I made it critical so that only one thread can enter while the value is being manipulated. What can I do different.
You are not protecting the entire value swap.
|
3

Unless it's a homework, sorting as few as 100 elements in parallel makes no sense: the overhead introduced by parallelism will far outweigh any performance benefit.

And, insertion sort algorithm is inherently serial. When a[i] is processed, it is supposed that all previous elemens in the array are already sorted. But if two elements are processed in parallel, there is obviously no such guarantee.

A more detailed explanation of why insertion sort cannot be parallelized in the suggested way is given by @dreamcrash in his answer to a similar question.

2 Comments

yea it is homework, bit of a last minute decision to do this in openmp. I do realize for an array this size insertion works much fast sequentially. The timings I get are 0.00016 secs versus 0.00216 in openmp.
+1, To bad the right answer was not selected. I had answer a similar question moments ago, where the OP use this same algorithm with this same parallelization, maybe the OP took from here, thinking it as correct.

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.