def _get_generic_filter_raw(rk, filter_size, mode, wshape, offsets, cval, int_type): """Generic filter implementation based on a raw kernel.""" setup = ''' int iv = 0; double values[{}]; double val_out;'''.format(filter_size) sub_call = '''raw_kernel::{}(values, {}, &val_out); y = cast<Y>(val_out);'''.format(rk.name, filter_size) return _filters_core._generate_nd_kernel( 'generic_{}_{}'.format(filter_size, rk.name), setup, 'values[iv++] = cast<double>({value});', sub_call, mode, wshape, int_type, offsets, cval, preamble='namespace raw_kernel {{\n{}\n}}'.format( # Users can test RawKernel independently, but when passed to here # it must be used as a device function here. In fact, RawKernel # wouldn't compile if code only contains device functions, so this # is necessary. rk.code.replace('__global__', '__device__')), options=rk.options)
def _get_generic_filter_red(rk, in_dtype, out_dtype, filter_size, mode, wshape, offsets, cval, int_type): """Generic filter implementation based on a reduction kernel.""" # Get the temporary output c type in_param, out_param = rk.in_params[0], rk.out_params[0] out_ctype = out_param.ctype if out_param.dtype is None: # resolve template out_ctype = cupy._core._scalar.get_typename( in_dtype if out_param.ctype == in_param.ctype else out_dtype) # Get code chunks setup = ''' int iv = 0; X values[{size}]; CArray<X, 1, true, true> sub_in(values, {{{size}}}); {out_ctype} val_out; CArray<{out_ctype}, 1, true, true> sub_out(&val_out, {{1}}); '''.format(size=filter_size, out_ctype=out_ctype) sub_call = '''reduction_kernel::{}(sub_in, sub_out); y = cast<Y>(val_out);'''.format(rk.name) sub_kernel = _reduction_kernel_code(rk, filter_size, out_dtype, in_dtype) # Get the final kernel return _filters_core._generate_nd_kernel( 'generic_{}_{}'.format(filter_size, rk.name), setup, 'values[iv++] = {value};', sub_call, mode, wshape, int_type, offsets, cval, preamble=sub_kernel, options=getattr(rk, 'options', ()))
def _get_generic_filter_raw(rk, filter_size, mode, wshape, offsets, cval, int_type): """Generic filter implementation based on a raw kernel.""" setup = ''' int iv = 0; double values[{}]; double val_out;'''.format(filter_size) sub_call = '''raw_kernel::{}(values, {}, &val_out); y = cast<Y>(val_out);'''.format(rk.name, filter_size) return _filters_core._generate_nd_kernel( 'generic_{}_{}'.format(filter_size, rk.name), setup, 'values[iv++] = cast<double>({value});', sub_call, mode, wshape, int_type, offsets, cval, preamble='namespace raw_kernel {{\n{}\n}}'.format(rk.code), options=rk.options)
def _get_binary_erosion_kernel(w_shape, int_type, offsets, center_is_true, border_value, invert, masked, all_weights_nonzero): if invert: border_value = int(not border_value) true_val = 0 false_val = 1 else: true_val = 1 false_val = 0 if masked: pre = """ bool mv = (bool)mask[i]; bool _in = (bool)x[i]; if (!mv) {{ y = cast<Y>(_in); return; }} else if ({center_is_true} && _in == {false_val}) {{ y = cast<Y>(_in); return; }}""".format(center_is_true=int(center_is_true), false_val=false_val) else: pre = """ bool _in = (bool)x[i]; if ({center_is_true} && _in == {false_val}) {{ y = cast<Y>(_in); return; }}""".format(center_is_true=int(center_is_true), false_val=false_val) pre = pre + """ y = cast<Y>({true_val});""".format(true_val=true_val) # {{{{ required because format is called again within _generate_nd_kernel found = """ if ({{cond}}) {{{{ if (!{border_value}) {{{{ y = cast<Y>({false_val}); return; }}}} }}}} else {{{{ bool nn = {{value}} ? {true_val} : {false_val}; if (!nn) {{{{ y = cast<Y>({false_val}); return; }}}} }}}}""".format( true_val=int(true_val), false_val=int(false_val), border_value=int(border_value), ) name = 'binary_erosion' if false_val: name += '_invert' return _filters_core._generate_nd_kernel( name, pre, found, '', 'constant', w_shape, int_type, offsets, 0, ctype='Y', has_weights=True, has_structure=False, has_mask=masked, binary_morphology=True, all_weights_nonzero=all_weights_nonzero)