Skip to content

Commit

Permalink
Correct comments
Browse files Browse the repository at this point in the history
Fixed comments on the first review.
  • Loading branch information
AntonChern committed Nov 20, 2023
1 parent b0f8011 commit 5c0f5b8
Show file tree
Hide file tree
Showing 21 changed files with 444 additions and 587 deletions.
6 changes: 4 additions & 2 deletions src/core/algorithms/algorithm_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace algos {
using AlgorithmTypes =
std::tuple<Depminer, DFD, FastFDs, FDep, Fd_mine, Pyro, Tane, FUN, hyfd::HyFD, Aid, Apriori,
metric::MetricVerifier, DataStats, fd_verifier::FDVerifier, HyUCC,
cfd::FDFirstAlgorithm, ACAlgorithm, GFDValidation, EGFDValidation>;
cfd::FDFirstAlgorithm, ACAlgorithm, GfdValidation, EGfdValidation, NaiveGfdValidation>;

// clang-format off
/* Enumeration of all supported non-pipeline algorithms. If you implement a new
Expand Down Expand Up @@ -52,8 +52,10 @@ BETTER_ENUM(AlgorithmType, char,
/* Algebraic constraints mining algorithm*/
ac,

/* Graph functional dependency mining algorithms */
gfdvalid,
egfdvalid
egfdvalid,
naivegfdvalid
)
// clang-format on

Expand Down
3 changes: 3 additions & 0 deletions src/core/algorithms/algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@

/* Algebraic constraints*/
#include "algorithms/algebraic_constraints/ac_algorithm.h"

/* Graph functional dependency mining algorithms */
#include "algorithms/gfd/egfd_validation.h"
#include "algorithms/gfd/gfd_validation.h"
#include "algorithms/gfd/naivegfd_validation.h"
72 changes: 45 additions & 27 deletions src/core/algorithms/gfd/balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <numeric>
#include <vector>

std::vector<std::vector<int>> Balancer::balance(const std::vector<int>& weights,
std::vector<std::vector<int>> Balancer::Balance(const std::vector<int>& weights,
const int& processors_num) {
std::size_t m = std::min(processors_num, (int)weights.size());
std::vector<std::vector<int>> result = {};
m = std::min(processors_num, (int)weights.size());
this->result = {};
if (weights.begin() == weights.end()) {
for (int i = 0; i < processors_num; ++i) {
std::vector<int> temp = {};
Expand All @@ -23,7 +23,7 @@ std::vector<std::vector<int>> Balancer::balance(const std::vector<int>& weights,
}
// fill processors initially
// count optimal
double optimal = 0;
optimal = 0;
std::size_t i = 0;
for (const int& weight : weights) {
result.at(i++).push_back(weight);
Expand All @@ -36,9 +36,25 @@ std::vector<std::vector<int>> Balancer::balance(const std::vector<int>& weights,
std::sort(processor.begin() + 1, processor.end());
}
// ALGORITHM
// 1st step
std::vector<int> deleted_large = {};
std::vector<int> deleted_small = {};
DeleteLarge();
Prepare();
DeleteFirstSmall();
DeleteSecondSmall();
FullLarge();
FullSmall();
// delete indices
for (std::vector<int>& processor : result) {
processor.erase(processor.begin());
}
for (std::size_t i = 0; i < processors_num - m; ++i) {
std::vector<int> empty = {};
result.push_back(empty);
}
return result;
}

void Balancer::DeleteLarge() {
deleted_large = {};
for (std::vector<int>& processor : result) {
auto border = processor.end();
for (auto it = --processor.end(); it != processor.begin() + 1; --it) {
Expand All @@ -51,8 +67,9 @@ std::vector<std::vector<int>> Balancer::balance(const std::vector<int>& weights,
}
processor.erase(border, processor.end());
}
// 2nd step
std::map<int, std::tuple<int, int, int>> quality;
}

void Balancer::Prepare() {
for (std::size_t i = 0; i < m; ++i) {
quality.emplace(i, std::tuple<int, int, int>(0, 0, 0));
}
Expand Down Expand Up @@ -83,8 +100,11 @@ std::vector<std::vector<int>> Balancer::balance(const std::vector<int>& weights,
std::get<1>(quality.at(processor.at(0))) = b;
std::get<2>(quality.at(processor.at(0))) = a - b;
}
// 3rd step
}

void Balancer::DeleteFirstSmall() {
// sort for convenience
deleted_small = {};
std::vector<std::vector<int>> small_processors = {};
std::vector<std::vector<int>> large_processors = {};
for (const std::vector<int>& processor : result) {
Expand All @@ -94,17 +114,18 @@ std::vector<std::vector<int>> Balancer::balance(const std::vector<int>& weights,
small_processors.push_back(processor);
}
}
auto cGreater = [&quality](std::vector<int> a, std::vector<int> b) {
return std::get<2>(quality.at(a.at(0))) > std::get<2>(quality.at(b.at(0)));
auto cGreater = [this](std::vector<int> a, std::vector<int> b) {
return std::get<2>(this->quality.at(a.at(0))) > std::get<2>(this->quality.at(b.at(0)));
};
sort(small_processors.begin(), small_processors.end(), cGreater);
sort(large_processors.begin(), large_processors.end(), cGreater);
result.clear();
result.insert(result.end(), small_processors.begin(), small_processors.end());
result.insert(result.end(), large_processors.begin(), large_processors.end());
numOfLargeProcs = large_processors.size();
std::size_t numOfLarges = large_processors.size() + deleted_large.size();
// work
auto border = numOfLarges < m ? result.end() - numOfLarges : result.begin();
border = numOfLarges < m ? result.end() - numOfLarges : result.begin();
for (auto it = border; it != result.end(); ++it) {
auto last = it->end();
if (*(last - 1) > optimal / 2) {
Expand All @@ -115,18 +136,22 @@ std::vector<std::vector<int>> Balancer::balance(const std::vector<int>& weights,
}
it->erase(last - std::get<0>(quality.at(*it->begin())), last);
}
// 4th step
}

void Balancer::DeleteSecondSmall() {
for (auto it = result.begin(); it != border; ++it) {
auto last = it->end();
for (auto cur = last - std::get<1>(quality.at(*it->begin())); cur != last; ++cur) {
deleted_small.push_back(*cur);
}
it->erase(last - std::get<1>(quality.at(*it->begin())), last);
}
// 5th step
i = 0;
}

void Balancer::FullLarge() {
std::size_t i = 0;
for (const int& weight : deleted_large) {
if (i < m - large_processors.size()) {
if (i < m - numOfLargeProcs) {
(result.begin() + i)->push_back(weight);
} else {
sort(result.begin(), result.end(), [](std::vector<int> a, std::vector<int> b) {
Expand All @@ -137,21 +162,14 @@ std::vector<std::vector<int>> Balancer::balance(const std::vector<int>& weights,
}
++i;
}
// 6th step
}

void Balancer::FullSmall() {
for (const int& weight : deleted_small) {
sort(result.begin(), result.end(), [](std::vector<int> a, std::vector<int> b) {
return std::accumulate(a.begin(), a.end(), 0, std::plus<int>()) <
std::accumulate(b.begin(), b.end(), 0, std::plus<int>());
});
result.begin()->push_back(weight);
}
// delete indices
for (std::vector<int>& processor : result) {
processor.erase(processor.begin());
}
for (std::size_t i = 0; i < processors_num - m; ++i) {
std::vector<int> empty = {};
result.push_back(empty);
}
return result;
}
25 changes: 23 additions & 2 deletions src/core/algorithms/gfd/balancer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
#pragma once
#include <map>
#include <vector>

class Balancer {
private:
std::size_t m;
double optimal;
std::vector<std::vector<int>>::iterator border;
std::vector<std::vector<int>> result;
std::vector<int> deleted_large = {};
std::vector<int> deleted_small = {};
std::size_t numOfLargeProcs;
std::map<int, std::tuple<int, int, int>> quality;

void DeleteLarge();
void Prepare();
void DeleteFirstSmall();
void DeleteSecondSmall();
void FullLarge();
void FullSmall();

public:
static std::vector<std::vector<int>> balance(const std::vector<int>& weights,
const int& processors_num);
Balancer() = default;
~Balancer() = default;

std::vector<std::vector<int>> Balance(const std::vector<int>& weights,
const int& processors_num);
};
Loading

0 comments on commit 5c0f5b8

Please sign in to comment.