Skip to content

Commit

Permalink
Added a function to return the best tuning parameters to the user; fa…
Browse files Browse the repository at this point in the history
…ctored out the logic to get the best tuning results
  • Loading branch information
CNugteren committed Feb 20, 2017
1 parent 115e14b commit 8eb3df1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -1,4 +1,8 @@

Development branch (next version)
- Added GetBestResult() to the tuner's API to retrieve the best parameters programmatically
- Changed std::initalizer_list in the AddParameters API to std::vector

Version 2.6.0
- Changed timing measurements to now also include the (varying) kernel launch overhead
- It is now possible to set OpenCL compiler options through the env variable CLTUNE_BUILD_OPTIONS
Expand Down
3 changes: 3 additions & 0 deletions doc/api.md
Expand Up @@ -89,6 +89,9 @@ Call this method *after* calling the `Tune()` method. Trains a machine learning
Output
-------------

* `std::unordered_map<std::string, size_t> GetBestResult()`:
Retrieves the parameters of the best tuning result and returns them to the caller as a map of strings (parameter names) to integers (parameter values).

* `void OutputSearchLog(const std::string &filename)`:
Outputs the search process to the file `filename`.

Expand Down
4 changes: 4 additions & 0 deletions include/cltune.h
Expand Up @@ -34,6 +34,7 @@
#include <memory> // std::unique_ptr
#include <functional> // std::function
#include <utility> // std::pair
#include <unordered_map> // std::unordered_map

// Exports library functions under Windows when building a DLL. See also:
// https://msdn.microsoft.com/en-us/library/a90k134d.aspx
Expand Down Expand Up @@ -144,6 +145,9 @@ class Tuner {
void PUBLIC_API ModelPrediction(const Model model_type, const float validation_fraction,
const size_t test_top_x_configurations);

// Retrieves the parameters of the best tuning result
std::unordered_map<std::string, size_t> GetBestResult() const;

// Prints the results of the tuning either to screen (stdout) or to a specific output-file.
// Returns the execution time in miliseconds.
double PUBLIC_API PrintToScreen() const;
Expand Down
3 changes: 3 additions & 0 deletions include/internal/tuner_impl.h
Expand Up @@ -136,6 +136,9 @@ class TunerImpl {
// Prints results of a particular kernel run
void PrintResult(FILE* fp, const TunerResult &result, const std::string &message) const;

// Retrieves the best tuning result
TunerResult GetBestResult() const;

// Loads a file from disk into a string
std::string LoadFile(const std::string &filename);

Expand Down
34 changes: 18 additions & 16 deletions src/cltune.cc
Expand Up @@ -283,19 +283,27 @@ void Tuner::ModelPrediction(const Model model_type, const float validation_fract

// =================================================================================================


// Retrieves the parameters of the best tuning result
std::unordered_map<std::string, size_t> Tuner::GetBestResult() const {
const auto best_result = pimpl->GetBestResult();
const auto best_configuration = best_result.configuration;

// Converts the std::vector<KernelInfo::Setting> into an unordere map of strings and integers
auto parameters = std::unordered_map<std::string, size_t>{};
for (const auto &parameter_setting : best_configuration) {
parameters[parameter_setting.name] = parameter_setting.value;
}
return parameters;
}

// Iterates over all tuning results and prints each parameter configuration and the corresponding
// timing-results. Printing is to stdout.
double Tuner::PrintToScreen() const {

// Finds the best result
auto best_result = pimpl->tuning_results_[0];
auto best_time = std::numeric_limits<double>::max();
for (auto &tuning_result: pimpl->tuning_results_) {
if (tuning_result.status && best_time >= tuning_result.time) {
best_result = tuning_result;
best_time = tuning_result.time;
}
}
const auto best_result = pimpl->GetBestResult();
const auto best_time = best_result.time;

// Aborts if there was no best time found
if (best_time == std::numeric_limits<double>::max()) {
Expand All @@ -321,14 +329,8 @@ double Tuner::PrintToScreen() const {
void Tuner::PrintFormatted() const {

// Finds the best result
auto best_result = pimpl->tuning_results_[0];
auto best_time = std::numeric_limits<double>::max();
for (auto &tuning_result: pimpl->tuning_results_) {
if (tuning_result.status && best_time >= tuning_result.time) {
best_result = tuning_result;
best_time = tuning_result.time;
}
}
const auto best_result = pimpl->GetBestResult();
const auto best_time = best_result.time;

// Prints the best result in C++ database format
auto count = size_t{0};
Expand Down
15 changes: 15 additions & 0 deletions src/tuner_impl.cc
Expand Up @@ -646,6 +646,21 @@ void TunerImpl::PrintResult(FILE* fp, const TunerResult &result, const std::stri

// =================================================================================================

// Finds the best result
TunerImpl::TunerResult TunerImpl::GetBestResult() const {
auto best_result = tuning_results_[0];
auto best_time = std::numeric_limits<double>::max();
for (auto &tuning_result: tuning_results_) {
if (tuning_result.status && best_time >= tuning_result.time) {
best_result = tuning_result;
best_time = tuning_result.time;
}
}
return best_result;
}

// =================================================================================================

// Loads a file into a stringstream and returns the result as a string
std::string TunerImpl::LoadFile(const std::string &filename) {
std::ifstream file(filename);
Expand Down

0 comments on commit 8eb3df1

Please sign in to comment.