def interpret(func, env, args, storage=None, **kwds): assert len(args) == len(func.args) # Make a copy, since we're going to mutate our IR! func, _ = copy_function(func) # If it's a BLZ output, we want an interpreter that streams # the processing through in chunks if storage is not None: if len(func.type.restype.shape) == 0: raise TypeError('Require an array, not a scalar, for outputting to BLZ') env['stream-outer'] = True result_ndim = env['result-ndim'] = len(func.type.restype.shape) else: # Convert any persistent inputs to memory # TODO: should stream the computation in this case for i, arg in enumerate(args): if isinstance(arg._data, BLZDataDescriptor): args[i] = arg[:] # Update environment with dynd type information dynd_types = dict((arg, get_dynd_type(array)) for arg, array in zip(func.args, args) if isinstance(array._data, DyNDDataDescriptor)) env['dynd-types'] = dynd_types # Lift ckernels func, env = run_pipeline(func, env, run_time_passes) if storage is None: # Evaluate once values = dict(zip(func.args, args)) interp = CKernelInterp(values) visit(interp, func) return interp.result else: res_shape, res_dt = datashape.to_numpy(func.type.restype) dim_size = operator.index(res_shape[0]) row_size = ndt.type(str(func.type.restype.subarray(1))).data_size chunk_size = min(max(1, (1024*1024) // row_size), dim_size) # Evaluate by streaming the outermost dimension, # and using the BLZ data descriptor's append dst_dd = BLZDataDescriptor(blz.zeros((0,)+res_shape[1:], res_dt, rootdir=storage.path)) # Loop through all the chunks for chunk_start in range(0, dim_size, chunk_size): # Tell the interpreter which chunk size to use (last # chunk might be smaller) chunk_size = min(chunk_size, dim_size - chunk_start) # Evaluate the chunk args_chunk = [arg[chunk_start:chunk_start+chunk_size] if len(arg.dshape.shape) == result_ndim else arg for arg in args] values = dict(zip(func.args, args_chunk)) interp = CKernelChunkInterp(values, chunk_size, result_ndim) visit(interp, func) chunk = interp.result._data.dynd_arr() dst_dd.append(chunk) return blaze.Array(dst_dd)
def build_kerneltrees(func, jitted): """ Builds the kernel trees for all the nodes that are to be jitted, also populating lists of arguments. """ fuser = BuildKernelTrees(func, jitted) visit(fuser, func) return fuser.trees, fuser.arguments
def test_combinator(self): visitor = SampleVisitor() def op_blah(op): visitor.recorded.append(op.opcode) comb = combine(visitor, {'op_blah': op_blah}) with self.b.at_front(self.entry): self.b.emit(Op('blah', None, [])) visit(comb, self.f) self.eq(visitor.recorded, ['blah', 'mul'])
def identify_jitnodes(func, env): """ Identifies which nodes (kernels and converters) should be jitted, and creates a dictionary mapping them to corresponding BlazeElementKernels. """ v = IdentifyJitKernels(func, env) visit(v, func) v = IdentifyJitConvertors(func, v.jitted) visit(v, func) return v.jitted
def test_combinator(self): visitor = SampleVisitor() def op_blah(op): visitor.recorded.append(op.opcode) comb = combine(visitor, {"op_blah": op_blah}) with self.b.at_front(self.entry): self.b.emit(Op("blah", None, [])) visit(comb, self.f) self.eq(visitor.recorded, ["blah", "mul"])
def interpret(func, env, args, **kwds): assert len(args) == len(func.args) # Make a copy, since we're going to mutate our IR! func = copy_function(func) # Update environment with dynd type information dynd_types = dict((arg, get_dynd_type(array)) for arg, array in zip(func.args, args) if isinstance(array._data, DyNDDataDescriptor)) env['dynd-types'] = dynd_types # Lift ckernels func, env = run_pipeline(func, env, run_time_passes) print(func) # Evaluate values = dict(zip(func.args, args)) interp = CKernelInterp(values) visit(interp, func) return interp.result
def interpret(func, env, storage=None, **kwds): args = env['runtime.arglist'] if storage is None: # Evaluate once values = dict(zip(func.args, args)) interp = CKernelInterp(values) visit(interp, func) return interp.result else: result_ndim = env['result-ndim'] res_shape, res_dt = datashape.to_numpy(func.type.restype) dim_size = operator.index(res_shape[0]) row_size = ndt.type(str(func.type.restype.subarray(1))).data_size chunk_size = min(max(1, (1024 * 1024) // row_size), dim_size) # Evaluate by streaming the outermost dimension, # and using the BLZ data descriptor's append dst_dd = BLZDataDescriptor( blz.zeros((0, ) + res_shape[1:], res_dt, rootdir=storage.path)) # Loop through all the chunks for chunk_start in range(0, dim_size, chunk_size): # Tell the interpreter which chunk size to use (last # chunk might be smaller) chunk_size = min(chunk_size, dim_size - chunk_start) # Evaluate the chunk args_chunk = [ arg[chunk_start:chunk_start + chunk_size] if len(arg.dshape.shape) == result_ndim else arg for arg in args ] values = dict(zip(func.args, args_chunk)) interp = CKernelChunkInterp(values, chunk_size, result_ndim) visit(interp, func) chunk = interp.result._data.dynd_arr() dst_dd.append(chunk) return blaze.Array(dst_dd)
def interpret(func, env, storage=None, **kwds): args = env['runtime.arglist'] if storage is None: # Evaluate once values = dict(zip(func.args, args)) interp = CKernelInterp(values) visit(interp, func) return interp.result else: result_ndim = env['result-ndim'] res_shape, res_dt = datashape.to_numpy(func.type.restype) dim_size = operator.index(res_shape[0]) row_size = ndt.type(str(func.type.restype.subarray(1))).data_size chunk_size = min(max(1, (1024*1024) // row_size), dim_size) # Evaluate by streaming the outermost dimension, # and using the BLZ data descriptor's append dst_dd = BLZDataDescriptor(blz.zeros((0,)+res_shape[1:], res_dt, rootdir=storage.path)) # Loop through all the chunks for chunk_start in range(0, dim_size, chunk_size): # Tell the interpreter which chunk size to use (last # chunk might be smaller) chunk_size = min(chunk_size, dim_size - chunk_start) # Evaluate the chunk args_chunk = [arg[chunk_start:chunk_start+chunk_size] if len(arg.dshape.shape) == result_ndim else arg for arg in args] values = dict(zip(func.args, args_chunk)) interp = CKernelChunkInterp(values, chunk_size, result_ndim) visit(interp, func) chunk = interp.result._data.dynd_arr() dst_dd.append(chunk) return blaze.Array(dst_dd)
def verify_semantics(func, env=None): verifier = combine(Verifier(), env and env.get("verify.handlers")) visit(verifier, func)
def generate_constraints(func, G): gen = ConstraintGenerator(func, G) ir.visit(gen, func, errmissing=True) return gen.constraints, gen.metadata
def run(func, env): visit(CKernelLifter(env), func)
def typecheck(func, env): context = env["flypy.typing.context"] visit(TypeChecker(context), func) check_scoping(func, env)
def run(func, env): """Generate runtime calls into thread library""" if env.get("verify"): visit(Verify(), func) visit(ExceptionChecking(func), func)
def jitter(func, jit_env): v = KernelJitter(func) visit(v, func) v = ConvertJitter(func, v.jitted) visit(v, func) jit_env['jitted'] = v.jitted
def typecheck(func, env): context = env['numba.typing.context'] visit(TypeChecker(context), func)
def lower_costful(func, env=None): visit(LowerExceptionChecksCostful(func), func)
def treebuilder(func, jit_env): fuser = JitFuser(func, jit_env['jitted']) visit(fuser, func) jit_env['trees'] = fuser.trees jit_env['arguments'] = fuser.arguments
def typecheck(func, env): context = env['flypy.typing.context'] visit(TypeChecker(context), func) check_scoping(func, env)
def jitter(func, jit_env): v = Jitter(func) visit(v, func) jit_env['jitted'] = v.jitted