1
2# This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This file follows the
3# PEP8 Python style guide and uses a max-width of 120 characters per line.
4#
5# Author(s):
6#   Cedric Nugteren <www.cedricnugteren.nl>
7
8import sys
9
10
11def get_best_results(database):
12    """Retrieves the results with the lowest execution times"""
13    sections_best = []
14    for section in database["sections"]:
15        section_best = {}
16
17        # Stores all the section's meta data
18        for attribute in section.keys():
19            if attribute != "results":
20                section_best[attribute] = section[attribute]
21
22        # Find the best result
23        parameters_best = None
24        time_best = sys.float_info.max
25        for result in section["results"]:
26            if result["time"] < time_best:
27                time_best = result["time"]
28                parameters_best = result["parameters"]
29
30        # Stores the best result
31        section_best["results"] = [{"time": time_best, "parameters": parameters_best}]
32        sections_best.append(section_best)
33
34    return {"sections": sections_best}
35
36
37def get_relative_bests(name, common_results, common_parameters, verbose=False):
38    """Retrieves the parameters with the relative best execution time over different devices"""
39
40    # Helper function
41    def argmax(iterable):
42        return max(enumerate(iterable), key=lambda x: x[1])[0]
43
44    # Computes the sum of the execution times over the different devices
45    performance_sums = []
46    for parameters in common_parameters:
47        performance_sum = sum([r["relative_performance"] for r in common_results if r["parameters"] == parameters])
48        performance_sums.append(performance_sum)
49
50    # Retrieves the entry with the highest performance
51    best_index = argmax(performance_sums)
52    best_performance = performance_sums[best_index]
53    best_parameters = common_parameters[best_index]
54
55    # Completed, report and return the results
56    if verbose:
57        print("[database] " + str(name) + " with performance " + str(best_performance))
58    return best_parameters
59