summaryrefslogtreecommitdiff
path: root/scripts/database
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/database')
-rwxr-xr-x[-rw-r--r--]scripts/database/database.py355
-rw-r--r--scripts/database/database/__init__.py0
-rw-r--r--scripts/database/database/bests.py58
-rw-r--r--scripts/database/database/clblast.py155
-rw-r--r--scripts/database/database/db.py64
-rw-r--r--scripts/database/database/defaults.py180
-rw-r--r--scripts/database/database/io.py60
7 files changed, 584 insertions, 288 deletions
diff --git a/scripts/database/database.py b/scripts/database/database.py
index 49bc1801..f758a2b7 100644..100755
--- a/scripts/database/database.py
+++ b/scripts/database/database.py
@@ -1,325 +1,104 @@
#!/usr/bin/env python
-# ==================================================================================================
-# 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 max-width of 100 characters per line.
+# This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This file follows the
+# PEP8 Python style guide and uses a max-width of 120 characters per line.
#
# Author(s):
# Cedric Nugteren <www.cedricnugteren.nl>
-#
-# ==================================================================================================
-# System modules
import sys
import os.path
import glob
-import re
-import json
-try:
- from urllib.request import urlopen # Python 3
-except ImportError:
- from urllib2 import urlopen # Python 2
+import argparse
-# Additional modules
-import pandas as pd
+import database.io as io
+import database.db as db
+import database.clblast as clblast
+import database.bests as bests
+import database.defaults as defaults
# Server storing a copy of the database
-DATABASE_SERVER_URL = "http://www.cedricnugteren.nl/tuning/clblast.db"
-
-# Constants
-VENDOR_DEFAULT = "default"
-DEVICETYPE_DEFAULT = "All"
-DEVICENAME_DEFAULT = "default"
-
-# Attributes
-DEVICETYPE_ATTRIBUTES = ["device_vendor", "device_type"]
-DEVICE_ATTRIBUTES = ["device", "device_core_clock", "device_compute_units"]
-KERNEL_ATTRIBUTES = ["precision", "kernel_family"]
-ARGUMENT_ATTRIBUTES = ["arg_m", "arg_n", "arg_k", "arg_alpha", "arg_beta"]
-ATTRIBUTES = DEVICE_ATTRIBUTES + DEVICETYPE_ATTRIBUTES + KERNEL_ATTRIBUTES + ARGUMENT_ATTRIBUTES
+DATABASE_SERVER_URL = "http://www.cedricnugteren.nl/tuning/clblast.json"
# OpenCL vendor names and their short name
-VENDOR_NAMES = { "device_vendor": {
+VENDOR_TRANSLATION_TABLE = {
"GenuineIntel": "Intel",
"Intel(R) Corporation": "Intel",
"Advanced Micro Devices, Inc.": "AMD",
"NVIDIA Corporation": "NVIDIA",
-}}
-
-# Pandas options
-pd.set_option('display.width', 1000)
-
-# ==================================================================================================
-# Database operations
-# ==================================================================================================
-
-# Downloads the database and save it to disk
-def DownloadDatabase(filename):
- print("## Downloading database from '"+DATABASE_SERVER_URL+"'...")
- df = urlopen(DATABASE_SERVER_URL)
- output = open(file_db,'wb')
- output.write(df.read())
- output.close()
-
-# Loads the database from disk
-def LoadDatabase(filename):
- return pd.read_pickle(filename)
-
-# Saves the database to disk
-def SaveDatabase(df, filename):
- df.to_pickle(filename)
-
-# Loads JSON data from file
-def ImportDataFromFile(filename):
- with open(filename) as f:
- data = json.load(f)
- json_data = pd.DataFrame(data)
- df = pd.io.json.json_normalize(json_data["results"])
- for attribute in ATTRIBUTES:
- if attribute == "kernel_family":
- df[attribute] = re.sub(r'_\d+', '', data[attribute])
- elif attribute in data:
- df[attribute] = data[attribute]
- else:
- df[attribute] = 0
- return df
-
-# Returns the row-wise concatenation of two dataframes
-def ConcatenateData(df1, df2):
- return pd.concat([df1, df2])
-
-# Removes duplicates from a dataframe
-def RemoveDuplicates(df):
- return df.drop_duplicates()
-
-# database = database[(database["device"] != "AMD Radeon R9 M370X Compute Engine") | (database["kernel_family"] != "xgemm") | (database["precision"] != "32")]
-def RemoveEntriesByDevice(df, devicename):
- return df[df["device"] != devicename]
-
-def RemoveEntriesByKernelFamily(df, familyname):
- return df[df["kernel_family"] != familyname]
-
-def GetEntriesByField(df, field, value):
- return df[df[field] == value]
-
-# Example usage:
-# df = UpdateDatabase(df, (df["kernel_family"] == "xdot") & (df["arg_n"] == "67108864"), "arg_n", "2097152")
-def UpdateDatabase(df, condition, field, value):
- df.loc[condition, field] = value
- return df
-
-# Fixes the problem that some vendors use multiple different names
-def SanitizeVendorNames(df):
- df = df.replace(VENDOR_NAMES)
- return df
-
-# Retrieves the results with the lowest execution times
-def GetBestResults(df):
- dfbest = pd.DataFrame()
- grouped = df.groupby(ATTRIBUTES+["kernel"])
- for name, dfgroup in grouped:
- besttime = dfgroup["time"].min()
- bestcase = dfgroup[dfgroup["time"] == besttime].iloc[0]
- dfbest = dfbest.append(bestcase, ignore_index=True)
- return dfbest
-
-# Sets defaults for devices of the same type/vendor based on the smallest values of all know
-# entries. The average might be better for performance but some parameters might not be supported
-# on other devices.
-def CalculateDefaults(df):
- dfdefault = pd.DataFrame()
-
- # Defaults per type/vendor
- groups = df.groupby(DEVICETYPE_ATTRIBUTES+KERNEL_ATTRIBUTES+ARGUMENT_ATTRIBUTES+["kernel"])
- for name, dfgroup in groups:
- default_values = dfgroup.min(axis=0)
- default_values["device"] = DEVICENAME_DEFAULT
- default_values["device_compute_units"] = 0
- default_values["device_core_clock"] = 0
- default_values["time"] = 0.0
- dfdefault = dfdefault.append(default_values, ignore_index=True)
-
- # Checks for mis-matched arguments
- groups = dfdefault.groupby(DEVICETYPE_ATTRIBUTES+KERNEL_ATTRIBUTES+["kernel"])
- for name, dfgroup in groups:
- if len(dfgroup) != 1:
- description = dfgroup["kernel"].min() + " " + dfgroup["device_vendor"].min()
- print("[WARNING] Entries for a single kernel with multiple argument values: " + description)
-
- # Defaults in general
- groups = df.groupby(KERNEL_ATTRIBUTES+ARGUMENT_ATTRIBUTES+["kernel"])
- for name, dfgroup in groups:
- default_values = dfgroup.min(axis=0)
- default_values["device_vendor"] = VENDOR_DEFAULT
- default_values["device_type"] = DEVICETYPE_DEFAULT
- default_values["device"] = DEVICENAME_DEFAULT
- default_values["device_compute_units"] = 0
- default_values["device_core_clock"] = 0
- default_values["time"] = 0.0
- dfdefault = dfdefault.append(default_values, ignore_index=True)
-
- # Database with both types of defaults only
- return dfdefault
-
-# ==================================================================================================
-# C++ header generation
-# ==================================================================================================
-
-# The C++ header
-def GetHeader(family):
- return("""
-// =================================================================================================
-// 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):
-// Database generator <database.py>
-//
-// This file populates the database with best-found tuning parameters for the '%s' kernels.
-//
-// =================================================================================================
-
-namespace clblast {
-// ================================================================================================="""
- % family.title())
-
-# The C++ footer
-def GetFooter():
- return("\n} // namespace clblast\n")
-
-# The start of a new C++ precision entry
-def GetPrecision(family, precision):
- precisionstring = ""
- if precision == "16":
- precisionstring = "Half"
- elif precision == "32":
- precisionstring = "Single"
- elif precision == "64":
- precisionstring = "Double"
- elif precision == "3232":
- precisionstring = "ComplexSingle"
- elif precision == "6464":
- precisionstring = "ComplexDouble"
- else:
- print("[ERROR] Unknown precision")
- sys.exit()
- return("\n\nconst Database::DatabaseEntry Database::%s%s = {\n \"%s\", Precision::k%s, {\n"
- % (family.title(), precisionstring, family.title(), precisionstring))
-
-# The C++ device type and vendor
-def GetDeviceVendor(vendor, devtype):
- if vendor == VENDOR_DEFAULT and devtype == DEVICETYPE_DEFAULT:
- return(" { // Default\n kDeviceType%s, \"%s\", {\n" % (devtype, vendor))
- return(" { // %s %ss\n kDeviceType%s, \"%s\", {\n" % (vendor, devtype, devtype[0].upper() + devtype[1:], vendor))
-
-# Prints the data to a C++ database
-def PrintData(df, outputdir):
-
- # Iterates over the kernel families: creates a new file per family
- for family, dffamily in df.groupby(["kernel_family"]):
- dffamily = dffamily.dropna(axis=1, how='all')
- f = open(os.path.join(outputdir, family+'.hpp'), 'w+')
- f.write(GetHeader(family))
-
- # Loops over the different entries for this family and prints their headers
- for precision, dfprecision in dffamily.groupby(["precision"]):
- f.write(GetPrecision(family, precision))
- for vendor, dfvendor in dfprecision.groupby(["device_vendor"]):
- for devtype, dfdevtype in dfvendor.groupby(["device_type"]):
- f.write(GetDeviceVendor(vendor, devtype))
- for device, dfdevice in dfdevtype.groupby(["device"]):
- devicename = "\"%s\"," % device
- f.write(" { %-50s { " % devicename)
+}
- # Collects the paramaters for this case and prints them
- parameters = []
- for kernel, dfkernel in dfdevice.groupby(["kernel"]):
- dfkernel = dfkernel.dropna(axis=1)
- col_names = [col for col in list(dfkernel) if col.startswith('parameters.') and col != "parameters.PRECISION"]
- parameters += ["{\"%s\",%d}" % (p.replace("parameters.",""), dfkernel[p].iloc[0]) for p in col_names]
- f.write(", ".join(parameters))
- f.write(" } },\n")
- # Prints the footers
- f.write(" }\n },\n")
- f.write(" }\n};\n\n// =================================================================================================")
- f.write(GetFooter())
+def main(argv):
-# ==================================================================================================
-# Command-line arguments parsing and verification
-# ==================================================================================================
+ # Parses the command-line arguments
+ parser = argparse.ArgumentParser()
+ parser.add_argument("source_folder", help="The folder with JSON files to parse to add to the database")
+ parser.add_argument("clblast_root", help="Root of the CLBlast sources")
+ parser.add_argument("-v", "--verbose", action="store_true", help="Increase verbosity of the script")
+ cl_args = parser.parse_args(argv)
-# Checks for the number of command-line arguments
-if len(sys.argv) != 3:
- print("[ERROR] Usage: database.py <folder_with_json_files> <root_of_clblast>")
- sys.exit()
+ # Parses the path arguments
+ database_filename = os.path.join(cl_args.clblast_root, "scripts", "database", "database.json")
+ database_best_filename = os.path.join(cl_args.clblast_root, "scripts", "database", "database_best.json")
+ json_files = os.path.join(cl_args.source_folder, "*.json")
+ cpp_database_path = os.path.join(cl_args.clblast_root, "src", "database", "kernels")
-# Parses the command-line arguments
-path_json = sys.argv[1]
-path_clblast = sys.argv[2]
-file_db = os.path.join(path_clblast, "scripts", "database", "database.db")
-glob_json = os.path.join(path_json, "*.json")
+ # Checks whether the command-line arguments are valid
+ clblast_header = os.path.join(cl_args.clblast_root, "include", "clblast.h") # Not used but just for validation
+ if not os.path.isfile(clblast_header):
+ raise RuntimeError("The path '" + cl_args.clblast_root + "' does not point to the root of the CLBlast library")
+ if len(glob.glob(json_files)) < 1:
+ print("[database] The path '" + cl_args.source_folder + "' does not contain any JSON files")
-# Checks whether the command-line arguments are valid; exists otherwise
-clblast_h = os.path.join(path_clblast, "include", "clblast.h") # Not used but just for validation
-if not os.path.isfile(clblast_h):
- print("[ERROR] The path '"+path_clblast+"' does not point to the root of the CLBlast library")
- sys.exit()
-if len(glob.glob(glob_json)) < 1:
- print("## The path '"+path_json+"' does not contain any JSON files")
+ # Downloads the database if a local copy is not present
+ if not os.path.isfile(database_filename):
+ io.download_database(database_filename, DATABASE_SERVER_URL)
-# ==================================================================================================
-# The main body of the script
-# ==================================================================================================
+ # Loads the database from disk
+ database = io.load_database(database_filename)
-# Downloads the database if a local copy is not present
-db_exists = os.path.isfile(file_db)
-if not db_exists:
- DownloadDatabase(file_db)
+ # Loops over all JSON files in the supplied folder
+ for file_json in glob.glob(json_files):
-# Loads the database from disk
-print("## Loading the database from disk...")
-database = LoadDatabase(file_db)
+ # Loads the newly imported data
+ sys.stdout.write("[database] Processing '" + file_json + "' ") # No newline printed
+ imported_data = io.load_tuning_results(file_json)
-# Loops over all JSON files in the supplied folder
-for file_json in glob.glob(glob_json):
+ # Fixes the problem that some vendors use multiple different names
+ for target in VENDOR_TRANSLATION_TABLE:
+ if imported_data["device_vendor"] == target:
+ imported_data["device_vendor"] = VENDOR_TRANSLATION_TABLE[target]
- # Loads the newly imported data
- sys.stdout.write("## Processing '"+file_json+"' ")
- imported_data = ImportDataFromFile(file_json)
- imported_data = SanitizeVendorNames(imported_data)
+ # Adds the new data to the database
+ old_size = db.length(database)
+ database = db.add_section(database, imported_data)
+ new_size = db.length(database)
+ print("with " + str(new_size - old_size) + " new items") # Newline printed here
- # Adds the new data to the database
- old_size = len(database.index)
- database = ConcatenateData(database, imported_data)
- database = RemoveDuplicates(database)
- new_size = len(database.index)
- print("with "+str(new_size-old_size)+" new items")
+ # Stores the modified database back to disk
+ if len(glob.glob(json_files)) >= 1:
+ io.save_database(database, database_filename)
-# Stores the modified database back to disk
-if len(glob.glob(glob_json)) >= 1:
- print("## Storing the database to disk...")
- SaveDatabase(database, file_db)
+ # Retrieves the best performing results
+ print("[database] Calculating the best results per device/kernel...")
+ database_best_results = bests.get_best_results(database)
-# Optional: update the database here. Default is disabled, code below is just an example
-if False:
- database = UpdateDatabase(database, ((database["kernel"] == "CopyMatrixFast") & (database["precision"] == "3232")), "arg_alpha", "2+0.5i")
- SaveDatabase(database, file_db)
+ # Determines the defaults for other vendors and per vendor
+ print("[database] Calculating the default values...")
+ database_defaults = defaults.calculate_defaults(database, cl_args.verbose)
+ database_best_results["sections"].extend(database_defaults["sections"])
-# Retrieves the best performing results
-print("## Calculating the best results per device/kernel...")
-bests = GetBestResults(database)
+ # Optionally outputs the database to disk
+ if cl_args.verbose:
+ io.save_database(database_best_results, database_best_filename)
-# Determines the defaults for other vendors and per vendor
-defaults = CalculateDefaults(bests)
-bests = ConcatenateData(bests, defaults)
+ # Outputs the database as a C++ database
+ print("[database] Producing a C++ database in '" + cpp_database_path + "'...")
+ clblast.print_cpp_database(database_best_results, cpp_database_path)
-# Outputs the data as a C++ database
-path_cpp_database = os.path.join(path_clblast, "src", "database", "kernels")
-print("## Producing a C++ database in '"+path_cpp_database+"'...")
-PrintData(bests, path_cpp_database)
+ print("[database] All done")
-print("## All done")
-# ==================================================================================================
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/scripts/database/database/__init__.py b/scripts/database/database/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/scripts/database/database/__init__.py
diff --git a/scripts/database/database/bests.py b/scripts/database/database/bests.py
new file mode 100644
index 00000000..c924efde
--- /dev/null
+++ b/scripts/database/database/bests.py
@@ -0,0 +1,58 @@
+
+# This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This file follows the
+# PEP8 Python style guide and uses a max-width of 120 characters per line.
+#
+# Author(s):
+# Cedric Nugteren <www.cedricnugteren.nl>
+
+import sys
+
+
+def get_best_results(database):
+ """Retrieves the results with the lowest execution times"""
+ sections_best = []
+ for section in database["sections"]:
+ section_best = {}
+
+ # Stores all the section's meta data
+ for attribute in section.keys():
+ if attribute != "results":
+ section_best[attribute] = section[attribute]
+
+ # Find the best result
+ parameters_best = None
+ time_best = sys.float_info.max
+ for result in section["results"]:
+ if result["time"] < time_best:
+ time_best = result["time"]
+ parameters_best = result["parameters"]
+
+ # Stores the best result
+ section_best["results"] = [{"time": time_best, "parameters": parameters_best}]
+ sections_best.append(section_best)
+
+ return {"sections": sections_best}
+
+
+def get_relative_bests(name, common_results, common_parameters, verbose=False):
+ """Retrieves the parameters with the relative best execution time over different devices"""
+
+ # Helper function
+ def argmax(iterable):
+ return max(enumerate(iterable), key=lambda x: x[1])[0]
+
+ # Computes the sum of the execution times over the different devices
+ performance_sums = []
+ for parameters in common_parameters:
+ performance_sum = sum([r["relative_performance"] for r in common_results if r["parameters"] == parameters])
+ performance_sums.append(performance_sum)
+
+ # Retrieves the entry with the highest performance
+ best_index = argmax(performance_sums)
+ best_performance = performance_sums[best_index]
+ best_parameters = common_parameters[best_index]
+
+ # Completed, report and return the results
+ if verbose:
+ print("[database] " + str(name) + " with performance " + str(best_performance))
+ return best_parameters
diff --git a/scripts/database/database/clblast.py b/scripts/database/database/clblast.py
new file mode 100644
index 00000000..8190f225
--- /dev/null
+++ b/scripts/database/database/clblast.py
@@ -0,0 +1,155 @@
+
+# This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This file follows the
+# PEP8 Python style guide and uses a max-width of 120 characters per line.
+#
+# Author(s):
+# Cedric Nugteren <www.cedricnugteren.nl>
+
+import os
+
+# Constants from the C++ code
+VENDOR_DEFAULT = "default"
+DEVICE_TYPE_DEFAULT = "All"
+DEVICE_NAME_DEFAULT = "default"
+
+# List of attributes
+DEVICE_TYPE_ATTRIBUTES = ["device_vendor", "device_type"]
+DEVICE_ATTRIBUTES = ["device", "device_core_clock", "device_compute_units"]
+KERNEL_ATTRIBUTES = ["precision", "kernel_family"]
+ARGUMENT_ATTRIBUTES = ["arg_m", "arg_n", "arg_k", "arg_alpha", "arg_beta"]
+ATTRIBUTES = DEVICE_ATTRIBUTES + DEVICE_TYPE_ATTRIBUTES + KERNEL_ATTRIBUTES + ARGUMENT_ATTRIBUTES
+GROUP_ATTRIBUTES = DEVICE_TYPE_ATTRIBUTES + KERNEL_ATTRIBUTES + ["kernel"] + ARGUMENT_ATTRIBUTES
+
+
+def precision_to_string(precision):
+ """Translates a precision number (represented as Python string) into a descriptive string"""
+ if precision == "16":
+ return "Half"
+ elif precision == "32":
+ return "Single"
+ elif precision == "64":
+ return "Double"
+ elif precision == "3232":
+ return "ComplexSingle"
+ elif precision == "6464":
+ return "ComplexDouble"
+ else:
+ raise("Unknown precision: " + precision)
+
+
+def get_cpp_separator():
+ """Retrieves a C++ comment separator"""
+ return "// ================================================================================================="
+
+
+def get_cpp_header(family):
+ """Retrieves the C++ header"""
+ return ("\n" + get_cpp_separator() + """
+// 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):
+// Database generator <database.py>
+//
+// This file populates the database with best-found tuning parameters for the '%s' kernels.
+//\n"""
+ % family.title() + get_cpp_separator() + "\n\nnamespace clblast {\n" + get_cpp_separator())
+
+
+def get_cpp_footer():
+ """Retrieves the C++ footer"""
+ return "\n} // namespace clblast\n"
+
+
+def get_cpp_precision(family, precision):
+ """Retrieves the C++ code for the start of a new precision"""
+ precision_string = precision_to_string(precision)
+ camelcase_name = family.title().replace("_", "")
+ return("\n\nconst Database::DatabaseEntry Database::%s%s = {\n \"%s\", Precision::k%s, {\n"
+ % (camelcase_name, precision_string, camelcase_name, precision_string))
+
+
+def get_cpp_device_vendor(vendor, device_type):
+ """Retrieves the C++ code for the (default) vendor and device type"""
+ if vendor == VENDOR_DEFAULT and device_type == DEVICE_TYPE_DEFAULT:
+ return " { // Default\n kDeviceType%s, \"%s\", {\n" % (device_type, vendor)
+ device_type_caps = device_type[0].upper() + device_type[1:]
+ return " { // %s %ss\n kDeviceType%s, \"%s\", {\n" % (vendor, device_type, device_type_caps, vendor)
+
+
+def print_cpp_database(database, output_dir):
+ """Outputs the database as C++ code"""
+
+ # Iterates over the kernel families
+ kernel_families = sorted(set([s["kernel_family"] for s in database["sections"]]))
+ for family_name in kernel_families:
+ family_database = [s for s in database["sections"] if s["kernel_family"] == family_name]
+
+ # Opens a new file for each kernel family
+ full_path = os.path.join(output_dir, family_name + ".hpp")
+ with open(full_path, 'w+') as f:
+ f.write(get_cpp_header(family_name))
+
+ # Loops over the different precision (e.g. 16, 32, 3232, 64, 6464)
+ precisions = sorted(set([s["precision"] for s in database["sections"]])) # Based on full database
+ for precision in precisions:
+ precision_database = [s for s in family_database if s["precision"] == precision]
+ f.write(get_cpp_precision(family_name, precision))
+
+ # In case there is nothing found at all (e.g. 16-bit): continue as if this was a precision of 32 but
+ # with the defaults only
+ if len(precision_database) == 0:
+ print("[database] No results found for %s:%s, retrieving defaults from %s:32" %
+ (family_name, precision, family_name))
+ precision_database = [s for s in family_database if s["precision"] == "32"
+ and s["device_vendor"] == VENDOR_DEFAULT
+ and s["device_type"] == DEVICE_TYPE_DEFAULT
+ and s["device"] == DEVICE_NAME_DEFAULT]
+
+ # Loops over device vendors (e.g. AMD)
+ device_vendors = sorted(set([s["device_vendor"] for s in precision_database]))
+ for vendor in device_vendors:
+ vendor_database = [s for s in precision_database if s["device_vendor"] == vendor]
+
+ # Loops over device types (e.g. GPU)
+ device_types = sorted(set([s["device_type"] for s in vendor_database]))
+ for device_type in device_types:
+ type_database = [s for s in vendor_database if s["device_type"] == device_type]
+ f.write(get_cpp_device_vendor(vendor, device_type))
+
+ # Loops over every device of this vendor-type combination
+ devices = sorted(set([s["device"] for s in type_database]))
+ for device_name in devices:
+ device_database = [s for s in type_database if s["device"] == device_name]
+ device_name_quoted = "\"%s\"," % device_name
+ device_name_cpp = " { %-50s { " % device_name_quoted
+ f.write(device_name_cpp)
+
+ # Collects the parameters for this entry
+ parameters = []
+ kernels = sorted(set([s["kernel"] for s in device_database]))
+ for kernel in kernels:
+ kernel_database = [s for s in device_database if s["kernel"] == kernel]
+
+ assert len(kernel_database) == 1
+ results = kernel_database[0]["results"]
+
+ assert len(results) == 1
+ new_parameters = results[0]["parameters"]
+ for parameter_name in sorted(new_parameters):
+ parameter_value = new_parameters[parameter_name]
+ parameters.append("{\"" + parameter_name + "\"," + str(parameter_value) + "}")
+
+ # Prints the entry
+ f.write(", ".join(parameters))
+ f.write(" } },\n")
+
+ # Prints the vendor-type combination footer
+ f.write(" }\n },\n")
+
+ # Prints the precision footer
+ f.write(" }\n};\n\n" + get_cpp_separator())
+
+ # Prints the file footer
+ f.write(get_cpp_footer())
diff --git a/scripts/database/database/db.py b/scripts/database/database/db.py
new file mode 100644
index 00000000..94948b1a
--- /dev/null
+++ b/scripts/database/database/db.py
@@ -0,0 +1,64 @@
+
+# This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This file follows the
+# PEP8 Python style guide and uses a max-width of 120 characters per line.
+#
+# Author(s):
+# Cedric Nugteren <www.cedricnugteren.nl>
+
+import clblast
+
+
+def length(database):
+ """Computes the total number of tuning entries"""
+ num_tuning_entries = 0
+ for section in database["sections"]:
+ num_tuning_entries += len(section["results"])
+ return num_tuning_entries
+
+
+def add_section(database, new_section):
+ """Adds a new section to the database"""
+ for old_section in database["sections"]:
+
+ # Verify whether the sections match
+ equal = True
+ for attribute in new_section.keys():
+ if attribute != "results":
+ if attribute not in old_section or new_section[attribute] != old_section[attribute]:
+ equal = False
+ break
+
+ # They match: append the new section's results to the corresponding entry in the database and return
+ if equal:
+ old_section["results"] = combine_results(old_section["results"], new_section["results"])
+ return database
+
+ # No match found: append the whole new section to the database
+ database["sections"].append(new_section)
+ return database
+
+
+def combine_results(old_results, new_results):
+ """Adds new results to the results JSON list"""
+ for new_result in new_results:
+ old_results = combine_result(old_results, new_result)
+ return old_results
+
+
+def combine_result(old_results, new_result):
+ """Adds a new result to the results JSON list; filters for duplicate entries and saves the best performing one"""
+
+ # Loops over all existing results to test for already existing entries with these parameters
+ for old_result in old_results:
+
+ # Verify whether the results match
+ equal = new_result["parameters"] == old_result["parameters"]
+
+ # They match: keep only the one with the minimum execution time
+ if equal:
+ old_result["time"] = min(old_result["time"], new_result["time"])
+ return old_results
+
+ # No match found: append a new result
+ old_results.append(new_result)
+ return old_results
diff --git a/scripts/database/database/defaults.py b/scripts/database/database/defaults.py
new file mode 100644
index 00000000..00405908
--- /dev/null
+++ b/scripts/database/database/defaults.py
@@ -0,0 +1,180 @@
+
+# This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This file follows the
+# PEP8 Python style guide and uses a max-width of 120 characters per line.
+#
+# Author(s):
+# Cedric Nugteren <www.cedricnugteren.nl>
+
+
+import clblast
+import bests
+
+
+def set_default_device(section):
+ """Sets the device name and parameters to some default values"""
+ section["device"] = clblast.DEVICE_NAME_DEFAULT
+ section["device_compute_units"] = 0
+ section["device_core_clock"] = 0
+ return section
+
+
+def set_identifiers(database, group_by_attributes, identifier_name):
+ """Sets a group-identifier based on a given set of attributes. Modifies the database but also returns a list of
+ unique identifiers."""
+ identifiers = []
+ for section in database["sections"]:
+ identifier = []
+ for attribute in group_by_attributes:
+ if attribute in section:
+ identifier.append(section[attribute])
+ section[identifier_name] = ";".join(identifier)
+ identifiers.append(section[identifier_name])
+ return sorted(set(identifiers))
+
+
+def remove_identifiers(database, identifier_name):
+ """Removes an identifier from all sections in the database"""
+ for section in database["sections"]:
+ section.pop(identifier_name, None)
+
+
+def get_groups_by_identifier(database, group_identifiers, identifier_name):
+ """Returns a list of (group, group_identifier) tuples based a previously made grouping"""
+ groups = []
+ for group_identifier in group_identifiers:
+
+ # Get all sections in this group
+ group = []
+ for section in database["sections"]:
+ if section[identifier_name] == group_identifier:
+ group.append(section)
+
+ groups.append((group, group_identifier))
+ return groups
+
+
+def calculate_defaults(database, verbose):
+ """Sets defaults for devices of the same type/vendor"""
+
+ # Groups the database by kernel, vendor and device type (e.g. AMD GPU)
+ group_identifiers = set_identifiers(database, clblast.GROUP_ATTRIBUTES, "group_identifier")
+ groups = get_groups_by_identifier(database, group_identifiers, "group_identifier")
+
+ # Loops over all groups
+ default_sections = {"sections": []}
+ for group, group_identifier in groups:
+
+ # Computes the best parameters
+ default_parameters = get_common_best_parameters(group, group_identifier, verbose)
+
+ # Stores all the section's data
+ assert len(group) > 0
+ default_section = {}
+ for attribute in group[0].keys():
+ if attribute != "results" and attribute != "group_identifier":
+ default_section[attribute] = group[0][attribute]
+ default_section = set_default_device(default_section)
+ default_section["results"] = [{"time": 0.0, "parameters": default_parameters}]
+ default_sections["sections"].append(default_section)
+
+ # Groups the database by kernel, vendor and device type (e.g. AMD GPU) - but not by arguments! This is to check for
+ # mis-matched arguments.
+ attributes = clblast.DEVICE_TYPE_ATTRIBUTES + clblast.KERNEL_ATTRIBUTES + ["kernel"]
+ group_identifiers = set_identifiers(default_sections, attributes, "temp_identifier")
+ groups = get_groups_by_identifier(default_sections, group_identifiers, "temp_identifier")
+ for group, group_identifier in groups:
+ if len(group) != 1:
+ print("[ERROR] Entries for a single kernel with multiple argument values: " + str(group_identifier))
+ assert len(group) == 1
+ remove_identifiers(default_sections, "temp_identifier")
+
+ # Groups the database by kernel only
+ group_identifiers = set_identifiers(database, clblast.KERNEL_ATTRIBUTES + ["kernel"], "group_identifier")
+ groups = get_groups_by_identifier(database, group_identifiers, "group_identifier")
+
+ # Loops over all groups
+ for group, group_identifier in groups:
+
+ # Computes the best parameters
+ default_parameters = get_common_best_parameters(group, group_identifier, verbose)
+
+ # Stores all the section's data
+ assert len(group) > 0
+ default_section = {}
+ for attribute in group[0].keys():
+ if attribute != "results" and attribute != "group_identifier":
+ default_section[attribute] = group[0][attribute]
+ default_section = set_default_device(default_section)
+ default_section["device_vendor"] = clblast.VENDOR_DEFAULT
+ default_section["device_type"] = clblast.DEVICE_TYPE_DEFAULT
+ default_section["results"] = [{"time": 0.0, "parameters": default_parameters}]
+ default_sections["sections"].append(default_section)
+
+ # Database with both types of defaults only
+ return default_sections
+
+
+def get_smallest_best_parameters(group):
+ """Sets defaults based on the smallest values of all known entries. The average might be better for performance but
+ some parameters might not be supported on other devices."""
+
+ # Counts the number of devices in this group
+ assert len(group) > 0
+
+ # Find the smallest values of the parameters
+ min_parameters = {}
+ for section in group:
+ assert len(section["results"]) > 0
+ minimum_time = min([result["time"] for result in section["results"]])
+ for result in section["results"]:
+ if result["time"] == minimum_time:
+ for parameter in result["parameters"]:
+ if parameter in min_parameters:
+ min_parameters[parameter] = min(min_parameters[parameter], result["parameters"][parameter])
+ else:
+ min_parameters[parameter] = result["parameters"][parameter]
+
+ return min_parameters
+
+
+def get_common_best_parameters(group, group_identifier, verbose):
+ """Sets defaults based on the best values of entries supported by all devices. This might cause a problem in case
+ not every device was tuned with the same parameters. In that case it falls back to the above method to retrieve
+ the smallest best execution time"""
+
+ # Counts the number of devices in this group
+ num_devices = len(group)
+ assert num_devices > 0
+
+ # Inserts the relative execution times into the database
+ for section in group:
+ assert len(section["results"]) > 0
+ minimum_time = min([result["time"] for result in section["results"]])
+ for result in section["results"]:
+ result["relative_performance"] = minimum_time / result["time"]
+
+ # Determine which parameters are available for all devices
+ common_parameters = [result["parameters"] for result in group[0]["results"]] # Parameters of the first section
+ for i in range(1, num_devices):
+ section_parameters = [result["parameters"] for result in group[i]["results"]]
+ common_parameters = [p for p in section_parameters if p in common_parameters] # Intersection of the parameters
+
+ # Fall back to another method in case there are no shared entries at all across devices
+ if len(common_parameters) == 0:
+ if verbose:
+ print("[database] No common kernels for: " + str(group_identifier) + " with devices: %d " % num_devices)
+ smallest_best_parameters = get_smallest_best_parameters(group)
+ if verbose:
+ print("[database] " + str(group_identifier))
+ return smallest_best_parameters
+
+ # Removes entries with parameters which are not common
+ common_results = []
+ for section in group:
+ for result in section["results"]:
+ if result["parameters"] in common_parameters:
+ common_results.append(result)
+
+ # Retrieves the entries with the highest relative performance
+ relative_best_parameters = bests.get_relative_bests(group_identifier, common_results, common_parameters, verbose)
+ return relative_best_parameters
diff --git a/scripts/database/database/io.py b/scripts/database/database/io.py
new file mode 100644
index 00000000..d14f1297
--- /dev/null
+++ b/scripts/database/database/io.py
@@ -0,0 +1,60 @@
+
+# This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This file follows the
+# PEP8 Python style guide and uses a max-width of 120 characters per line.
+#
+# Author(s):
+# Cedric Nugteren <www.cedricnugteren.nl>
+
+import re
+import json
+
+try:
+ from urllib.request import urlopen # Python 3
+except ImportError:
+ from urllib2 import urlopen # Python 2
+
+
+def download_database(filename, database_url):
+ """Downloads a database and saves it to disk"""
+ print("[database] Downloading database from '" + database_url + "'...")
+ database = urlopen(database_url)
+ with open(filename, "wb") as f:
+ f.write(database.read())
+
+
+def load_database(filename):
+ """Loads a database from disk"""
+ print("[database] Loading database from '" + filename + "'")
+ with open(filename) as f:
+ return json.load(f)
+
+
+def save_database(database, filename):
+ """Saves a database to disk"""
+ print("[database] Saving database to '" + filename + "'")
+ with open(filename, "wb") as f:
+ json.dump(database, f, sort_keys=True, indent=4)
+
+
+def load_tuning_results(filename):
+ """Loads JSON data from file and pre-processes it"""
+ with open(filename) as f:
+ json_data = json.load(f)
+
+ # Removes the numbering following the kernel family name
+ json_data["kernel_family"] = re.sub(r'_\d+', '', json_data["kernel_family"])
+
+ # Adds the kernel name to the section instead of to the individual results
+ assert len(json_data["results"]) > 0
+ json_data["kernel"] = json_data["results"][0]["kernel"]
+ for result in json_data["results"]:
+ assert json_data["kernel"] == result["kernel"]
+ result.pop("kernel", None)
+
+ # Removes the 'PRECISION' parameter from the individual results: it is redundant
+ for result in json_data["results"]:
+ assert json_data["precision"] == str(result["parameters"]["PRECISION"])
+ result["parameters"].pop("PRECISION", None)
+
+ # All done
+ return json_data