forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTbb.cpp
More file actions
109 lines (78 loc) · 3.16 KB
/
Copy pathTbb.cpp
File metadata and controls
109 lines (78 loc) · 3.16 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
104
105
106
107
108
109
// Copyright (C) 2012-2024, UNIGE and contributors (for the Euclid Science Ground Segment)
// This file is part of Elements <https://gitlab.euclid-sgs.uk/ST-TOOLS/Elements>
// SPDX-License-Identifier: LGPL-3.0-or-later
/// @file Tbb.cpp
/// @brief Example using the Threading Building Blocks
/// @date 2024-09-27
/// @author Hubert Degaudenzi
#include <chrono> // for high_resolution_clock
#include <iostream>
#include <map> // for maps
#include <string> // for strings
#include <thread> // for std::thread
#include <oneapi/tbb.h> // for the Threading Building Blocks
#include "ElementsKernel/Main.h" // for MAIN_FOR
#include "ElementsKernel/Program.h" // for Program
#include "ElementsKernel/Threading.h" // for initBuildingBlocks
#include "ElementsKernel/Unused.h" // for ELEMENTS_UNUSED
using namespace oneapi::tbb;
namespace Elements {
auto LOG = Logging::getLogger("Tbb");
namespace Examples {
int simpleSum(const int& first_number, const int& last_number) {
int sum = 0;
for (auto i = first_number; i < last_number; i++) {
sum += i;
}
return sum;
}
int parallelSum(const int& first_number, const int& last_number) {
const int sum = parallel_reduce(
blocked_range(first_number, last_number), 0,
[](blocked_range<int> const& r, int init) -> int {
for (int v = r.begin(); v != r.end(); v++) {
init += v;
}
return init;
},
[](const int lhs, const int rhs) -> int {
return lhs + rhs;
});
return sum;
}
class Tbb final : public Program {
public:
/**
* Executes the main operation of the program. This method serves as the entry point
* for the application's logic and handles the primary workflow of the system.
*
* @param args an array of command-line arguments passed to the program
* @return void does not return any value as it serves as a procedural entry point
*/
ExitCode mainMethod(ELEMENTS_UNUSED std::map<std::string, VariableValue>& args) override {
namespace chrono = std::chrono;
Threading::initBuildingBlocks();
using clock = chrono::high_resolution_clock;
using ms = chrono::microseconds;
const auto start = clock::now();
constexpr int first_number = 1;
constexpr int last_number = 10001;
const auto simple_sum = simpleSum(first_number, last_number);
LOG.info() << "Simple sum: " << simple_sum;
const auto simple_stop = clock::now();
const auto simple_duration = chrono::duration_cast<ms>(simple_stop - start);
LOG.info() << "Simple duration: " << simple_duration.count();
const auto parallel_sum = parallelSum(first_number, last_number);
LOG.info() << "Parallel sum: " << parallel_sum;
const auto parallel_stop = clock::now();
const auto parallel_duration = chrono::duration_cast<ms>(parallel_stop - simple_stop);
LOG.info() << "Parallel duration: " << parallel_duration.count();
const auto stop = clock::now();
const auto total_duration = chrono::duration_cast<ms>(stop - start);
LOG.info() << "Total duration: " << total_duration.count();
return ExitCode::OK;
}
};
} // namespace Examples
} // namespace Elements
MAIN_FOR(Elements::Examples::Tbb)