Reputation: 46
I have Selection sort function
inline void selection_sort(double* arrayPtr, int length_array)
{
for (auto i = 0; i < length_array; i++)
{
for (auto j = i + 1; j < length_array; j++)
{
if (arrayPtr[i] > arrayPtr[j])
{
std::swap(arrayPtr[i], arrayPtr[j]);
}
}
}
}
How to optimize it with multithreading?
I found solutions only for quicksort but these solutions did not help me.
Upvotes: 1
Views: 938
Reputation: 51453
How to optimize it with multithreading? I found solutions only for quick sort but but these solutions did not help me.
You cannot find much information about it because it is not a good idea to do so. It will not be efficient, the parallelization of the selection sort has some of the same issues as the parallelization of the insert sort. Namely, this code:
for (auto i = 0; i < length_array; i++)
{
for (auto j = i + 1; j < length_array; j++)
{
if (arrayPtr[i] > arrayPtr[j])
{
std::swap(arrayPtr[i], arrayPtr[j]);
}
}
}
You cannot efficiently parallelize the outer loop because of the interdependencies with the inner loop. So you are already limited on the number of parallel tasks and their granularity. Then you have the problem of the data dependencies during the swap phase which you also need to solve, and which will consequently also impair the speedups of the parallel version.
Algorithms like merge-sort are more parallelizable (i.e., their parallelization yields better speedups) due to their divide-and-conquer nature, where one can parallelize the recursive calls.
Notwithstanding, you can find in stackoverflow a parallel version of the selection sorting in OpenMP/C++. That being said the code runs is parallel, but I would suspect without much (if any) speedups.
Upvotes: 3
Reputation: 3955
Disclaimer: this is only for the homework requirements. For all practical purposes std::sort
is better.
Assume that the requirements is that the exact set of operations must be performed, but independent operations can be performed in any order.
Note that:
length_array
passes through the array (indexed by i
)Therefore one possible way to solve the problem is (be very careful with the offset errors, you have to work out the details)
Upvotes: 1