-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOMPBegin6.cpp
More file actions
93 lines (86 loc) · 1.65 KB
/
OMPBegin6.cpp
File metadata and controls
93 lines (86 loc) · 1.65 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "pt4.h"
#include "omp.h"
double np_F(double x, int n)
{
double res = 0.0;
for (int i = 1; i <= n; i++)
{
double z = 0.0;
for (int j = 1; j <= i + n; j++)
{
z += (j + sin(x + j)) / (2 * i * j - 1);
}
res += 1 / z;
}
return res;
}
double p_F(double x, int n)
{
double res = 0.0;
#pragma omp parallel num_threads(4) reduction(+:res)
{
int num = omp_get_thread_num();
int num_threads = omp_get_num_threads();
int num_procs = omp_get_num_procs();
int count = 0;
double time = omp_get_wtime();
if (num == 0)
{
Show("num_procs: ", num_procs);
ShowLine();
Show("num_threads: ", num_threads);
ShowLine();
}
for (int i = 1; i <= n; i += 8)
{
double z = 0.0;
int i1 = i + num;
int i2 = i + 7 - num;
for (int j = 1; j <= i1 + n; j++)
{
z += (j + sin(x + j)) / (2 * i1 * j - 1);
count++;
}
res += 1 / z;
z = 0.0;
for (int j = 1; j <= i2 + n; j++)
{
z += (j + sin(x + j)) / (2 * i2 * j - 1);
count++;
}
res += 1 / z;
}
Show("thread_num: ", num);
ShowLine();
Show("Count: ", count);
ShowLine();
Show("Thread time: ", omp_get_wtime() - time);
ShowLine();
}
return res;
}
void Solve()
{
Task("OMPBegin6");
//non parallel
double x = 0.0, res = 0.0, start = omp_get_wtime();;
int n = 0;
pt >> x >> n;
res = np_F(x, n);
Show("Non-parallel time: ");
double np_time = omp_get_wtime() - start;
Show(np_time);
ShowLine();
pt << res;
//parallel
pt >> x >> n;
start = omp_get_wtime();
res = p_F(x, n);
double p_time = omp_get_wtime() - start;
Show("Total parallel time: ");
Show(p_time);
ShowLine();
pt << res;
Show("Rate: ");
Show(np_time / p_time);
}