-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_integral_omp.cpp
More file actions
69 lines (55 loc) · 1.57 KB
/
Copy pathparallel_integral_omp.cpp
File metadata and controls
69 lines (55 loc) · 1.57 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
#include "parallel_integral_omp.hpp"
#include <iostream>
#include <cmath>
namespace parallel_integral {
namespace {
extern "C" double omp_get_wtime(void);
static const double kAccuracy = 0.0001;
double Function(const double& arg) {
return 3*std::pow(arg, -1) + 2;
}
struct AccuracyParameters {
unsigned long parts;
double step;
const Limits limits;
AccuracyParameters(std::ifstream& input_file): limits(input_file){
parts = 2;
step = (limits.right - limits.left) / parts;
}
void Increment() {
parts *= 2;
step = (limits.right - limits.left) / parts;
}
operator double() {
return step;
}
};
} //namespace
Limits::Limits(std::ifstream& input_file) {
input_file.open("input.txt");
input_file >> left >> right;
input_file.close();
}
ResultAndTime ComputeIntegral() {
std::ifstream input_file;
AccuracyParameters accuracy_parameters(input_file);
double previous_result = 0, result = 0;
unsigned long i = 0;
double time = omp_get_wtime();
do {
previous_result = result;
result = 0;
accuracy_parameters.Increment();
#pragma omp parallel shared(accuracy_parameters) reduction(+:result)
{
#pragma omp for
for (i = 0; i < accuracy_parameters.parts; ++i) {
double x = accuracy_parameters.limits.left + i * accuracy_parameters.step;
result += accuracy_parameters.step * Function(x + accuracy_parameters.step / 2);
}
}
} while (fabs(result - previous_result) >= kAccuracy);
time = omp_get_wtime() - time;
return ResultAndTime(result, time);
}
}