0

This is what I have so far, the regular merge sort works perfectly but I'm not sure if what I've done for the parallel merge sort with the openMP is correct or not. The goal is to optimize the merge sort by incorporating openMP into it and using data taken from a class of applications. For the data taken I figured I'd make it be threads since the use of multiple threads should optimize it for the better. Any advice on where I should implement the threads here or tips?

void mergeSort(int arr[], int start, int end) 
{
    if(start < end) 
    {
        int middle = (start + end) / 2;
        /* sort left half */
        mergeSort(arr, start, middle);
        /* sort right half */
        mergeSort(arr, middle + 1, end);
        /* merge the two halves */
        merge(arr, start, end);
    }
}

void mergesort_parallel_omp(int a[], int start, int end)//parallelized version
{                                    
    int threads = 2;
    if(start < end) 
    {
        int middle = (start + end) / 2;
        omp_set_nested(1);                               /////
        omp_set_num_threads(threads);
        #pragma omp parallel sections
        {
            #pragma omp section
                mergesort_parallel_omp(a, start, middle);
            #pragma omp section
                mergesort_parallel_omp(a, middle + 1, end);
        }
        merge(a, start, end);
    }  
}
  • Possible duplicate of [Parallel Merge-Sort in OpenMP](https://stackoverflow.com/questions/13811114/parallel-merge-sort-in-openmp) – Zulan Nov 26 '17 at 10:57

1 Answers1

0

Refer to this documentation, you need to use omp_set_nested as shown in the link. To have parallelism, declare it where you've done #pragma omp parallel sections and also don't forget to set OMP_NUM_THREADS

Shivansh Jagga
  • 1,541
  • 1
  • 15
  • 24
  • So put "omp_set_nested()" above "#pragma omp parallel sections" and where should omp_ num_threads() be set at? – user6088127 Nov 25 '17 at 20:31
  • 1
    The link appears to be horribly out of date and spiked with irrelevant proprietary extensions. Please use the [official specification](http://www.openmp.org/specifications/) instead. Besides the question is a clear and easy to find duplicate https://stackoverflow.com/questions/13811114/parallel-merge-sort-in-openmp – Zulan Nov 26 '17 at 10:57