forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.cpp
More file actions
103 lines (89 loc) · 3.25 KB
/
task.cpp
File metadata and controls
103 lines (89 loc) · 3.25 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
94
95
96
97
98
99
100
101
102
103
#define _CRT_SECURE_NO_WARNINGS
#include "core/task/include/task.hpp"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdlib>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
using namespace std::chrono;
ppc::core::Task::Task(StateOfTesting state_of_testing) : state_of_testing_(state_of_testing) {
auto custom_terminate = []() {
std::cerr << "ORDER OF FUNCTIONS IS NOT RIGHT! \n"
"Expected - \"Validation\", \"PreProcessing\", \"Run\", \"PostProcessing\" \n";
};
std::set_terminate(custom_terminate);
functions_order_.clear();
}
bool ppc::core::Task::Validation() {
InternalOrderTest(__builtin_FUNCTION());
return ValidationImpl();
}
bool ppc::core::Task::PreProcessing() {
InternalOrderTest(__builtin_FUNCTION());
if (state_of_testing_ == StateOfTesting::kFunc) {
InternalTimeTest(__builtin_FUNCTION());
}
return PreProcessingImpl();
}
bool ppc::core::Task::Run() {
InternalOrderTest(__builtin_FUNCTION());
return RunImpl();
}
bool ppc::core::Task::PostProcessing() {
InternalOrderTest(__builtin_FUNCTION());
if (state_of_testing_ == StateOfTesting::kFunc) {
InternalTimeTest(__builtin_FUNCTION());
}
return PostProcessingImpl();
}
void ppc::core::Task::InternalOrderTest(const std::string &str) {
functions_order_.push_back(str);
if (str == "PostProcessing" && IsFullPipelineStage()) {
functions_order_.clear();
} else {
was_worked_ = true;
}
}
void ppc::core::Task::InternalTimeTest(const std::string &str) {
if (str == "PreProcessing") {
tmp_time_point_ = std::chrono::high_resolution_clock::now();
}
if (str == "PostProcessing") {
auto duration = duration_cast<nanoseconds>(high_resolution_clock::now() - tmp_time_point_).count();
auto diff = static_cast<double>(duration) * 1e-9;
std::stringstream err_msg;
// NOLINTNEXTLINE(concurrency-mt-unsafe)
if (auto *env = std::getenv("PPC_IGNORE_TEST_TIME_LIMIT"); env != nullptr && std::string(env) == "1") {
err_msg << "Test time:" << std::fixed << std::setprecision(10) << diff << " (no time limit)" << '\n';
} else if (diff < kMaxTestTime) {
err_msg << "Test time:" << std::fixed << std::setprecision(10) << diff << '\n';
} else {
err_msg << "\nTask execute time need to be: ";
err_msg << "time < " << kMaxTestTime << " secs.\n";
err_msg << "Original time in secs: " << diff << '\n';
throw std::runtime_error(err_msg.str().c_str());
}
}
}
bool ppc::core::Task::IsFullPipelineStage() {
auto it = std::adjacent_find(functions_order_.begin() + 2,
functions_order_.begin() + static_cast<long>(functions_order_.size() - 2),
std::not_equal_to<>());
return (functions_order_.size() >= 4 && functions_order_[0] == "Validation" &&
functions_order_[1] == "PreProcessing" && functions_order_[2] == "Run" &&
it == (functions_order_.begin() + static_cast<long>(functions_order_.size() - 2)) &&
functions_order_[functions_order_.size() - 1] == "PostProcessing");
}
ppc::core::Task::~Task() {
if (!functions_order_.empty() || !was_worked_) {
std::terminate();
} else {
functions_order_.clear();
}
}