Advertisement
sepi0l

hpc_merge

Apr 25th, 2024
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <omp.h>
  3.  
  4. using namespace std;
  5.  
  6. void merge(int arr[], int low, int mid, int high) {
  7.     // Create arrays of left and right partititons
  8.     int n1 = mid - low + 1;
  9.     int n2 = high - mid;
  10.  
  11.     int left[n1];
  12.     int right[n2];
  13.    
  14.     // Copy all left elements
  15.     for (int i = 0; i < n1; i++) left[i] = arr[low + i];
  16.     // Copy all right elements
  17.     for (int j = 0; j < n2; j++) right[j] = arr[mid + 1 + j];
  18.     // Compare and place elements
  19.     int i = 0, j = 0, k = low;
  20.     while (i < n1 && j < n2) {
  21.         if (left[i] <= right[j]){
  22.             arr[k] = left[i];
  23.             i++;
  24.         }
  25.         else{
  26.             arr[k] = right[j];
  27.             j++;
  28.         }
  29.         k++;
  30.     }
  31.  
  32.     // If any elements are left out
  33.     while (i < n1) {
  34.         arr[k] = left[i];
  35.         i++;
  36.         k++;
  37.     }
  38.     while (j < n2) {
  39.         arr[k] = right[j];
  40.         j++;
  41.         k++;
  42.     }
  43. }
  44.  
  45. void parallelMergeSort(int arr[], int low, int high) {
  46.     if (low < high) {
  47.         int mid = (low + high) / 2;
  48.         #pragma omp parallel sections
  49.         {
  50.             #pragma omp section
  51.             {
  52.                 parallelMergeSort(arr, low, mid);
  53.             }
  54.             #pragma omp section
  55.             {
  56.                 parallelMergeSort(arr, mid + 1, high);
  57.             }
  58.         }
  59.         merge(arr, low, mid, high);
  60.     }
  61. }
  62.  
  63. void mergeSort(int arr[], int low, int high) {
  64.     if (low < high) {
  65.         int mid = (low + high) / 2;
  66.         mergeSort(arr, low, mid);
  67.         mergeSort(arr, mid + 1, high);
  68.         merge(arr, low, mid, high);
  69.     }
  70. }
  71.  
  72. int main() {
  73.     int n;
  74.     cout<<"Enter the maximum element: ";
  75.     cin>>n;
  76.     int arr[n];
  77.     double start_time, end_time;
  78.  
  79.     // Create an array with numbers starting from n to 1.
  80.     for(int i = 0, j = n; i < n; i++, j--) arr[i] = j;
  81.    
  82.     // Measure Sequential Time
  83.     start_time = omp_get_wtime();
  84.     mergeSort(arr, 0, n - 1);
  85.     end_time = omp_get_wtime();
  86.     cout << "Time taken by sequential algorithm: " << end_time - start_time << " seconds\n"<<endl;
  87.     cout<<endl;
  88.    
  89.     // Print sorted array
  90.     cout << "Sequential Sorted Array: ";
  91.     for(int i = 0; i < n; i++)
  92.         cout << arr[i] << " ";
  93.     cout << endl;
  94.     cout<<endl;
  95.  
  96.     // Reset the array
  97.     for(int i = 0, j = n; i < n; i++, j--) arr[i] = j;
  98.    
  99.     //Measure Parallel time
  100.     start_time = omp_get_wtime() ;
  101.     parallelMergeSort(arr, 0, n - 1);
  102.     end_time = omp_get_wtime();
  103.     cout << "Time taken by parallel algorithm: " << end_time - start_time << " seconds"<<endl;
  104.     cout<<endl;
  105.    
  106.     // Print sorted array
  107.     cout << "Parallel Sorted Array: ";
  108.     for(int i = 0; i < n; i++)
  109.         cout << arr[i] << " ";
  110.     cout << endl;
  111.    
  112.     return 0;
  113. }
  114.  
  115. /*
  116. This code demonstrates the parallel implementation of the merge sort algorithm using OpenMP. Let's break it down step by step:
  117.  
  118. **Merge Sort Overview:**
  119. - Merge sort is a popular sorting algorithm that follows the divide and conquer approach.
  120. - It divides the input array into two halves, recursively sorts the halves, and then merges them.
  121. - The merge step involves comparing elements from the two halves and placing them in sorted order.
  122. - Merge sort has a time complexity of O(n log n) for all cases (best, average, and worst).
  123.  
  124. **Code Explanation:**
  125. 1. **`merge()` Function:** This function merges two sorted subarrays into a single sorted array.
  126.    - It takes four parameters: the array `arr[]`, indices `low`, `mid`, and `high` representing the two subarrays.
  127.    - It creates temporary arrays `left[]` and `right[]` to store the elements of the left and right subarrays respectively.
  128.    - It compares elements from `left[]` and `right[]`, placing the smaller element in the original array `arr[]`.
  129.    - After merging, any remaining elements in `left[]` or `right[]` are copied to `arr[]`.
  130.  
  131. 2. **`mergeSort()` Function:** This is the sequential implementation of merge sort.
  132.    - It recursively divides the array into two halves until each subarray contains only one element.
  133.    - It then merges the sorted halves using the `merge()` function.
  134.  
  135. 3. **`parallelMergeSort()` Function:** This function is the parallel implementation of merge sort using OpenMP.
  136.    - Similar to `mergeSort()`, it recursively divides the array into two halves.
  137.    - However, the division of work is parallelized using OpenMP sections.
  138.    - Each section recursively sorts its assigned portion of the array, and then the sorted halves are merged.
  139.  
  140. 4. **`main()` Function:**
  141.    - It initializes the input array with numbers from `n` to 1.
  142.    - It measures the time taken for sequential merge sort and prints the sorted array.
  143.    - Then, it resets the array, measures the time taken for parallel merge sort, and prints the sorted array.
  144.  
  145. **Time Complexity Analysis:**
  146. - Merge sort has a time complexity of O(n log n) for all cases (best, average, and worst).
  147. - In the parallel implementation, the work is divided among multiple threads using OpenMP sections.
  148. - However, the merging step remains inherently sequential, as merging two sorted arrays cannot be parallelized efficiently.
  149. - Despite the parallelization, the overall time complexity of the parallel merge sort algorithm remains O(n log n).
  150.  
  151. In summary, this code demonstrates how merge sort can be parallelized using OpenMP to potentially
  152. improve performance while maintaining the same time complexity as the sequential version.
  153. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement