-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathompfor.cpp
More file actions
62 lines (57 loc) · 1.02 KB
/
ompfor.cpp
File metadata and controls
62 lines (57 loc) · 1.02 KB
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
#include "../../utils/utils.hh"
#include "leibniz.hh"
#include <omp.h>
//leibniz.hh.
double parallelfor(long int n, int nthreads){
double ans=0;
#pragma omp parallel for \
num_threads(nthreads) \
schedule(static) \
default(none) \
shared(n) \
reduction(+:ans)
for(long int i=0; i < n; i = i+2)
{
ans += 4.0/(2*i+1);
ans -= 4.0/(2*i+3);
}
return ans;
}
//leibniz.hh.
double ompfor(long int n){
double ans=0;
#pragma omp parallel \
default(none) \
shared(n, ans)
{
double sum=0;
#pragma omp for
for(long int i=0; i < n; i = i+2)
{
sum += 4.0/(2*i+1);
sum -= 4.0/(2*i+3);
}
#pragma omp critical
ans += sum;
}
return ans;
}
//leibniz.hh.
double ompforchunk(long int n, int chunk){
double ans=0;
#pragma omp parallel \
default(none) \
shared(n, ans, chunk)
{
double sum = 0;
#pragma omp for \
schedule(static, chunk)
for(long int i=0; i < n; i = i+2){
sum += 4.0/(2*i+1);
sum -= 4.0/(2*i+3);
}
#pragma omp critical
ans += sum;
}
return ans;
}