Skip to content

Commit

Permalink
Merge pull request #1286 from trws/refactor-subsystem-identity
Browse files Browse the repository at this point in the history
Speed up final constraint enforcement
  • Loading branch information
mergify[bot] authored Sep 3, 2024
2 parents 4d9fc6f + 8b63d37 commit 758258e
Show file tree
Hide file tree
Showing 48 changed files with 119,985 additions and 130,487 deletions.
153 changes: 25 additions & 128 deletions resource/evaluators/scoring_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,169 +23,78 @@ namespace resource_model {
// Scoring API Private Method Definitions
////////////////////////////////////////////////////////////////////////////////

void scoring_api_t::handle_new_keys (subsystem_t s, resource_type_t r)
{
handle_new_subsystem (s);
handle_new_resrc_type (s, r);
}

void scoring_api_t::handle_new_subsystem (subsystem_t s)
{
if (m_ssys_map.find (s) == m_ssys_map.end ()) {
auto o = new std::map<resource_type_t, detail::evals_t *> ();
m_ssys_map.insert (std::make_pair (s, o));
}
}

void scoring_api_t::handle_new_resrc_type (subsystem_t s, resource_type_t r)
{
if (m_ssys_map[s]->find (r) == m_ssys_map[s]->end ()) {
auto e = new detail::evals_t (r);
m_ssys_map[s]->insert (std::make_pair (r, e));
}
}

////////////////////////////////////////////////////////////////////////////////
// Scoring API Public Method Definitions
////////////////////////////////////////////////////////////////////////////////

scoring_api_t::scoring_api_t ()
{
}

scoring_api_t::scoring_api_t (const scoring_api_t &o)
{
for (auto &p : o.m_ssys_map) {
subsystem_t s = p.first;
auto obj = new std::map<resource_type_t, detail::evals_t *> ();
m_ssys_map.insert (std::make_pair (s, obj));
auto &tmap = *(p.second);
for (auto &p2 : tmap) {
resource_type_t res_type = p2.first;
detail::evals_t *ne = new detail::evals_t ();
*ne = *(p2.second);
(*m_ssys_map[s]).insert (std::make_pair (res_type, ne));
}
}
}

scoring_api_t &scoring_api_t::operator= (const scoring_api_t &o)
{
for (auto &p : o.m_ssys_map) {
subsystem_t s = p.first;
auto obj = new std::map<resource_type_t, detail::evals_t *> ();
m_ssys_map.insert (std::make_pair (s, obj));
auto &tmap = *(p.second);
for (auto &p2 : tmap) {
resource_type_t res_type = p2.first;
detail::evals_t *ne = new detail::evals_t ();
*ne = *(p2.second);
(*m_ssys_map[s]).insert (std::make_pair (res_type, ne));
}
}
return *this;
}

scoring_api_t::~scoring_api_t ()
{
auto i = m_ssys_map.begin ();
while (i != m_ssys_map.end ()) {
auto tmap = i->second;
auto j = tmap->begin ();
while (j != tmap->end ()) {
delete j->second;
j = tmap->erase (j);
}
delete i->second;
i = m_ssys_map.erase (i);
}
}
scoring_api_t::scoring_api_t () = default;
scoring_api_t::scoring_api_t (const scoring_api_t &o) = default;
scoring_api_t::scoring_api_t (scoring_api_t &&o) = default;
scoring_api_t &scoring_api_t::operator= (const scoring_api_t &o) = default;
scoring_api_t &scoring_api_t::operator= (scoring_api_t &&o) = default;
scoring_api_t::~scoring_api_t () = default;

int64_t scoring_api_t::cutline (subsystem_t s, resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->cutline ();
return m_ssys_map[s][r].cutline ();
}

int64_t scoring_api_t::set_cutline (subsystem_t s, resource_type_t r, int64_t c)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->set_cutline (c);
return m_ssys_map[s][r].set_cutline (c);
}

void scoring_api_t::eval_egroups_iter_reset (subsystem_t s, resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
res_evals->eval_egroups_iter_reset ();
m_ssys_map[s][r].eval_egroups_iter_reset ();
}

std::vector<eval_egroup_t>::iterator scoring_api_t::eval_egroups_iter_next (subsystem_t s,
resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->eval_egroups_iter_next ();
return m_ssys_map[s][r].eval_egroups_iter_next ();
}

std::vector<eval_egroup_t>::iterator scoring_api_t::eval_egroups_end (subsystem_t s,
resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->eval_egroups_end ();
return m_ssys_map[s][r].eval_egroups_end ();
}

int scoring_api_t::add (subsystem_t s, resource_type_t r, const eval_egroup_t &eg)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->add (eg);
return m_ssys_map[s][r].add (eg);
}

//! Can throw an out_of_range exception
const eval_egroup_t &scoring_api_t::at (subsystem_t s, resource_type_t r, unsigned int i)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->at (i);
return m_ssys_map[s][r].at (i);
}

unsigned int scoring_api_t::qualified_count (subsystem_t s, resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->qualified_count ();
return m_ssys_map[s][r].qualified_count ();
}

unsigned int scoring_api_t::qualified_granules (subsystem_t s, resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->qualified_granules ();
return m_ssys_map[s][r].qualified_granules ();
}

unsigned int scoring_api_t::total_count (subsystem_t s, resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->total_count ();
return m_ssys_map[s][r].total_count ();
}

unsigned int scoring_api_t::best_k (subsystem_t s, resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->best_k ();
return m_ssys_map[s][r].best_k ();
}

unsigned int scoring_api_t::best_i (subsystem_t s, resource_type_t r)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->best_i ();
return m_ssys_map[s][r].best_i ();
}

bool scoring_api_t::hier_constrain_now ()
Expand All @@ -195,23 +104,17 @@ bool scoring_api_t::hier_constrain_now ()

void scoring_api_t::merge (const scoring_api_t &o)
{
for (auto &kv : o.m_ssys_map) {
subsystem_t s = kv.first;
auto &tmap = *(kv.second);
for (auto &kv2 : tmap) {
resource_type_t r = kv2.first;
auto &ev = *(kv2.second);
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
res_evals->merge (ev);
for (const auto &s : o.m_ssys_map.key_range ()) {
auto &tmap = o.m_ssys_map.at (s);
for (auto &[r, ev] : tmap) {
m_ssys_map[s][r].merge (ev);
}
}
}

void scoring_api_t::resrc_types (subsystem_t s, std::vector<resource_type_t> &v)
{
handle_new_subsystem (s);
for (auto &kv : *(m_ssys_map[s]))
for (auto &kv : m_ssys_map[s])
v.push_back (kv.first);
}

Expand All @@ -237,15 +140,9 @@ void scoring_api_t::set_avail (unsigned int avail)
m_avail = avail;
}

bool scoring_api_t::is_contained (subsystem_t s, resource_type_t r)
bool scoring_api_t::is_contained (subsystem_t s, resource_type_t const &r)
{
bool rc = false;
if (m_ssys_map.find (s) != m_ssys_map.end ()) {
if (m_ssys_map[s]->find (r) != m_ssys_map[s]->end ()) {
rc = true;
}
}
return rc;
return m_ssys_map[s].contains (r);
}

} // namespace resource_model
Expand Down
31 changes: 13 additions & 18 deletions resource/evaluators/scoring_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class scoring_api_t {
public:
scoring_api_t ();
scoring_api_t (const scoring_api_t &o);
scoring_api_t (scoring_api_t &&o);
scoring_api_t &operator= (const scoring_api_t &o);
scoring_api_t &operator= (scoring_api_t &&o);
~scoring_api_t ();

int64_t cutline (subsystem_t s, resource_type_t r);
Expand All @@ -54,7 +56,7 @@ class scoring_api_t {
void set_overall_score (int64_t overall);
unsigned int avail ();
void set_avail (unsigned int avail);
bool is_contained (subsystem_t s, resource_type_t r);
bool is_contained (subsystem_t s, resource_type_t const &r);

template<class compare_op = fold::greater, class binary_op = fold::plus>
int64_t choose_accum_best_k (subsystem_t s,
Expand All @@ -64,11 +66,10 @@ class scoring_api_t {
binary_op accum = fold::plus ())
{
int64_t rc;
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
if ((rc = res_evals->choose_best_k<compare_op> (k, comp)) != -1) {
auto &res_evals = m_ssys_map[s][r];
if ((rc = res_evals.choose_best_k<compare_op> (k, comp)) != -1) {
m_hier_constrain_now = true;
rc = res_evals->accum_best_k<binary_op> (accum);
rc = res_evals.accum_best_k<binary_op> (accum);
}
return rc;
}
Expand All @@ -80,30 +81,24 @@ class scoring_api_t {
binary_op accum = fold::plus ())
{
int64_t rc;
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
unsigned int k = res_evals->qualified_count ();
if ((rc = res_evals->choose_best_k<compare_op> (k, comp)) != -1) {
auto &res_evals = m_ssys_map[s][r];
unsigned int k = res_evals.qualified_count ();
if ((rc = res_evals.choose_best_k<compare_op> (k, comp)) != -1) {
m_hier_constrain_now = true;
rc = res_evals->accum_best_k<binary_op> (accum);
rc = res_evals.accum_best_k<binary_op> (accum);
}
return rc;
}

template<class output_it, class unary_op>
output_it transform (subsystem_t s, resource_type_t r, output_it o_it, unary_op uop)
{
handle_new_keys (s, r);
auto res_evals = (*m_ssys_map[s])[r];
return res_evals->transform<output_it, unary_op> (o_it, uop);
auto &res_evals = m_ssys_map[s][r];
return res_evals.transform<output_it, unary_op> (o_it, uop);
}

private:
void handle_new_keys (subsystem_t s, resource_type_t r);
void handle_new_subsystem (subsystem_t s);
void handle_new_resrc_type (subsystem_t s, resource_type_t r);

std::map<subsystem_t, std::map<resource_type_t, detail::evals_t *> *> m_ssys_map;
intern::interned_key_vec<subsystem_t, std::map<resource_type_t, detail::evals_t>> m_ssys_map;
bool m_hier_constrain_now = false;
int64_t m_overall_score = -1;
unsigned int m_avail = 0;
Expand Down
1 change: 1 addition & 0 deletions resource/modules/resource_match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern "C" {
}

#include <cstdint>
#include <fstream>
#include <limits>
#include <sstream>
#include <cerrno>
Expand Down
4 changes: 2 additions & 2 deletions resource/readers/resource_reader_grug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ void dfs_emitter_t::emit_edges (gge_t ge, const gg_t &recipe, vtx_t src_v, vtx_t
resource_graph_t &g = *m_g_p;
if ((rc = raw_edge (src_v, tgt_v, e)) < 0)
return;
g[e].idata.member_of[recipe[ge].e_subsystem] = recipe[ge].relation;
g[e].name[recipe[ge].e_subsystem] = recipe[ge].relation;
g[e].idata.member_of[recipe[ge].e_subsystem] = true;
g[e].subsystem = recipe[ge].e_subsystem;
}

vtx_t dfs_emitter_t::emit_vertex (ggv_t u,
Expand Down
4 changes: 2 additions & 2 deletions resource/readers/resource_reader_hwloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ int resource_reader_hwloc_t::walk_hwloc (resource_graph_t &g,
"error inserting a new edge: " + g[parent].name + " -> " + g[v].name + "; ";
return -1;
}
g[e].idata.member_of[subsys] = relation;
g[e].name[subsys] = relation;
g[e].idata.member_of[subsys] = true;
g[e].subsystem = subsys;
if (add_metadata (m, e, parent, v, g) < 0)
return -1;
}
Expand Down
Loading

0 comments on commit 758258e

Please sign in to comment.