Advertisement
sepi0l

hpc_minmax

Apr 25th, 2024
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <omp.h>
  3. #include <climits>
  4. using namespace std;
  5. void min_reduction(int arr[], int n) {
  6.   int min_value = INT_MAX;
  7.   #pragma omp parallel for reduction(min: min_value)
  8.   for (int i = 0; i < n; i++) {
  9.     if (arr[i] < min_value) {
  10.     min_value = arr[i];
  11.     }
  12.   }
  13.   cout << "Minimum value: " << min_value << endl;
  14. }
  15.  
  16. void max_reduction(int arr[], int n) {
  17.   int max_value = INT_MIN;
  18.   #pragma omp parallel for reduction(max: max_value)
  19.   for (int i = 0; i < n; i++) {
  20.     if (arr[i] > max_value) {
  21.     max_value = arr[i];
  22.     }
  23.   }
  24.   cout << "Maximum value: " << max_value << endl;
  25. }
  26.  
  27. void sum_reduction(int arr[], int n) {
  28.   int sum = 0;
  29.    #pragma omp parallel for reduction(+: sum)
  30.    for (int i = 0; i < n; i++) {
  31.     sum += arr[i];
  32.   }
  33.   cout << "Sum: " << sum << endl;
  34. }
  35.  
  36. void average_reduction(int arr[], int n) {
  37.   int sum = 0;
  38.   #pragma omp parallel for reduction(+: sum)
  39.   for (int i = 0; i < n; i++) {
  40.     sum += arr[i];
  41.   }
  42.   cout << "Average: " << (double)sum / (n-1) << endl;
  43. }
  44.  
  45. int main() {
  46.     int *arr,n;
  47.     cout<<"\n enter total no of elements=>";
  48.     cin>>n;
  49.     arr=new int[n];
  50.     cout<<"\n enter elements=>";
  51.     for(int i=0;i<n;i++)
  52.     {
  53.      cin>>arr[i];
  54.     }
  55.  
  56. //   int arr[] = {5, 2, 9, 1, 7, 6, 8, 3, 4};
  57. //   int n = size(arr);
  58.  
  59.   min_reduction(arr, n);
  60.   max_reduction(arr, n);
  61.   sum_reduction(arr, n);
  62.   average_reduction(arr, n);
  63. }
  64.  
  65. /*
  66. Output:
  67. unix@unix-HP-280-G1-MT:~/codes$ g++ Para_merge.cpp
  68. unix@unix-HP-280-G1-MT:~/codes$ g++ Min_max.cpp
  69. unix@unix-HP-280-G1-MT:~/codes$ ./a.out
  70.  
  71.  enter total no of elements=>5
  72.  
  73.  enter elements=>2 3 4 5 2
  74. Minimum value: 2
  75. Maximum value: 5
  76. Sum: 16
  77. Average: 4
  78.  
  79.  
  80. */
  81.  
  82.  
  83. /*
  84. This code demonstrates how to perform reduction operations (finding minimum, maximum, sum, and average)
  85.  on an array using OpenMP parallelization. Let's go through it step by step:
  86.  
  87. 1. **Function Definitions:**
  88.    - `min_reduction`: Finds the minimum value in the array using OpenMP's reduction clause with the `min` operation.
  89.    - `max_reduction`: Finds the maximum value in the array using OpenMP's reduction clause with the `max` operation.
  90.    - `sum_reduction`: Calculates the sum of all elements in the array using OpenMP's reduction clause with the `+` operation.
  91.    - `average_reduction`: Calculates the average of all elements in the array using OpenMP's reduction clause with the `+` operation.
  92.  
  93. 2. **`main` Function:**
  94.    - Dynamically allocates memory for the array based on user input for the number of elements `n`.
  95.    - Takes user input for the array elements.
  96.    - Calls each of the reduction functions with the array and its size as arguments.
  97.  
  98. 3. **OpenMP Parallelization:**
  99.    - OpenMP directives (`#pragma omp parallel for reduction`) are used to parallelize the loop iterations.
  100.    - The `reduction` clause specifies that each thread maintains a private copy of the reduction variable and
  101.    combines them at the end of the parallel region.
  102.    - For example, in `min_reduction`, each thread maintains a private `min_value`, and at the end of the loop,
  103.    all the private `min_value` variables are combined to find the global minimum.
  104.  
  105. 4. **Output:**
  106.    - The code outputs the minimum value, maximum value, sum, and average of the array elements.
  107.  
  108. 5. **Sample Output:**
  109.    - For the given input array `{2, 3, 4, 5, 2}`, the output shows:
  110.      - Minimum value: 2
  111.      - Maximum value: 5
  112.      - Sum: 16
  113.      - Average: 4
  114.  
  115. This code efficiently utilizes OpenMP parallelization to perform reduction operations on arrays, improving
  116. performance by leveraging multiple threads to process array elements concurrently.
  117. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement