Skip to Content

Hpc 03 min and max reduction:

#include <iostream>

#include <omp.h>

#include <vector>

#include <climits>

using namespace std;

int main()

{

    int n;

    cout << "Enter number of elements: ";

    cin >> n;

    vector<int> arr(n);

    cout << "Enter " << n << " elements:\n";

    for (int i = 0; i < n; ++i)

        cin >> arr[i];

    // Sequential Operations

    int seq_min = arr[0], seq_max = arr[0], seq_sum = 0;

    double seq_start = omp_get_wtime();

    for (int i = 0; i < n; ++i)

    {

        if (arr[i] < seq_min)

            seq_min = arr[i];

        if (arr[i] > seq_max)

            seq_max = arr[i];

        seq_sum += arr[i];

    }

    double seq_end = omp_get_wtime();

    // Parallel Reduction Operations

    int par_min = INT_MAX, par_max = INT_MIN, par_sum = 0;

    double par_start = omp_get_wtime();

 

#pragma omp parallel for reduction(min : par_min) reduction(max : par_max) reduction(+ : par_sum)

    for (int i = 0; i < n; ++i)

    {

        par_min = min(par_min, arr[i]);

        par_max = max(par_max, arr[i]);

        par_sum += arr[i];

    }

    double par_end = omp_get_wtime();

    // Output Results

    cout << "\nSequential Results:\n";

    cout << "Min: " << seq_min << ", Max: " << seq_max << ", Sum: " << seq_sum

         << ", Average: " << (double)seq_sum / n << endl;

    cout << "Time taken: " << (seq_end - seq_start) << " seconds\n";

    cout << "\nParallel Results (using OpenMP reduction):\n";

    cout << "Min: " << par_min << ", Max: " << par_max << ", Sum: " << par_sum

         << ", Average: " << (double)par_sum / n << endl;

    cout << "Time taken: " << (par_end - par_start) << " seconds\n";

    return 0;

}

Output:

C:\Users\Admin\Desktop\Final year\Practical code\LP-V HPC>g++ -fopenmp min_max.cpp -o min_max

C:\Users\Admin\Desktop\Final year\Practical code\LP-V HPC>.\min_max

Enter number of elements: 5

Enter 5 elements:

10 20 5 15 30

Sequential Results:

Min: 5, Max: 30, Sum: 80, Average: 16

Time taken: 0 seconds

Parallel Results (using OpenMP reduction):

Min: 5, Max: 30, Sum: 80, Average: 16

Time taken: 0.000999928 seconds


HPC 03 Min and Max Commented:

#include <iostream>             // For input/output operations

#include <omp.h>                // For OpenMP parallel programming

#include <vector>               // To use the vector container

#include <climits>              // For INT_MAX and INT_MIN

using namespace std;            // Use the standard namespace


int main()                      // Main function

{

    int n;                      // Variable to store number of elements

    cout << "Enter number of elements: ";  // Prompt user for input

    cin >> n;                   // Take input for number of elements


    vector<int> arr(n);         // Declare a vector of size n

    cout << "Enter " << n << " elements:\n";  // Prompt to enter elements

    for (int i = 0; i < n; ++i) // Loop to read n elements

        cin >> arr[i];          // Store input in the array


    // Sequential Operations

    int seq_min = arr[0], seq_max = arr[0], seq_sum = 0;  // Initialize min, max and sum with first element and 0

    double seq_start = omp_get_wtime();  // Start time for sequential computation


    for (int i = 0; i < n; ++i)          // Loop over all elements

    {

        if (arr[i] < seq_min)            // If current element is less than seq_min

            seq_min = arr[i];            // Update seq_min

        if (arr[i] > seq_max)            // If current element is greater than seq_max

            seq_max = arr[i];            // Update seq_max

        seq_sum += arr[i];               // Add current element to seq_sum

    }


    double seq_end = omp_get_wtime();    // End time for sequential computation


    // Parallel Reduction Operations

    int par_min = INT_MAX, par_max = INT_MIN, par_sum = 0;  // Initialize min, max, sum with extreme values

    double par_start = omp_get_wtime();   // Start time for parallel computation


#pragma omp parallel for reduction(min : par_min) reduction(max : par_max) reduction(+ : par_sum)

    for (int i = 0; i < n; ++i)           // Parallel for loop with OpenMP reduction for min, max, sum

    {

        par_min = min(par_min, arr[i]);   // Compute minimum in parallel

        par_max = max(par_max, arr[i]);   // Compute maximum in parallel

        par_sum += arr[i];                // Compute sum in parallel

    }


    double par_end = omp_get_wtime();     // End time for parallel computation


    // Output Results

    cout << "\nSequential Results:\n";    // Display header for sequential results

    cout << "Min: " << seq_min << ", Max: " << seq_max << ", Sum: " << seq_sum

         << ", Average: " << (double)seq_sum / n << endl;  // Output sequential stats

    cout << "Time taken: " << (seq_end - seq_start) << " seconds\n"; // Output time taken


    cout << "\nParallel Results (using OpenMP reduction):\n"; // Display header for parallel results

    cout << "Min: " << par_min << ", Max: " << par_max << ", Sum: " << par_sum

         << ", Average: " << (double)par_sum / n << endl;  // Output parallel stats

    cout << "Time taken: " << (par_end - par_start) << " seconds\n"; // Output time taken


    return 0; // Return from main function

}