Beispiel #1
0
    def transform(self):

        # generate kgen_driver.f90 in kernel directory
        self.driver = self.create_tree()
        program = self.create_program(self.driver)
        program.name = self.kernel_name
        self.append_program_in_tree(self.driver, program)

        # init plugin framework
        init_plugins([KERNEL_ID_0])

        # construct a generation tree
        for filepath, (srcobj, mods_used,
                       units_used) in State.srcfiles.iteritems():
            if hasattr(srcobj.tree, 'geninfo') and KGGenType.has_state(
                    srcobj.tree.geninfo):
                kfile = genkobj(None, srcobj.tree, KERNEL_ID_0)
                sfile = gensobj(None, srcobj.tree, KERNEL_ID_0)
                if kfile is None or sfile is None:
                    raise ProgramException(
                        'Kernel source file is not generated for %s.' %
                        filepath)
                self.genfiles.append((kfile, sfile, filepath))
                State.used_srcfiles[filepath] = (srcobj, mods_used, units_used)

        # process each nodes in the tree
        for plugin_name in event_register.keys():
            for kfile, sfile, filepath in self.genfiles:
                kfile.created([plugin_name])
                sfile.created([plugin_name])
            for tree in self._trees:
                tree.created([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.process([plugin_name])
                sfile.process([plugin_name])
            for tree in self._trees:
                tree.process([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.finalize([plugin_name])
                sfile.finalize([plugin_name])
            for tree in self._trees:
                tree.finalize([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.flatten(KERNEL_ID_0, [plugin_name])
                sfile.flatten(KERNEL_ID_0, [plugin_name])
            for tree in self._trees:
                tree.flatten(KERNEL_ID_0, [plugin_name])
Beispiel #2
0
def update_state_info(parent):
    def get_nodes(node, bag, depth):
        from Fortran2003 import Name
        if isinstance(
                node, Name
        ) and node.string == bag['name'] and not node.parent in bag:
            anc = [node]
            while hasattr(node, 'parent'):
                anc.insert(0, node.parent)
                node = node.parent
            bag['lineage'].append(anc)

    if hasattr(parent, 'content'):
        for stmt in parent.content:
            if isinstance(stmt, TypeDeclarationStatement) and \
                "parameter" not in stmt.attrspec and hasattr(stmt, 'geninfo') and \
                any(len(v)>0 for v in stmt.geninfo.values()):
                for uname, req in KGGenType.get_state_in(stmt.geninfo):
                    if KGGenType.has_uname_out(uname, stmt.geninfo): continue

                    org = req.originator
                    if org in State.callsite['stmts']:
                        bag = {'name': uname.firstpartname(), 'lineage': []}
                        traverse(org.f2003, get_nodes, bag)
                        for lineage in bag['lineage']:
                            copied = False
                            for lidx, anc in enumerate(lineage):
                                # get callname
                                callname = None
                                if anc.__class__ in [
                                        Call_Stmt, Function_Reference
                                ]:
                                    callname = anc.items[0].string
                                elif anc.__class__ == Part_Ref:
                                    callname = anc.items[0].string
                                elif anc.__class__ == Interface_Stmt:
                                    callname = anc.items[0].string

                                # get caller and callee objects
                                callobj = None
                                subpobj = None
                                if callname:
                                    for org_uname, org_req in org.unknowns.iteritems(
                                    ):
                                        if org_uname.firstpartname(
                                        ) == callname:
                                            if isinstance(
                                                    org_req.res_stmts[0],
                                                    SubProgramStatement):
                                                callobj = anc
                                                subpobj = org_req.res_stmts[0]
                                            break

                                # get argument index
                                argidx = -1
                                is_keyword = False
                                if callobj and subpobj:
                                    if callobj.__class__ in [
                                            Call_Stmt, Function_Reference
                                    ]:
                                        arglist = callobj.items[1]
                                        if arglist is None: pass
                                        elif isinstance(
                                                arglist, Actual_Arg_Spec):
                                            argobj = lineage[lidx + 1]
                                            kword = argobj.items[0].string
                                            argidx = subpobj.args.index(kword)
                                        elif isinstance(
                                                arglist, Actual_Arg_Spec_List):
                                            #if len(lineage)<(lidx+3): import pdb; pdb.set_trace()
                                            argobj = lineage[lidx + 2]
                                            argidx = arglist.items.index(
                                                argobj)
                                            if isinstance(
                                                    argobj, Actual_Arg_Spec):
                                                kword = argobj.items[0].string
                                                argidx = subpobj.args.index(
                                                    kword)
                                        else:
                                            argidx = 0
                                    elif anc.__class__ == Part_Ref:
                                        arglist = callobj.items[1]
                                        if arglist is None: pass
                                        elif isinstance(
                                                arglist,
                                                Structure_Constructor_2):
                                            argobj = lineage[lidx + 1]
                                            kword = argobj.items[0].string
                                            argidx = subpobj.args.index(kword)
                                        elif isinstance(
                                                arglist,
                                                Section_Subscript_List):
                                            #if len(lineage)<(lidx+3): import pdb; pdb.set_trace()
                                            argobj = lineage[lidx + 2]
                                            argidx = arglist.items.index(
                                                argobj)
                                            if isinstance(
                                                    argobj,
                                                    Structure_Constructor_2):
                                                kword = argobj.items[0].string
                                                argidx = subpobj.args.index(
                                                    kword)
                                        else:
                                            argidx = 0
                                    elif anc.__class__ == Interface_Stmt:
                                        import pdb
                                        pdb.set_trace()

                                # get intent
                                if argidx >= 0:
                                    argname = subpobj.args[argidx]
                                    var = subpobj.a.variables[
                                        subpobj.args[argidx]]
                                    if var.is_intent_out(
                                    ) or var.is_intent_inout():
                                        req.gentype = KGGenType.STATE_OUT
                                        stmt.add_geninfo(uname, req)
                                        copied = True
                                        break
                            if copied: break

    if hasattr(parent, 'parent'):
        update_state_info(parent.parent)
Beispiel #3
0
    def main(self):

        Logger.info('Starting KExt', stdout=True)

        # create state directories
        if not os.path.exists(Config.path['state']):
            os.makedirs(Config.path['state'])

        # create kernel directories
        if not os.path.exists(Config.path['kernel']):
            os.makedirs(Config.path['kernel'])

        os.system('rm -f %s/kgen_statefile.lst'%Config.path['kernel'])
        os.system('rm -f %s/done.*'%Config.path['kernel'])

        preprocess()
        Logger.info('Pre-processing is done', stdout=True)
    
        analyze()
        Logger.info('Program is analyzed', stdout=True)

        # generate kgen_driver.f90 in kernel directory
        self.driver = self.create_tree()
        program = self.create_program(self.driver)
        program.name = self.kernel_name
        self.append_program_in_tree(self.driver, program)

        # init plugin framework
        init_plugins([KERNEL_ID_0])

        # construct a generation tree
        for filepath, (srcobj, mods_used, units_used) in State.srcfiles.iteritems():
            if hasattr(srcobj.tree, 'geninfo') and KGGenType.has_state(srcobj.tree.geninfo):
                kfile = genkobj(None, srcobj.tree, KERNEL_ID_0)
                sfile = gensobj(None, srcobj.tree, KERNEL_ID_0)
                if kfile is None or sfile is None:
                    raise ProgramException('Kernel source file is not generated for %s.'%filepath)
                self.genfiles.append((kfile, sfile, filepath))
                State.used_srcfiles[filepath] = (srcobj, mods_used, units_used)

        # process each nodes in the tree
        for plugin_name in event_register.keys():
            for kfile, sfile, filepath in self.genfiles:
                kfile.created([plugin_name])
                sfile.created([plugin_name])
            for tree in self._trees:
                tree.created([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.process([plugin_name])
                sfile.process([plugin_name])
            for tree in self._trees:
                tree.process([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.finalize([plugin_name])
                sfile.finalize([plugin_name])
            for tree in self._trees:
                tree.finalize([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.flatten(KERNEL_ID_0, [plugin_name])
                sfile.flatten(KERNEL_ID_0, [plugin_name])
            for tree in self._trees:
                tree.flatten(KERNEL_ID_0, [plugin_name])
Beispiel #4
0
    def main(self):

        Logger.info('Starting KExt', stdout=True)

        # create state directories
        if not os.path.exists(Config.path['state']):
            os.makedirs(Config.path['state'])

        # create kernel directories
        if not os.path.exists(Config.path['kernel']):
            os.makedirs(Config.path['kernel'])

        os.system('rm -f %s/kgen_statefile.lst' % Config.path['kernel'])
        os.system('rm -f %s/done.*' % Config.path['kernel'])

        preprocess()
        Logger.info('Pre-processing is done', stdout=True)

        analyze()
        Logger.info('Program is analyzed', stdout=True)

        # generate kgen_driver.f90 in kernel directory
        self.driver = self.create_tree()
        program = self.create_program(self.driver)
        program.name = self.kernel_name
        self.append_program_in_tree(self.driver, program)

        # init plugin framework
        init_plugins([KERNEL_ID_0])

        # construct a generation tree
        for filepath, (srcobj, mods_used,
                       units_used) in State.srcfiles.iteritems():
            if hasattr(srcobj.tree, 'geninfo') and KGGenType.has_state(
                    srcobj.tree.geninfo):
                kfile = genkobj(None, srcobj.tree, KERNEL_ID_0)
                sfile = gensobj(None, srcobj.tree, KERNEL_ID_0)
                if kfile is None or sfile is None:
                    raise ProgramException(
                        'Kernel source file is not generated for %s.' %
                        filepath)
                self.genfiles.append((kfile, sfile, filepath))
                State.used_srcfiles[filepath] = (srcobj, mods_used, units_used)

        # process each nodes in the tree
        for plugin_name in event_register.keys():
            for kfile, sfile, filepath in self.genfiles:
                kfile.created([plugin_name])
                sfile.created([plugin_name])
            for tree in self._trees:
                tree.created([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.process([plugin_name])
                sfile.process([plugin_name])
            for tree in self._trees:
                tree.process([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.finalize([plugin_name])
                sfile.finalize([plugin_name])
            for tree in self._trees:
                tree.finalize([plugin_name])

            for kfile, sfile, filepath in self.genfiles:
                kfile.flatten(KERNEL_ID_0, [plugin_name])
                sfile.flatten(KERNEL_ID_0, [plugin_name])
            for tree in self._trees:
                tree.flatten(KERNEL_ID_0, [plugin_name])
Beispiel #5
0
def update_state_info(parent):

    def get_nodes(node, bag, depth):
        from Fortran2003 import Name
        if isinstance(node, Name) and node.string==bag['name'] and not node.parent in bag:
            anc = [node]
            while hasattr(node, 'parent'):
                anc.insert(0, node.parent)
                node = node.parent
            bag['lineage'].append(anc)

    if hasattr(parent, 'content'):
        for stmt in parent.content:
            if isinstance(stmt, TypeDeclarationStatement) and \
                "parameter" not in stmt.attrspec and hasattr(stmt, 'geninfo') and \
                any(len(v)>0 for v in stmt.geninfo.values()):
                for uname, req in KGGenType.get_state_in(stmt.geninfo):
                    if KGGenType.has_uname_out(uname, stmt.geninfo): continue

                    org = req.originator
                    if org in State.callsite['stmts']:
                        bag = {'name': uname.firstpartname(), 'lineage': [] }
                        traverse(org.f2003, get_nodes, bag)
                        for lineage in bag['lineage']:
                            copied = False
                            for lidx, anc in enumerate(lineage):
                                # get callname
                                callname = None
                                if anc.__class__ in [ Call_Stmt, Function_Reference ]:
                                    callname = anc.items[0].string
                                elif anc.__class__ == Part_Ref:
                                    callname = anc.items[0].string
                                elif anc.__class__ == Interface_Stmt:
                                    callname = anc.items[0].string

                                # get caller and callee objects
                                callobj = None
                                subpobj = None
                                if callname:
                                    for org_uname, org_req in org.unknowns.iteritems():
                                        if org_uname.firstpartname()==callname:
                                            if isinstance(org_req.res_stmts[0], SubProgramStatement):
                                                callobj = anc
                                                subpobj = org_req.res_stmts[0]
                                            break
                                    
                                # get argument index
                                argidx = -1
                                is_keyword = False
                                if callobj and subpobj:
                                    if callobj.__class__ in [ Call_Stmt, Function_Reference ]:
                                        arglist = callobj.items[1]
                                        if arglist is None: pass
                                        elif isinstance(arglist, Actual_Arg_Spec):
                                            argobj = lineage[lidx+1]
                                            kword = argobj.items[0].string
                                            argidx = subpobj.args.index(kword)
                                        elif isinstance(arglist, Actual_Arg_Spec_List):
                                            #if len(lineage)<(lidx+3): import pdb; pdb.set_trace()
                                            argobj = lineage[lidx+2]
                                            argidx = arglist.items.index(argobj)
                                            if isinstance(argobj, Actual_Arg_Spec):
                                                kword = argobj.items[0].string
                                                argidx = subpobj.args.index(kword)
                                        else:
                                            argidx = 0
                                    elif anc.__class__ == Part_Ref:
                                        arglist = callobj.items[1]
                                        if arglist is None: pass
                                        elif isinstance(arglist, Structure_Constructor_2):
                                            argobj = lineage[lidx+1]
                                            kword = argobj.items[0].string
                                            argidx = subpobj.args.index(kword)
                                        elif isinstance(arglist, Section_Subscript_List):
                                            #if len(lineage)<(lidx+3): import pdb; pdb.set_trace()
                                            argobj = lineage[lidx+2]
                                            argidx = arglist.items.index(argobj)
                                            if isinstance(argobj, Structure_Constructor_2):
                                                kword = argobj.items[0].string
                                                argidx = subpobj.args.index(kword)
                                        else:
                                            argidx = 0
                                    elif anc.__class__ == Interface_Stmt:
                                        import pdb; pdb.set_trace()

                                # get intent
                                if argidx>=0:
                                    argname = subpobj.args[argidx]
                                    var = subpobj.a.variables[subpobj.args[argidx]]
                                    if var.is_intent_out() or var.is_intent_inout():
                                        req.gentype = KGGenType.STATE_OUT
                                        stmt.add_geninfo(uname, req)
                                        copied = True
                                        break
                            if copied: break

    if hasattr(parent, 'parent'):
        update_state_info(parent.parent)