Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "sabirov_s_monte_carlo/omp/include/ops_seq.hpp"
#include "sabirov_s_monte_carlo/omp/include/ops_omp.hpp"

#include <cmath>
#include <cstddef>
Expand Down
121 changes: 121 additions & 0 deletions tasks/sabirov_s_monte_carlo/tbb/include/ops_tbb.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#pragma once

#include <cmath>
#include <vector>

#include "sabirov_s_monte_carlo/common/include/common.hpp"
#include "task/include/task.hpp"

namespace sabirov_s_monte_carlo {

namespace detail {

inline double EvalLinear(const std::vector<double> &point) {
double s = 0.0;
for (double x : point) {
s += x;
}
return s;
}

inline double EvalSumCubes(const std::vector<double> &point) {
double s = 0.0;
for (double x : point) {
s += x * x * x;
}
return s;
}

inline double EvalCosProduct(const std::vector<double> &point) {
double p = 1.0;
for (double x : point) {
p *= std::cos(x);
}
return p;
}

inline double EvalExpNeg(const std::vector<double> &point) {
double s = 0.0;
for (double x : point) {
s += x;
}
return std::exp(-s);
}

inline double EvalMixedPoly(const std::vector<double> &point) {
double s = 0.0;
for (double x : point) {
s += (x * x) + x;
}
return s;
}

inline double EvalSinSum(const std::vector<double> &point) {
double s = 0.0;
for (double x : point) {
s += std::sin(x);
}
return s;
}

inline double EvalSqrtSum(const std::vector<double> &point) {
double s = 0.0;
for (double x : point) {
s += std::sqrt(x);
}
return s;
}

inline double EvalQuarticSum(const std::vector<double> &point) {
double s = 0.0;
for (double x : point) {
s += x * x * x * x;
}
return s;
}

inline double EvaluateAt(FuncType func_type, const std::vector<double> &point) {
switch (func_type) {
case FuncType::kLinear:
return EvalLinear(point);
case FuncType::kSumCubes:
return EvalSumCubes(point);
case FuncType::kCosProduct:
return EvalCosProduct(point);
case FuncType::kExpNeg:
return EvalExpNeg(point);
case FuncType::kMixedPoly:
return EvalMixedPoly(point);
case FuncType::kSinSum:
return EvalSinSum(point);
case FuncType::kSqrtSum:
return EvalSqrtSum(point);
case FuncType::kQuarticSum:
return EvalQuarticSum(point);
default:
return 0.0;
}
}

} // namespace detail

class SabirovSMonteCarloTBB : public BaseTask {
public:
static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() {
return ppc::task::TypeOfTask::kTBB;
}
explicit SabirovSMonteCarloTBB(const InType &in);

private:
bool ValidationImpl() override;
bool PreProcessingImpl() override;
bool RunImpl() override;
bool PostProcessingImpl() override;

std::vector<double> lower_;
std::vector<double> upper_;
int num_samples_{};
FuncType func_type_{};
};

} // namespace sabirov_s_monte_carlo
91 changes: 91 additions & 0 deletions tasks/sabirov_s_monte_carlo/tbb/src/ops_tbb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "sabirov_s_monte_carlo/tbb/include/ops_tbb.hpp"

#include <cstddef>
#include <cstdint>
#include <random>
#include <vector>

#include "oneapi/tbb/blocked_range.h"
#include "oneapi/tbb/parallel_reduce.h"
#include "sabirov_s_monte_carlo/common/include/common.hpp"

namespace sabirov_s_monte_carlo {

SabirovSMonteCarloTBB::SabirovSMonteCarloTBB(const InType &in) {
SetTypeOfTask(GetStaticTypeOfTask());
GetInput() = in;
GetOutput() = 0.0;
}

bool SabirovSMonteCarloTBB::ValidationImpl() {
const auto &in = GetInput();
if (in.lower.size() != in.upper.size() || in.lower.empty()) {
return false;
}
if (in.num_samples <= 0) {
return false;
}
for (size_t i = 0; i < in.lower.size(); ++i) {
if (in.lower[i] >= in.upper[i]) {
return false;
}
}
if (in.func_type < FuncType::kLinear || in.func_type > FuncType::kQuarticSum) {
return false;
}
constexpr size_t kMaxDimensions = 10;
return in.lower.size() <= kMaxDimensions;
}

bool SabirovSMonteCarloTBB::PreProcessingImpl() {
const auto &in = GetInput();
lower_ = in.lower;
upper_ = in.upper;
num_samples_ = in.num_samples;
func_type_ = in.func_type;
GetOutput() = 0.0;
return true;
}

bool SabirovSMonteCarloTBB::RunImpl() {
const int dims = static_cast<int>(lower_.size());

std::vector<std::uniform_real_distribution<double>> dists;
dists.reserve(static_cast<size_t>(dims));
for (int j = 0; j < dims; ++j) {
dists.emplace_back(lower_[j], upper_[j]);
}

double volume = 1.0;
for (int j = 0; j < dims; ++j) {
volume *= (upper_[j] - lower_[j]);
}

const FuncType ftype = func_type_;
const int n_samples = num_samples_;

const double sum = tbb::parallel_reduce(tbb::blocked_range<int>(0, n_samples), 0.0,
[&](const tbb::blocked_range<int> &r, double init) {
double local = init;
std::vector<double> point(static_cast<size_t>(dims));
std::seed_seq seed{static_cast<uint32_t>(r.begin()), static_cast<uint32_t>(r.end()),
static_cast<uint32_t>(n_samples)};
std::mt19937 gen(seed);
for (int i = r.begin(); i != r.end(); ++i) {
for (int j = 0; j < dims; ++j) {
point[static_cast<size_t>(j)] = dists[static_cast<size_t>(j)](gen);
}
local += detail::EvaluateAt(ftype, point);
}
return local;
}, [](double a, double b) { return a + b; });

GetOutput() = volume * sum / static_cast<double>(num_samples_);
return true;
}

bool SabirovSMonteCarloTBB::PostProcessingImpl() {
return true;
}

} // namespace sabirov_s_monte_carlo
6 changes: 4 additions & 2 deletions tasks/sabirov_s_monte_carlo/tests/functional/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include <vector>

#include "sabirov_s_monte_carlo/common/include/common.hpp"
#include "sabirov_s_monte_carlo/omp/include/ops_seq.hpp"
#include "sabirov_s_monte_carlo/omp/include/ops_omp.hpp"
#include "sabirov_s_monte_carlo/seq/include/ops_seq.hpp"
#include "sabirov_s_monte_carlo/tbb/include/ops_tbb.hpp"
#include "util/include/func_test_util.hpp"
#include "util/include/util.hpp"

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

const auto kTestTasksList = std::tuple_cat(
ppc::util::AddFuncTask<SabirovSMonteCarloSEQ, InType>(kTestParam, PPC_SETTINGS_sabirov_s_monte_carlo),
ppc::util::AddFuncTask<SabirovSMonteCarloOMP, InType>(kTestParam, PPC_SETTINGS_sabirov_s_monte_carlo));
ppc::util::AddFuncTask<SabirovSMonteCarloOMP, InType>(kTestParam, PPC_SETTINGS_sabirov_s_monte_carlo),
ppc::util::AddFuncTask<SabirovSMonteCarloTBB, InType>(kTestParam, PPC_SETTINGS_sabirov_s_monte_carlo));

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

Expand Down
6 changes: 4 additions & 2 deletions tasks/sabirov_s_monte_carlo/tests/performance/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include <tuple>

#include "sabirov_s_monte_carlo/common/include/common.hpp"
#include "sabirov_s_monte_carlo/omp/include/ops_seq.hpp"
#include "sabirov_s_monte_carlo/omp/include/ops_omp.hpp"
#include "sabirov_s_monte_carlo/seq/include/ops_seq.hpp"
#include "sabirov_s_monte_carlo/tbb/include/ops_tbb.hpp"
#include "util/include/perf_test_util.hpp"

namespace sabirov_s_monte_carlo {
Expand Down Expand Up @@ -37,7 +38,8 @@ namespace {

const auto kAllPerfTasks =
std::tuple_cat(ppc::util::MakePerfTaskTuples<SabirovSMonteCarloSEQ, InType>(PPC_SETTINGS_sabirov_s_monte_carlo),
ppc::util::MakePerfTaskTuples<SabirovSMonteCarloOMP, InType>(PPC_SETTINGS_sabirov_s_monte_carlo));
ppc::util::MakePerfTaskTuples<SabirovSMonteCarloOMP, InType>(PPC_SETTINGS_sabirov_s_monte_carlo),
ppc::util::MakePerfTaskTuples<SabirovSMonteCarloTBB, InType>(PPC_SETTINGS_sabirov_s_monte_carlo));

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

Expand Down
Loading