def convert_kernel(value, dshapes=None): template = None from llvm.core import FunctionType if isinstance(value, tuple): # Ex: ('cpp', source) or ('f8(f8,f8)', _add) if len(value) == 2 and isinstance(value[1], types.FunctionType) and \ isinstance(value[0], _strtypes): if '*' in value[0]: return None, (to_dshapes(value[0]), value[1]) krnl = frompyfunc(value[1], value[0]) # use Numba elif len(value) == 2 and isinstance(value[1], _strtypes) and \ isinstance(value[0], _strtypes): krnl = _convert_string(value[0], value[1]) # Use blaze_kernels.from<val0> else: raise TypeError("Cannot parse kernel specification %s" % value) elif isinstance(value, types.FunctionType): # Called when a function is used for value directly # dshapes must be present as the mapping and as datashapes istemplate = any(isinstance(ds, TypeSet) for ds in dshapes[:-1]) if istemplate: krnl = None template = (dshapes[:-1], value) else: args = ','.join(str(to_numba(ds)) for ds in dshapes[:-1]) signature = '{0}({1})'.format(str(to_numba(dshapes[-1])), args) krnl = frompyfunc(value, signature, dshapes=dshapes) elif isinstance(value, FunctionType): # Called the LLVM Function is used in directly krnl = BlazeElementKernel(value, dshapes=dshapes) else: raise TypeError("Cannot convert value = %s and dshapes = %s" % (value, dshapes)) return krnl, template
def find_best_kernel(self, types): mtypes = [ds.sigform() for ds in types] ranks = [len(ds)-1 for ds in types] test_rank = min(ranks) mtypes = tuple(ds.subarray(rank-test_rank) for ds, rank in zip(mtypes, ranks)) krnl = None while test_rank >= 0: krnl = self.dispatch.get(mtypes, None) if krnl is not None or test_rank==0: break test_rank -= 1 mtypes = tuple(ds.subarray(1) for ds in mtypes) # Templates Only works for "measures" if krnl is None: measures = tuple(ds.measure for ds in types) for sig, template in self.templates: if sig == measures or matches_typeset(measures, sig): krnl = frompyfunc(template, [to_numba(x) for x in measures]) self.dispatch[measures] = krnl break if krnl is None: raise ValueError("Did not find matching kernel for " + str(mtypes)) return krnl