-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallelAggregateOps.cpp
72 lines (67 loc) · 1.47 KB
/
parallelAggregateOps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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) << endl;
}
int main()
{
int *arr, n;
cout << "Enter total no of elements => ";
cin >> n;
arr = new int[n];
cout << "Enter elements:"<< endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout<<"------------ Results ------------"<<endl;
min_reduction(arr, n);
max_reduction(arr, n);
sum_reduction(arr, n);
average_reduction(arr, n);
}