Ejemplo n.º 1
0
def loop_interchange(outer_stmts, inner_stmts):

    for outer_stmt in outer_stmts:

        if not isinstance(outer_stmt, Do):
            Logger.warn('Outer statment is not Do type: %s'%outer_stmt.__class__)
            continue

        for inner_stmt in inner_stmts:
            if not isinstance(inner_stmt, Do):
                Logger.warn('Inner statment is not Do type: %s'%inner_stmt.__class__)
                continue

            lines = []
            for stmt, depth in walk(outer_stmt, -1):
                if stmt is outer_stmt:
                    lines.append(inner_stmt.tooc())
                elif stmt is inner_stmt:
                    lines.append(outer_stmt.tooc())
                elif stmt is inner_stmt.content[-1]:
                    lines.append(outer_stmt.content[-1].tooc())
                elif stmt is outer_stmt.content[-1]:
                    lines.append(inner_stmt.content[-1].tooc())
                else:
                    lines.append(stmt.tooc())

            if lines:
                parsed = parse('\n'.join(lines), analyze=False)
                if len(parsed.content)>0:
                    parsed.content[0].parent = outer_stmt.parent
                    for stmt, depth in walk(parsed, -1): stmt.parse_f2003() 
                    insert_content(outer_stmt, parsed.content)
Ejemplo n.º 2
0
def loop_interchange(outer_stmts, inner_stmts):

    for outer_stmt in outer_stmts:

        if not isinstance(outer_stmt, Do):
            Logger.warn('Outer statment is not Do type: %s' %
                        outer_stmt.__class__)
            continue

        for inner_stmt in inner_stmts:
            if not isinstance(inner_stmt, Do):
                Logger.warn('Inner statment is not Do type: %s' %
                            inner_stmt.__class__)
                continue

            lines = []
            for stmt, depth in walk(outer_stmt, -1):
                if stmt is outer_stmt:
                    lines.append(inner_stmt.tooc())
                elif stmt is inner_stmt:
                    lines.append(outer_stmt.tooc())
                elif stmt is inner_stmt.content[-1]:
                    lines.append(outer_stmt.content[-1].tooc())
                elif stmt is outer_stmt.content[-1]:
                    lines.append(inner_stmt.content[-1].tooc())
                else:
                    lines.append(stmt.tooc())

            if lines:
                parsed = parse('\n'.join(lines), analyze=False)
                if len(parsed.content) > 0:
                    parsed.content[0].parent = outer_stmt.parent
                    for stmt, depth in walk(parsed, -1):
                        stmt.parse_f2003()
                    insert_content(outer_stmt, parsed.content)
Ejemplo n.º 3
0
def loop_split(stmts, add_stmt, before=True):

    for stmt in stmts:
        parent = stmt.parent

        if not isinstance(parent, Do):
            Logger.warn('Parent of statment is not Do type: %s' %
                        parent.__class__)
            continue

        doblk1 = []
        doblk2 = []

        #if add_stmt: doblk1.append(add_stmt[0])
        doblk1.append(parent.tooc())

        if add_stmt: doblk2.append(add_stmt[0])
        doblk2.append(parent.tooc(remove_label=True))

        enddo_stmt = parent.content[-1]

        doblk = doblk1
        remove_label = False
        for childstmt, depth in walk(parent, -1):
            if childstmt not in [parent, enddo_stmt]:
                if not before:
                    doblk.append(childstmt.tooc(remove_label=remove_label))
                if childstmt == stmt:
                    doblk = doblk2
                    remove_label = True
                if before:
                    doblk.append(childstmt.tooc(remove_label=remove_label))

        doblk1.append(enddo_stmt.tooc())
        doblk2.append(enddo_stmt.tooc(remove_label=True))

        if doblk1:
            parsed = parse('\n'.join(doblk1),
                           analyze=False,
                           ignore_comments=False)
            if len(parsed.content) > 0:
                parsed.content[0].parent = parent.parent
                for stmt, depth in walk(parsed, -1):
                    stmt.parse_f2003()
                insert_content(parent, parsed.content, remove_olditem=False)

        if doblk2:
            parsed = parse('\n'.join(doblk2),
                           analyze=False,
                           ignore_comments=False)
            if len(parsed.content) > 0:
                parsed.content[0].parent = parent.parent
                for stmt, depth in walk(parsed, -1):
                    stmt.parse_f2003()
                insert_content(parent, parsed.content, remove_olditem=True)
Ejemplo n.º 4
0
def loop_split(stmts, add_stmt, before=True):

    for stmt in stmts:
        parent = stmt.parent

        if not isinstance(parent, Do):
            Logger.warn('Parent of statment is not Do type: %s'%parent.__class__)
            continue

        doblk1 = []
        doblk2 = []

        #if add_stmt: doblk1.append(add_stmt[0])
        doblk1.append(parent.tooc())

        if add_stmt: doblk2.append(add_stmt[0])
        doblk2.append(parent.tooc(remove_label=True))

        enddo_stmt = parent.content[-1]

        doblk = doblk1
        remove_label = False
        for childstmt, depth in walk(parent, -1):
            if childstmt not in [ parent, enddo_stmt]:
                if not before:
                    doblk.append(childstmt.tooc(remove_label=remove_label))
                if childstmt==stmt:
                    doblk = doblk2
                    remove_label = True
                if before:
                    doblk.append(childstmt.tooc(remove_label=remove_label))
            
        doblk1.append(enddo_stmt.tooc())
        doblk2.append(enddo_stmt.tooc(remove_label=True))

        if doblk1:
            parsed = parse('\n'.join(doblk1), analyze=False, ignore_comments=False)
            if len(parsed.content)>0:
                parsed.content[0].parent = parent.parent
                for stmt, depth in walk(parsed, -1): stmt.parse_f2003()
                insert_content(parent, parsed.content, remove_olditem=False)

        if doblk2:
            parsed = parse('\n'.join(doblk2), analyze=False, ignore_comments=False)
            if len(parsed.content)>0:
                parsed.content[0].parent = parent.parent
                for stmt, depth in walk(parsed, -1): stmt.parse_f2003()
                insert_content(parent, parsed.content, remove_olditem=True)
Ejemplo n.º 5
0
def loop_merge(from_stmts, to_stmts):

    for from_stmt in from_stmts:

        if not isinstance(from_stmt, Do):
            Logger.warn('From statment is not Do type: %s'%from_stmt.__class__)
            continue

        from_f2003 = from_stmt.f2003
        if isinstance(from_f2003, Nonlabel_Do_Stmt):
            from_loop_control = from_f2003.items[1]
            from_loop_var = from_loop_control.items[0].string.lower()
            from_start_idx = from_loop_control.items[1][0]
            from_end_idx = from_loop_control.items[1][1]
            if len(from_loop_control.items[1])==3:
                from_step = Int_Literal_Constant(str(1))
            else:
                from_step = loop_control.items[1][2]
        else: raise ProgramException('Not supported type: %s'%from_f2003.__class__)

        # collect loop controls through static analysis
        from_start_num = from_stmt.get_param(from_start_idx)
        from_end_num = from_stmt.get_param(from_end_idx)
        from_step_num = from_stmt.get_param(from_step)
        try: from_loop_indices = range(from_start_num, from_end_num+1, from_step_num)
        except: from_loop_indices = None

        for to_stmt in to_stmts:
            if not isinstance(to_stmt, Do):
                Logger.warn('To statment is not Do type: %s'%to_stmt.__class__)
                continue

            to_f2003 = to_stmt.f2003
            if isinstance(to_f2003, Nonlabel_Do_Stmt):
                to_loop_control = to_f2003.items[1]
                to_loop_var = to_loop_control.items[0].string.lower()
                to_start_idx = to_loop_control.items[1][0]
                to_end_idx = to_loop_control.items[1][1]
                if len(to_loop_control.items[1])==3:
                    to_step = Int_Literal_Constant(str(1))
                else:
                    to_step = to_loop_control.items[1][2]
            else: raise ProgramException('Not supported type: %s'%to_f2003.__class__)

            # collect loop controls through static analysis
            to_start_num = to_stmt.get_param(to_start_idx)
            to_end_num = to_stmt.get_param(to_end_idx)
            to_step_num = to_stmt.get_param(to_step)
            try: to_loop_indices = range(to_start_num, to_end_num+1, to_step_num)
            except: to_loop_indices = None


            if ( from_loop_indices and to_loop_indices and from_loop_indices==to_loop_indices ) or \
                ( from_loop_indices is None and to_loop_indices is None and from_loop_var==to_loop_var and \
                from_start_idx==to_start_idx and from_end_idx==to_end_idx and from_step==to_step ):

                insert_content(to_stmt.content[-1], from_stmt.content[:-1], remove_olditem=False)
                remove_content(from_stmt)
            else:
                Logger.warn('Can not merge due to different loop control')
Ejemplo n.º 6
0
def loop_unroll(targets, factor, method):
    for target_stmt in targets:
        if not isinstance(target_stmt, Do):
            Logger.warn('Target statment is not Do type: %s' %
                        target_stmt.__class__)
            continue

        # collect loop control
        target_f2003 = target_stmt.f2003
        if isinstance(target_f2003, Nonlabel_Do_Stmt):
            loop_control = target_f2003.items[1]
            loop_var = loop_control.items[0].string.lower()
            start_idx = loop_control.items[1][0]
            end_idx = loop_control.items[1][1]
            if len(loop_control.items[1]) == 3:
                step = Int_Literal_Constant(str(1))
            else:
                step = loop_control.items[1][2]
        else:
            raise ProgramException('Not supported type: %s' %
                                   f2003obj.__class__)

        # collect loop controls through static analysis
        start_num = target_stmt.get_param(start_idx)
        end_num = target_stmt.get_param(end_idx)
        step_num = target_stmt.get_param(step)
        try:
            loop_indices = range(start_num, end_num + 1, step_num)
        except:
            loop_indices = None

        # TODO: modify analysis if required
        lines = []
        if factor == 'full':
            if loop_indices is not None:
                lines = _unroll(target_stmt.content,
                                loop_var,
                                len(loop_indices),
                                method,
                                start_index=start_num)
            else:
                Logger.warn('Loopcontrol is not collected')

            # save in tree
        elif factor.isdigit():
            factor_num = int(factor)
            if loop_indices is not None and len(loop_indices) == factor_num:
                lines = _unroll(target_stmt.content,
                                loop_var,
                                factor_num,
                                method,
                                start_index=start_num)
            else:
                # replace end and step
                newstep = '%s*%s' % (step.tofortran(), factor)
                newend = '%s-%s' % (end_idx.tofortran(), newstep)
                lines.append(target_stmt.tooc(do_end=newend, do_step=newstep))
                lines.extend(
                    _unroll(target_stmt.content, loop_var, factor_num, method))
                lines.append(target_stmt.content[-1].tooc())

                # replace start
                newstart = loop_var
                lines.append(
                    target_stmt.tooc(do_start=newstart, remove_label=True))
                lines.extend(_unroll(target_stmt.content, loop_var, 1, method))
                lines.append(target_stmt.content[-1].tooc(remove_label=True))
        else:
            raise UserException('Unknown unroll factor: %s' % factor)

        if lines:
            parsed = parse('\n'.join(lines), analyze=False)
            if len(parsed.content) > 0:
                for stmt, depth in walk(parsed, -1):
                    stmt.parse_f2003()
                insert_content(target_stmt, parsed.content)
Ejemplo n.º 7
0
def loop_unroll(targets, factor, method):
    for target_stmt in targets:
        if not isinstance(target_stmt, Do):
            Logger.warn("Target statment is not Do type: %s" % target_stmt.__class__)
            continue

        # collect loop control
        target_f2003 = target_stmt.f2003
        if isinstance(target_f2003, Nonlabel_Do_Stmt):
            loop_control = target_f2003.items[1]
            loop_var = loop_control.items[0].string.lower()
            start_idx = loop_control.items[1][0]
            end_idx = loop_control.items[1][1]
            if len(loop_control.items[1]) == 3:
                step = Int_Literal_Constant(str(1))
            else:
                step = loop_control.items[1][2]
        else:
            raise ProgramException("Not supported type: %s" % f2003obj.__class__)

        # collect loop controls through static analysis
        start_num = target_stmt.get_param(start_idx)
        end_num = target_stmt.get_param(end_idx)
        step_num = target_stmt.get_param(step)
        try:
            loop_indices = range(start_num, end_num + 1, step_num)
        except:
            loop_indices = None

        # TODO: modify analysis if required
        lines = []
        if factor == "full":
            if loop_indices is not None:
                lines = _unroll(target_stmt.content, loop_var, len(loop_indices), method, start_index=start_num)
            else:
                Logger.warn("Loopcontrol is not collected")

            # save in tree
        elif factor.isdigit():
            factor_num = int(factor)
            if loop_indices is not None and len(loop_indices) == factor_num:
                lines = _unroll(target_stmt.content, loop_var, factor_num, method, start_index=start_num)
            else:
                # replace end and step
                newstep = "%s*%s" % (step.tofortran(), factor)
                newend = "%s-%s" % (end_idx.tofortran(), newstep)
                lines.append(target_stmt.tooc(do_end=newend, do_step=newstep))
                lines.extend(_unroll(target_stmt.content, loop_var, factor_num, method))
                lines.append(target_stmt.content[-1].tooc())

                # replace start
                newstart = loop_var
                lines.append(target_stmt.tooc(do_start=newstart, remove_label=True))
                lines.extend(_unroll(target_stmt.content, loop_var, 1, method))
                lines.append(target_stmt.content[-1].tooc(remove_label=True))
        else:
            raise UserException("Unknown unroll factor: %s" % factor)

        if lines:
            parsed = parse("\n".join(lines), analyze=False)
            if len(parsed.content) > 0:
                for stmt, depth in walk(parsed, -1):
                    stmt.parse_f2003()
                insert_content(target_stmt, parsed.content)