diff --git a/examples/c/vector_add.py b/examples/c/vector_add.py index 38122b49..d224601b 100644 --- a/examples/c/vector_add.py +++ b/examples/c/vector_add.py @@ -7,10 +7,22 @@ kernel_string = """ #include +#include typedef float vfloat __attribute__ ((vector_size (vecsize*4))); -extern "C" float vector_add(vfloat *c, vfloat *a, vfloat *b, int n) { +// TODO: Supply a C header file which provides this struct and helper functions +// to fill it +// TODO: Add versioning to this strict somehow. (In the name or a first field?) +struct KernelTunerExtendedReturnValue { + bool success; + char error[256]; + int count; + char keys[8][16]; + float values[8]; +}; + +extern "C" void vector_add(vfloat *c, vfloat *a, vfloat *b, int n, KernelTunerExtendedReturnValue *ret) { double start = omp_get_wtime(); int chunk = n/(vecsize*nthreads); @@ -22,7 +34,10 @@ } } - return (float)((omp_get_wtime() - start)*1e3); + ret->success = true; + ret->count = 1; + strcpy(ret->keys[0], "time"); + ret->values[0] = (float)((omp_get_wtime() - start)*1e3); } """ @@ -33,12 +48,12 @@ c = numpy.zeros_like(b) n = numpy.int32(size) -args = [c, a, b, n] +args = [c, a, b, n, "KernelTunerExtendedReturnValue"] tune_params = OrderedDict() tune_params["nthreads"] = [1, 2, 3, 4, 8, 12, 16, 24, 32] tune_params["vecsize"] = [1, 2, 4, 8, 16] -answer = [a + b, None, None, None] +answer = [a + b, None, None, None, None] tune_kernel("vector_add", kernel_string, size, args, tune_params, answer=answer, compiler_options=["-fopenmp", "-O3"]) diff --git a/kernel_tuner/backends/compiler.py b/kernel_tuner/backends/compiler.py index ec2b2da6..c62b2b6e 100644 --- a/kernel_tuner/backends/compiler.py +++ b/kernel_tuner/backends/compiler.py @@ -13,7 +13,7 @@ import numpy.ctypeslib from kernel_tuner.backends.backend import CompilerBackend -from kernel_tuner.observers.compiler import CompilerRuntimeObserver +from kernel_tuner.observers.compiler import CompilerDefaultObserver from kernel_tuner.util import ( delete_temp_file, get_temp_filename, @@ -82,6 +82,26 @@ def get_array_module(*args): # of the argument data. For an ndarray, the ctypes object is a wrapper for the ndarray's data. Argument = namedtuple("Argument", ["numpy", "ctypes"]) +# This represents an extended return value from C functions, allowing +# it to pass multiple objectives and an error string. +class ExtendedReturnValue(C.Structure): + _fields_ = [ + ("success", C.c_bool), + ("error", C.c_char * 256), + ("count", C.c_int), + ("keys", (C.c_char * 16) * 8), # TODO: double-check memory order + ("values", C.c_float * 8) + ] + # TODO: fix this hack + def copy(self): + return self + def convert(self): + result = {} + for i in range(self.count): + key = C.cast(self.keys[i], C.c_char_p).value.decode("ascii") + result[key] = self.values[i] + return result + class CompilerFunctions(CompilerBackend): """Class that groups the code for running and compiling C functions""" @@ -95,7 +115,7 @@ def __init__(self, iterations=7, compiler_options=None, compiler=None, observers # allocations contains a clean copy of the memory self.allocations = [] self.observers = observers or [] - self.observers.append(CompilerRuntimeObserver(self)) + self.observers.append(CompilerDefaultObserver(self)) for obs in self.observers: obs.register_device(self) @@ -158,18 +178,21 @@ def ready_argument_list(self, arguments): ctype_args = [None for _ in arguments] for i, arg in enumerate(arguments): - if not (isinstance(arg, (np.ndarray, np.number, DeviceArray)) or is_cupy_array(arg)): + if not (isinstance(arg, (np.ndarray, np.number, DeviceArray)) or is_cupy_array(arg) or (isinstance(arg, str) and arg == "KernelTunerExtendedReturnValue")): raise TypeError( f"Argument is not numpy or cupy ndarray or numpy scalar or HIP Python DeviceArray but a {type(arg)}" ) - dtype_str = arg.typestr if isinstance(arg, DeviceArray) else str(arg.dtype) - if isinstance(arg, np.ndarray): - if dtype_str in dtype_map.keys(): - data_ctypes = arg.ctypes.data_as(C.POINTER(dtype_map[dtype_str])) + if isinstance(arg, str) and arg == "KernelTunerExtendedReturnValue": + # TODO: double check that this gets handled properly everywhere + arg = ExtendedReturnValue() + data_ctypes = C.pointer(arg) + elif isinstance(arg, np.ndarray): + if str(arg.dtype) in dtype_map.keys(): + data_ctypes = arg.ctypes.data_as(C.POINTER(dtype_map[str(arg.dtype)])) else: raise TypeError("unknown dtype for ndarray") elif isinstance(arg, np.generic): - data_ctypes = dtype_map[dtype_str](arg) + data_ctypes = dtype_map[str(arg.dtype)](arg) elif is_cupy_array(arg): data_ctypes = C.c_void_p(arg.data.ptr) elif isinstance(arg, DeviceArray): @@ -364,8 +387,17 @@ def run_kernel(self, func, c_args, threads, grid, stream=None): logging.debug("run_kernel") logging.debug("arguments=" + str([str(arg.ctypes) for arg in c_args])) - time = func(*[arg.ctypes for arg in c_args]) - self.last_result = time + # HACK: detect mode + if True: + func(*[arg.ctypes for arg in c_args]) + # TODO: Use error status and raise exception if appropriate + self.last_result = c_args[-1].numpy.convert() + + # TODO: fix this + time = self.last_result["time"] + else: + time = func(*[arg.ctypes for arg in c_args]) + self.last_result = time return time diff --git a/kernel_tuner/observers/compiler.py b/kernel_tuner/observers/compiler.py index c0488723..b842ba7e 100644 --- a/kernel_tuner/observers/compiler.py +++ b/kernel_tuner/observers/compiler.py @@ -3,21 +3,34 @@ from kernel_tuner.observers.observer import BenchmarkObserver -class CompilerRuntimeObserver(BenchmarkObserver): +class CompilerDefaultObserver(BenchmarkObserver): """Observer that collects results returned by benchmarking function in the C backend""" def __init__(self, dev): self.dev = dev + # The default objective name if a single float is returned self.objective = "time" - self.times = [] + self.results = {} def after_finish(self): - self.times.append(self.dev.last_result) + # Result can be either a single float, or a dict of str:float keyvals + + if not isinstance(self.dev.last_result, dict): + # Single float result is treated as self.objective + last_result = { self.objective: self.dev.last_result } + else: + last_result = self.dev.last_result.copy() + + for obs in last_result: + plural = obs + "s" + if plural not in self.results: + self.results[plural] = [] + self.results[plural].append(last_result[obs]) def get_results(self): - results = { - self.objective: np.average(self.times), - self.objective + "s": self.times.copy(), - } - self.times = [] + # Return both the lists of individual results and their averages + results = self.results | { obs[:-1]: np.average(self.results[obs]) for obs in self.results } + + self.results = {} + return results diff --git a/kernel_tuner/util.py b/kernel_tuner/util.py index a917b68f..ccc7f99f 100644 --- a/kernel_tuner/util.py +++ b/kernel_tuner/util.py @@ -225,6 +225,11 @@ def check_individual_arguments(i, arg, kernel_argument): if isinstance(arg, Tunable): return correct, "" + # Handle compiler backend extended return values + # TODO: De-duplicate this string literal + if isinstance(arg, str) and arg == "KernelTunerExtendedReturnValue": + return correct, "" + # Handle numpy arrays and other array types if not isinstance(arg, (np.ndarray, np.generic, torch.Tensor, DeviceArray) + cupy_ndarray): if is_julia_array(arg): diff --git a/test/test_compiler_functions.py b/test/test_compiler_functions.py index 85391d44..12a966c9 100644 --- a/test/test_compiler_functions.py +++ b/test/test_compiler_functions.py @@ -12,6 +12,7 @@ import kernel_tuner from kernel_tuner.backends.compiler import Argument, CompilerFunctions, get_array_module, is_cupy_array +from kernel_tuner.observers.compiler import CompilerDefaultObserver from kernel_tuner.core import KernelInstance, KernelSource from kernel_tuner.util import delete_temp_file @@ -391,3 +392,34 @@ def test_memcpy_dtoh(): assert np.all(ready_arguments[0].numpy != output) cfunc.memcpy_dtoh(output, ready_arguments[0]) assert np.all(ready_arguments[0].numpy == output) + +def test_observer(): + dev = Mock() + observer = CompilerDefaultObserver(dev) + + res = observer.get_results() + assert res == {} + + dev.last_result = 1.0 + observer.after_finish() + dev.last_result = 2.0 + observer.after_finish() + res = observer.get_results() + assert res == { "times": [ 1.0, 2.0 ], "time": 1.5 } + + res = observer.get_results() + assert res == {} + + dev.last_result = { "time": 3.0 } + observer.after_finish() + dev.last_result = { "time": 4.0 } + observer.after_finish() + res = observer.get_results() + assert res == { "times": [ 3.0, 4.0 ], "time": 3.5 } + + dev.last_result = { "time": 5.0, "energy": 7.0 } + observer.after_finish() + dev.last_result = { "time": 6.0, "energy": 8.0 } + observer.after_finish() + res = observer.get_results() + assert res == { "times": [ 5.0, 6.0 ], "time": 5.5, "energys": [ 7.0, 8.0 ], "energy": 7.5 }