Beispiel #1
0
    def __new__( cls, t_domain, t_codomain ):
        assert(isinstance(t_domain, BasicTypeVariable))
        assert(isinstance(t_codomain, (TypeVariable, TypeTuple, TypeList)))

        obj = Basic.__new__(cls, t_domain, t_codomain)
        obj._tag = random_string( 4 )

        return obj
Beispiel #2
0
    def __new__(cls, expr, args):

        # ...
        if not isinstance(args, (tuple, list, Tuple)):
            args = [args]

        args = Tuple(*args)
        # ...

        return Basic.__new__(cls, expr, args)
Beispiel #3
0
    def __new__( cls, func, target ):

        if not isinstance(target, (list, tuple, Tuple)):
            target = [target]

        target = Tuple(*target)

        obj = Basic.__new__(cls, func, target)
        obj._tag = random_string( 4 )

        return obj
Beispiel #4
0
    def __new__( cls, func, target ):

        assert(isinstance( func, FunctionSymbol ))
        assert(isinstance( target, Dict ))

        obj = Basic.__new__( cls, func, target )

        tag = random_string( 4 )
        obj._name = 'partial_{func}_{tag}'.format( func=func.name, tag=tag )
        obj._funcdef = None

        return obj
Beispiel #5
0
    def __new__(cls, *args):
        # ... create iterator and index variables
        iterable = args
        if len(args) == 1:
            iterable = args[0]

        tag = random_string(4)

        index = new_variable('int', iterable, tag=tag)
        iterator = new_variable('real', iterable, tag=tag)
        length = new_variable('int', iterable, tag=tag, kind='len')
        # ...

        return Basic.__new__(cls, iterable, index, iterator, length)
Beispiel #6
0
    def __new__( cls, var, rank=0 ):
        assert(isinstance(var, (Variable, TypeVariable)))

        dtype          = var.dtype
        rank           = var.rank + rank
        is_stack_array = var.is_stack_array
        order          = var.order
        precision      = var.precision
        shape          = var.shape

        obj = Basic.__new__( cls, dtype, rank, is_stack_array, order, precision, shape )
        obj._tag = random_string( 4 )

        return obj
Beispiel #7
0
    def __new__(cls, *args):
        # ... create iterator and index variables
        target = args
        if len(args) == 1:
            print(args)
            raise NotImplementedError('')

        tag = random_string(4)

        length = new_variable('int', target[0], tag=tag, kind='len')
        index = new_variable('int', target[0], tag=tag)
        iterator = new_variable('real', target, tag=tag)
        # ...

        return Basic.__new__(cls, args, index, iterator, length)
Beispiel #8
0
    def __new__(cls, *args):
        # ... create iterator and index variables
        target = args
        assert (len(args) > 1)

        tag = random_string(4)

        length = new_variable('int', target, tag=tag, kind='len')
        index = new_variable('int', target, tag=tag)
        iterator = new_variable('real', target, tag=tag)
        multi_index = new_variable('int', target[0], tag=tag, kind='multi')
        # ...

        obj = Basic.__new__(cls, args, index, iterator, length, multi_index)
        obj._is_list = False

        return obj
Beispiel #9
0
    def __new__( cls, var, rank=0 ):
        assert(isinstance(var, (tuple, list, Tuple)))

        for i in var:
            assert( isinstance(i, (Variable, TypeVariable)) )

        t_vars = []
        for i in var:
            t_var = TypeVariable( i, rank=rank )
            t_vars.append(t_var)

        t_vars = Tuple(*t_vars)

        obj = Basic.__new__(cls, t_vars)
        obj._tag = random_string( 4 )

        return obj
Beispiel #10
0
    def __new__(cls, block, **kwargs):

        # ...
        settings = kwargs.copy()

        accelerator = settings.pop('accelerator', None)
        # ...

        # ...
        assert (isinstance(block, GeneratorBlock))
        # ...

        # ...
        decs = block.decs
        body = block.body
        # ...

        # ... add parallel loop
        if accelerator:
            if not (accelerator == 'omp'):
                raise NotImplementedError('Only OpenMP is available')

            # ... create clauses
            clauses = []

            #            ##### DEBUG
            #            clauses += [OMP_NumThread(4)]
            # ...

            # ... create variables
            variables = []
            # ...

            # ...
            body = [OMP_Parallel(clauses, variables, body)]
            # ...

            # TODO this is a hack to handle the last comment after a loop, so that
            #      it can be parsed and added to the For
            body += [Pass()]
        # ...

        decs = Tuple(*decs)
        body = Tuple(*body)

        return Basic.__new__(cls, decs, body)
Beispiel #11
0
    def __new__( cls, var ):
        assert(isinstance(var, (TypeVariable, TypeTuple, TypeList)))

        obj = Basic.__new__(cls, var)
        obj._tag = random_string( 4 )

        # ...
        def _get_core_type(expr):
            if isinstance(expr, TypeList):
                return _get_core_type(expr.parent)

            else:
                return expr
        # ...

        obj._types = _get_core_type(var)

        return obj
Beispiel #12
0
    def __new__(cls, generator):

        # ... define the name for the shape and the statements to be able to
        #     compute it inside the python interface

        if isinstance(generator, VariableGenerator):
            var = generator.length
            stmts = [Assign(var, Len(generator.arguments))]

        elif isinstance(generator, ZipGenerator):
            var = generator.length
            stmts = [Assign(var, Len(generator.arguments[0]))]

        elif isinstance(generator, ProductGenerator):
            arguments = generator.arguments
            length = generator.length

            stmts = [Assign(l, Len(a)) for l, a in zip(length, arguments)]

            if generator.is_list:
                n = 1
                for i in length:
                    n *= i

            else:
                n = length

            var = Dummy()
            stmts += [Assign(var, n)]

        else:
            msg = 'not available for {}'.format(type(generator))
            raise NotImplementedError(msg)

        stmts = Tuple(*stmts)

        return Basic.__new__(cls, var, stmts)
Beispiel #13
0
    def __new__( cls, target ):

        obj = Basic.__new__(cls, target)
        obj._name = 'reduce_{}'.format( random_string( 4 ) )
        return obj
Beispiel #14
0
 def __new__( cls, *target ):
     target = Tuple(*target)
     obj = Basic.__new__(cls, target)
     obj._name = 'product_{}'.format( random_string( 4 ) )
     return obj
Beispiel #15
0
    def __new__( cls, func ):

        assert(isinstance( func, Lambda ))

        return Basic.__new__( cls, func )
Beispiel #16
0
    def __new__(cls, op, arguments):
        #        assert( isinstance( generator, BasicGenerator ) )
        assert (isinstance(op, str))
        assert (op in ['+', '-'])

        return Basic.__new__(cls, op, arguments)
Beispiel #17
0
    def __new__(cls, generator, stmts, **kwargs):

        # ...
        settings = kwargs.copy()

        accelerator = settings.pop('accelerator')
        nowait = settings.pop('nowait')
        schedule = settings.pop('schedule')
        chunk = settings.pop('chunk')
        # ...

        # ...
        assert (isinstance(generator, BasicGenerator))

        decs, body = _build_block(generator, stmts)
        # ...

        # ...
        assert (isinstance(decs, (tuple, list, Tuple)))
        assert (isinstance(body, (tuple, list, Tuple)))

        decs = Tuple(*decs)
        body = Tuple(*body)
        # ...

        # ... define private variables of the current block
        private_vars = generator.private
        # TODO add private vars from internal blocks
        private_vars = Tuple(*private_vars)
        # ...

        # ... add parallel loop
        if accelerator:
            if not (accelerator == 'omp'):
                raise NotImplementedError('Only OpenMP is available')

            # ... create clauses
            clauses = []

            # TODO move this treatment to OMP_Schedule
            if chunk is None:
                clauses += [OMP_Schedule(schedule)]

            else:
                clauses += [OMP_Schedule(schedule, chunk)]

            if private_vars:
                clauses += [OMP_Private(*private_vars)]
            # ...

            # ...
            assert (len(body) == 1)
            loop = body[0]
            body = [OMP_For(loop, Tuple(*clauses), nowait)]
            # ...
        # ...

        decs = Tuple(*decs)
        body = Tuple(*body)

        return Basic.__new__(cls, generator, decs, body, private_vars)
Beispiel #18
0
    def __new__(cls, block, reduction, lhs, **kwargs):

        # ...
        settings = kwargs.copy()

        accelerator = settings.pop('accelerator')
        # ...

        assert (isinstance(reduction, Reduction))

        # ...
        if isinstance(lhs, (list, tuple, Tuple)):
            raise NotImplementedError()
        # ...

        # ...
        generator = block.generator
        decs = block.decs
        body = block.body
        private_vars = generator.private
        # ...

        # ...
        lhs_name = lhs.name

        assign_stmts = list(block.body.atoms(Assign))
        # ...

        # ...
        ls = [i for i in assign_stmts if _get_name(i.lhs) == lhs_name]
        if len(ls) > 1:
            raise NotImplementedError()

        assign_iterable = ls[0]
        rhs = assign_iterable.rhs
        new_stmt = AugAssign(lhs, reduction.op, rhs)

        body = block.body.subs(assign_iterable, new_stmt)

        decs = block.decs
        # ...

        # ... add initial values for reduced variables
        _reduction_init = lambda i: _get_default_value(i, op=reduction.op)
        reduction_stmts = [
            Assign(r, _reduction_init(r)) for r in reduction.arguments
        ]

        # update declarations
        decs = list(decs) + reduction_stmts
        decs = Tuple(*decs)
        # ...

        # ... define private variables of the current block
        # TODO add private vars from internal blocks
        private_vars = Tuple(*private_vars)
        # ...

        # ... add parallel loop
        if accelerator:
            if not (accelerator == 'omp'):
                raise NotImplementedError('Only OpenMP is available')

            # ...
            omp_fors = list(body.atoms(OMP_For))
            omp_for = [i for i in body if isinstance(i, OMP_For)]
            assert (len(omp_for) == 1)
            omp_for = omp_for[0]

            loop = omp_for.loop
            clauses = list(omp_for.clauses)
            nowait = omp_for.nowait

            # ...
            reduction_args = [reduction.op] + list(reduction.arguments)
            clauses += [OMP_Reduction(*reduction_args)]
            clauses = Tuple(*clauses)
            # ...

            new = OMP_For(loop, clauses, nowait)
            body = body.subs(omp_for, new)
        # ...

        decs = Tuple(*decs)
        body = Tuple(*body)

        return Basic.__new__(cls, generator, decs, body, private_vars)