235 lines
8.2 KiB
C++
235 lines
8.2 KiB
C++
#include "affinity.hpp"
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <queue>
|
|
#include <utility>
|
|
|
|
namespace affinity {
|
|
namespace {
|
|
|
|
struct Edge {
|
|
int to;
|
|
int reverse;
|
|
int capacity;
|
|
int cost;
|
|
};
|
|
|
|
struct FlowResult {
|
|
int sent = 0;
|
|
int cost = 0;
|
|
};
|
|
|
|
class Network {
|
|
public:
|
|
explicit Network(std::size_t count) : graph_(count) {}
|
|
|
|
void connect(int from, int to, int capacity, int cost)
|
|
{
|
|
const int reverse_at_to = static_cast<int>(graph_[to].size());
|
|
const int reverse_at_from = static_cast<int>(graph_[from].size());
|
|
graph_[from].push_back(Edge{to, reverse_at_to, capacity, cost});
|
|
graph_[to].push_back(Edge{from, reverse_at_from, 0, -cost});
|
|
}
|
|
|
|
FlowResult send(int source, int sink, int wanted)
|
|
{
|
|
FlowResult result;
|
|
while (result.sent < wanted) {
|
|
std::vector<int> distance(graph_.size(), infinity());
|
|
std::vector<int> parent(graph_.size(), -1);
|
|
std::vector<int> parent_edge(graph_.size(), -1);
|
|
std::vector<bool> queued(graph_.size(), false);
|
|
std::queue<int> pending;
|
|
distance[source] = 0;
|
|
queued[source] = true;
|
|
pending.push(source);
|
|
while (!pending.empty()) {
|
|
const int from = pending.front();
|
|
pending.pop();
|
|
queued[from] = false;
|
|
for (std::size_t index = 0; index < graph_[from].size(); ++index) {
|
|
const Edge &edge = graph_[from][index];
|
|
if (edge.capacity == 0 || distance[from] + edge.cost >= distance[edge.to])
|
|
continue;
|
|
distance[edge.to] = distance[from] + edge.cost;
|
|
parent[edge.to] = from;
|
|
parent_edge[edge.to] = static_cast<int>(index);
|
|
if (!queued[edge.to]) {
|
|
queued[edge.to] = true;
|
|
pending.push(edge.to);
|
|
}
|
|
}
|
|
}
|
|
if (parent[sink] == -1) break;
|
|
int amount = wanted - result.sent;
|
|
for (int at = sink; at != source; at = parent[at]) {
|
|
amount = std::min(amount, graph_[parent[at]][parent_edge[at]].capacity);
|
|
}
|
|
for (int at = sink; at != source; at = parent[at]) {
|
|
Edge &edge = graph_[parent[at]][parent_edge[at]];
|
|
edge.capacity -= amount;
|
|
graph_[at][edge.reverse].capacity += amount;
|
|
}
|
|
result.sent += amount;
|
|
result.cost += amount * distance[sink];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
static int infinity() { return std::numeric_limits<int>::max() / 4; }
|
|
std::vector<std::vector<Edge>> graph_;
|
|
};
|
|
|
|
struct Score {
|
|
bool complete = false;
|
|
int retained = 0;
|
|
};
|
|
|
|
class Planner {
|
|
public:
|
|
explicit Planner(const Problem &normalized)
|
|
: problem_(normalized), eligible_(normalized.jobs.size())
|
|
{
|
|
for (std::size_t job = 0; job < problem_.jobs.size(); ++job) {
|
|
const auto &allowed = problem_.jobs[job].allowed.ids;
|
|
for (std::size_t core = 0; core < problem_.cores.size(); ++core) {
|
|
if (std::binary_search(allowed.begin(), allowed.end(), problem_.cores[core].id))
|
|
eligible_[job].push_back(static_cast<int>(core));
|
|
}
|
|
}
|
|
}
|
|
|
|
Plan run() const
|
|
{
|
|
const std::vector<bool> accepted = choose_accepted();
|
|
std::vector<int> fixed(problem_.jobs.size(), unassigned());
|
|
for (std::size_t job = 0; job < accepted.size(); ++job) {
|
|
if (accepted[job]) fixed[job] = undecided();
|
|
}
|
|
const Score optimum = score(accepted, fixed);
|
|
choose_bindings(accepted, optimum.retained, fixed);
|
|
return publish(fixed);
|
|
}
|
|
|
|
private:
|
|
static int undecided() { return -2; }
|
|
static int unassigned() { return -1; }
|
|
|
|
bool preferred(std::size_t job, int core) const
|
|
{
|
|
const auto previous = problem_.jobs[job].previous;
|
|
return previous.has_value() && *previous == problem_.cores[core].id;
|
|
}
|
|
|
|
Score score(const std::vector<bool> &accepted, const std::vector<int> &fixed) const
|
|
{
|
|
std::vector<int> remaining;
|
|
for (const Core &core : problem_.cores) remaining.push_back(core.capacity);
|
|
Score result;
|
|
int pending_count = 0;
|
|
for (std::size_t job = 0; job < accepted.size(); ++job) {
|
|
if (!accepted[job]) continue;
|
|
const int core = fixed[job];
|
|
if (core == undecided()) {
|
|
++pending_count;
|
|
continue;
|
|
}
|
|
if (core < 0 || static_cast<std::size_t>(core) >= remaining.size() ||
|
|
!std::binary_search(eligible_[job].begin(), eligible_[job].end(), core) ||
|
|
remaining[core] == 0) return result;
|
|
--remaining[core];
|
|
result.retained += preferred(job, core);
|
|
}
|
|
const int source = 0;
|
|
const int jobs_begin = 1;
|
|
const int cores_begin = jobs_begin + static_cast<int>(problem_.jobs.size());
|
|
const int sink = cores_begin + static_cast<int>(problem_.cores.size());
|
|
Network network(static_cast<std::size_t>(sink + 1));
|
|
for (std::size_t core = 0; core < remaining.size(); ++core) {
|
|
network.connect(cores_begin + static_cast<int>(core), sink, remaining[core], 0);
|
|
}
|
|
for (std::size_t job = 0; job < accepted.size(); ++job) {
|
|
if (!accepted[job] || fixed[job] != undecided()) continue;
|
|
const int node = jobs_begin + static_cast<int>(job);
|
|
network.connect(source, node, 1, 0);
|
|
for (int core : eligible_[job]) {
|
|
network.connect(node, cores_begin + core, 1, preferred(job, core) ? -1 : 0);
|
|
}
|
|
}
|
|
const FlowResult flow = network.send(source, sink, pending_count);
|
|
result.complete = flow.sent == pending_count;
|
|
result.retained -= flow.cost;
|
|
return result;
|
|
}
|
|
|
|
std::vector<bool> choose_accepted() const
|
|
{
|
|
std::vector<bool> selected(problem_.jobs.size(), false);
|
|
std::vector<unsigned> used(problem_.cores.size(), 0);
|
|
for (std::size_t job = 0; job < selected.size(); ++job) {
|
|
for (int core : eligible_[job]) {
|
|
if (used[core] < problem_.cores[core].capacity) {
|
|
++used[core];
|
|
selected[job] = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return selected;
|
|
}
|
|
|
|
void choose_bindings(const std::vector<bool> &accepted, int,
|
|
std::vector<int> &fixed) const
|
|
{
|
|
for (std::size_t job = 0; job < accepted.size(); ++job) {
|
|
if (!accepted[job]) continue;
|
|
std::vector<int> candidates = eligible_[job];
|
|
const auto previous = std::find_if(candidates.begin(), candidates.end(),
|
|
[&](int core) { return preferred(job, core); });
|
|
if (previous != candidates.end()) std::rotate(candidates.begin(), previous, previous + 1);
|
|
for (int core : candidates) {
|
|
fixed[job] = core;
|
|
const Score rest = score(accepted, fixed);
|
|
if (rest.complete) break;
|
|
fixed[job] = undecided();
|
|
}
|
|
}
|
|
}
|
|
|
|
Plan publish(const std::vector<int> &fixed) const
|
|
{
|
|
Plan result;
|
|
for (const Core &core : problem_.cores) result.loads.push_back(CoreLoad{core.id, 0});
|
|
for (std::size_t job = 0; job < fixed.size(); ++job) {
|
|
Binding binding;
|
|
binding.job = problem_.jobs[job].id;
|
|
const int core = fixed[job];
|
|
if (core >= 0) {
|
|
binding.core = problem_.cores[core].id;
|
|
++result.accepted;
|
|
result.retained += preferred(job, core);
|
|
++result.loads[core].used;
|
|
}
|
|
result.bindings.push_back(std::move(binding));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
const Problem &problem_;
|
|
std::vector<std::vector<int>> eligible_;
|
|
};
|
|
|
|
}
|
|
|
|
Error make_plan(const Problem &input, Plan &output)
|
|
{
|
|
output = Plan{};
|
|
Problem normalized;
|
|
if (canonical_problem(input, normalized) != Error::none) return Error::invalid;
|
|
output = Planner(normalized).run();
|
|
return Error::none;
|
|
}
|
|
|
|
}
|