Skip to content

Commit

Permalink
Adding direct multidimensional simulated annealing search.
Browse files Browse the repository at this point in the history
  • Loading branch information
khuck committed May 13, 2021
1 parent ca03deb commit d1c503d
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/apex/CMakeLists.hpx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ set(apex_headers
profiler.hpp
profiler_listener.hpp
semaphore.hpp
simulated_annealing.hpp
thread_instance.hpp
task_identifier.hpp
task_wrapper.hpp
Expand All @@ -311,6 +312,7 @@ set(apex_sources
memory_wrapper.cpp
policy_handler.cpp
profiler_listener.cpp
simulated_annealing.cpp
task_identifier.cpp
tau_listener.cpp
tau_dummy.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/apex/CMakeLists.standalone
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ policy_handler.cpp
${PROC_SOURCE}
profiler_listener.cpp
${SENSOR_SOURCE}
simulated_annealing.cpp
task_identifier.cpp
tcmalloc_hooks.cpp
${tau_SOURCE}
Expand Down Expand Up @@ -144,6 +145,7 @@ INSTALL(FILES apex.h
utils.hpp
apex_options.hpp
profiler.hpp
simulated_annealing.hpp
task_wrapper.hpp
task_identifier.hpp
DESTINATION include)
Expand Down
12 changes: 6 additions & 6 deletions src/apex/apex_kokkos_tuning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class KokkosSession {
public:
// EXHAUSTIVE, RANDOM, NELDER_MEAD, PARALLEL_RANK_ORDER
KokkosSession() :
window(1),
strategy(apex_ah_tuning_strategy::NELDER_MEAD),
window(3),
strategy(apex_ah_tuning_strategy::SIMULATED_ANNEALING),
verbose(false),
use_history(false),
running(false),
Expand Down Expand Up @@ -213,20 +213,20 @@ void Variable::makeSpace(void) {
dstep = info.candidates.range.step.double_value;
dmin = info.candidates.range.lower.double_value;
dmax = info.candidates.range.upper.double_value;
if (info.candidates.range.openLower) {
if (info.candidates.range.openLower == 0) {
dmin = dmin + dstep;
}
if (info.candidates.range.openUpper) {
if (info.candidates.range.openUpper == 0) {
dmax = dmax - dstep;
}
} else if (info.type == kokkos_value_int64) {
lstep = info.candidates.range.step.int_value;
lmin = info.candidates.range.lower.int_value;
lmax = info.candidates.range.upper.int_value;
if (info.candidates.range.openLower) {
if (info.candidates.range.openLower == 0) {
lmin = lmin + lstep;
}
if (info.candidates.range.openUpper) {
if (info.candidates.range.openUpper == 0) {
lmax = lmax - lstep;
}
}
Expand Down
123 changes: 114 additions & 9 deletions src/apex/apex_policies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static const char * library_for_strategy(apex_ah_tuning_strategy s) {
return "pro.so";
default:
std::cerr <<
"ERROR: Unknown tuning strategy encountered." << std::endl;
"ERROR: Unknown Active Harmony tuning strategy encountered." << std::endl;
return "";
}
}
Expand Down Expand Up @@ -878,6 +878,36 @@ int apex_custom_tuning_policy(shared_ptr<apex_tuning_session> tuning_session,
}
#endif // APEX_HAVE_ACTIVEHARMONY

int apex_sa_policy(shared_ptr<apex_tuning_session> tuning_session,
apex_context const context) {
APEX_UNUSED(context);
if (apex_final) return APEX_NOERROR; // we terminated
std::unique_lock<std::mutex> l{shutdown_mutex};
if (tuning_session->sa_session.converged()) {
if (!tuning_session->converged_message) {
tuning_session->converged_message = true;
cout << "Tuning has converged for session " << tuning_session->id
<< "." << endl;
tuning_session->sa_session.saveBestSettings();
tuning_session->sa_session.printBestSettings();
}
tuning_session->sa_session.saveBestSettings();
return APEX_NOERROR;
}

// get a measurement of our current setting
double new_value = tuning_session->metric_of_interest();

/* Report the performance we've just measured. */
tuning_session->sa_session.evaluate(new_value);

/* Request new settings for next time */
tuning_session->sa_session.getNewSettings();

return APEX_NOERROR;
}


/// ----------------------------------------------------------------------------
///
/// Functions to setup and shutdown energy measurements during execution
Expand Down Expand Up @@ -1363,6 +1393,67 @@ inline int __active_harmony_custom_setup(shared_ptr<apex_tuning_session>
inline void __apex_active_harmony_shutdown(void) { }
#endif

inline int __sa_setup(shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request) {
APEX_UNUSED(tuning_session);
// set up the Simulated annealing!
// iterate over the parameters, and create variables.
using namespace apex::simulated_annealing;
for(auto & kv : request.params) {
auto & param = kv.second;
const char * param_name = param->get_name().c_str();
switch(param->get_type()) {
case apex_param_type::LONG: {
auto param_long =
std::static_pointer_cast<apex_param_long>(param);
Variable v(VariableType::longtype, param_long->value.get());
long lvalue = param_long->min;
do {
v.lvalues.push_back(lvalue);
lvalue = lvalue + param_long->step;
} while (lvalue < param_long->max);
v.set_init();
tuning_session->sa_session.add_var(param_name, std::move(v));
}
break;
case apex_param_type::DOUBLE: {
auto param_double =
std::static_pointer_cast<apex_param_double>(param);
Variable v(VariableType::doubletype, param_double->value.get());
double dvalue = param_double->min;
do {
v.dvalues.push_back(dvalue);
dvalue = dvalue + param_double->step;
} while (dvalue < param_double->max);
v.set_init();
tuning_session->sa_session.add_var(param_name, std::move(v));
}
break;
case apex_param_type::ENUM: {
auto param_enum =
std::static_pointer_cast<apex_param_enum>(param);
Variable v(VariableType::stringtype, param_enum->value.get());
for(const std::string & possible_value :
param_enum->possible_values) {
v.svalues.push_back(possible_value);
}
v.set_init();
tuning_session->sa_session.add_var(param_name, std::move(v));
}
break;
default:
cerr <<
"ERROR: Attempted to register tuning parameter with unknown type."
<< endl;
return APEX_ERROR;
}
}
/* request initial settings */
tuning_session->sa_session.getNewSettings();

return APEX_NOERROR;
}

inline int __common_setup_timer_throttling(apex_optimization_criteria_t
criteria, apex_optimization_method_t method, unsigned long update_interval)
{
Expand Down Expand Up @@ -1444,14 +1535,28 @@ inline int __common_setup_custom_tuning(shared_ptr<apex_tuning_session>
inline int __common_setup_custom_tuning(shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request) {
__read_common_variables(tuning_session);
int status = __active_harmony_custom_setup(tuning_session, request);
if(status == APEX_NOERROR) {
apex::register_policy(
request.trigger,
[=](apex_context const & context)->int {
return apex_custom_tuning_policy(tuning_session, context);
}
);
int status = APEX_NOERROR;
// if using the simulated annealing strategy, don't use AH!
if (request.strategy == apex_ah_tuning_strategy::SIMULATED_ANNEALING) {
status = __sa_setup(tuning_session, request);
if(status == APEX_NOERROR) {
apex::register_policy(
request.trigger,
[=](apex_context const & context)->int {
return apex_sa_policy(tuning_session, context);
}
);
}
} else {
int status = __active_harmony_custom_setup(tuning_session, request);
if(status == APEX_NOERROR) {
apex::register_policy(
request.trigger,
[=](apex_context const & context)->int {
return apex_custom_tuning_policy(tuning_session, context);
}
);
}
}
return status;
}
Expand Down
17 changes: 15 additions & 2 deletions src/apex/apex_policies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@

// include the C API
#include "apex_policies.h"
// include the simulated annealing class
#include "simulated_annealing.hpp"

enum class apex_param_type : int {NONE, LONG, DOUBLE, ENUM};
enum class apex_ah_tuning_strategy : int {EXHAUSTIVE, RANDOM, NELDER_MEAD,
PARALLEL_RANK_ORDER};
PARALLEL_RANK_ORDER, SIMULATED_ANNEALING};

struct apex_tuning_session;
class apex_tuning_request;
Expand Down Expand Up @@ -63,6 +65,8 @@ class apex_param {
friend int
__active_harmony_custom_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
friend int __sa_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
};

class apex_param_long : public apex_param {
Expand Down Expand Up @@ -94,6 +98,8 @@ class apex_param_long : public apex_param {
friend int
__active_harmony_custom_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
friend int __sa_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
};

class apex_param_double : public apex_param {
Expand Down Expand Up @@ -125,6 +131,8 @@ class apex_param_double : public apex_param {
friend int
__active_harmony_custom_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
friend int __sa_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
};

class apex_param_enum : public apex_param {
Expand Down Expand Up @@ -155,6 +163,8 @@ class apex_param_enum : public apex_param {
friend int
__active_harmony_custom_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
friend int __sa_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
};


Expand Down Expand Up @@ -289,7 +299,8 @@ class apex_tuning_request {
friend int
__active_harmony_custom_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);

friend int __sa_setup(std::shared_ptr<apex_tuning_session>
tuning_session, apex_tuning_request & request);
};


Expand All @@ -310,6 +321,8 @@ struct apex_tuning_session {
std::atomic<bool> apex_energy_init{false};
std::atomic<bool> apex_timer_init{false};

// if using simulated annealing, this is the request.
apex::simulated_annealing::SimulatedAnnealing sa_session;
bool converged_message = false;

// variables related to power throttling
Expand Down
Loading

0 comments on commit d1c503d

Please sign in to comment.