0

Rows represents the number of elements which were sorted and time is in milliseconds:

Rows represents the number of elements which were sorted and time is in milliseconds

I have set thread using export OMP_NUM_THREADS=n There is a constant increasing in execution time irrespective of the number of elements I am taking. Where am I going wrong?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "omp.h"

/*
OpenMP implementation example
Details of implementation/tutorial can be found here: 
http://madhugnadig.com/articles/parallel- 
processing/2017/02/25/parallel-computing-in-c-using-openMP.html
*/

void mergeSort( int a[], int i, int j);
void merge( int a[], int i1, int j1, int i2, int j2);

int main()
{   clock_t t;
t = clock();

int *a, num, i;
scanf("%d",&num);

a = ( int *)malloc(sizeof(int) * num);
for(i=0;i<num;i++)
  scanf("%d",&a[i]);

mergeSort(a, 0, num-1);

printf("\nsorted array :\n");
for(i=0;i<num;i++)
  printf("%d ",a[i]);



t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds

printf("\n\n\nYour sorting took %f seconds to execute \n", time_taken);

return 0;
}

void mergeSort( int a[], int i, int j)
{
int mid;

 if(i<j)
{
  mid=(i+j)/2;

  #pragma omp parallel sections
 {

    #pragma omp section
    {
        mergeSort(a,i,mid);        //left recursion
    }

    #pragma omp section
    {
        mergeSort(a,mid+1,j);    //right recursion
    }
}

merge(a,i,mid,mid+1,j);    //merging of two sorted sub-arrays
}
}

void merge( int a[], int i1, int j1, int i2, int j2)
{
 int temp[1001000];    //array used for merging
 int i,j,k;
 i=i1;    //beginning of the first list
 j=i2;    //beginning of the second list
 k=0;

 while(i<=j1 && j<=j2)    //while elements in both lists
  {
   if(a[i]<a[j])
      temp[k++]=a[i++];
   else
       temp[k++]=a[j++];
   }

  while(i<=j1)    //copy remaining elements of the first list
    temp[k++]=a[i++];

  while(j<=j2)    //copy remaining elements of the second list
    temp[k++]=a[j++];

   //Transfer elements from temp[] back to a[]
    for(i=i1,j=0;i<=j2;i++,j++)
       a[i]=temp[j];
   }

This is how I have run the code on my macbook:

This is how I have run the code on my macbook

MBT
  • 21,733
  • 19
  • 84
  • 102
  • 1
    Possible duplicate of [OpenMP time and clock() calculates two different results](https://stackoverflow.com/questions/10673732/openmp-time-and-clock-calculates-two-different-results) – Zulan Aug 30 '18 at 17:26
  • Welcome to SO. Some general remarks, please always include actual measurement results in your question rather than a vague description, always copy&paste text into the question instead of using images, please also make some effort in properly formatting your code (consistent indentation). This all helps in answering your question - if it wasn't such a common duplicate anyway. – Zulan Aug 30 '18 at 17:29
  • P.S. Using `parallel sections` for parallelizing recursive algorithms in 2018 is a bad idea. Instead tasks should be used. I recommend to look for a better tutorial. – Zulan Aug 31 '18 at 09:10

0 Answers0