Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions examples/c/vector_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@

kernel_string = """
#include <omp.h>
#include <string.h>

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);

Expand All @@ -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);
}
"""

Expand All @@ -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"])
52 changes: 42 additions & 10 deletions kernel_tuner/backends/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -82,6 +82,26 @@
# 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

Check warning on line 92 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this trailing comment on the previous empty line.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJA&open=AaHKh7WxFt_lLS8lgtJA&pullRequest=421

Check warning on line 92 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJC&open=AaHKh7WxFt_lLS8lgtJC&pullRequest=421

Check warning on line 92 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a citation of the person who can best explain this comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJB&open=AaHKh7WxFt_lLS8lgtJB&pullRequest=421
("values", C.c_float * 8)
]
# TODO: fix this hack

Check warning on line 95 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJE&open=AaHKh7WxFt_lLS8lgtJE&pullRequest=421

Check warning on line 95 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a citation of the person who can best explain this comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJD&open=AaHKh7WxFt_lLS8lgtJD&pullRequest=421
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"""
Expand All @@ -95,7 +115,7 @@
# 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)

Expand Down Expand Up @@ -158,18 +178,21 @@
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

Check warning on line 186 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a citation of the person who can best explain this comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJF&open=AaHKh7WxFt_lLS8lgtJF&pullRequest=421

Check warning on line 186 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJG&open=AaHKh7WxFt_lLS8lgtJG&pullRequest=421
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):
Expand Down Expand Up @@ -364,8 +387,17 @@
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:

Check failure on line 391 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this expression; used as a condition it will always be constant.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtI_&open=AaHKh7WxFt_lLS8lgtI_&pullRequest=421
func(*[arg.ctypes for arg in c_args])
# TODO: Use error status and raise exception if appropriate

Check warning on line 393 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJI&open=AaHKh7WxFt_lLS8lgtJI&pullRequest=421

Check warning on line 393 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a citation of the person who can best explain this comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJH&open=AaHKh7WxFt_lLS8lgtJH&pullRequest=421
self.last_result = c_args[-1].numpy.convert()

# TODO: fix this

Check warning on line 396 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a citation of the person who can best explain this comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJJ&open=AaHKh7WxFt_lLS8lgtJJ&pullRequest=421

Check warning on line 396 in kernel_tuner/backends/compiler.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7WxFt_lLS8lgtJK&open=AaHKh7WxFt_lLS8lgtJK&pullRequest=421
time = self.last_result["time"]
else:
time = func(*[arg.ctypes for arg in c_args])
self.last_result = time

return time

Expand Down
29 changes: 21 additions & 8 deletions kernel_tuner/observers/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions kernel_tuner/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@
if isinstance(arg, Tunable):
return correct, ""

# Handle compiler backend extended return values
# TODO: De-duplicate this string literal

Check warning on line 229 in kernel_tuner/util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a citation of the person who can best explain this comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7cmFt_lLS8lgtJL&open=AaHKh7cmFt_lLS8lgtJL&pullRequest=421

Check warning on line 229 in kernel_tuner/util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHKh7cmFt_lLS8lgtJM&open=AaHKh7cmFt_lLS8lgtJM&pullRequest=421
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):
Expand Down
32 changes: 32 additions & 0 deletions test/test_compiler_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 }
Loading