Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <omp.h>
- #include <climits>
- using namespace std;
- void min_reduction(int arr[], int n) {
- int min_value = INT_MAX;
- #pragma omp parallel for reduction(min: min_value)
- for (int i = 0; i < n; i++) {
- if (arr[i] < min_value) {
- min_value = arr[i];
- }
- }
- cout << "Minimum value: " << min_value << endl;
- }
- void max_reduction(int arr[], int n) {
- int max_value = INT_MIN;
- #pragma omp parallel for reduction(max: max_value)
- for (int i = 0; i < n; i++) {
- if (arr[i] > max_value) {
- max_value = arr[i];
- }
- }
- cout << "Maximum value: " << max_value << endl;
- }
- void sum_reduction(int arr[], int n) {
- int sum = 0;
- #pragma omp parallel for reduction(+: sum)
- for (int i = 0; i < n; i++) {
- sum += arr[i];
- }
- cout << "Sum: " << sum << endl;
- }
- void average_reduction(int arr[], int n) {
- int sum = 0;
- #pragma omp parallel for reduction(+: sum)
- for (int i = 0; i < n; i++) {
- sum += arr[i];
- }
- cout << "Average: " << (double)sum / (n-1) << endl;
- }
- int main() {
- int *arr,n;
- cout<<"\n enter total no of elements=>";
- cin>>n;
- arr=new int[n];
- cout<<"\n enter elements=>";
- for(int i=0;i<n;i++)
- {
- cin>>arr[i];
- }
- // int arr[] = {5, 2, 9, 1, 7, 6, 8, 3, 4};
- // int n = size(arr);
- min_reduction(arr, n);
- max_reduction(arr, n);
- sum_reduction(arr, n);
- average_reduction(arr, n);
- }
- /*
- Output:
- unix@unix-HP-280-G1-MT:~/codes$ g++ Para_merge.cpp
- unix@unix-HP-280-G1-MT:~/codes$ g++ Min_max.cpp
- unix@unix-HP-280-G1-MT:~/codes$ ./a.out
- enter total no of elements=>5
- enter elements=>2 3 4 5 2
- Minimum value: 2
- Maximum value: 5
- Sum: 16
- Average: 4
- */
- /*
- This code demonstrates how to perform reduction operations (finding minimum, maximum, sum, and average)
- on an array using OpenMP parallelization. Let's go through it step by step:
- 1. **Function Definitions:**
- - `min_reduction`: Finds the minimum value in the array using OpenMP's reduction clause with the `min` operation.
- - `max_reduction`: Finds the maximum value in the array using OpenMP's reduction clause with the `max` operation.
- - `sum_reduction`: Calculates the sum of all elements in the array using OpenMP's reduction clause with the `+` operation.
- - `average_reduction`: Calculates the average of all elements in the array using OpenMP's reduction clause with the `+` operation.
- 2. **`main` Function:**
- - Dynamically allocates memory for the array based on user input for the number of elements `n`.
- - Takes user input for the array elements.
- - Calls each of the reduction functions with the array and its size as arguments.
- 3. **OpenMP Parallelization:**
- - OpenMP directives (`#pragma omp parallel for reduction`) are used to parallelize the loop iterations.
- - The `reduction` clause specifies that each thread maintains a private copy of the reduction variable and
- combines them at the end of the parallel region.
- - For example, in `min_reduction`, each thread maintains a private `min_value`, and at the end of the loop,
- all the private `min_value` variables are combined to find the global minimum.
- 4. **Output:**
- - The code outputs the minimum value, maximum value, sum, and average of the array elements.
- 5. **Sample Output:**
- - For the given input array `{2, 3, 4, 5, 2}`, the output shows:
- - Minimum value: 2
- - Maximum value: 5
- - Sum: 16
- - Average: 4
- This code efficiently utilizes OpenMP parallelization to perform reduction operations on arrays, improving
- performance by leveraging multiple threads to process array elements concurrently.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement