Advertisement
Guest User

C++ OpenMP merge sort

a guest
Oct 8th, 2013
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include "iostream"
  2. #include "omp.h"
  3. #include "vector"
  4. #include "time.h"
  5.  
  6. using namespace std;
  7.  
  8. vector<long> merge(const vector<long>& left, const vector<long>& right)
  9. {
  10.     vector<long> result;
  11.     unsigned left_it = 0, right_it = 0;
  12.  
  13.     while(left_it < left.size() && right_it < right.size())
  14.     {
  15.         if(left[left_it] < right[right_it])
  16.         {
  17.             result.push_back(left[left_it]);
  18.             left_it++;
  19.         }
  20.         else                   
  21.         {
  22.             result.push_back(right[right_it]);
  23.             right_it++;
  24.         }
  25.     }
  26.  
  27.     // Push the remaining data from both vectors onto the resultant
  28.     while(left_it < left.size())
  29.     {
  30.         result.push_back(left[left_it]);
  31.         left_it++;
  32.     }
  33.  
  34.     while(right_it < right.size())
  35.     {
  36.         result.push_back(right[right_it]);
  37.         right_it++;
  38.     }
  39.  
  40.     return result;
  41. }
  42.  
  43. vector<long> mergesort(vector<long>& vec, int threads)
  44. {
  45.     // Termination condition: List is completely sorted if it
  46.     // only contains a single element.
  47.     if(vec.size() == 1)
  48.     {
  49.         return vec;
  50.     }
  51.  
  52.     // Determine the location of the middle element in the vector
  53.     std::vector<long>::iterator middle = vec.begin() + (vec.size() / 2);
  54.  
  55.     vector<long> left(vec.begin(), middle);
  56.     vector<long> right(middle, vec.end());
  57.  
  58.     // Perform a merge sort on the two smaller vectors
  59.  
  60.     if (threads > 1)
  61.     {
  62.       #pragma omp parallel sections
  63.       {
  64.         #pragma omp section
  65.         {
  66.           left = mergesort(left, threads/2);
  67.         }
  68.         #pragma omp section
  69.         {
  70.           right = mergesort(right, threads - threads/2);
  71.         }
  72.       }
  73.     }
  74.     else
  75.     {
  76.       left = mergesort(left, 1);
  77.       right = mergesort(right, 1);
  78.     }
  79.  
  80.     return merge(left, right);
  81. }
  82.  
  83. int main(){
  84.   int size;
  85.   omp_set_nested(1);
  86.   cout<<omp_get_num_threads()<<"\n";
  87.   cout<<"Input size of array\n";
  88.   cin>>size;
  89.   vector<long> v(size);
  90.   srand(time(NULL));
  91.   cout<<"Initializing array...\n";
  92.   for (long i=0; i<size; ++i){
  93.     v[i] = rand()%100000;
  94.   }
  95.   /*for(int i = 0;i<size;++i){
  96.       cout<<v[i]<<" ";
  97.   }
  98.   cout<<"\n";*/
  99.   cout<<"Array has been initialized! Merge sort started with OpenMP\n";
  100.   clock_t begin = clock();
  101.   v = mergesort(v, 1);
  102.   cout<<"Finished in "<<((clock()-begin)*1000)/CLOCKS_PER_SEC<<"ms \n";  
  103.   /*for(int i = 0;i<size;++i){
  104.       cout<<v[i]<<" ";
  105.   }
  106.   cout<<"\n";*/
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement