Skip to content

Commit b269cc9

Browse files
committed
implement monte carlo tbb
1 parent a5f3241 commit b269cc9

6 files changed

Lines changed: 221 additions & 5 deletions

File tree

File renamed without changes.

tasks/sabirov_s_monte_carlo/omp/src/ops_seq.cpp renamed to tasks/sabirov_s_monte_carlo/omp/src/ops_omp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "sabirov_s_monte_carlo/omp/include/ops_seq.hpp"
1+
#include "sabirov_s_monte_carlo/omp/include/ops_omp.hpp"
22

33
#include <cmath>
44
#include <cstddef>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#pragma once
2+
3+
#include <cmath>
4+
#include <vector>
5+
6+
#include "sabirov_s_monte_carlo/common/include/common.hpp"
7+
#include "task/include/task.hpp"
8+
9+
namespace sabirov_s_monte_carlo {
10+
11+
namespace detail {
12+
13+
inline double EvalLinear(const std::vector<double> &point) {
14+
double s = 0.0;
15+
for (double x : point) {
16+
s += x;
17+
}
18+
return s;
19+
}
20+
21+
inline double EvalSumCubes(const std::vector<double> &point) {
22+
double s = 0.0;
23+
for (double x : point) {
24+
s += x * x * x;
25+
}
26+
return s;
27+
}
28+
29+
inline double EvalCosProduct(const std::vector<double> &point) {
30+
double p = 1.0;
31+
for (double x : point) {
32+
p *= std::cos(x);
33+
}
34+
return p;
35+
}
36+
37+
inline double EvalExpNeg(const std::vector<double> &point) {
38+
double s = 0.0;
39+
for (double x : point) {
40+
s += x;
41+
}
42+
return std::exp(-s);
43+
}
44+
45+
inline double EvalMixedPoly(const std::vector<double> &point) {
46+
double s = 0.0;
47+
for (double x : point) {
48+
s += (x * x) + x;
49+
}
50+
return s;
51+
}
52+
53+
inline double EvalSinSum(const std::vector<double> &point) {
54+
double s = 0.0;
55+
for (double x : point) {
56+
s += std::sin(x);
57+
}
58+
return s;
59+
}
60+
61+
inline double EvalSqrtSum(const std::vector<double> &point) {
62+
double s = 0.0;
63+
for (double x : point) {
64+
s += std::sqrt(x);
65+
}
66+
return s;
67+
}
68+
69+
inline double EvalQuarticSum(const std::vector<double> &point) {
70+
double s = 0.0;
71+
for (double x : point) {
72+
s += x * x * x * x;
73+
}
74+
return s;
75+
}
76+
77+
inline double EvaluateAt(FuncType func_type, const std::vector<double> &point) {
78+
switch (func_type) {
79+
case FuncType::kLinear:
80+
return EvalLinear(point);
81+
case FuncType::kSumCubes:
82+
return EvalSumCubes(point);
83+
case FuncType::kCosProduct:
84+
return EvalCosProduct(point);
85+
case FuncType::kExpNeg:
86+
return EvalExpNeg(point);
87+
case FuncType::kMixedPoly:
88+
return EvalMixedPoly(point);
89+
case FuncType::kSinSum:
90+
return EvalSinSum(point);
91+
case FuncType::kSqrtSum:
92+
return EvalSqrtSum(point);
93+
case FuncType::kQuarticSum:
94+
return EvalQuarticSum(point);
95+
default:
96+
return 0.0;
97+
}
98+
}
99+
100+
} // namespace detail
101+
102+
class SabirovSMonteCarloTBB : public BaseTask {
103+
public:
104+
static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() {
105+
return ppc::task::TypeOfTask::kTBB;
106+
}
107+
explicit SabirovSMonteCarloTBB(const InType &in);
108+
109+
private:
110+
bool ValidationImpl() override;
111+
bool PreProcessingImpl() override;
112+
bool RunImpl() override;
113+
bool PostProcessingImpl() override;
114+
115+
std::vector<double> lower_;
116+
std::vector<double> upper_;
117+
int num_samples_{};
118+
FuncType func_type_{};
119+
};
120+
121+
} // namespace sabirov_s_monte_carlo
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "sabirov_s_monte_carlo/tbb/include/ops_tbb.hpp"
2+
3+
#include <cstddef>
4+
#include <cstdint>
5+
#include <random>
6+
#include <vector>
7+
8+
#include "oneapi/tbb/blocked_range.h"
9+
#include "oneapi/tbb/parallel_reduce.h"
10+
#include "sabirov_s_monte_carlo/common/include/common.hpp"
11+
12+
namespace sabirov_s_monte_carlo {
13+
14+
SabirovSMonteCarloTBB::SabirovSMonteCarloTBB(const InType &in) {
15+
SetTypeOfTask(GetStaticTypeOfTask());
16+
GetInput() = in;
17+
GetOutput() = 0.0;
18+
}
19+
20+
bool SabirovSMonteCarloTBB::ValidationImpl() {
21+
const auto &in = GetInput();
22+
if (in.lower.size() != in.upper.size() || in.lower.empty()) {
23+
return false;
24+
}
25+
if (in.num_samples <= 0) {
26+
return false;
27+
}
28+
for (size_t i = 0; i < in.lower.size(); ++i) {
29+
if (in.lower[i] >= in.upper[i]) {
30+
return false;
31+
}
32+
}
33+
if (in.func_type < FuncType::kLinear || in.func_type > FuncType::kQuarticSum) {
34+
return false;
35+
}
36+
constexpr size_t kMaxDimensions = 10;
37+
return in.lower.size() <= kMaxDimensions;
38+
}
39+
40+
bool SabirovSMonteCarloTBB::PreProcessingImpl() {
41+
const auto &in = GetInput();
42+
lower_ = in.lower;
43+
upper_ = in.upper;
44+
num_samples_ = in.num_samples;
45+
func_type_ = in.func_type;
46+
GetOutput() = 0.0;
47+
return true;
48+
}
49+
50+
bool SabirovSMonteCarloTBB::RunImpl() {
51+
const int dims = static_cast<int>(lower_.size());
52+
53+
std::vector<std::uniform_real_distribution<double>> dists;
54+
dists.reserve(static_cast<size_t>(dims));
55+
for (int j = 0; j < dims; ++j) {
56+
dists.emplace_back(lower_[j], upper_[j]);
57+
}
58+
59+
double volume = 1.0;
60+
for (int j = 0; j < dims; ++j) {
61+
volume *= (upper_[j] - lower_[j]);
62+
}
63+
64+
const FuncType ftype = func_type_;
65+
const int n_samples = num_samples_;
66+
67+
const double sum = tbb::parallel_reduce(tbb::blocked_range<int>(0, n_samples), 0.0,
68+
[&](const tbb::blocked_range<int> &r, double init) {
69+
double local = init;
70+
std::vector<double> point(static_cast<size_t>(dims));
71+
std::seed_seq seed{static_cast<uint32_t>(r.begin()), static_cast<uint32_t>(r.end()),
72+
static_cast<uint32_t>(n_samples)};
73+
std::mt19937 gen(seed);
74+
for (int i = r.begin(); i != r.end(); ++i) {
75+
for (int j = 0; j < dims; ++j) {
76+
point[static_cast<size_t>(j)] = dists[static_cast<size_t>(j)](gen);
77+
}
78+
local += detail::EvaluateAt(ftype, point);
79+
}
80+
return local;
81+
}, [](double a, double b) { return a + b; });
82+
83+
GetOutput() = volume * sum / static_cast<double>(num_samples_);
84+
return true;
85+
}
86+
87+
bool SabirovSMonteCarloTBB::PostProcessingImpl() {
88+
return true;
89+
}
90+
91+
} // namespace sabirov_s_monte_carlo

tasks/sabirov_s_monte_carlo/tests/functional/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#include <vector>
99

1010
#include "sabirov_s_monte_carlo/common/include/common.hpp"
11-
#include "sabirov_s_monte_carlo/omp/include/ops_seq.hpp"
11+
#include "sabirov_s_monte_carlo/omp/include/ops_omp.hpp"
1212
#include "sabirov_s_monte_carlo/seq/include/ops_seq.hpp"
13+
#include "sabirov_s_monte_carlo/tbb/include/ops_tbb.hpp"
1314
#include "util/include/func_test_util.hpp"
1415
#include "util/include/util.hpp"
1516

@@ -196,7 +197,8 @@ const std::array<TestType, 13> kTestParam = {{
196197

197198
const auto kTestTasksList = std::tuple_cat(
198199
ppc::util::AddFuncTask<SabirovSMonteCarloSEQ, InType>(kTestParam, PPC_SETTINGS_sabirov_s_monte_carlo),
199-
ppc::util::AddFuncTask<SabirovSMonteCarloOMP, InType>(kTestParam, PPC_SETTINGS_sabirov_s_monte_carlo));
200+
ppc::util::AddFuncTask<SabirovSMonteCarloOMP, InType>(kTestParam, PPC_SETTINGS_sabirov_s_monte_carlo),
201+
ppc::util::AddFuncTask<SabirovSMonteCarloTBB, InType>(kTestParam, PPC_SETTINGS_sabirov_s_monte_carlo));
200202

201203
const auto kGtestValues = ppc::util::ExpandToValues(kTestTasksList);
202204

tasks/sabirov_s_monte_carlo/tests/performance/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include <tuple>
55

66
#include "sabirov_s_monte_carlo/common/include/common.hpp"
7-
#include "sabirov_s_monte_carlo/omp/include/ops_seq.hpp"
7+
#include "sabirov_s_monte_carlo/omp/include/ops_omp.hpp"
88
#include "sabirov_s_monte_carlo/seq/include/ops_seq.hpp"
9+
#include "sabirov_s_monte_carlo/tbb/include/ops_tbb.hpp"
910
#include "util/include/perf_test_util.hpp"
1011

1112
namespace sabirov_s_monte_carlo {
@@ -37,7 +38,8 @@ namespace {
3738

3839
const auto kAllPerfTasks =
3940
std::tuple_cat(ppc::util::MakePerfTaskTuples<SabirovSMonteCarloSEQ, InType>(PPC_SETTINGS_sabirov_s_monte_carlo),
40-
ppc::util::MakePerfTaskTuples<SabirovSMonteCarloOMP, InType>(PPC_SETTINGS_sabirov_s_monte_carlo));
41+
ppc::util::MakePerfTaskTuples<SabirovSMonteCarloOMP, InType>(PPC_SETTINGS_sabirov_s_monte_carlo),
42+
ppc::util::MakePerfTaskTuples<SabirovSMonteCarloTBB, InType>(PPC_SETTINGS_sabirov_s_monte_carlo));
4143

4244
const auto kGtestValues = ppc::util::TupleToGTestValues(kAllPerfTasks);
4345

0 commit comments

Comments
 (0)