How to use the pytools.py_codegen.Indentation function in pytools

To help you get started, we’ve selected a few pytools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github inducer / pyopencl / pyopencl / invoker.py View on Github external
gen.extend(err_handler)

        gen("""
            return _cl.enqueue_nd_range_kernel(queue, self, global_size, local_size,
                    global_offset, wait_for, g_times_l=g_times_l)
            """)

    # }}}

    # {{{ generate set_args

    gen("")
    gen("def set_args(%s):"
            % (", ".join(["self"] + arg_names)))

    with Indentation(gen):
        add_local_imports(gen)
        gen.extend(err_handler)

    # }}}

    return gen.get_picklable_module(), enqueue_name
github inducer / loopy / loopy / target / pyopencl_execution.py View on Github external
args.append(arg.name)
            continue

        gen("# {{{ process %s" % arg.name)
        gen("")

        if not options.no_numpy:
            gen("if isinstance(%s, _lpy_np.ndarray):" % arg.name)
            with Indentation(gen):
                gen("# synchronous, nothing to worry about")
                gen("%s = _lpy_cl_array.to_device("
                        "queue, %s, allocator=allocator)"
                        % (arg.name, arg.name))
                gen("_lpy_encountered_numpy = True")
            gen("elif %s is not None:" % arg.name)
            with Indentation(gen):
                gen("_lpy_encountered_dev = True")

            gen("")

        if not options.skip_arg_checks and not is_written:
            gen("if %s is None:" % arg.name)
            with Indentation(gen):
                gen("raise RuntimeError(\"input argument '%s' must "
                        "be supplied\")" % arg.name)
                gen("")

        if (is_written
                and arg.arg_class is lp.ImageArg
                and not options.skip_arg_checks):
            gen("if %s is None:" % arg.name)
            with Indentation(gen):
github inducer / loopy / loopy / target / pyopencl_execution.py View on Github external
def generate_value_arg_check(gen, kernel, implemented_data_info):
    if kernel.options.skip_arg_checks:
        return

    from loopy.kernel.data import ValueArg

    gen("# {{{ check that value args are present")
    gen("")

    for arg in implemented_data_info:
        if not issubclass(arg.arg_class, ValueArg):
            continue

        gen("if %s is None:" % arg.name)
        with Indentation(gen):
            gen("raise TypeError(\"value argument '%s' "
                    "was not given and could not be automatically "
                    "determined\")" % arg.name)

    gen("# }}}")
    gen("")
github inducer / pyopencl / pyopencl / invoker.py View on Github external
def generate_buffer_arg_setter(gen, arg_idx, buf_var):
    from pytools.py_codegen import Indentation

    if _CPY2 or _PYPY:
        # https://github.com/numpy/numpy/issues/5381
        gen("if isinstance({buf_var}, np.generic):".format(buf_var=buf_var))
        with Indentation(gen):
            if _PYPY:
                gen("{buf_var} = np.asarray({buf_var})".format(buf_var=buf_var))
            else:
                gen("{buf_var} = np.getbuffer({buf_var})".format(buf_var=buf_var))

    gen("""
        self._set_arg_buf({arg_idx}, {buf_var})
        """
        .format(arg_idx=arg_idx, buf_var=buf_var))
github inducer / loopy / loopy / target / pyopencl_execution.py View on Github external
def generate_integer_arg_finding_from_strides(gen, kernel, implemented_data_info):
    options = kernel.options

    gen("# {{{ find integer arguments from strides")
    gen("")

    for arg in implemented_data_info:
        if arg.stride_for_name_and_axis is not None:
            impl_array_name, stride_impl_axis = arg.stride_for_name_and_axis

            gen("if %s is None:" % arg.name)
            with Indentation(gen):
                if not options.skip_arg_checks:
                    gen("if %s is None:" % impl_array_name)
                    with Indentation(gen):
                        gen("raise RuntimeError(\"required stride '%s' for "
                                "argument '%s' not given or deducible from "
                                "passed array\")"
                                % (arg.name, impl_array_name))

                    base_arg = kernel.impl_arg_to_arg[impl_array_name]

                    if not options.skip_arg_checks:
                        gen("%s, _lpy_remdr = divmod(%s.strides[%d], %d)"
                                % (arg.name, impl_array_name, stride_impl_axis,
                                    base_arg.dtype.dtype.itemsize))

                        gen("assert _lpy_remdr == 0, \"Stride %d of array '%s' is "
github inducer / loopy / loopy / target / execution.py View on Github external
def generate_integer_arg_finding_from_strides(
            self, gen, kernel, implemented_data_info):
        options = kernel.options

        gen("# {{{ find integer arguments from strides")
        gen("")

        for arg in implemented_data_info:
            if arg.stride_for_name_and_axis is not None:
                impl_array_name, stride_impl_axis = arg.stride_for_name_and_axis

                gen("if %s is None:" % arg.name)
                with Indentation(gen):
                    if not options.skip_arg_checks:
                        gen("if %s is None:" % impl_array_name)
                        with Indentation(gen):
                            gen("raise RuntimeError(\"required stride '%s' for "
                                    "argument '%s' not given or deducible from "
                                    "passed array\")"
                                    % (arg.name, impl_array_name))

                        base_arg = kernel.impl_arg_to_arg[impl_array_name]

                        if not options.skip_arg_checks:
                            gen("%s, _lpy_remdr = divmod(%s.strides[%d], %d)"
                                    % (arg.name, impl_array_name, stride_impl_axis,
                                        base_arg.dtype.dtype.itemsize))

                            gen("assert _lpy_remdr == 0, \"Stride %d of array '%s' "
github inducer / loopy / loopy / target / pyopencl_execution.py View on Github external
def generate_integer_arg_finding_from_offsets(gen, kernel, implemented_data_info):
    options = kernel.options

    gen("# {{{ find integer arguments from offsets")
    gen("")

    for arg in implemented_data_info:
        impl_array_name = arg.offset_for_name
        if impl_array_name is not None:
            gen("if %s is None:" % arg.name)
            with Indentation(gen):
                gen("if %s is None:" % impl_array_name)
                with Indentation(gen):
                    gen("# Output variable, we'll be allocating "
                            "it, with zero offset.")
                    gen("%s = 0" % arg.name)
                gen("else:")
                with Indentation(gen):
                    if not options.no_numpy:
                        gen("_lpy_offset = getattr(%s, \"offset\", 0)"
                                % impl_array_name)
                    else:
                        gen("_lpy_offset = %s.offset" % impl_array_name)

                    base_arg = kernel.impl_arg_to_arg[impl_array_name]

                    if not options.skip_arg_checks:
                        gen("%s, _lpy_remdr = divmod(_lpy_offset, %d)"
                                % (arg.name, base_arg.dtype.itemsize))
github inducer / loopy / loopy / target / execution.py View on Github external
def generate_value_arg_check(
            self, gen, kernel, implemented_data_info):
        if kernel.options.skip_arg_checks:
            return

        from loopy.kernel.data import ValueArg

        gen("# {{{ check that value args are present")
        gen("")

        for arg in implemented_data_info:
            if not issubclass(arg.arg_class, ValueArg):
                continue

            gen("if %s is None:" % arg.name)
            with Indentation(gen):
                gen("raise TypeError(\"value argument '%s' "
                        "was not given and could not be automatically "
                        "determined\")" % arg.name)

        gen("# }}}")
        gen("")
github inducer / loopy / loopy / target / pyopencl_execution.py View on Github external
def handle_non_numpy_arg(self, gen, arg):
        gen("if isinstance(%s, _lpy_np.ndarray):" % arg.name)
        with Indentation(gen):
            gen("# synchronous, nothing to worry about")
            gen("%s = _lpy_cl_array.to_device("
                    "queue, %s, allocator=allocator)"
                    % (arg.name, arg.name))
            gen("_lpy_encountered_numpy = True")
        gen("elif %s is not None:" % arg.name)
        with Indentation(gen):
            gen("_lpy_encountered_dev = True")

        gen("")