0% found this document useful (0 votes)
11 views1 page

Sum Avg Paarller Reduction

This C++ program initializes an array of 20 random integers and calculates the minimum, maximum, sum, and average of the array elements using OpenMP for parallel processing. It prints the array and the results of the calculations, including the contributions of each thread during processing. The program demonstrates the use of parallel reduction to efficiently compute these statistics.

Uploaded by

AARTI. SUTAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Sum Avg Paarller Reduction

This C++ program initializes an array of 20 random integers and calculates the minimum, maximum, sum, and average of the array elements using OpenMP for parallel processing. It prints the array and the results of the calculations, including the contributions of each thread during processing. The program demonstrates the use of parallel reduction to efficiently compute these statistics.

Uploaded by

AARTI. SUTAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

#include <omp.h>
#include <cstdlib>
#include <ctime>

#include <climits>

int main() {
const int n = 20;
int arr[n];

// Initialize random array


std::srand(std::time(nullptr));
for (int i = 0; i < n; i++) {
arr[i] = std::rand() % 100; // values from 0 to 99
}

std::cout << "Array: ";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << "\n\n";

int minVal = INT_MAX;


int maxVal = INT_MIN;
long long sum = 0;

// Start parallel region


#pragma omp parallel for reduction(min:minVal) reduction(max:maxVal)
reduction(+:sum)
for (int i = 0; i < n; i++) {
if (arr[i] < minVal) minVal = arr[i];
if (arr[i] > maxVal) maxVal = arr[i];
sum += arr[i];

int tid = omp_get_thread_num();


std::cout << "Thread " << tid << " processing element " << arr[i] << "\n";
}

double average = static_cast<double>(sum) / n;

std::cout << "\nResults using Parallel Reduction:\n";


std::cout << "Minimum Value: " << minVal << "\n";
std::cout << "Maximum Value: " << maxVal << "\n";
std::cout << "Sum: " << sum << "\n";
std::cout << "Average: " << average << "\n";

return 0;
}

You might also like