summaryrefslogtreecommitdiff
path: root/src/tuning/configurations.cpp
diff options
context:
space:
mode:
authorCedric Nugteren <web@cedricnugteren.nl>2017-11-15 22:49:35 +0100
committerCedric Nugteren <web@cedricnugteren.nl>2017-11-15 22:49:35 +0100
commit1b2b46f2f073863a1faac9307583c9d5bb276e10 (patch)
treef947114b0abbf0c3d810eb7330629b2929b5a327 /src/tuning/configurations.cpp
parent0cd78bb6f9fbb3f30217e5b0f2267e76a43601a3 (diff)
Added first version of integrated and re-written auto-tuner
Diffstat (limited to 'src/tuning/configurations.cpp')
-rw-r--r--src/tuning/configurations.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/tuning/configurations.cpp b/src/tuning/configurations.cpp
new file mode 100644
index 00000000..459d66b1
--- /dev/null
+++ b/src/tuning/configurations.cpp
@@ -0,0 +1,99 @@
+
+// =================================================================================================
+// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This
+// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max-
+// width of 100 characters per line.
+//
+// Author(s):
+// Cedric Nugteren <www.cedricnugteren.nl>
+//
+// This file implements the parameter configurations for the CLBlast auto-tuner (taken from CLTune).
+// This is only used for the optional tuner binaries and not part of the core of CLBlast.
+//
+// =================================================================================================
+
+#include <vector>
+#include <string>
+
+#include "tuning/configurations.hpp"
+
+namespace clblast {
+// =================================================================================================
+
+// Finds all configurations. It also applies the user-defined constraints within.
+std::vector<Configuration> SetConfigurations(const std::vector<Parameter> parameters,
+ const Constraints& constraints) {
+ auto config = Configuration();
+ auto configurations = std::vector<Configuration>();
+ PopulateConfigurations(parameters, 0, config, configurations, constraints);
+ return configurations;
+}
+
+// Iterates recursively over all permutations of the user-defined parameters
+void PopulateConfigurations(const std::vector<Parameter> &parameters,
+ const size_t index, const Configuration &config,
+ std::vector<Configuration> &configuration,
+ const Constraints& constraints) {
+
+ // End of the chain: all parameters are considered, store the resulting configuration if it is a
+ // valid one according to the constraints
+ if (index == parameters.size()) {
+ if (ValidConfiguration(config, constraints)) {
+ configuration.push_back(config);
+ }
+ return;
+ }
+
+ // This loop iterates over all values of the current parameter and calls this function recursively
+ Parameter parameter = parameters[index];
+ for (auto &value: parameter.second) {
+ auto config_copy = config;
+ config_copy[parameter.first] = value;
+ PopulateConfigurations(parameters, index+1, config_copy, configuration, constraints);
+ }
+}
+
+// Loops over all user-defined constraints to check whether or not the configuration is valid
+bool ValidConfiguration(const Configuration &config,
+ const Constraints& constraints) {
+
+ // Iterates over all constraints
+ for (auto &constraint: constraints) {
+
+ // Finds the values of the parameters
+ auto values = std::vector<size_t>(constraint.parameters.size());
+ for (auto i=size_t{0}; i<constraint.parameters.size(); ++i) {
+ values[i] = config.at(constraint.parameters[i]);
+ }
+
+ // Checks this constraint for these values
+ if (!constraint.valid_if(values)) {
+ return false;
+ }
+ }
+
+ // Everything was OK: this configuration is valid
+ return true;
+}
+
+// Multiplies and/or dividers a thread configuration (local/global)
+std::vector<size_t> SetThreadConfiguration(const Configuration& config,
+ const std::vector<size_t> base,
+ const TransformVector& mul_config,
+ const TransformVector& div_config) {
+ auto result = base;
+ for (const auto &multipliers: mul_config) {
+ for (auto i = size_t{0}; i < multipliers.size(); ++i) {
+ result[i] *= config.at(multipliers[i]);
+ }
+ }
+ for (const auto &dividers: div_config) {
+ for (auto i = size_t{0}; i < dividers.size(); ++i) {
+ result[i] /= config.at(dividers[i]);
+ }
+ }
+ return result;
+}
+
+// =================================================================================================
+} // namespace clblast