Skip to content
Merged
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
31 changes: 31 additions & 0 deletions tasks/gasenin_l_djstra/tbb/include/ops_tbb.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <limits>
#include <utility>
#include <vector>

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

namespace gasenin_l_djstra {

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

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

int vertex_count_ = 0;
std::vector<std::vector<std::pair<int, int>>> adj_;
std::vector<int> dist_;
static constexpr int kInf = std::numeric_limits<int>::max() / 2;
};

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

#include <tbb/tbb.h>

#include <algorithm>
#include <cstddef>
#include <vector>

#include "gasenin_l_djstra/common/include/common.hpp"

namespace gasenin_l_djstra {

GaseninLDjstraTBB::GaseninLDjstraTBB(const InType &in) {
SetTypeOfTask(GetStaticTypeOfTask());
GetInput() = in;
GetOutput() = 0;
}

bool GaseninLDjstraTBB::ValidationImpl() {
return (GetInput() > 0) && (GetOutput() == 0);
}

bool GaseninLDjstraTBB::PreProcessingImpl() {
vertex_count_ = GetInput();
adj_.resize(vertex_count_);
for (int i = 0; i < vertex_count_ - 1; ++i) {
adj_[i].emplace_back(i + 1, 1);
}
dist_.assign(vertex_count_, kInf);
dist_[0] = 0;
return true;
}

bool GaseninLDjstraTBB::RunImpl() {
std::vector<bool> visited(vertex_count_, false);

for (int i = 0; i < vertex_count_; ++i) {
int u = -1;
int min_dist = kInf;
for (int j = 0; j < vertex_count_; ++j) {
if (!visited[j] && dist_[j] < min_dist) {
min_dist = dist_[j];
u = j;
}
}
if (u == -1 || dist_[u] == kInf) {
break;
}
visited[u] = true;

const auto &neighbors = adj_[u];
tbb::parallel_for(static_cast<size_t>(0), neighbors.size(), [&](size_t idx) {
int v = neighbors[idx].first;
int w = neighbors[idx].second;
int new_dist = dist_[u] + w;
dist_[v] = std::min(new_dist, dist_[v]);
});
}
return true;
}

bool GaseninLDjstraTBB::PostProcessingImpl() {
int sum = 0;
for (int d : dist_) {
if (d < kInf) {
sum += d;
}
}
GetOutput() = sum;
return true;
}

} // namespace gasenin_l_djstra
4 changes: 3 additions & 1 deletion tasks/gasenin_l_djstra/tests/functional/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "gasenin_l_djstra/common/include/common.hpp"
#include "gasenin_l_djstra/omp/include/ops_omp.hpp"
#include "gasenin_l_djstra/seq/include/ops_seq.hpp"
#include "gasenin_l_djstra/tbb/include/ops_tbb.hpp"
#include "util/include/func_test_util.hpp"
#include "util/include/util.hpp"

Expand Down Expand Up @@ -49,7 +50,8 @@ const std::array<TestType, 3> kTestParam = {std::make_tuple(3, "3"), std::make_t

const auto kSeqTasks = ppc::util::AddFuncTask<GaseninLDjstraSEQ, InType>(kTestParam, PPC_SETTINGS_gasenin_l_djstra);
const auto kOmpTasks = ppc::util::AddFuncTask<GaseninLDjstraOMP, InType>(kTestParam, PPC_SETTINGS_gasenin_l_djstra);
const auto kTestTasksList = std::tuple_cat(kSeqTasks, kOmpTasks);
const auto kTbbTasks = ppc::util::AddFuncTask<GaseninLDjstraTBB, InType>(kTestParam, PPC_SETTINGS_gasenin_l_djstra);
const auto kTestTasksList = std::tuple_cat(kSeqTasks, kOmpTasks, kTbbTasks);

const auto kGtestValues = ppc::util::ExpandToValues(kTestTasksList);
const auto kPerfTestName = GaseninLRunFuncTestsThreads::PrintFuncTestName<GaseninLRunFuncTestsThreads>;
Expand Down
4 changes: 3 additions & 1 deletion tasks/gasenin_l_djstra/tests/performance/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "gasenin_l_djstra/common/include/common.hpp"
#include "gasenin_l_djstra/omp/include/ops_omp.hpp"
#include "gasenin_l_djstra/seq/include/ops_seq.hpp"
#include "gasenin_l_djstra/tbb/include/ops_tbb.hpp"
#include "util/include/perf_test_util.hpp"

namespace gasenin_l_djstra {
Expand Down Expand Up @@ -35,7 +36,8 @@ namespace {

const auto kSeqPerfTasks = ppc::util::MakeAllPerfTasks<InType, GaseninLDjstraSEQ>(PPC_SETTINGS_gasenin_l_djstra);
const auto kOmpPerfTasks = ppc::util::MakeAllPerfTasks<InType, GaseninLDjstraOMP>(PPC_SETTINGS_gasenin_l_djstra);
const auto kAllPerfTasks = std::tuple_cat(kSeqPerfTasks, kOmpPerfTasks);
const auto kTbbPerfTasks = ppc::util::MakeAllPerfTasks<InType, GaseninLDjstraTBB>(PPC_SETTINGS_gasenin_l_djstra);
const auto kAllPerfTasks = std::tuple_cat(kSeqPerfTasks, kOmpPerfTasks, kTbbPerfTasks);
const auto kGtestValues = ppc::util::TupleToGTestValues(kAllPerfTasks);
const auto kPerfTestName = DjkstraPerfTests::CustomPerfTestName;

Expand Down
Loading