for this code explain How are the private variables created by the reduction parameter initialized? Code #include "pch.h" #include #include #include using namespace std; int main() { int n; cout << "Enter Num:"; cin >> n; int sum = 0; int thread_num; # pragma omp parallel num_threads(2) reduction(+:sum) { if (omp_get_thread_num() == 0) { for (int i = 0; i <= n / 2; ++i) { sum = sum + i; } cout << "[" << omp_get_thread_num() << "]: Sum = " << sum << "\n"; } else { for (int i = n / 2 + 1; i <= n; ++i) { sum = sum + i; } cout << "[" << omp_get_thread_num() << "]: Sum = " << sum << "\n"; } } cout << "Sum = " << sum; }
for this code explain How are the private variables created by the reduction parameter initialized?
Code
#include "pch.h"
#include <iostream>
#include <omp.h>
#include <windows.h>
using namespace std;
int main()
{
int n;
cout << "Enter Num:";
cin >> n;
int sum = 0;
int thread_num;
# pragma omp parallel num_threads(2) reduction(+:sum)
{
if (omp_get_thread_num() == 0) {
for (int i = 0; i <= n / 2; ++i) {
sum = sum + i;
}
cout << "[" << omp_get_thread_num() << "]: Sum = " << sum << "\n";
}
else {
for (int i = n / 2 + 1; i <= n; ++i) {
sum = sum + i;
}
cout << "[" << omp_get_thread_num() << "]: Sum = " << sum << "\n";
}
}
cout << "Sum = " << sum;
}
Step by step
Solved in 2 steps