-
Notifications
You must be signed in to change notification settings - Fork 9
/
parallel.cpp
54 lines (50 loc) · 1 KB
/
parallel.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
#include "../../utils/utils.hh"
#include "leibniz.hh"
#include <omp.h>
//leibniz.hh.
double parallel(long int n, int nthreads){
assrt(nthreads%2==0);
double ans=0;
#pragma omp parallel \
num_threads(nthreads) \
default(none) \
shared(n, nthreads, ans)
{
double sum=0;
int offset = omp_get_thread_num();
for(long int i=offset; i < n; i=i+nthreads)
sum += 4.0/(2*i+1);
#pragma omp critical
if(offset%2==0)
ans += sum;
else
ans -= sum;
}
return ans;
}
//leibniz.hh.
void partialsum(int offset, long int n, int nthreads,
double & ans){
double sum = 0;
for(long int i=offset; i < n; i=i+nthreads)
sum += 4.0/(2*i+1);
#pragma omp critical
if(offset%2==0)
ans += sum;
else
ans -= sum;
}
//leibniz.hh.
double parallelX(long int n, int nthreads){
assrt(nthreads%2==0);
double ans=0;
#pragma omp parallel \
num_threads(nthreads) \
default(none) \
shared(n, nthreads, ans)
{
int offset = omp_get_thread_num();
partialsum(offset, n, nthreads, ans);
}
return ans;
}