Skip to content

Commit

Permalink
Adding automatic search strategy.
Browse files Browse the repository at this point in the history
The basic logic for automatic is:
 - if there is one variable, and it is a set of categoricals, use
 exhaustive.
 - if there is one variable and it is an integer range, use simulated
 annealing.
 - if there is one variable and it is a double, use nelder mead.
 - if there are more than one variable, and any of them are categorical
 sets, then use genetic search
 - if any of them are integer ranges, use simulated annealing
 - if all double ranges, use nelder mead.
  • Loading branch information
khuck committed Sep 17, 2024
1 parent 3ceaea8 commit 8a8e9d3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
80 changes: 77 additions & 3 deletions src/apex/apex_kokkos_tuning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,11 @@ class KokkosSession {
} else if (strncmp(apex::apex_options::kokkos_tuning_policy(),
"nelder_mead", strlen("nelder_mead")) == 0) {
strategy = apex_ah_tuning_strategy::NELDER_MEAD_INTERNAL;
} else if (strncmp(apex::apex_options::kokkos_tuning_policy(),
"automatic", strlen("automatic")) == 0) {
strategy = apex_ah_tuning_strategy::AUTOMATIC;
} else {
strategy = apex_ah_tuning_strategy::NELDER_MEAD;
strategy = apex_ah_tuning_strategy::AUTOMATIC;
}
}
public:
Expand Down Expand Up @@ -357,6 +360,25 @@ void KokkosSession::saveOutputVar(size_t id, Variable * var) {
all_vars.insert(std::make_pair(id, var));
}

std::string strategy_to_string(std::shared_ptr<apex_tuning_request> request) {
if (request->get_strategy() == apex_ah_tuning_strategy::APEX_RANDOM) {
return std::string("random");
}
if (request->get_strategy() == apex_ah_tuning_strategy::APEX_EXHAUSTIVE) {
return std::string("exhaustive");
}
if (request->get_strategy() == apex_ah_tuning_strategy::SIMULATED_ANNEALING) {
return std::string("simulated annealing");
}
if (request->get_strategy() == apex_ah_tuning_strategy::GENETIC_SEARCH) {
return std::string("genetic search");
}
if (request->get_strategy() == apex_ah_tuning_strategy::NELDER_MEAD_INTERNAL) {
return std::string("nelder mead");
}
return "unknown?";
}

void KokkosSession::writeCache(void) {
if(use_history) { return; }
if(!saveCache) { return; }
Expand Down Expand Up @@ -388,6 +410,12 @@ void KokkosSession::writeCache(void) {
// always write the random search out
bool converged = request->has_converged() ||
strategy == apex_ah_tuning_strategy::APEX_RANDOM;
results << " Strategy: \"" <<
strategy_to_string(request);
if (strategy == apex_ah_tuning_strategy::AUTOMATIC) {
results << " (auto)";
}
results << "\"" << std::endl;
results << " Converged: " <<
(converged ? "true" : "false") << std::endl;
if (converged) {
Expand Down Expand Up @@ -501,6 +529,8 @@ void KokkosSession::parseContextCache(std::ifstream& results) {
std::getline(results, line);
std::string name = line.substr(line.find(delimiter)+2);
name.erase(std::remove(name.begin(),name.end(),'\"'),name.end());
// strategy
std::getline(results, line);
// converged?
std::getline(results, line);
std::string converged = line.substr(line.find(delimiter)+2);
Expand Down Expand Up @@ -968,8 +998,52 @@ bool handle_start(const std::string & name, const size_t vars,
};
request->set_metric(metric);

// Set apex_openmp_policy_tuning_strategy
request->set_strategy(session.strategy);
// Set apex tuning strategy
if (session.strategy == apex_ah_tuning_strategy::AUTOMATIC) {
// just one variable?
if (vars == 1) {
auto id = values[0].type_id;
Variable* var{session.outputs[id]};
// and it's a small set of candidate values?
if (var->info.valueQuantity == kokkos_value_set &&
var->info.candidates.set.size < 4) {
request->set_strategy(apex_ah_tuning_strategy::APEX_EXHAUSTIVE);
// if integer, use simulated annealing
} else if (var->info.type == kokkos_value_int64) {
request->set_strategy(apex_ah_tuning_strategy::SIMULATED_ANNEALING);
// if double, use nelder mead
} else if (var->info.type == kokkos_value_double) {
request->set_strategy(apex_ah_tuning_strategy::NELDER_MEAD_INTERNAL);
}
// more than one variable...
} else {
// are any of them categorical?
bool haveSet = false;
bool allDouble = true;
for (size_t i = 0 ; i < vars ; i++) {
auto id = values[i].type_id;
Variable* var{session.outputs[id]};
if (var->info.valueQuantity == kokkos_value_set) {
haveSet = true;
allDouble = false;
} else if (var->info.type == kokkos_value_int64) {
allDouble = false;
}
}
// if have a categorical set, use genetic search
if (haveSet) {
request->set_strategy(apex_ah_tuning_strategy::GENETIC_SEARCH);
// if all double values, use nelder mead
} else if (allDouble) {
request->set_strategy(apex_ah_tuning_strategy::NELDER_MEAD_INTERNAL);
// as default, use simulated annealing
} else {
request->set_strategy(apex_ah_tuning_strategy::SIMULATED_ANNEALING);
}
}
} else {
request->set_strategy(session.strategy);
}
request->set_radius(0.5);
request->set_aggregation_times(3);
// min, max, mean
Expand Down
3 changes: 1 addition & 2 deletions src/apex/apex_policies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ enum class apex_param_type : int {NONE, LONG, DOUBLE, ENUM};
enum class apex_ah_tuning_strategy : int {
EXHAUSTIVE, RANDOM, NELDER_MEAD, NELDER_MEAD_INTERNAL,
PARALLEL_RANK_ORDER, SIMULATED_ANNEALING,
APEX_EXHAUSTIVE, APEX_RANDOM,
GENETIC_SEARCH};
APEX_EXHAUSTIVE, APEX_RANDOM, GENETIC_SEARCH, AUTOMATIC};

struct apex_tuning_session;
class apex_tuning_request;
Expand Down
2 changes: 1 addition & 1 deletion src/apex/apex_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ inline unsigned int sc_nprocessors_onln(void)
APEX_DEFAULT_OTF2_ARCHIVE_NAME, "OTF2 trace filename.") \
macro (APEX_EVENT_FILTER_FILE, task_event_filter_file, char*, "", "File containing names of timers to include/exclude during data collection.") \
macro (APEX_KOKKOS_TUNING_CACHE, kokkos_tuning_cache, char*, "", "Filename contining Kokkos autotuned results, tuned offline.") \
macro (APEX_KOKKOS_TUNING_POLICY, kokkos_tuning_policy, char*, "simulated_annealing", "Kokkos autotuning policy: random, exhaustive, simulated_annealing, nelder_mead.") \
macro (APEX_KOKKOS_TUNING_POLICY, kokkos_tuning_policy, char*, "simulated_annealing", "Kokkos autotuning policy: random, exhaustive, simulated_annealing, nelder_mead, automatic.") \
macro (APEX_ROCPROF_METRICS, rocprof_metrics, char*, "", "List of metrics to periodically sample with the Rocprofiler library (see /opt/rocm/rocprofiler/lib/metrics.xml).") \
macro (APEX_NVTX_LIBRARY, nvtx_library, char*, "libnvToolsExt.so", "With NVTX listener, specify the location of libnvToolsExt.so.")
// macro (APEX_ROCPROF_METRICS, rocprof_metrics, char*, "MemUnitBusy,MemUnitStalled,VALUUtilization,VALUBusy,SALUBusy,L2CacheHit,WriteUnitStalled,ALUStalledByLDS,LDSBankConflict", "")
Expand Down

0 comments on commit 8a8e9d3

Please sign in to comment.