forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops_stl.cpp
More file actions
58 lines (46 loc) · 1.41 KB
/
ops_stl.cpp
File metadata and controls
58 lines (46 loc) · 1.41 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
#include "example_threads/stl/include/ops_stl.hpp"
#include <atomic>
#include <numeric>
#include <thread>
#include <vector>
#include "example_threads/common/include/common.hpp"
#include "util/include/util.hpp"
namespace nesterov_a_test_task_threads {
NesterovATestTaskSTL::NesterovATestTaskSTL(const InType &in) {
SetTypeOfTask(GetStaticTypeOfTask());
GetInput() = in;
GetOutput() = 0;
}
bool NesterovATestTaskSTL::ValidationImpl() {
return (GetInput() > 0) && (GetOutput() == 0);
}
bool NesterovATestTaskSTL::PreProcessingImpl() {
GetOutput() = 2 * GetInput();
return GetOutput() > 0;
}
bool NesterovATestTaskSTL::RunImpl() {
for (InType i = 0; i < GetInput(); i++) {
for (InType j = 0; j < GetInput(); j++) {
for (InType k = 0; k < GetInput(); k++) {
std::vector<InType> tmp(i + j + k, 1);
GetOutput() += std::accumulate(tmp.begin(), tmp.end(), 0);
GetOutput() -= i + j + k;
}
}
}
const int num_threads = ppc::util::GetNumThreads();
std::vector<std::thread> threads(num_threads);
GetOutput() *= num_threads;
std::atomic<int> counter(0);
for (std::thread &thread : threads) {
thread = std::thread([&counter]() { counter++; });
thread.join();
}
GetOutput() /= counter;
return GetOutput() > 0;
}
bool NesterovATestTaskSTL::PostProcessingImpl() {
GetOutput() -= GetInput();
return GetOutput() > 0;
}
} // namespace nesterov_a_test_task_threads