mirror of
https://github.com/OpenMP/Examples.git
synced 2025-04-08 07:32:12 +01:00
40 lines
704 B
C++
40 lines
704 B
C++
/*
|
|
* @@name: scope_reduction.1c
|
|
* @@type: C++
|
|
* @@compilable: yes
|
|
* @@linkable: no
|
|
* @@expect: success
|
|
* @@version: omp_5.1
|
|
*/
|
|
#include <stdio.h>
|
|
void do_work(int n, float a[], float &s)
|
|
{
|
|
float loc_s = 0.0f; // local sum
|
|
static int nthrs;
|
|
#pragma omp for
|
|
for (int i = 0; i < n; i++)
|
|
loc_s += a[i];
|
|
#pragma omp single
|
|
{
|
|
s = 0.0f; // total sum
|
|
nthrs = 0;
|
|
}
|
|
#pragma omp scope reduction(+:s,nthrs)
|
|
{
|
|
s += loc_s;
|
|
nthrs++;
|
|
}
|
|
#pragma omp masked
|
|
printf("total sum = %f, nthrs = %d\n", s, nthrs);
|
|
}
|
|
|
|
float work(int n, float a[])
|
|
{
|
|
float s;
|
|
#pragma omp parallel
|
|
{
|
|
do_work(n, a, s);
|
|
}
|
|
return s;
|
|
}
|