loopy/preprocess.py
def infer_unknown_types(kernel, expect_completion=False):
    """Infer types on temporaries and arguments."""

    logger.debug("%s: infer types" % kernel.name)

    def debug(s):
        logger.debug("%s: %s" % (kernel.name, s))

    unexpanded_kernel = kernel
    if kernel.substitutions:
        from loopy.transform.subst import expand_subst
        kernel = expand_subst(kernel)

    new_temp_vars = kernel.temporary_variables.copy()
    new_arg_dict = kernel.arg_dict.copy()

    # {{{ fill queue

    # queue contains temporary variables
    queue = []

    import loopy as lp
    for tv in six.itervalues(kernel.temporary_variables):
        if tv.dtype is lp.auto:
            queue.append(tv)

    for arg in kernel.args:
        if arg.dtype is None:
            queue.append(arg)

    # }}}

    from loopy.expression import TypeInferenceMapper
    type_inf_mapper = TypeInferenceMapper(kernel,
            _DictUnionView([
                new_temp_vars,
                new_arg_dict
                ]))

    from loopy.symbolic import SubstitutionRuleExpander
    subst_expander = SubstitutionRuleExpander(kernel.substitutions)

    # {{{ work on type inference queue

    from loopy.kernel.data import TemporaryVariable, KernelArgument

    failed_names = set()
    while queue:
        item = queue.pop(0)

        debug("inferring type for %s %s" % (type(item).__name__, item.name))

        result, symbols_with_unavailable_types = \
                _infer_var_type(kernel, item.name, type_inf_mapper, subst_expander)

        failed = result is None
        if not failed:
            debug("     success: %s" % result)
            if isinstance(item, TemporaryVariable):
                new_temp_vars[item.name] = item.copy(dtype=result)
            elif isinstance(item, KernelArgument):
                new_arg_dict[item.name] = item.copy(dtype=result)
            else:
                raise LoopyError("unexpected item type in type inference")
        else:
            debug("     failure")

        if failed:
            if item.name in failed_names:
                # this item has failed before, give up.
                advice = ""
                if symbols_with_unavailable_types:
                    advice += (
                            " (need type of '%s'--check for missing arguments)"
                            % ", ".join(symbols_with_unavailable_types))

                if expect_completion:
                    raise LoopyError(
                            "could not determine type of '%s'%s"
                            % (item.name, advice))

                else:
                    # We're done here.
                    break

            # remember that this item failed
            failed_names.add(item.name)

            queue_names = set(qi.name for qi in queue)

            if queue_names == failed_names:
                # We did what we could...
                print(queue_names, failed_names, item.name)
                assert not expect_completion
                break

            # can't infer type yet, put back into queue
            queue.append(item)
        else:
            # we've made progress, reset failure markers
            failed_names = set()

    # }}}

    return unexpanded_kernel.copy(
            temporary_variables=new_temp_vars,
            args=[new_arg_dict[arg.name] for arg in kernel.args],
            )
Infer types on temporaries and arguments.
Infer types on temporaries and arguments.
Infer types on temporaries and arguments .
def infer_unknown_types(kernel, expect_completion=False):
    logger.debug("%s: infer types" % kernel.name)
    def debug(s):
        logger.debug("%s: %s" % (kernel.name, s))
    unexpanded_kernel = kernel
    if kernel.substitutions:
        from loopy.transform.subst import expand_subst
        kernel = expand_subst(kernel)
    new_temp_vars = kernel.temporary_variables.copy()
    new_arg_dict = kernel.arg_dict.copy()
    queue = []
    import loopy as lp
    for tv in six.itervalues(kernel.temporary_variables):
        if tv.dtype is lp.auto:
            queue.append(tv)
    for arg in kernel.args:
        if arg.dtype is None:
            queue.append(arg)
    from loopy.expression import TypeInferenceMapper
    type_inf_mapper = TypeInferenceMapper(kernel,
            _DictUnionView([
                new_temp_vars,
                new_arg_dict
                ]))
    from loopy.symbolic import SubstitutionRuleExpander
    subst_expander = SubstitutionRuleExpander(kernel.substitutions)
    from loopy.kernel.data import TemporaryVariable, KernelArgument
    failed_names = set()
    while queue:
        item = queue.pop(0)
        debug("inferring type for %s %s" % (type(item).__name__, item.name))
        result, symbols_with_unavailable_types = \
                _infer_var_type(kernel, item.name, type_inf_mapper, subst_expander)
        failed = result is None
        if not failed:
            debug("     success: %s" % result)
            if isinstance(item, TemporaryVariable):
                new_temp_vars[item.name] = item.copy(dtype=result)
            elif isinstance(item, KernelArgument):
                new_arg_dict[item.name] = item.copy(dtype=result)
            else:
                raise LoopyError("unexpected item type in type inference")
        else:
            debug("     failure")
        if failed:
            if item.name in failed_names:
                advice = ""
                if symbols_with_unavailable_types:
                    advice += (
                            " (need type of '%s'--check for missing arguments)"
                            % ", ".join(symbols_with_unavailable_types))
                if expect_completion:
                    raise LoopyError(
                            "could not determine type of '%s'%s"
                            % (item.name, advice))
                else:
                    break
            failed_names.add(item.name)
            queue_names = set(qi.name for qi in queue)
            if queue_names == failed_names:
                print(queue_names, failed_names, item.name)
                assert not expect_completion
                break
            queue.append(item)
        else:
            failed_names = set()
    return unexpanded_kernel.copy(
            temporary_variables=new_temp_vars,
            args=[new_arg_dict[arg.name] for arg in kernel.args],
            )
def infer_unknown_types ( kernel , expect_completion = False ) : logger . debug ( "%s: infer types" % kernel . name ) def debug ( s ) : logger . debug ( "%s: %s" % ( kernel . name , s ) ) unexpanded_kernel = kernel if kernel . substitutions : from loopy . transform . subst import expand_subst kernel = expand_subst ( kernel ) new_temp_vars = kernel . temporary_variables . copy ( ) new_arg_dict = kernel . arg_dict . copy ( ) queue = [ ] import loopy as lp for tv in six . itervalues ( kernel . temporary_variables ) : if tv . dtype is lp . auto : queue . append ( tv ) for arg in kernel . args : if arg . dtype is None : queue . append ( arg ) from loopy . expression import TypeInferenceMapper type_inf_mapper = TypeInferenceMapper ( kernel , _DictUnionView ( [ new_temp_vars , new_arg_dict ] ) ) from loopy . symbolic import SubstitutionRuleExpander subst_expander = SubstitutionRuleExpander ( kernel . substitutions ) from loopy . kernel . data import TemporaryVariable , KernelArgument failed_names = set ( ) while queue : item = queue . pop ( 0 ) debug ( "inferring type for %s %s" % ( type ( item ) . __name__ , item . name ) ) result , symbols_with_unavailable_types = _infer_var_type ( kernel , item . name , type_inf_mapper , subst_expander ) failed = result is None if not failed : debug ( "     success: %s" % result ) if isinstance ( item , TemporaryVariable ) : new_temp_vars [ item . name ] = item . copy ( dtype = result ) elif isinstance ( item , KernelArgument ) : new_arg_dict [ item . name ] = item . copy ( dtype = result ) else : raise LoopyError ( "unexpected item type in type inference" ) else : debug ( "     failure" ) if failed : if item . name in failed_names : advice = "" if symbols_with_unavailable_types : advice += ( " (need type of '%s'--check for missing arguments)" % ", " . join ( symbols_with_unavailable_types ) ) if expect_completion : raise LoopyError ( "could not determine type of '%s'%s" % ( item . name , advice ) ) else : break failed_names . add ( item . name ) queue_names = set ( qi . name for qi in queue ) if queue_names == failed_names : print ( queue_names , failed_names , item . name ) assert not expect_completion break queue . append ( item ) else : failed_names = set ( ) return unexpanded_kernel . copy ( temporary_variables = new_temp_vars , args = [ new_arg_dict [ arg . name ] for arg in kernel . args ] , )
Infer types on temporaries and arguments.
Infer types on temporaries and arguments .
"""Infer types on temporaries and arguments.""" # {{{ fill queue # queue contains temporary variables # }}} # {{{ work on type inference queue # this item has failed before, give up. # We're done here. # remember that this item failed # We did what we could... # can't infer type yet, put back into queue # we've made progress, reset failure markers # }}}
kernel
expect_completion