'How would you implement sections with open MP with reduction
I was messing around with openMP. There was this question that asked to apply section to display the calculation result of each variable: totalsection1, totalsection2, and totalsection3. Each section is supposed to display each total value.I've tried implementing the sections before the pragma omp for reduction and within but both give me syntax errors.
#include<omp.h>
#define ARRAYSIZE 4000
int main(int argc, const char* argv[])
{
int ID = 0;
int section1[ARRAYSIZE], section2[ARRAYSIZE], section3[ARRAYSIZE];
int i, totalsection1 = 0, totalsection2 = 0, totalsection3 = 0;
for (i = 0; i < ARRAYSIZE; i++) {
section1[i] = (i + 1) * 10;
}
for (i = 0; i < ARRAYSIZE; i++) {
section2[i] = (i + 1) + 10;
}
for (i = 0; i < ARRAYSIZE; i++) {
section3[i] = (i + 1) * 2;
}
#pragma omp parallel num_threads(3)
{ //start of parallel region
#pragma omp for reduction (+:totalsection1)
for (i = 0; i < ARRAYSIZE; i++) {
#pragma omp critical
totalsection1 = totalsection1 + section1[i];
}
#pragma omp for reduction (+:totalsection2)
for (i = 0; i < ARRAYSIZE; i++) {
#pragma omp critical
totalsection2 = totalsection2 + section2[i];
}
#pragma omp for reduction (+:totalsection3)
for (i = 0; i < ARRAYSIZE; i++) {
#pragma omp critical
totalsection3 = totalsection3 + section3[i];
}
#pragma omp sections
{
#pragma omp section
{
printf("Hello from section 1\n");
printf("The total value for section 1 is : %d", totalsection1);
printf("\n");
}
#pragma omp section
{
printf("Hello from section 2\n");
printf("The total value for section 2 is : %d", totalsection2);
printf("\n");
}
#pragma omp section
{
printf("Hello from section 3\n");
printf("The total value for section 3 is : %d", totalsection3);
printf("\n");
}
}
} //end of parallel region
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
