Skip to content

Commit

Permalink
Parametrized the control flow.
Browse files Browse the repository at this point in the history
* mapMAP_control struct allows modifying some parameters for the control flow, e.g. switch on/off heuristics and force certain modes.
  • Loading branch information
dthuerck committed Apr 10, 2017
1 parent 3e2e0de commit 52ffd64
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 19 deletions.
15 changes: 12 additions & 3 deletions demo/mapmap_demo.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016, Daniel Thuerck
* Copyright (C) 2016-2017, Daniel Thuerck
* All rights reserved.
*
* This software may be modified and distributed under the terms
Expand Down Expand Up @@ -50,8 +50,9 @@ main(
std::unique_ptr<unary_t> unaries;
std::unique_ptr<pairwise_t> pairwise;

/* termination criterion */
/* termination criterion and control flow */
std::unique_ptr<TerminationCriterion<cost_t, simd_w>> terminate;
mapMAP_control ctr;

/* solver instance */
mapMAP<cost_t, simd_w, unary_t, pairwise_t> mapmap;
Expand Down Expand Up @@ -170,6 +171,14 @@ main(
terminate = std::unique_ptr<TerminationCriterion<cost_t, simd_w>>(
new StopWhenReturnsDiminish<cost_t, simd_w>(5, 0.0001));

/* create (optional) control flow settings */
ctr.use_multilevel = true;
ctr.use_spanning_tree = true;
ctr.use_acyclic = true;
ctr.spanning_tree_multilevel_after_n_iterations = 5;
ctr.force_acyclic = true;
ctr.min_acyclic_iterations = 5;

/* construct optimizer */
mapmap.set_graph(graph.get());
mapmap.set_label_set(label_set.get());
Expand Down Expand Up @@ -200,7 +209,7 @@ main(
/* catch errors thrown during optimization */
try
{
mapmap.optimize(solution);
mapmap.optimize(solution, ctr);
}
catch(std::runtime_error& e)
{
Expand Down
34 changes: 33 additions & 1 deletion mapmap/header/mapmap.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016, Daniel Thuerck
* Copyright (C) 2016-2017, Daniel Thuerck
* TU Darmstadt - Graphics, Capture and Massively Parallel Computing
* All rights reserved.
*
Expand Down Expand Up @@ -48,6 +48,10 @@ const char * const UNIX_COLOR_YELLOW = "\033[1;33m";
const char * const UNIX_COLOR_WHITE = "\033[1;37m";
const char * const UNIX_COLOR_RESET = "\033[0m";

/* forward declarations */
struct mapMAP_control;

/* MAPMAP main class */
template<typename COSTTYPE, uint_t SIMDWIDTH, typename UNARY, typename PAIRWISE>
class mapMAP
{
Expand Down Expand Up @@ -89,6 +93,10 @@ class mapMAP
_s_t<COSTTYPE, SIMDWIDTH> optimize(std::vector<_iv_st<COSTTYPE, SIMDWIDTH>>&
solution) throw();

/* start optimization with customized control flow */
_s_t<COSTTYPE, SIMDWIDTH> optimize(std::vector<_iv_st<COSTTYPE, SIMDWIDTH>>&
solution, const mapMAP_control& control_flow) throw();

protected:
/* setup, sanity checks and tools */
void create_std_modules();
Expand Down Expand Up @@ -165,6 +173,30 @@ class mapMAP
m_callback;
};

/**
* control flow struct (default values for flowgraph in paper)
*
* NOTE: despite settings concerning minimum iteration numbers, early
* termination may be forced by a user-defined termination criterion
*/
struct mapMAP_control
{
/* switch modes on/off */
bool use_multilevel = true;
bool use_spanning_tree = true;
bool use_acyclic = true;

/* multilevel settings */
/* none */

/* spanning tree settings */
uint_t spanning_tree_multilevel_after_n_iterations = 5;

/* acyclic settings */
bool force_acyclic = true; /* force using acyclic even if terminated */
uint_t min_acyclic_iterations = 5;
};

NS_MAPMAP_END

#include "source/mapmap.impl.h"
Expand Down
52 changes: 37 additions & 15 deletions mapmap/source/mapmap.impl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016, Daniel Thuerck
* Copyright (C) 2016-2017, Daniel Thuerck
* TU Darmstadt - Graphics, Capture and Massively Parallel Computing
* All rights reserved.
*
Expand Down Expand Up @@ -232,6 +232,23 @@ mapMAP<COSTTYPE, SIMDWIDTH, UNARY, PAIRWISE>::
optimize(
std::vector<_iv_st<COSTTYPE, SIMDWIDTH>>& solution)
throw()
{
/* initialize control flow with standard values */
mapMAP_control std_control;

return optimize(solution, std_control);
}

/* ************************************************************************** */

template<typename COSTTYPE, uint_t SIMDWIDTH, typename UNARY, typename PAIRWISE>
FORCEINLINE
_s_t<COSTTYPE, SIMDWIDTH>
mapMAP<COSTTYPE, SIMDWIDTH, UNARY, PAIRWISE>::
optimize(
std::vector<_iv_st<COSTTYPE, SIMDWIDTH>>& solution,
const mapMAP_control& control_flow)
throw()
{
_s_t<COSTTYPE, SIMDWIDTH> obj;

Expand Down Expand Up @@ -273,27 +290,26 @@ throw()
}

/* rapid initial descent by multilevel */
m_objective = opt_step_multilevel();
record_time_from_start();
print_status();
if(control_flow.use_multilevel)
{
m_objective = opt_step_multilevel();
record_time_from_start();
print_status();
}

/* take spanning tree steps until no more improvements occur */
luint_t sp_it = 0;

_s_t<COSTTYPE, SIMDWIDTH> old_objective = m_objective;
while(true)
while(control_flow.use_spanning_tree)
{
++sp_it;

/* check if algorithms needs to terminate */
/* check if algorithm needs to terminate this mode */
if(check_termination())
{
solution.assign(m_solution.begin(), m_solution.end());

return m_objective;
}
break;

/* execute spannign tree step */
/* execute spanning tree step */
obj = opt_step_spanning_tree();
record_time_from_start();

Expand All @@ -304,17 +320,23 @@ throw()

print_status();

if(sp_it % 5 == 0)
if(control_flow.use_multilevel && sp_it %
control_flow.spanning_tree_multilevel_after_n_iterations == 0)
{
m_objective = opt_step_multilevel();
record_time_from_start();
print_status();
}
}

/* lastly, execute acyclic steps until termination */
while(!check_termination())
/* lastly, execute (forced) acyclic steps until termination */
luint_t ac_it = 0;
while(control_flow.use_acyclic &&
(!check_termination() || (control_flow.force_acyclic &&
ac_it < control_flow.min_acyclic_iterations)))
{
++ac_it;

m_objective = opt_step_acyclic();

record_time_from_start();
Expand Down

0 comments on commit 52ffd64

Please sign in to comment.