Esempio n. 1
0
    def create_subr_write_typedecl_in_type(self, node):

        stmt = node.kgen_stmt
        parent = node.kgen_parent.kgen_parent
        entity_names = [get_entity_name(decl) for decl in stmt.entity_decls]

        for entity_name, entity_decl in zip(entity_names, stmt.entity_decls):
            var = stmt.get_variable(entity_name)
            subrname = get_typedecl_writename(stmt, entity_name)
            if subrname is None: raise Exception('Can not get subroutinename')

            if var.is_array():
                if is_zero_array(var, stmt): continue

                if stmt.is_derived():
                    self.create_write_subr(subrname, entity_name, parent, var,
                                           stmt)
                else:  # intrinsic type
                    if var.is_explicit_shape_array():
                        pass
                    else:  # implicit array
                        self.create_write_subr(subrname, entity_name, parent,
                                               var, stmt)
            else:
                if stmt.is_derived():
                    if var.is_allocatable() or var.is_pointer():
                        self.create_write_subr(subrname, entity_name, parent,
                                               var, stmt)
Esempio n. 2
0
    def create_subr_write_typedecl_in_type(self, node):

        stmt = node.kgen_stmt
        parent = node.kgen_parent.kgen_parent
        entity_names = [ get_entity_name(decl) for decl in stmt.entity_decls ]

        for entity_name, entity_decl in zip(entity_names, stmt.entity_decls):
            var = stmt.get_variable(entity_name)
            subrname = get_typedecl_writename(stmt, entity_name)
            if subrname is None: raise Exception('Can not get subroutinename')

            if var.is_array():
                if is_zero_array(var, stmt): continue

                if stmt.is_derived():
                    self.create_write_subr(subrname, entity_name, parent, var, stmt)
                else: # intrinsic type
                    if var.is_explicit_shape_array():
                        pass
                    else: # implicit array
                        self.create_write_subr(subrname, entity_name, parent, var, stmt)
            else:
                if stmt.is_derived():
                    if var.is_allocatable() or var.is_pointer():
                        self.create_write_subr(subrname, entity_name, parent, var, stmt)
Esempio n. 3
0
    def create_subr_write_typedecl_in_module(self, node):
        parent = node.kgen_parent
        stmt = node.kgen_stmt

        raw_entity_names = set([
            uname.firstpartname()
            for uname, req in KGGenType.get_state(stmt.geninfo)
        ])
        entity_names = [
            e for e in raw_entity_names
            if not stmt.get_variable(e).is_parameter()
        ]

        raw_out_entity_names = set([
            uname.firstpartname()
            for uname, req in KGGenType.get_state_out(stmt.geninfo)
        ])
        out_entity_names = [
            e for e in raw_out_entity_names
            if not stmt.get_variable(e).is_parameter()
        ]

        #entity_names = set([ uname.firstpartname() for uname, req in KGGenType.get_state(stmt.geninfo)])
        #out_entity_names = set([ uname.firstpartname() for uname, req in KGGenType.get_state_out(stmt.geninfo)])

        is_class_derived = check_class_derived(stmt)

        for entity_name, entity_decl in zip(entity_names, stmt.entity_decls):
            if node.kgen_parent.name + entity_name in self.state_extern_writes:
                continue
            if is_remove_state(entity_name, stmt): continue

            self.state_extern_writes.append(node.kgen_parent.name +
                                            entity_name)

            var = stmt.get_variable(entity_name)
            subrname = get_typedecl_writename(stmt, entity_name)

            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    self.create_write_call(
                        self.state_externs_subrs[node.kgen_parent][0],
                        subrname, entity_name, stmt, var)
                    if entity_name in out_entity_names:
                        self.create_write_call(
                            self.state_externs_subrs[node.kgen_parent][1],
                            subrname, entity_name, stmt, var)
                    if subrname not in self.state_created_subrs:
                        create_write_subr(subrname, entity_name, parent, var,
                                          stmt)
                        self.state_created_subrs.append(subrname)
                else:  # intrinsic type
                    if var.is_explicit_shape_array():
                        self.create_write_intrinsic(
                            self.state_externs_subrs[node.kgen_parent][0],
                            entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_write_intrinsic(
                                self.state_externs_subrs[node.kgen_parent][1],
                                entity_name, stmt, var)
                    else:  # implicit array
                        self.create_write_call(
                            self.state_externs_subrs[node.kgen_parent][0],
                            subrname, entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_write_call(
                                self.state_externs_subrs[node.kgen_parent][1],
                                subrname, entity_name, stmt, var)
                        if subrname not in self.state_created_subrs:
                            create_write_subr(subrname, entity_name, parent,
                                              var, stmt)
                            self.state_created_subrs.append(subrname)
            else:  # scalar
                if stmt.is_derived() or is_class_derived:
                    if var.is_allocatable() or var.is_pointer():
                        self.create_write_call(
                            self.state_externs_subrs[node.kgen_parent][0],
                            subrname, entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_write_call(
                                self.state_externs_subrs[node.kgen_parent][1],
                                subrname, entity_name, stmt, var)
                        if subrname not in self.state_created_subrs:
                            create_write_subr(subrname, entity_name, parent,
                                              var, stmt)
                            self.state_created_subrs.append(subrname)
                    else:
                        subrname = None
                        for uname, req in stmt.unknowns.iteritems():
                            if (is_class_derived and uname.firstpartname()
                                    == stmt.selector[1]
                                ) or uname.firstpartname() == stmt.name:
                                #if uname.firstpartname()==stmt.name:
                                if len(req.res_stmts) > 0:
                                    res = req.res_stmts[0]
                                    subrname = get_dtype_writename(res)
                                    break
                        if subrname is None:
                            print 'WARNING: Can not find Type resolver for %s' % stmt.name
                            namedpart_append_comment(self.state_externs_subrs[node.kgen_parent][0], EXEC_PART, \
                                'ERROR: "%s" is not resolved. Call statements to write "%s" is not created here.'%\
                                (stmt.name, stmt.name))
                        else:
                            self.create_write_call(
                                self.state_externs_subrs[node.kgen_parent][0],
                                subrname, entity_name, stmt, var)
                            if entity_name in out_entity_names:
                                self.create_write_call(
                                    self.state_externs_subrs[node.kgen_parent]
                                    [1], subrname, entity_name, stmt, var)
                else:  # intrinsic type
                    self.create_write_intrinsic(
                        self.state_externs_subrs[node.kgen_parent][0],
                        entity_name, stmt, var)
                    if entity_name in out_entity_names:
                        self.create_write_intrinsic(
                            self.state_externs_subrs[node.kgen_parent][1],
                            entity_name, stmt, var)
Esempio n. 4
0
    def create_subr_read_typedecl_in_module(self, node):

        parent = node.kgen_parent
        stmt = node.kgen_stmt
        raw_entity_names = set([
            uname.firstpartname()
            for uname, req in KGGenType.get_state(stmt.geninfo)
        ])
        entity_names = [
            e for e in raw_entity_names
            if not stmt.get_variable(e).is_parameter()
        ]

        raw_out_entity_names = set([
            uname.firstpartname()
            for uname, req in KGGenType.get_state_out(stmt.geninfo)
        ])
        out_entity_names = [
            e for e in raw_out_entity_names
            if not stmt.get_variable(e).is_parameter()
        ]

        def get_attrs(attrspec, allowed_attrs):
            attrspec = []
            for attr in stmt.attrspec:
                if any(
                        attr.startswith(allowed_attr)
                        for allowed_attr in allowed_attrs):
                    attrspec.append(attr)
            return attrspec

        def get_decls(names, decls, prefix=''):
            import re
            entity_decls = []
            for decl in decls:
                ename = re.split('\(|\*|=', decl)[0].strip()
                if ename in names:
                    entity_decls.append(prefix + decl)
            return entity_decls

        if len(entity_names) == 0:
            node.kgen_forced_line = False
        elif len(entity_names) != len(stmt.entity_decls):
            attrspec = get_attrs(
                stmt.attrspec,
                ['pointer', 'allocatable', 'dimension', 'public', 'target'])
            entity_decls = get_decls(entity_names, stmt.entity_decls)

            attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                'selector':stmt.selector, 'entity_decls': entity_decls}

            if stmt.is_derived():
                node.type_spec = 'TYPE'
            else:
                node.type_spec = stmt.__class__.__name__.upper()
            node.attrspec = attrspec
            node.selector = stmt.selector
            node.entity_decls = entity_decls
            node.kgen_use_tokgen = True
            #part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)
            #part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)

        if len(out_entity_names) > 0:
            attrspec = get_attrs(stmt.attrspec,
                                 ['pointer', 'allocatable', 'dimension'])

            entity_decls = get_decls(out_entity_names,
                                     stmt.entity_decls,
                                     prefix='kgenref_')

            attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                'selector':stmt.selector, 'entity_decls': entity_decls}
            part_append_genknode(node.kgen_parent,
                                 DECL_PART,
                                 stmt.__class__,
                                 attrs=attrs)

        is_class_derived = check_class_derived(stmt)

        for entity_name, entity_decl in zip(entity_names, stmt.entity_decls):
            if node.kgen_parent.name + entity_name in self.kernel_extern_reads:
                continue

            if is_remove_state(entity_name, stmt): continue

            self.kernel_extern_reads.append(node.kgen_parent.name +
                                            entity_name)

            var = stmt.get_variable(entity_name)
            subrname = get_typedecl_readname(stmt, entity_name)

            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    self.create_read_call(
                        self.kernel_externs_subrs[node.kgen_parent][0],
                        subrname, entity_name, stmt, var)
                    if entity_name in out_entity_names:
                        self.create_read_call(
                            self.kernel_externs_subrs[node.kgen_parent][1],
                            subrname,
                            entity_name,
                            stmt,
                            var,
                            prefix='kgenref_')
                    if subrname not in self.kernel_created_subrs:
                        create_read_subr(subrname, entity_name, parent, var,
                                         stmt)
                        self.kernel_created_subrs.append(subrname)
                else:  # intrinsic type
                    if var.is_explicit_shape_array():
                        self.create_read_intrinsic(
                            self.kernel_externs_subrs[node.kgen_parent][0],
                            entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_read_intrinsic(
                                self.kernel_externs_subrs[node.kgen_parent][1],
                                entity_name,
                                stmt,
                                var,
                                prefix='kgenref_')
                    else:  # implicit array
                        self.create_read_call(
                            self.kernel_externs_subrs[node.kgen_parent][0],
                            subrname, entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_read_call(
                                self.kernel_externs_subrs[node.kgen_parent][1],
                                subrname,
                                entity_name,
                                stmt,
                                var,
                                prefix='kgenref_')
                        if subrname not in self.kernel_created_subrs:
                            create_read_subr(subrname, entity_name, parent,
                                             var, stmt)
                            self.kernel_created_subrs.append(subrname)
            else:  # scalar
                if stmt.is_derived() or is_class_derived:
                    if var.is_allocatable() or var.is_pointer():
                        self.create_read_call(
                            self.kernel_externs_subrs[node.kgen_parent][0],
                            subrname, entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_read_call(
                                self.kernel_externs_subrs[node.kgen_parent][1],
                                subrname,
                                entity_name,
                                stmt,
                                var,
                                prefix='kgenref_')
                        if subrname not in self.kernel_created_subrs:
                            create_read_subr(subrname, entity_name, parent,
                                             var, stmt)
                            self.kernel_created_subrs.append(subrname)
                    else:
                        subrname = None
                        for uname, req in stmt.unknowns.iteritems():
                            if (is_class_derived and uname.firstpartname()
                                    == stmt.selector[1]
                                ) or uname.firstpartname() == stmt.name:
                                #if uname.firstpartname()==stmt.name:
                                if len(req.res_stmts) > 0:
                                    res = req.res_stmts[0]
                                    subrname = get_dtype_readname(res)
                                    break
                        if subrname is None:
                            print 'WARNING: Can not find Type resolver for %s' % stmt.name
                            namedpart_append_comment(self.kernel_externs_subrs[node.kgen_parent][0], EXEC_PART, \
                                'ERROR: "%s" is not resolved. Call statements to read "%s" is not created here.'%\
                                (stmt.name, stmt.name))
                        else:
                            self.create_read_call(
                                self.kernel_externs_subrs[node.kgen_parent][0],
                                subrname, entity_name, stmt, var)
                            if entity_name in out_entity_names:
                                self.create_read_call(
                                    self.kernel_externs_subrs[
                                        node.kgen_parent][1],
                                    subrname,
                                    entity_name,
                                    stmt,
                                    var,
                                    prefix='kgenref_')
                else:  # intrinsic type
                    self.create_read_intrinsic(
                        self.kernel_externs_subrs[node.kgen_parent][0],
                        entity_name, stmt, var)
                    if entity_name in out_entity_names:
                        self.create_read_intrinsic(
                            self.kernel_externs_subrs[node.kgen_parent][1],
                            entity_name,
                            stmt,
                            var,
                            prefix='kgenref_')
Esempio n. 5
0
    def write_state(self, node):
        index, partname, part = get_part_index(node)
        tstmt = node.kgen_stmt.ancestors()[0]
        pstmt = node.kgen_stmt.ancestors()[-1]
        pnode = pstmt.genspair
        node.kgen_stmt.top.used4genstate = True
        filename = os.path.splitext(os.path.basename(
            node.kgen_stmt.reader.id))[0]
        lineno = node.kgen_stmt.item.span[0]

        if not hasattr(node, '__write_commonpart_statewrite'):
            if hasattr(tstmt,
                       'name') and tstmt.name != getinfo('topblock_stmt').name:
                attrs = {'type_spec': 'INTEGER', 'entity_decls': ['kgen_ierr']}
                part_append_gensnode(pnode,
                                     DECL_PART,
                                     typedecl_statements.Integer,
                                     attrs=attrs)

                attrs = {
                    'type_spec': 'INTEGER',
                    'entity_decls': ['kgen_mpirank']
                }
                part_append_gensnode(pnode,
                                     DECL_PART,
                                     typedecl_statements.Integer,
                                     attrs=attrs)

                if getinfo('is_openmp_app'):
                    attrs = {
                        'type_spec': 'INTEGER',
                        'attrspec': ['DIMENSION(0:1023)'],
                        'entity_decls': ['kgen_openmp_issave']
                    }
                    part_append_gensnode(pnode,
                                         DECL_PART,
                                         typedecl_statements.Integer,
                                         attrs=attrs)

                    attrs = {
                        'type_spec': 'INTEGER',
                        'attrspec': ['SAVE', 'DIMENSION(0:1023)'],
                        'entity_decls': ['kgen_previnvoke = -1']
                    }
                    part_append_gensnode(pnode,
                                         DECL_PART,
                                         typedecl_statements.Integer,
                                         attrs=attrs)

                    attrs = {
                        'type_spec': 'INTEGER',
                        'attrspec': ['SAVE', 'DIMENSION(0:1023)'],
                        'entity_decls':
                        ['kgen_writesubp_invoke_L%d = 0' % lineno]
                    }
                    part_append_gensnode(pnode,
                                         DECL_PART,
                                         typedecl_statements.Integer,
                                         attrs=attrs)

                    attrs = {
                        'type_spec': 'INTEGER',
                        'entity_decls': ['OMP_GET_THREAD_NUM']
                    }
                    part_append_gensnode(pnode,
                                         DECL_PART,
                                         typedecl_statements.Integer,
                                         attrs=attrs)

                else:
                    attrs = {
                        'type_spec': 'INTEGER',
                        'entity_decls': ['kgen_openmp_issave']
                    }
                    part_append_gensnode(pnode,
                                         DECL_PART,
                                         typedecl_statements.Integer,
                                         attrs=attrs)

                    attrs = {
                        'type_spec': 'INTEGER',
                        'attrspec': ['SAVE'],
                        'entity_decls': ['kgen_previnvoke = -1']
                    }
                    part_append_gensnode(pnode,
                                         DECL_PART,
                                         typedecl_statements.Integer,
                                         attrs=attrs)

                    attrs = {
                        'type_spec': 'INTEGER',
                        'attrspec': ['SAVE'],
                        'entity_decls':
                        ['kgen_writesubp_invoke_L%d = 0' % lineno]
                    }
                    part_append_gensnode(pnode,
                                         DECL_PART,
                                         typedecl_statements.Integer,
                                         attrs=attrs)

                attrs = {
                    'type_spec': 'INTEGER',
                    'entity_decls': ['kgen_write_unit']
                }
                part_append_gensnode(pnode,
                                     DECL_PART,
                                     typedecl_statements.Integer,
                                     attrs=attrs)

                attrs = {
                    'items':
                    [('state', ('kgen_mpirank', 'kgen_openmp_issave'))]
                }
                part_append_gensnode(pnode,
                                     DECL_PART,
                                     statements.Common,
                                     attrs=attrs)

                part_append_comment(pnode, DECL_PART, '')
            else:
                node.__write_commonpart_statewrite = True

        attrs = {
            'type_spec': 'CHARACTER',
            'entity_decls': ['kgen_write_filepath_L%d' % lineno],
            'selector': ('1024', None)
        }
        part_append_gensnode(pnode,
                             DECL_PART,
                             typedecl_statements.Character,
                             attrs=attrs)

        idx = index + 1

        if getinfo('is_openmp_app'):

            attrs = {'expr': 'kgen_openmp_issave(OMP_GET_THREAD_NUM()) .GE. 0'}
            ifsave = part_insert_gensnode(node.kgen_parent,
                                          EXEC_PART,
                                          block_statements.IfThen,
                                          attrs=attrs,
                                          index=idx)

            idx += 1

            attrs = {
                'expr':
                'kgen_previnvoke(OMP_GET_THREAD_NUM()) .LT. 0 .OR. kgen_previnvoke(OMP_GET_THREAD_NUM()) .NE. kgen_openmp_issave(OMP_GET_THREAD_NUM())'
            }
            ifprev = part_append_gensnode(ifsave,
                                          EXEC_PART,
                                          block_statements.IfThen,
                                          attrs=attrs)

            attrs = {
                'variable': 'kgen_previnvoke(OMP_GET_THREAD_NUM())',
                'sign': '=',
                'expr': 'kgen_openmp_issave(OMP_GET_THREAD_NUM())'
            }
            part_append_gensnode(ifprev,
                                 EXEC_PART,
                                 statements.Assignment,
                                 attrs=attrs)

            attrs = {
                'variable':
                'kgen_writesubp_invoke_L%d(OMP_GET_THREAD_NUM())' % lineno,
                'sign': '=',
                'expr': '0'
            }
            part_append_gensnode(ifprev,
                                 EXEC_PART,
                                 statements.Assignment,
                                 attrs=attrs)

            l = [
                'kgen_mpirank', '"."', 'OMP_GET_THREAD_NUM()', '"."',
                'kgen_openmp_issave(OMP_GET_THREAD_NUM())', '"."',
                'kgen_writesubp_invoke_L%d(OMP_GET_THREAD_NUM())' % lineno
            ]

            part_append_comment(ifsave,
                                EXEC_PART,
                                'CRITICAL (%s_L%d)' % (filename, lineno),
                                style='openmp')
        else:

            attrs = {'expr': 'kgen_openmp_issave .GE. 0'}
            ifsave = part_insert_gensnode(node.kgen_parent,
                                          EXEC_PART,
                                          block_statements.IfThen,
                                          attrs=attrs,
                                          index=idx)

            idx += 1

            attrs = {
                'expr':
                'kgen_previnvoke .LT. 0 .OR. kgen_previnvoke .NE. kgen_openmp_issave'
            }
            ifprev = part_append_gensnode(ifsave,
                                          EXEC_PART,
                                          block_statements.IfThen,
                                          attrs=attrs)

            attrs = {
                'variable': 'kgen_previnvoke',
                'sign': '=',
                'expr': 'kgen_openmp_issave'
            }
            part_append_gensnode(ifprev,
                                 EXEC_PART,
                                 statements.Assignment,
                                 attrs=attrs)

            attrs = {
                'variable': 'kgen_writesubp_invoke_L%d' % lineno,
                'sign': '=',
                'expr': '0'
            }
            part_append_gensnode(ifprev,
                                 EXEC_PART,
                                 statements.Assignment,
                                 attrs=attrs)

            l = [
                'kgen_mpirank', '"."', '0', '"."', 'kgen_openmp_issave', '"."',
                'kgen_writesubp_invoke_L%d' % lineno
            ]

        # file open
        attrs = {
            'specs': [
                'kgen_write_filepath_L%d' % lineno,
                'FMT="(A,I0,A,I0,A,I0,A,I0)"'
            ],
            'items':
            ['"%s/%s.L%d."' % (getinfo('kernel_path'), filename, lineno)] + l
        }
        part_append_gensnode(ifsave, EXEC_PART, statements.Write, attrs=attrs)

        attrs = {'specs': ['NEWUNIT=kgen_write_unit', 'FILE=kgen_write_filepath_L%d'%lineno, 'STATUS="NEW"', 'ACCESS="STREAM"', \
            'FORM="UNFORMATTED"', 'ACTION="WRITE"', 'CONVERT="BIG_ENDIAN"', 'IOSTAT=kgen_ierr']}
        part_append_gensnode(ifsave, EXEC_PART, statements.Open, attrs=attrs)

        vars = []
        for varstr in node.kgen_stmt.write_state:
            parts = varstr.lower().split('%')
            if len(parts) > 1:
                # search through use stmts until the leaf stmt
                partstmt = node.kgen_stmt

                match = re.match(r'\w+[^\w]*', parts[0].strip())
                if match:
                    varname = match.group(0)
                    for unknown, res in partstmt.unknowns.items():
                        if unknown.firstpartname(
                        ) == varname and res.res_stmts[0].is_derived():
                            partstmt = res.res_stmts[0]
                            break

                var = self.get_orgstmt(parts[1:], partstmt)
                vars.append((varstr, var))
            else:
                match = re.match(r'\w+[^\w]*', varstr.strip())
                if match:
                    varname = match.group(0)
                    for unknown, res in node.kgen_stmt.unknowns.items():
                        if unknown.firstpartname() == varname:
                            res_stmt = res.res_stmts[0]
                            var = res_stmt.get_variable(varname)
                            vars.append((varstr, var))
                            break

        for varstr, var in vars:
            stmt = var.parent
            is_class_derived = check_class_derived(stmt)
            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    parts = varstr.split('%')
                    parts[-1] = var.name
                    newvarstr = '%'.join(parts)

                    for rank in range(var.rank):
                        attrs = {
                            'items':
                            ['LBOUND( %s, %d )' % (newvarstr, rank + 1)],
                            'specs': ['UNIT = kgen_write_unit']
                        }
                        part_append_gensnode(ifsave,
                                             EXEC_PART,
                                             statements.Write,
                                             attrs=attrs)

                        attrs = {
                            'items':
                            ['UBOUND( %s, %d )' % (newvarstr, rank + 1)],
                            'specs': ['UNIT = kgen_write_unit']
                        }
                        part_append_gensnode(ifsave,
                                             EXEC_PART,
                                             statements.Write,
                                             attrs=attrs)

                    attrs = {
                        'items': [newvarstr],
                        'specs': ['UNIT = kgen_write_unit']
                    }
                    part_append_gensnode(ifsave,
                                         EXEC_PART,
                                         statements.Write,
                                         attrs=attrs)
            else:  # scalar
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    attrs = {
                        'items': [var.name],
                        'specs': ['UNIT = kgen_write_unit']
                    }
                    part_append_gensnode(ifsave,
                                         EXEC_PART,
                                         statements.Write,
                                         attrs=attrs)

        # file close
        attrs = {'specs': ['UNIT=kgen_write_unit']}
        part_append_gensnode(ifsave, EXEC_PART, statements.Close, attrs=attrs)

        if getinfo('is_openmp_app'):
            part_append_comment(ifsave,
                                EXEC_PART,
                                'END CRITICAL (%s_L%d)' % (filename, lineno),
                                style='openmp')

            attrs = {
                'variable':
                'kgen_writesubp_invoke_L%d(OMP_GET_THREAD_NUM())' % lineno,
                'sign':
                '=',
                'expr':
                'kgen_writesubp_invoke_L%d(OMP_GET_THREAD_NUM()) + 1' % lineno
            }
            part_append_gensnode(ifsave,
                                 EXEC_PART,
                                 statements.Assignment,
                                 attrs=attrs)
        else:
            attrs = {
                'variable': 'kgen_writesubp_invoke_L%d' % lineno,
                'sign': '=',
                'expr': 'kgen_writesubp_invoke_L%d + 1' % lineno
            }
            part_append_gensnode(ifsave,
                                 EXEC_PART,
                                 statements.Assignment,
                                 attrs=attrs)

        part_insert_comment(node.kgen_parent, EXEC_PART, idx + 1, '')
Esempio n. 6
0
    def read_state(self, node):
        index, partname, part = get_part_index(node)
        ancs = node.kgen_stmt.ancestors()
        tstmt = ancs[0]
        pstmt = ancs[-1]
        pnode = pstmt.genkpair
        filename = os.path.splitext(os.path.basename(
            node.kgen_stmt.reader.id))[0]
        lineno = node.kgen_stmt.item.span[0]

        if not hasattr(node, '__write_commonpart_stateread'):

            if hasattr(tstmt,
                       'name') and tstmt.name != getinfo('topblock_stmt').name:

                attrs = {'type_spec': 'INTEGER', 'entity_decls': ['kgen_ierr']}
                part_append_genknode(pnode,
                                     DECL_PART,
                                     typedecl_statements.Integer,
                                     attrs=attrs)

                attrs = {
                    'type_spec': 'INTEGER',
                    'entity_decls': ['kgen_read_unit = -1']
                }
                part_append_genknode(pnode,
                                     DECL_PART,
                                     typedecl_statements.Integer,
                                     attrs=attrs)

                attrs = {
                    'type_spec':
                    'INTEGER',
                    'entity_decls':
                    ['kgen_mpirank', 'kgen_openmptid', 'kgen_kernelinvoke']
                }
                part_append_genknode(pnode,
                                     DECL_PART,
                                     typedecl_statements.Integer,
                                     attrs=attrs)

                attrs = {
                    'type_spec':
                    'LOGICAL',
                    'entity_decls':
                    ['kgen_evalstage', 'kgen_warmupstage', 'kgen_mainstage']
                }
                part_append_genknode(pnode,
                                     DECL_PART,
                                     typedecl_statements.Logical,
                                     attrs=attrs)

                attrs = {
                    'items':
                    [('state', ('kgen_mpirank', 'kgen_openmptid',
                                'kgen_kernelinvoke', 'kgen_evalstage',
                                'kgen_warmupstage', 'kgen_mainstage'))]
                }
                part_append_genknode(pnode,
                                     DECL_PART,
                                     statements.Common,
                                     attrs=attrs)

            attrs = {'type_spec': 'INTEGER', 'entity_decls': ['kgen_ldim1', 'kgen_udim1', 'kgen_ldim2', 'kgen_udim2', \
                'kgen_ldim3', 'kgen_udim3', 'kgen_ldim4', 'kgen_udim4', 'kgen_ldim5', 'kgen_udim5' ]}
            part_append_genknode(pnode,
                                 DECL_PART,
                                 typedecl_statements.Integer,
                                 attrs=attrs)

            attrs = {'type_spec': 'INTEGER', 'attrspec': ['SAVE'], \
                'entity_decls': ['kgen_mindim1 = HUGE(0)', 'kgen_maxdim1 = 0', 'kgen_mindim2 = HUGE(0)', 'kgen_maxdim2 = 0', \
                'kgen_mindim3 = HUGE(0)', 'kgen_maxdim3 = 0', 'kgen_mindim4 = HUGE(0)', 'kgen_maxdim4 = 0', 'kgen_mindim5 = HUGE(0)', 'kgen_maxdim5 = 0' ]}
            part_append_genknode(pnode,
                                 DECL_PART,
                                 typedecl_statements.Integer,
                                 attrs=attrs)

            node.__write_commonpart_stateread = True

        attrs = {'type_spec': 'INTEGER', 'attrspec': ['SAVE'], 'entity_decls': ['kgen_readsubp_evalinvoke_L%d = 0'%lineno, 'kgen_readsubp_warmupinvoke_L%d = 0'%lineno, \
            'kgen_readsubp_maininvoke_L%d = 0'%lineno, 'kgen_readsubp_maxinvoke_L%d = 0'%lineno ]}
        part_append_genknode(pnode,
                             DECL_PART,
                             typedecl_statements.Integer,
                             attrs=attrs)

        attrs = {
            'type_spec': 'CHARACTER',
            'entity_decls': ['kgen_read_filepath_L%d' % lineno],
            'selector': ('128', None)
        }
        part_append_genknode(pnode,
                             DECL_PART,
                             typedecl_statements.Character,
                             attrs=attrs)

        # collect variables for manual state generation
        vars = []
        for varstr in node.kgen_stmt.write_state:
            parts = varstr.lower().split('%')
            if len(parts) > 1:
                # search through use stmts until the leaf stmt
                partstmt = node.kgen_stmt

                match = re.match(r'\w+[^\w]*', parts[0].strip())
                if match:
                    varname = match.group(0)
                    for unknown, res in partstmt.unknowns.items():
                        if unknown.firstpartname(
                        ) == varname and res.res_stmts[0].is_derived():
                            partstmt = res.res_stmts[0]
                            break

                    var = self.get_orgstmt(parts[1:], partstmt)
                    vars.append((varstr, var))
            else:
                match = re.match(r'\w+[^\w]*', varstr.strip())
                if match:
                    varname = match.group(0)
                    for unknown, res in node.kgen_stmt.unknowns.items():
                        if unknown.firstpartname() == varname:
                            res_stmt = res.res_stmts[0]
                            var = res_stmt.get_variable(varname)
                            vars.append((varstr, var))
                            break

        # create typedecls for array of variable pointer
        for varstr, var in vars:
            stmt = var.parent
            is_class_derived = check_class_derived(stmt)

            type_spec = stmt.name.upper()
            attrspec = list(stmt.attrspec)
            if 'save' not in attrspec:
                attrspec.append('SAVE')
            if 'allocatable' not in attrspec:
                attrspec.append('ALLOCATABLE')
            excludes = []
            for spec in attrspec:
                if spec.startswith('dimension'):
                    excludes.append(spec)
                if spec.startswith('pointer'):
                    excludes.append(spec)
            for exclude in excludes:
                attrspec.remove(exclude)
            selector = tuple(stmt.selector)
            entity_decls = ['kgen_arr_%s_L%d' % (var.name, lineno)]

            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    if var.is_explicit_shape_array():
                        attrspec.append('DIMENSION(%s)' %
                                        (', '.join([':'] * (var.rank + 1))))
                    else:  # implicit array
                        attrspec.append('DIMENSION(%s)' %
                                        (', '.join([':'] * (var.rank + 1))))
            else:  # scalar
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    attrspec.append('DIMENSION(:)')

            if hasattr(stmt, 'unknowns'):
                for unk, res in stmt.unknowns.items():
                    res_parent_stmt = res.res_stmts[0].ancestors()[-1]
                    if res_parent_stmt not in ancs:
                        if unk.firstpartname() == selector[0]:
                            self.add_useonlyname(ancs, unk, res.res_stmts)
                        elif unk.firstpartname() == selector[1]:
                            self.add_useonlyname(ancs, unk, res.res_stmts)

            attrs = {
                'type_spec': type_spec,
                'attrspec': attrspec,
                'selector': selector,
                'entity_decls': entity_decls
            }
            part_append_genknode(pnode, DECL_PART, stmt.__class__, attrs=attrs)

        idx = index + 1

        # if eval stage
        # - count max number of local invocations
        attrs = {
            'expr':
            'kgen_evalstage .AND. .NOT. kgen_warmupstage .AND. .NOT. kgen_mainstage'
        }
        ifeval = part_insert_genknode(node.kgen_parent,
                                      EXEC_PART,
                                      block_statements.IfThen,
                                      attrs=attrs,
                                      index=idx)
        idx += 1

        attrs = {
            'variable': 'kgen_readsubp_maxinvoke_L%d' % lineno,
            'sign': '=',
            'expr': '0'
        }
        part_append_genknode(ifeval,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_warmupinvoke_L%d' % lineno,
            'sign': '=',
            'expr': '0'
        }
        part_append_genknode(ifeval,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_maininvoke_L%d' % lineno,
            'sign': '=',
            'expr': '0'
        }
        part_append_genknode(ifeval,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        attrs = {'expr': 'ALLOCATED(kgen_arr_%s_L%d)' % (var.name, lineno)}
        ifalloc = part_append_genknode(ifeval,
                                       EXEC_PART,
                                       block_statements.IfThen,
                                       attrs=attrs)

        attrs = {'items': ['kgen_arr_%s_L%d' % (var.name, lineno)]}
        part_append_genknode(ifalloc,
                             EXEC_PART,
                             statements.Deallocate,
                             attrs=attrs)

        l = [
            'kgen_mpirank', '"."', 'kgen_openmptid', '"."',
            'kgen_kernelinvoke', '"."',
            'kgen_readsubp_evalinvoke_L%d' % lineno
        ]
        attrs = {
            'specs':
            ['kgen_read_filepath_L%d' % lineno, 'FMT="(A,I0,A,I0,A,I0,A,I0)"'],
            'items': ['"%s.L%d."' % (filename, lineno)] + l
        }
        part_append_genknode(ifeval, EXEC_PART, statements.Write, attrs=attrs)

        # file open
        attrs = {'specs': ['NEWUNIT=kgen_read_unit', 'FILE=kgen_read_filepath_L%d'%lineno, 'STATUS="OLD"', 'ACCESS="STREAM"', \
            'FORM="UNFORMATTED"', 'ACTION="READ"', 'CONVERT="BIG_ENDIAN"', 'IOSTAT=kgen_ierr']}
        part_append_genknode(ifeval, EXEC_PART, statements.Open, attrs=attrs)

        for varstr, var in vars:
            stmt = var.parent
            is_class_derived = check_class_derived(stmt)
            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    parts = varstr.split('%')
                    parts[-1] = var.name
                    newvarstr = '%'.join(parts)

                    bounds = []
                    for rank in range(var.rank):
                        attrs = {
                            'items': ['kgen_ldim%d' % (rank + 1)],
                            'specs': ['UNIT = kgen_read_unit']
                        }
                        part_append_genknode(ifeval,
                                             EXEC_PART,
                                             statements.Read,
                                             attrs=attrs)

                        attrs = {
                            'expr':
                            'kgen_ldim%d .LT. kgen_mindim%d' %
                            (rank + 1, rank + 1)
                        }
                        ifmindim = part_append_genknode(
                            ifeval,
                            EXEC_PART,
                            block_statements.IfThen,
                            attrs=attrs)

                        attrs = {
                            'variable': 'kgen_mindim%d' % (rank + 1),
                            'sign': '=',
                            'expr': 'kgen_ldim%d' % (rank + 1)
                        }
                        part_append_genknode(ifmindim,
                                             EXEC_PART,
                                             statements.Assignment,
                                             attrs=attrs)

                        attrs = {
                            'items': ['kgen_udim%d' % (rank + 1)],
                            'specs': ['UNIT = kgen_read_unit']
                        }
                        part_append_genknode(ifeval,
                                             EXEC_PART,
                                             statements.Read,
                                             attrs=attrs)
                        bounds.append('kgen_ldim%d:kgen_udim%d' %
                                      (rank + 1, rank + 1))

                        attrs = {
                            'expr':
                            'kgen_udim%d .GT. kgen_maxdim%d' %
                            (rank + 1, rank + 1)
                        }
                        ifmaxdim = part_append_genknode(
                            ifeval,
                            EXEC_PART,
                            block_statements.IfThen,
                            attrs=attrs)

                        attrs = {
                            'variable': 'kgen_maxdim%d' % (rank + 1),
                            'sign': '=',
                            'expr': 'kgen_udim%d' % (rank + 1)
                        }
                        part_append_genknode(ifmaxdim,
                                             EXEC_PART,
                                             statements.Assignment,
                                             attrs=attrs)

                    attrs = {
                        'items': ['%s(%s)' % (newvarstr, ', '.join(bounds))],
                        'specs': ['UNIT = kgen_read_unit']
                    }
                    part_append_genknode(ifeval,
                                         EXEC_PART,
                                         statements.Read,
                                         attrs=attrs)

            else:  # scalar
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    attrs = {
                        'specs': ['UNIT=kgen_read_unit'],
                        'items': [var.name]
                    }
                    part_append_genknode(ifeval,
                                         EXEC_PART,
                                         statements.Read,
                                         attrs=attrs)

        attrs = {'specs': ['UNIT=kgen_read_unit']}
        part_append_genknode(ifeval, EXEC_PART, statements.Close, attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_evalinvoke_L%d' % lineno,
            'sign': '=',
            'expr': 'kgen_readsubp_evalinvoke_L%d + 1' % lineno
        }
        part_append_genknode(ifeval,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        # if warmup stage
        # - allocate an array and save data variable into the array
        attrs = {
            'expr':
            '.NOT. kgen_evalstage .AND. kgen_warmupstage .AND. .NOT. kgen_mainstage'
        }
        ifwarmup = part_insert_genknode(node.kgen_parent,
                                        EXEC_PART,
                                        block_statements.IfThen,
                                        attrs=attrs,
                                        index=idx)
        idx += 1

        attrs = {
            'expr': '.NOT. ALLOCATED(kgen_arr_%s_L%d)' % (var.name, lineno)
        }
        ifnoalloc = part_append_genknode(ifwarmup,
                                         EXEC_PART,
                                         block_statements.IfThen,
                                         attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_maxinvoke_L%d' % lineno,
            'sign': '=',
            'expr': 'kgen_readsubp_evalinvoke_L%d + 1' % lineno
        }
        part_append_genknode(ifnoalloc,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        for varstr, var in vars:
            stmt = var.parent
            is_class_derived = check_class_derived(stmt)
            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    parts = varstr.split('%')
                    parts[-1] = var.name
                    newvarstr = '%'.join(parts)

                    bounds = []
                    for rank in range(var.rank):
                        bounds.append('kgen_mindim%d:kgen_maxdim%d' %
                                      (rank + 1, rank + 1))

                    attrs = {
                        'items': [
                            'kgen_arr_%s_L%d(kgen_readsubp_maxinvoke_L%d, %s)'
                            % (var.name, lineno, lineno, ', '.join(bounds))
                        ]
                    }
                    part_append_genknode(ifnoalloc,
                                         EXEC_PART,
                                         statements.Allocate,
                                         attrs=attrs)
            else:  # scalar
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    pass
                attrs = {
                    'items': [
                        'kgen_arr_%s_L%d(kgen_readsubp_maxinvoke_L%d)' %
                        (var.name, lineno, lineno)
                    ]
                }
                part_append_genknode(ifnoalloc,
                                     EXEC_PART,
                                     statements.Allocate,
                                     attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_evalinvoke_L%d' % lineno,
            'sign': '=',
            'expr': '0'
        }
        part_append_genknode(ifwarmup,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_maininvoke_L%d' % lineno,
            'sign': '=',
            'expr': '0'
        }
        part_append_genknode(ifwarmup,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        l = [
            'kgen_mpirank', '"."', 'kgen_openmptid', '"."',
            'kgen_kernelinvoke', '"."',
            'kgen_readsubp_warmupinvoke_L%d' % lineno
        ]
        attrs = {
            'specs':
            ['kgen_read_filepath_L%d' % lineno, 'FMT="(A,I0,A,I0,A,I0,A,I0)"'],
            'items': ['"%s.L%d."' % (filename, lineno)] + l
        }
        part_append_genknode(ifwarmup,
                             EXEC_PART,
                             statements.Write,
                             attrs=attrs)

        # file open
        attrs = {'specs': ['NEWUNIT=kgen_read_unit', 'FILE=kgen_read_filepath_L%d'%lineno, 'STATUS="OLD"', 'ACCESS="STREAM"', \
            'FORM="UNFORMATTED"', 'ACTION="READ"', 'CONVERT="BIG_ENDIAN"', 'IOSTAT=kgen_ierr']}
        part_append_genknode(ifwarmup, EXEC_PART, statements.Open, attrs=attrs)

        for varstr, var in vars:
            stmt = var.parent
            is_class_derived = check_class_derived(stmt)
            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    parts = varstr.split('%')
                    parts[-1] = var.name
                    newvarstr = '%'.join(parts)

                    bounds = []
                    for rank in range(var.rank):
                        attrs = {
                            'items': ['kgen_ldim%d' % (rank + 1)],
                            'specs': ['UNIT = kgen_read_unit']
                        }
                        part_append_genknode(ifwarmup,
                                             EXEC_PART,
                                             statements.Read,
                                             attrs=attrs)

                        attrs = {
                            'items': ['kgen_udim%d' % (rank + 1)],
                            'specs': ['UNIT = kgen_read_unit']
                        }
                        part_append_genknode(ifwarmup,
                                             EXEC_PART,
                                             statements.Read,
                                             attrs=attrs)
                        bounds.append('kgen_ldim%d:kgen_udim%d' %
                                      (rank + 1, rank + 1))

                    attrs = {
                        'items': ['%s(%s)' % (newvarstr, ', '.join(bounds))],
                        'specs': ['UNIT = kgen_read_unit']
                    }
                    part_append_genknode(ifwarmup,
                                         EXEC_PART,
                                         statements.Read,
                                         attrs=attrs)

                    attrs = {'variable': 'kgen_arr_%s_L%d(kgen_readsubp_invoke_L%d + 1, %s)'%(var.name, lineno, lineno, ', '.join(bounds)), \
                        'sign': '=', 'expr': '%s( %s )'%(newvarstr, ', '.join(bounds)) }
                    part_append_genknode(ifwarmup,
                                         EXEC_PART,
                                         statements.Assignment,
                                         attrs=attrs)

            else:  # scalar
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    attrs = {
                        'specs': ['UNIT=kgen_read_unit'],
                        'items': [var.name]
                    }
                    part_append_genknode(ifwarmup,
                                         EXEC_PART,
                                         statements.Read,
                                         attrs=attrs)

                attrs = {
                    'variable':
                    'kgen_arr_%s_L%d(kgen_readsubp_invoke_L%d + 1)' %
                    (var.name, lineno, lineno),
                    'sign':
                    '=',
                    'expr':
                    var.name
                }
                part_append_genknode(ifwarmup,
                                     EXEC_PART,
                                     statements.Assignment,
                                     attrs=attrs)

        # file close
        attrs = {'specs': ['UNIT=kgen_read_unit']}
        part_append_genknode(ifwarmup,
                             EXEC_PART,
                             statements.Close,
                             attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_warmupinvoke_L%d' % lineno,
            'sign': '=',
            'expr': 'kgen_readsubp_warmupinvoke_L%d + 1' % lineno
        }
        part_append_genknode(ifwarmup,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        # if main stage
        # - copy data from array to data variable

        attrs = {
            'expr':
            '.NOT. kgen_evalstage .AND. .NOT. kgen_warmupstage .AND. kgen_mainstage'
        }
        ifmain = part_insert_genknode(node.kgen_parent,
                                      EXEC_PART,
                                      block_statements.IfThen,
                                      attrs=attrs,
                                      index=idx)
        idx += 1

        attrs = {
            'variable': 'kgen_readsubp_evalinvoke_L%d' % lineno,
            'sign': '=',
            'expr': '0'
        }
        part_append_genknode(ifmain,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_warmupinvoke_L%d' % lineno,
            'sign': '=',
            'expr': '0'
        }
        part_append_genknode(ifmain,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        for varstr, var in vars:
            stmt = var.parent
            is_class_derived = check_class_derived(stmt)
            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    parts = varstr.split('%')
                    parts[-1] = var.name
                    newvarstr = '%'.join(parts)

                    bounds = []
                    for rank in range(var.rank):
                        bounds.append(
                            'LBOUND(%s, %d):UBOUND(%s, %d)' %
                            (newvarstr, rank + 1, newvarstr, rank + 1))

                    attrs = {
                        'variable':
                        newvarstr,
                        'sign':
                        '=',
                        'expr':
                        'kgen_arr_%s_L%d(kgen_readsubp_invoke_L%d + 1, %s)' %
                        (var.name, lineno, lineno, ', '.join(bounds))
                    }
                    part_append_genknode(ifmain,
                                         EXEC_PART,
                                         statements.Assignment,
                                         attrs=attrs)
            else:  # scalar
                if stmt.is_derived() or is_class_derived:
                    raise Exception(
                        'Derived type is not supported for manual state generation yet.'
                    )
                else:  # intrinsic type
                    pass

                attrs = {
                    'variable':
                    var.name,
                    'sign':
                    '=',
                    'expr':
                    'kgen_arr_%s_L%d(kgen_readsubp_invoke_L%d)' %
                    (var.name, lineno, lineno)
                }
                part_append_genknode(ifmain,
                                     EXEC_PART,
                                     statements.Assignment,
                                     attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_maininvoke_L%d' % lineno,
            'sign': '=',
            'expr': 'kgen_readsubp_maininvoke_L%d + 1' % lineno
        }
        part_append_genknode(ifmain,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        attrs = {
            'expr':
            'kgen_readsubp_maininvoke_L%d .GE. kgen_readsubp_maxinvoke_L%d' %
            (lineno, lineno)
        }
        ifmax = part_append_genknode(ifmain,
                                     EXEC_PART,
                                     block_statements.IfThen,
                                     attrs=attrs)

        attrs = {
            'variable': 'kgen_readsubp_maininvoke_L%d' % lineno,
            'sign': '=',
            'expr': '0'
        }
        part_append_genknode(ifmax,
                             EXEC_PART,
                             statements.Assignment,
                             attrs=attrs)

        part_insert_comment(node.kgen_parent, EXEC_PART, idx, '')
Esempio n. 7
0
    def create_subr_write_typedecl_in_parentblock(self, node):
        stmt = node.kgen_stmt

        argintype = []
        localintype = []
        localouttype = []
        for uname, req in KGGenType.get_state_in(stmt.geninfo):
            entity_name = uname.firstpartname()
            var = stmt.get_variable(entity_name)

            if var.is_parameter(): continue
            if is_remove_state(entity_name, stmt): continue

            if self.check_intent(entity_name, stmt):
                if (entity_name, STATE_PBLOCK_WRITE_IN_ARGS) not in argintype:
                    argintype.append((entity_name, STATE_PBLOCK_WRITE_IN_ARGS))
            elif (entity_name,
                  STATE_PBLOCK_WRITE_IN_LOCALS) not in localintype and (
                      entity_name,
                      STATE_PBLOCK_WRITE_IN_ARGS) not in argintype:
                localintype.append(
                    (uname.firstpartname(), STATE_PBLOCK_WRITE_IN_LOCALS))

        for uname, req in KGGenType.get_state_out(stmt.geninfo):
            entity_name = uname.firstpartname()
            var = stmt.get_variable(entity_name)

            if var.is_parameter(): continue
            if is_remove_state(entity_name, stmt): continue

            if (entity_name,
                    STATE_PBLOCK_WRITE_OUT_LOCALS) not in localouttype:
                localouttype.append(
                    (uname.firstpartname(), STATE_PBLOCK_WRITE_OUT_LOCALS))

            if (entity_name, STATE_PBLOCK_WRITE_IN_ARGS) in argintype: continue

            if (entity_name, STATE_PBLOCK_WRITE_IN_LOCALS) not in localintype:
                localintype.append(
                    (uname.firstpartname(), STATE_PBLOCK_WRITE_IN_LOCALS))
        vartypes = {
            'argintype': argintype,
            'localintype': localintype,
            'localouttype': localouttype
        }

        # for state
        is_class_derived = check_class_derived(stmt)
        for vartypename, vartype in vartypes.iteritems():
            for entity_name, partid in vartype:
                var = stmt.get_variable(entity_name)
                subrname = get_typedecl_writename(stmt, entity_name)
                if var.is_array():
                    if is_zero_array(var, stmt): continue
                    if stmt.is_derived() or is_class_derived:
                        self.create_write_call(node.kgen_kernel_id, partid,
                                               subrname, entity_name, stmt,
                                               var)
                        if subrname not in self.state_created_subrs:
                            create_write_subr(subrname, entity_name,
                                              node.kgen_parent, var, stmt)
                            self.state_created_subrs.append(subrname)
                    else:  # intrinsic type
                        if var.is_explicit_shape_array():
                            if vartypename == 'argintype' or var.is_pointer():
                                self.create_write_call(node.kgen_kernel_id,
                                                       partid, subrname,
                                                       entity_name, stmt, var)
                                if subrname not in self.state_created_subrs:
                                    create_write_subr(subrname, entity_name,
                                                      node.kgen_parent, var,
                                                      stmt)
                                    self.state_created_subrs.append(subrname)
                            else:
                                self.create_write_intrinsic(
                                    node.kgen_kernel_id, partid, entity_name,
                                    stmt, var)
                        else:  # implicit array
                            self.create_write_call(node.kgen_kernel_id, partid,
                                                   subrname, entity_name, stmt,
                                                   var)
                            if subrname not in self.state_created_subrs:
                                create_write_subr(subrname, entity_name,
                                                  node.kgen_parent, var, stmt)
                                self.state_created_subrs.append(subrname)
                else:  # scalar
                    if stmt.is_derived() or is_class_derived or var.is_pointer(
                    ):
                        if var.is_allocatable() or var.is_pointer(
                        ) or var.is_pointer():
                            self.create_write_call(node.kgen_kernel_id, partid,
                                                   subrname, entity_name, stmt,
                                                   var)
                            if subrname not in self.state_created_subrs:
                                create_write_subr(subrname, entity_name,
                                                  node.kgen_parent, var, stmt)
                                self.state_created_subrs.append(subrname)
                        else:
                            subrname = None
                            for uname, req in stmt.unknowns.iteritems():
                                if uname.firstpartname() == stmt.name and len(
                                        req.res_stmts) > 0:
                                    res = req.res_stmts[0]
                                    subrname = get_dtype_writename(res)
                                    break
                            if subrname is None:
                                print 'WARNING: Can not find Type resolver for %s' % stmt.name
                                namedpart_append_comment(node.kgen_kernel_id, partid, \
                                    'ERROR: "%s" is not resolved. Call statements to write "%s" is not created here.'%\
                                    (stmt.name, stmt.name))
                            else:
                                self.create_write_call(node.kgen_kernel_id,
                                                       partid, subrname,
                                                       entity_name, stmt, var)
                    else:  # intrinsic type
                        self.create_write_intrinsic(node.kgen_kernel_id,
                                                    partid, entity_name, stmt,
                                                    var)
Esempio n. 8
0
    def create_subr_read_typedecl_in_parentblock(self, node):
        stmt = node.kgen_stmt

        argintype = []
        localintype = []
        localouttype = []
        for uname, req in KGGenType.get_state_in(stmt.geninfo):
            entity_name = uname.firstpartname()
            var = stmt.get_variable(entity_name)

            if var.is_parameter(): continue
            if is_remove_state(entity_name, stmt): continue

            if self.check_intent(entity_name, stmt):
                if (entity_name, DRIVER_READ_IN_ARGS) not in argintype:

                    argintype.append((entity_name, DRIVER_READ_IN_ARGS))

                    if not entity_name in getinfo(
                            'kernel_driver_callsite_args'):
                        getinfo('kernel_driver_callsite_args').append(
                            entity_name)

                    # add typedecl in driver
                    attrs = {
                        'type_spec': stmt.__class__.__name__.upper(),
                        'selector': stmt.selector,
                        'entity_decls': [entity_name]
                    }
                    attrspec = []
                    if var.is_array():
                        attrspec.append('DIMENSION(%s)' %
                                        ','.join(':' * var.rank))
                        if not var.is_pointer(): attrspec.append('ALLOCATABLE')
                        # deallocate
                    if var.is_pointer(): attrspec.append('POINTER')
                    attrs['attrspec'] = attrspec
                    namedpart_append_genknode(node.kgen_kernel_id,
                                              DRIVER_DECL_PART,
                                              stmt.__class__,
                                              attrs=attrs)

                    if hasattr(stmt, 'unknowns'):
                        for uname, req in stmt.unknowns.iteritems():
                            if len(req.res_stmts) > 0:
                                if req.res_stmts[
                                        -1].__class__ == statements.Use:
                                    checks = lambda n: n.kgen_match_class==statements.Use and n.kgen_stmt and n.kgen_stmt.name==req.res_stmts[-1].name \
                                        and ( n.kgen_stmt.isonly and uname.firstpartname() in [ item.split('=>')[0].strip() for item in n.kgen_stmt.items])
                                    if not namedpart_has_node(
                                            node.kgen_kernel_id,
                                            DRIVER_USE_PART, checks):
                                        item_name = uname.firstpartname()
                                        for new_name, old_name in req.res_stmts[
                                                -1].renames:
                                            if new_name == item_name:
                                                item_name = '%s => %s' % (
                                                    new_name, old_name)
                                                break
                                        if not (req.res_stmts[-1].name,
                                                item_name
                                                ) in self.driver_created_uses:
                                            attrs = {
                                                'name': req.res_stmts[-1].name,
                                                'isonly': True,
                                                'items': [item_name]
                                            }
                                            namedpart_append_genknode(
                                                node.kgen_kernel_id,
                                                DRIVER_USE_PART,
                                                statements.Use,
                                                attrs=attrs)
                                            self.driver_created_uses.append(
                                                (req.res_stmts[-1].name,
                                                 item_name))

                                        if stmt.is_derived(
                                        ) and stmt.name == uname.firstpartname(
                                        ):
                                            readname = get_dtype_readname(
                                                req.res_stmts[0])
                                            if not (
                                                    req.res_stmts[-1].name,
                                                    readname
                                            ) in self.driver_created_uses:
                                                attrs = {
                                                    'name':
                                                    req.res_stmts[-1].name,
                                                    'isonly': True,
                                                    'items': [readname]
                                                }
                                                namedpart_append_genknode(
                                                    node.kgen_kernel_id,
                                                    DRIVER_USE_PART,
                                                    statements.Use,
                                                    attrs=attrs)
                                                self.driver_created_uses.append(
                                                    (req.res_stmts[-1].name,
                                                     readname))

                                else:
                                    if req.res_stmts[
                                            0].genkpair.kgen_parent != node.kgen_parent:
                                        checks = lambda n: n.kgen_match_class==statements.Use and n.kgen_stmt and n.kgen_stmt.name==get_topname(req.res_stmts[-1]) and \
                                            ( n.kgen_stmt.isonly and uname.firstpartname() in [ item.split('=>')[0].strip() for item in n.kgen_stmt.items])
                                        if not namedpart_has_node(
                                                node.kgen_kernel_id,
                                                DRIVER_USE_PART, checks):
                                            item_name = uname.firstpartname()
                                            for new_name, old_name in req.res_stmts[
                                                    -1].renames:
                                                if new_name == item_name:
                                                    item_name = '%s => %s' % (
                                                        new_name, old_name)
                                                    break
                                            if not (
                                                    get_topname(
                                                        req.res_stmts[-1]),
                                                    item_name
                                            ) in self.driver_created_uses:
                                                attrs = {
                                                    'name':
                                                    get_topname(
                                                        req.res_stmts[-1]),
                                                    'isonly':
                                                    True,
                                                    'items': [item_name]
                                                }
                                                namedpart_append_genknode(
                                                    node.kgen_kernel_id,
                                                    DRIVER_USE_PART,
                                                    statements.Use,
                                                    attrs=attrs)
                                                self.driver_created_uses.append(
                                                    (get_topname(
                                                        req.res_stmts[-1]),
                                                     item_name))

                                            if stmt.is_derived(
                                            ) and stmt.name == uname.firstpartname(
                                            ):
                                                readname = get_dtype_readname(
                                                    req.res_stmts[-1])
                                                if not (
                                                        get_topname(
                                                            req.res_stmts[-1]),
                                                        readname
                                                ) in self.driver_created_uses:
                                                    attrs = {
                                                        'name':
                                                        get_topname(
                                                            req.res_stmts[-1]),
                                                        'isonly':
                                                        True,
                                                        'items': [readname]
                                                    }
                                                    namedpart_append_genknode(
                                                        node.kgen_kernel_id,
                                                        DRIVER_USE_PART,
                                                        statements.Use,
                                                        attrs=attrs)
                                                    self.driver_created_uses.append(
                                                        (get_topname(
                                                            req.res_stmts[-1]),
                                                         readname))
            elif (entity_name,
                  KERNEL_PBLOCK_READ_IN_LOCALS) not in localintype and (
                      entity_name, DRIVER_READ_IN_ARGS) not in argintype:
                localintype.append(
                    (uname.firstpartname(), KERNEL_PBLOCK_READ_IN_LOCALS))
        for uname, req in KGGenType.get_state_out(stmt.geninfo):
            entity_name = uname.firstpartname()
            var = stmt.get_variable(entity_name)

            if var.is_parameter(): continue
            if is_remove_state(entity_name, stmt): continue

            if (entity_name,
                    KERNEL_PBLOCK_READ_OUT_LOCALS) not in localouttype:
                localouttype.append(
                    (uname.firstpartname(), KERNEL_PBLOCK_READ_OUT_LOCALS))

            if (entity_name, DRIVER_READ_IN_ARGS) in argintype: continue

            if (entity_name, KERNEL_PBLOCK_READ_IN_LOCALS) not in localintype:
                localintype.append(
                    (uname.firstpartname(), KERNEL_PBLOCK_READ_IN_LOCALS))
        localvartypes = {
            'localintype': localintype,
            'localouttype': localouttype
        }

        def get_attrs(attrspec, allowed_attrs):
            attrspec = []
            for attr in stmt.attrspec:
                if any(
                        attr.startswith(allowed_attr)
                        for allowed_attr in allowed_attrs):
                    attrspec.append(attr)
            return attrspec

        def get_decls(names, decls, prefix=''):
            import re
            entity_decls = []
            for decl in decls:
                ename = re.split('\(|\*|=', decl)[0].strip()
                if ename in names:
                    entity_decls.append(prefix + decl)
            return entity_decls

        def get_enames(names, decls, prefix=''):
            import re
            entity_enames = []
            for decl in decls:
                ename = re.split('\(|\*|=', decl)[0].strip()
                if ename in names:
                    entity_enames.append(prefix + ename)
            return entity_enames

        if len(argintype) > 0:
            attrspec = get_attrs(
                stmt.attrspec,
                ['pointer', 'allocatable', 'dimension', 'target'])
            attrspec.append('INTENT(INOUT)')

            argin_names = [argin_name for argin_name, pname in argintype]
            entity_decls = get_decls(argin_names, stmt.entity_decls)

            attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                'selector':stmt.selector, 'entity_decls': entity_decls}
            if stmt.is_derived():
                node.type_spec = 'TYPE'
            else:
                node.type_spec = stmt.__class__.__name__.upper()
            node.attrspec = attrspec
            node.selector = stmt.selector
            node.entity_decls = entity_decls
            node.kgen_use_tokgen = True
            #part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)

        if len(localintype) == 0 and len(argintype) == 0 and len(
                localouttype) == 0:
            node.kgen_forced_line = False
        elif len(localintype) > 0:
            attrspec = get_attrs(
                stmt.attrspec,
                ['pointer', 'allocatable', 'dimension', 'target'])

            localin_names = [
                localin_name for localin_name, pname in localintype
            ]
            entity_decls = get_decls(localin_names, stmt.entity_decls)

            attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                'selector':stmt.selector, 'entity_decls': entity_decls}
            if stmt.is_derived():
                node.type_spec = 'TYPE'
            else:
                node.type_spec = stmt.__class__.__name__.upper()
            node.attrspec = attrspec
            node.selector = stmt.selector
            node.entity_decls = entity_decls
            node.kgen_use_tokgen = True
            #part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)

        local_allocate = False
        if len(localouttype) > 0:
            for localout_name, partid in localouttype:
                attrspec = get_attrs(stmt.attrspec, ['pointer', 'allocatable'])
                var = stmt.get_variable(localout_name)
                if var.is_array():
                    if var.is_explicit_shape_array():
                        attrspec.append('dimension(%s)' % ','.join([
                            ':'.join(slice) for slice in var.get_array_spec()
                        ]))
                    else:
                        attrspec.append('dimension(%s)' %
                                        ','.join([':'] * var.rank))
                        if 'allocatable' not in attrspec and 'pointer' not in attrspec:
                            local_allocate = True
                            attrspec.append('allocatable')

                #localout_names = [ localout_name for localout_name, pname in localouttype]
                #entity_decls = get_decls(localout_names, stmt.entity_decls, prefix='kgenref_')
                #entity_enames = get_enames(localout_names, stmt.entity_decls, prefix='kgenref_')

                attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                    'selector':stmt.selector, 'entity_decls': [ 'kgenref_%s'%localout_name ]}
                #'selector':stmt.selector, 'entity_decls': entity_decls}
                part_append_genknode(node.kgen_parent,
                                     DECL_PART,
                                     stmt.__class__,
                                     attrs=attrs)

        # for kernel - local variables
        is_class_derived = check_class_derived(stmt)
        for vartypename, vartype in localvartypes.iteritems():
            for entity_name, partid in vartype:
                if vartypename == 'localouttype': ename_prefix = 'kgenref_'
                else: ename_prefix = ''
                var = stmt.get_variable(entity_name)
                subrname = get_typedecl_readname(stmt, entity_name)
                if var.is_array():
                    if is_zero_array(var, stmt): continue
                    if stmt.is_derived() or is_class_derived:
                        self.create_read_call(node.kgen_kernel_id,
                                              partid,
                                              subrname,
                                              entity_name,
                                              stmt,
                                              var,
                                              ename_prefix=ename_prefix)
                        if subrname not in self.kernel_created_subrs:
                            create_read_subr(subrname,
                                             entity_name,
                                             node.kgen_parent,
                                             var,
                                             stmt,
                                             ename_prefix=ename_prefix,
                                             allocate=local_allocate)
                            self.kernel_created_subrs.append(subrname)
                    else:  # intrinsic type
                        if var.is_explicit_shape_array():
                            if var.is_pointer():
                                self.create_read_call(
                                    node.kgen_kernel_id,
                                    partid,
                                    subrname,
                                    entity_name,
                                    stmt,
                                    var,
                                    ename_prefix=ename_prefix)
                                if subrname not in self.kernel_created_subrs:
                                    create_read_subr(subrname,
                                                     entity_name,
                                                     node.kgen_parent,
                                                     var,
                                                     stmt,
                                                     ename_prefix=ename_prefix,
                                                     allocate=local_allocate)
                                    self.kernel_created_subrs.append(subrname)
                            else:
                                self.create_read_intrinsic(
                                    node.kgen_kernel_id,
                                    partid,
                                    entity_name,
                                    stmt,
                                    var,
                                    ename_prefix=ename_prefix)
                        else:  # implicit array
                            self.create_read_call(node.kgen_kernel_id,
                                                  partid,
                                                  subrname,
                                                  entity_name,
                                                  stmt,
                                                  var,
                                                  ename_prefix=ename_prefix)
                            if subrname not in self.kernel_created_subrs:
                                create_read_subr(subrname,
                                                 entity_name,
                                                 node.kgen_parent,
                                                 var,
                                                 stmt,
                                                 ename_prefix=ename_prefix,
                                                 allocate=local_allocate)
                                self.kernel_created_subrs.append(subrname)
                else:  # scalar
                    if stmt.is_derived() or is_class_derived or var.is_pointer(
                    ):
                        if var.is_allocatable() or var.is_pointer():
                            self.create_read_call(node.kgen_kernel_id,
                                                  partid,
                                                  subrname,
                                                  entity_name,
                                                  stmt,
                                                  var,
                                                  ename_prefix=ename_prefix)
                            if subrname not in self.kernel_created_subrs:
                                create_read_subr(subrname,
                                                 entity_name,
                                                 node.kgen_parent,
                                                 var,
                                                 stmt,
                                                 ename_prefix=ename_prefix,
                                                 allocate=local_allocate)
                                self.kernel_created_subrs.append(subrname)
                        else:
                            subrname = None
                            for uname, req in stmt.unknowns.iteritems():
                                if uname.firstpartname() == stmt.name and len(
                                        req.res_stmts) > 0:
                                    res = req.res_stmts[0]
                                    subrname = get_dtype_readname(res)
                                    break
                            if subrname is None:
                                print 'WARNING: Can not find Type resolver for %s' % stmt.name
                                namedpart_append_comment(node.kgen_kernel_id, partid, \
                                    'ERROR: "%s" is not resolved. Call statements to read "%s" is not created here.'%\
                                    (stmt.name, stmt.name))
                            else:
                                self.create_read_call(
                                    node.kgen_kernel_id,
                                    partid,
                                    subrname,
                                    entity_name,
                                    stmt,
                                    var,
                                    ename_prefix=ename_prefix)
                    else:  # intrinsic type
                        self.create_read_intrinsic(node.kgen_kernel_id,
                                                   partid,
                                                   entity_name,
                                                   stmt,
                                                   var,
                                                   ename_prefix=ename_prefix)

        # for kernel - argument variables
        for entity_name, partid in argintype:
            var = stmt.get_variable(entity_name)
            subrname = get_typedecl_readname(stmt, entity_name)
            if var.is_array():
                self.create_read_call(node.kgen_kernel_id, partid, subrname,
                                      entity_name, stmt, var)
                if subrname not in self.driver_created_subrs:
                    create_read_subr(subrname,
                                     entity_name,
                                     shared_objects['driver_object'],
                                     var,
                                     stmt,
                                     allocate=True)
                    self.driver_created_subrs.append(subrname)
            else:  # scalar
                if stmt.is_derived() or is_class_derived or var.is_pointer():
                    if var.is_allocatable() or var.is_pointer():
                        self.create_read_call(node.kgen_kernel_id,
                                              partid,
                                              subrname,
                                              entity_name,
                                              stmt,
                                              var,
                                              ename_prefix=ename_prefix)
                        if subrname not in self.kernel_created_subrs:
                            create_read_subr(subrname,
                                             entity_name,
                                             node.kgen_parent,
                                             var,
                                             stmt,
                                             ename_prefix=ename_prefix)
                            self.kernel_created_subrs.append(subrname)
                    else:
                        subrname = None
                        for uname, req in stmt.unknowns.iteritems():
                            if uname.firstpartname() == stmt.name and len(
                                    req.res_stmts) > 0:
                                res = req.res_stmts[0]
                                subrname = get_dtype_readname(res)
                                break
                        if subrname is None:
                            print 'WARNING: Can not find Type resolver for %s' % stmt.name
                            namedpart_append_comment(node.kgen_kernel_id, partid, \
                                'ERROR: "%s" is not resolved. Call statements to read "%s" is not created here.'%\
                                (stmt.name, stmt.name))
                        else:
                            self.create_read_call(node.kgen_kernel_id, partid,
                                                  subrname, entity_name, stmt,
                                                  var)
                else:  # intrinsic type
                    self.create_read_intrinsic(node.kgen_kernel_id, partid,
                                               entity_name, stmt, var)
Esempio n. 9
0
    def create_subr_write_typedecl_in_parentblock(self, node):
        stmt = node.kgen_stmt

        argintype = []
        localintype = []
        localouttype = []
        for uname, req in KGGenType.get_state_in(stmt.geninfo):
            entity_name = uname.firstpartname()
            var = stmt.get_variable(entity_name)

            if var.is_parameter(): continue
            if is_remove_state(entity_name, stmt): continue

            if self.check_intent(entity_name, stmt):
                if (entity_name,STATE_PBLOCK_WRITE_IN_ARGS) not in argintype:
                    argintype.append((entity_name, STATE_PBLOCK_WRITE_IN_ARGS))
            elif (entity_name,STATE_PBLOCK_WRITE_IN_LOCALS) not in localintype and (entity_name,STATE_PBLOCK_WRITE_IN_ARGS) not in argintype:
                localintype.append((uname.firstpartname(), STATE_PBLOCK_WRITE_IN_LOCALS))

        for uname, req in KGGenType.get_state_out(stmt.geninfo):
            entity_name = uname.firstpartname()
            var = stmt.get_variable(entity_name)

            if var.is_parameter(): continue
            if is_remove_state(entity_name, stmt): continue

            if (entity_name,STATE_PBLOCK_WRITE_OUT_LOCALS) not in localouttype:
                localouttype.append((uname.firstpartname(), STATE_PBLOCK_WRITE_OUT_LOCALS))

            if (entity_name,STATE_PBLOCK_WRITE_IN_ARGS) in argintype: continue

            if (entity_name,STATE_PBLOCK_WRITE_IN_LOCALS) not in localintype:
                localintype.append((uname.firstpartname(), STATE_PBLOCK_WRITE_IN_LOCALS))
        vartypes = { 'argintype': argintype, 'localintype': localintype, 'localouttype': localouttype }

        # for state
        is_class_derived = check_class_derived(stmt)
        for vartypename, vartype in vartypes.iteritems():
            for entity_name, partid in vartype:
                var = stmt.get_variable(entity_name)
                subrname = get_typedecl_writename(stmt, entity_name)
                if var.is_array():
                    if is_zero_array(var, stmt): continue
                    if stmt.is_derived() or is_class_derived:
                        self.create_write_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var)
                        if subrname not in self.state_created_subrs:
                            create_write_subr(subrname, entity_name, node.kgen_parent, var, stmt)
                            self.state_created_subrs.append(subrname)
                    else: # intrinsic type
                        if var.is_explicit_shape_array():
                            if vartypename=='argintype' or var.is_pointer():
                                self.create_write_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var)
                                if subrname not in self.state_created_subrs:
                                    create_write_subr(subrname, entity_name, node.kgen_parent, var, stmt)
                                    self.state_created_subrs.append(subrname)
                            else:
                                self.create_write_intrinsic(node.kgen_kernel_id, partid, entity_name, stmt, var)
                        else: # implicit array
                            self.create_write_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var)
                            if subrname not in self.state_created_subrs:
                                create_write_subr(subrname, entity_name, node.kgen_parent, var, stmt)
                                self.state_created_subrs.append(subrname)
                else: # scalar
                    if stmt.is_derived() or is_class_derived or var.is_pointer():
                        if var.is_allocatable() or var.is_pointer() or var.is_pointer():
                            self.create_write_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var)
                            if subrname not in self.state_created_subrs:
                                create_write_subr(subrname, entity_name, node.kgen_parent, var, stmt)
                                self.state_created_subrs.append(subrname)
                        else:
                            subrname = None
                            for uname, req in stmt.unknowns.iteritems():
                                if uname.firstpartname()==stmt.name and len(req.res_stmts)>0:
                                    res = req.res_stmts[0]
                                    subrname = get_dtype_writename(res)
                                    break
                            if subrname is None:
                                print 'WARNING: Can not find Type resolver for %s'%stmt.name
                                namedpart_append_comment(node.kgen_kernel_id, partid, \
                                    'ERROR: "%s" is not resolved. Call statements to write "%s" is not created here.'%\
                                    (stmt.name, stmt.name))
                            else:
                                self.create_write_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var)
                    else: # intrinsic type
                        self.create_write_intrinsic(node.kgen_kernel_id, partid, entity_name, stmt, var)
Esempio n. 10
0
    def create_subr_read_typedecl_in_parentblock(self, node):
        stmt = node.kgen_stmt

        argintype = []
        localintype = []
        localouttype = []
        for uname, req in KGGenType.get_state_in(stmt.geninfo):
            entity_name = uname.firstpartname()
            var = stmt.get_variable(entity_name)

            if var.is_parameter(): continue
            if is_remove_state(entity_name, stmt): continue

            if self.check_intent(entity_name, stmt):
                if (entity_name,DRIVER_READ_IN_ARGS) not in argintype:

                    argintype.append((entity_name, DRIVER_READ_IN_ARGS))

                    if not entity_name in getinfo('kernel_driver_callsite_args'):
                        getinfo('kernel_driver_callsite_args').append(entity_name)

                    # add typedecl in driver
                    attrs={'type_spec':stmt.__class__.__name__.upper(), 'selector':stmt.selector, 'entity_decls': [entity_name]}
                    attrspec = []
                    if var.is_array():
                        attrspec.append('DIMENSION(%s)'%','.join(':'*var.rank))
                        if not var.is_pointer(): attrspec.append('ALLOCATABLE')
                        # deallocate
                    if var.is_pointer(): attrspec.append('POINTER')
                    attrs['attrspec'] = attrspec 
                    namedpart_append_genknode(node.kgen_kernel_id, DRIVER_DECL_PART, stmt.__class__, attrs=attrs)

                    if hasattr(stmt, 'unknowns'):
                        for uname, req in stmt.unknowns.iteritems():
                            if len(req.res_stmts)>0:
                                if req.res_stmts[-1].__class__==statements.Use:
                                    checks = lambda n: n.kgen_match_class==statements.Use and n.kgen_stmt and n.kgen_stmt.name==req.res_stmts[-1].name \
                                        and ( n.kgen_stmt.isonly and uname.firstpartname() in [ item.split('=>')[0].strip() for item in n.kgen_stmt.items])
                                    if not namedpart_has_node(node.kgen_kernel_id, DRIVER_USE_PART, checks):
                                        item_name = uname.firstpartname()
                                        for new_name, old_name in req.res_stmts[-1].renames:
                                            if new_name==item_name:
                                                item_name = '%s => %s'%(new_name, old_name) 
                                                break
                                        if not (req.res_stmts[-1].name, item_name) in self.driver_created_uses:
                                            attrs = {'name':req.res_stmts[-1].name, 'isonly': True, 'items':[item_name]}
                                            namedpart_append_genknode(node.kgen_kernel_id, DRIVER_USE_PART, statements.Use, attrs=attrs)
                                            self.driver_created_uses.append((req.res_stmts[-1].name, item_name))

                                        if stmt.is_derived() and stmt.name==uname.firstpartname():
                                            readname = get_dtype_readname(req.res_stmts[0])
                                            if not (req.res_stmts[-1].name, readname) in self.driver_created_uses:
                                                attrs = {'name':req.res_stmts[-1].name, 'isonly': True, 'items':[readname]}
                                                namedpart_append_genknode(node.kgen_kernel_id, DRIVER_USE_PART, statements.Use, attrs=attrs)
                                                self.driver_created_uses.append((req.res_stmts[-1].name, readname))

                                else:
                                    if req.res_stmts[0].genkpair.kgen_parent!=node.kgen_parent:
                                        checks = lambda n: n.kgen_match_class==statements.Use and n.kgen_stmt and n.kgen_stmt.name==get_topname(req.res_stmts[-1]) and \
                                            ( n.kgen_stmt.isonly and uname.firstpartname() in [ item.split('=>')[0].strip() for item in n.kgen_stmt.items])
                                        if not namedpart_has_node(node.kgen_kernel_id, DRIVER_USE_PART, checks):
                                            item_name = uname.firstpartname()
                                            for new_name, old_name in req.res_stmts[-1].renames:
                                                if new_name==item_name:
                                                    item_name = '%s => %s'%(new_name, old_name) 
                                                    break
                                            if not (get_topname(req.res_stmts[-1]), item_name) in self.driver_created_uses:
                                                attrs = {'name':get_topname(req.res_stmts[-1]), 'isonly': True, 'items':[item_name]}
                                                namedpart_append_genknode(node.kgen_kernel_id, DRIVER_USE_PART, statements.Use, attrs=attrs)
                                                self.driver_created_uses.append((get_topname(req.res_stmts[-1]), item_name))

                                            if stmt.is_derived() and stmt.name==uname.firstpartname():
                                                readname = get_dtype_readname(req.res_stmts[-1])
                                                if not (get_topname(req.res_stmts[-1]), readname) in self.driver_created_uses:
                                                    attrs = {'name':get_topname(req.res_stmts[-1]), 'isonly': True, 'items':[readname]}
                                                    namedpart_append_genknode(node.kgen_kernel_id, DRIVER_USE_PART, statements.Use, attrs=attrs)
                                                    self.driver_created_uses.append((get_topname(req.res_stmts[-1]), readname))
            elif (entity_name,KERNEL_PBLOCK_READ_IN_LOCALS) not in localintype and (entity_name,DRIVER_READ_IN_ARGS) not in argintype:
                localintype.append((uname.firstpartname(), KERNEL_PBLOCK_READ_IN_LOCALS))
        for uname, req in KGGenType.get_state_out(stmt.geninfo):
            entity_name = uname.firstpartname()
            var = stmt.get_variable(entity_name)

            if var.is_parameter(): continue
            if is_remove_state(entity_name, stmt): continue

            if (entity_name,KERNEL_PBLOCK_READ_OUT_LOCALS) not in localouttype:
                localouttype.append((uname.firstpartname(), KERNEL_PBLOCK_READ_OUT_LOCALS))

            if (entity_name,DRIVER_READ_IN_ARGS) in argintype: continue

            if (entity_name,KERNEL_PBLOCK_READ_IN_LOCALS) not in localintype:
                localintype.append((uname.firstpartname(), KERNEL_PBLOCK_READ_IN_LOCALS))
        localvartypes = { 'localintype': localintype, 'localouttype': localouttype }

        def get_attrs(attrspec, allowed_attrs):
            attrspec = []
            for attr in stmt.attrspec:
                if any( attr.startswith(allowed_attr) for allowed_attr in allowed_attrs):
                    attrspec.append(attr)
            return attrspec

        def get_decls(names, decls, prefix=''):
            import re
            entity_decls = []
            for decl in decls:
                ename = re.split('\(|\*|=', decl)[0].strip()
                if ename in names:
                    entity_decls.append(prefix+decl)
            return entity_decls

        def get_enames(names, decls, prefix=''):
            import re
            entity_enames = []
            for decl in decls:
                ename = re.split('\(|\*|=', decl)[0].strip()
                if ename in names:
                    entity_enames.append(prefix+ename)
            return entity_enames

        if len(argintype)>0:
            attrspec = get_attrs(stmt.attrspec, ['pointer', 'allocatable', 'dimension', 'target'])
            attrspec.append('INTENT(INOUT)')

            argin_names = [ argin_name for argin_name, pname in argintype]
            entity_decls = get_decls(argin_names, stmt.entity_decls)

            attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                'selector':stmt.selector, 'entity_decls': entity_decls}
            if stmt.is_derived():
                node.type_spec = 'TYPE'
            else:
                node.type_spec = stmt.__class__.__name__.upper()
            node.attrspec = attrspec
            node.selector = stmt.selector
            node.entity_decls = entity_decls
            node.kgen_use_tokgen = True
            #part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)

        if len(localintype)==0 and len(argintype)==0 and len(localouttype)==0:
            node.kgen_forced_line = False
        elif len(localintype)>0:
            attrspec = get_attrs(stmt.attrspec, ['pointer', 'allocatable', 'dimension', 'target'])

            localin_names = [ localin_name for localin_name, pname in localintype]
            entity_decls = get_decls(localin_names, stmt.entity_decls)

            attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                'selector':stmt.selector, 'entity_decls': entity_decls}
            if stmt.is_derived():
                node.type_spec = 'TYPE'
            else:
                node.type_spec = stmt.__class__.__name__.upper()
            node.attrspec = attrspec
            node.selector = stmt.selector
            node.entity_decls = entity_decls
            node.kgen_use_tokgen = True
            #part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)

        local_allocate = False
        if len(localouttype)>0:
            for localout_name, partid in localouttype:
                attrspec = get_attrs(stmt.attrspec, ['pointer', 'allocatable'])
                var = stmt.get_variable(localout_name)
                if var.is_array():
                    if var.is_explicit_shape_array():
                        attrspec.append('dimension(%s)'%','.join(var.shape))
                    else:
                        attrspec.append('dimension(%s)'%','.join([':']*var.rank))
                        if  'allocatable' not in attrspec and 'pointer' not in attrspec:
                            local_allocate = True
                            attrspec.append('allocatable')

                #localout_names = [ localout_name for localout_name, pname in localouttype]
                #entity_decls = get_decls(localout_names, stmt.entity_decls, prefix='kgenref_')
                #entity_enames = get_enames(localout_names, stmt.entity_decls, prefix='kgenref_')

                attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                    'selector':stmt.selector, 'entity_decls': [ 'kgenref_%s'%localout_name ]}
                    #'selector':stmt.selector, 'entity_decls': entity_decls}
                part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)

        # for kernel - local variables
        is_class_derived = check_class_derived(stmt)
        for vartypename, vartype in localvartypes.iteritems():
            for entity_name, partid in vartype:
                if vartypename=='localouttype': ename_prefix = 'kgenref_'
                else: ename_prefix = ''
                var = stmt.get_variable(entity_name)
                subrname = get_typedecl_readname(stmt, entity_name)
                if var.is_array():
                    if is_zero_array(var, stmt): continue
                    if stmt.is_derived() or is_class_derived:
                        self.create_read_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var, ename_prefix=ename_prefix)
                        if subrname not in self.kernel_created_subrs:
                            create_read_subr(subrname, entity_name, node.kgen_parent, var, stmt, ename_prefix=ename_prefix, allocate=local_allocate)
                            self.kernel_created_subrs.append(subrname)
                    else: # intrinsic type
                        if var.is_explicit_shape_array():
                            if var.is_pointer():
                                self.create_read_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var, ename_prefix=ename_prefix)
                                if subrname not in self.kernel_created_subrs:
                                    create_read_subr(subrname, entity_name, node.kgen_parent, var, stmt, ename_prefix=ename_prefix, allocate=local_allocate)
                                    self.kernel_created_subrs.append(subrname)
                            else:
                                self.create_read_intrinsic(node.kgen_kernel_id, partid, entity_name, stmt, var, ename_prefix=ename_prefix)
                        else: # implicit array
                            self.create_read_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var, ename_prefix=ename_prefix)
                            if subrname not in self.kernel_created_subrs:
                                create_read_subr(subrname, entity_name, node.kgen_parent, var, stmt, ename_prefix=ename_prefix, allocate=local_allocate)
                                self.kernel_created_subrs.append(subrname)
                else: # scalar
                    if stmt.is_derived() or is_class_derived or var.is_pointer():
                        if var.is_allocatable() or var.is_pointer():
                            self.create_read_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var, ename_prefix=ename_prefix)
                            if subrname not in self.kernel_created_subrs:
                                create_read_subr(subrname, entity_name, node.kgen_parent, var, stmt, ename_prefix=ename_prefix, allocate=local_allocate)
                                self.kernel_created_subrs.append(subrname)
                        else:
                            subrname = None
                            for uname, req in stmt.unknowns.iteritems():
                                if uname.firstpartname()==stmt.name and len(req.res_stmts)>0:
                                    res = req.res_stmts[0]
                                    subrname = get_dtype_readname(res)
                                    break
                            if subrname is None:
                                print 'WARNING: Can not find Type resolver for %s'%stmt.name
                                namedpart_append_comment(node.kgen_kernel_id, partid, \
                                    'ERROR: "%s" is not resolved. Call statements to read "%s" is not created here.'%\
                                    (stmt.name, stmt.name))
                            else:
                                self.create_read_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var, ename_prefix=ename_prefix)
                    else: # intrinsic type
                        self.create_read_intrinsic(node.kgen_kernel_id, partid, entity_name, stmt, var, ename_prefix=ename_prefix)

        # for kernel - argument variables
        for entity_name, partid in argintype:
            var = stmt.get_variable(entity_name)
            subrname = get_typedecl_readname(stmt, entity_name)
            if var.is_array():
                self.create_read_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var)
                if subrname not in self.driver_created_subrs:
                    create_read_subr(subrname, entity_name, shared_objects['driver_object'], var, stmt, allocate=True)
                    self.driver_created_subrs.append(subrname)
            else: # scalar
                if stmt.is_derived() or is_class_derived or var.is_pointer():
                    if var.is_allocatable() or var.is_pointer():
                        self.create_read_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var, ename_prefix=ename_prefix)
                        if subrname not in self.kernel_created_subrs:
                            create_read_subr(subrname, entity_name, node.kgen_parent, var, stmt, ename_prefix=ename_prefix)
                            self.kernel_created_subrs.append(subrname)
                    else:
                        subrname = None
                        for uname, req in stmt.unknowns.iteritems():
                            if uname.firstpartname()==stmt.name and len(req.res_stmts)>0:
                                res = req.res_stmts[0]
                                subrname = get_dtype_readname(res)
                                break
                        if subrname is None:
                            print 'WARNING: Can not find Type resolver for %s'%stmt.name
                            namedpart_append_comment(node.kgen_kernel_id, partid, \
                                'ERROR: "%s" is not resolved. Call statements to read "%s" is not created here.'%\
                                (stmt.name, stmt.name))
                        else:
                            self.create_read_call(node.kgen_kernel_id, partid, subrname, entity_name, stmt, var)
                else: # intrinsic type
                    self.create_read_intrinsic(node.kgen_kernel_id, partid, entity_name, stmt, var)
Esempio n. 11
0
    def create_subr_write_typedecl_in_module(self, node):
        parent = node.kgen_parent
        stmt = node.kgen_stmt

        raw_entity_names = set([ uname.firstpartname() for uname, req in KGGenType.get_state(stmt.geninfo)])
        entity_names = [ e for e in raw_entity_names if not stmt.get_variable(e).is_parameter() ]
        
        raw_out_entity_names = set([ uname.firstpartname() for uname, req in KGGenType.get_state_out(stmt.geninfo)])
        out_entity_names = [ e for e in raw_out_entity_names if not stmt.get_variable(e).is_parameter() ]

        #entity_names = set([ uname.firstpartname() for uname, req in KGGenType.get_state(stmt.geninfo)])
        #out_entity_names = set([ uname.firstpartname() for uname, req in KGGenType.get_state_out(stmt.geninfo)])

        is_class_derived = check_class_derived(stmt)

        for entity_name, entity_decl in zip(entity_names, stmt.entity_decls):
            if node.kgen_parent.name+entity_name in self.state_extern_writes: continue
            if is_remove_state(entity_name, stmt): continue

            self.state_extern_writes.append(node.kgen_parent.name+entity_name)

            var = stmt.get_variable(entity_name)
            subrname = get_typedecl_writename(stmt, entity_name)

            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    self.create_write_call(self.state_externs_subrs[node.kgen_parent][0], subrname, entity_name, stmt, var)
                    if entity_name in out_entity_names:
                        self.create_write_call(self.state_externs_subrs[node.kgen_parent][1], subrname, entity_name, stmt, var)
                    if subrname not in self.state_created_subrs:
                        create_write_subr(subrname, entity_name, parent, var, stmt)
                        self.state_created_subrs.append(subrname)
                else: # intrinsic type
                    if var.is_explicit_shape_array():
                        self.create_write_intrinsic(self.state_externs_subrs[node.kgen_parent][0], entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_write_intrinsic(self.state_externs_subrs[node.kgen_parent][1], entity_name, stmt, var)
                    else: # implicit array
                        self.create_write_call(self.state_externs_subrs[node.kgen_parent][0], subrname, entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_write_call(self.state_externs_subrs[node.kgen_parent][1], subrname, entity_name, stmt, var)
                        if subrname not in self.state_created_subrs:
                            create_write_subr(subrname, entity_name, parent, var, stmt)
                            self.state_created_subrs.append(subrname)
            else: # scalar
                if stmt.is_derived() or is_class_derived:
                    if var.is_allocatable() or var.is_pointer():
                        self.create_write_call(self.state_externs_subrs[node.kgen_parent][0], subrname, entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_write_call(self.state_externs_subrs[node.kgen_parent][1], subrname, entity_name, stmt, var)
                        if subrname not in self.state_created_subrs:
                            create_write_subr(subrname, entity_name, parent, var, stmt)
                            self.state_created_subrs.append(subrname)
                    else:
                        subrname = None
                        for uname, req in stmt.unknowns.iteritems():
                            if ( is_class_derived and uname.firstpartname()==stmt.selector[1]) or uname.firstpartname()==stmt.name:
                            #if uname.firstpartname()==stmt.name:
                                if len(req.res_stmts)>0:
                                    res = req.res_stmts[0]
                                    subrname = get_dtype_writename(res)
                                    break
                        if subrname is None:
                            print 'WARNING: Can not find Type resolver for %s'%stmt.name
                            namedpart_append_comment(self.state_externs_subrs[node.kgen_parent][0], EXEC_PART, \
                                'ERROR: "%s" is not resolved. Call statements to write "%s" is not created here.'%\
                                (stmt.name, stmt.name))
                        else:
                            self.create_write_call(self.state_externs_subrs[node.kgen_parent][0], subrname, entity_name, stmt, var)
                            if entity_name in out_entity_names:
                                self.create_write_call(self.state_externs_subrs[node.kgen_parent][1], subrname, entity_name, stmt, var)
                else: # intrinsic type
                    self.create_write_intrinsic(self.state_externs_subrs[node.kgen_parent][0], entity_name, stmt, var)
                    if entity_name in out_entity_names:
                        self.create_write_intrinsic(self.state_externs_subrs[node.kgen_parent][1], entity_name, stmt, var)
Esempio n. 12
0
    def create_subr_read_typedecl_in_module(self, node):

        parent = node.kgen_parent
        stmt = node.kgen_stmt
        raw_entity_names = set([ uname.firstpartname() for uname, req in KGGenType.get_state(stmt.geninfo)])
        entity_names = [ e for e in raw_entity_names if not stmt.get_variable(e).is_parameter() ]
        
        raw_out_entity_names = set([ uname.firstpartname() for uname, req in KGGenType.get_state_out(stmt.geninfo)])
        out_entity_names = [ e for e in raw_out_entity_names if not stmt.get_variable(e).is_parameter() ]

        def get_attrs(attrspec, allowed_attrs):
            attrspec = []
            for attr in stmt.attrspec:
                if any( attr.startswith(allowed_attr) for allowed_attr in allowed_attrs):
                    attrspec.append(attr)
            return attrspec

        def get_decls(names, decls, prefix=''):
            import re
            entity_decls = []
            for decl in decls:
                ename = re.split('\(|\*|=', decl)[0].strip()
                if ename in names:
                    entity_decls.append(prefix+decl)
            return entity_decls

        if len(entity_names)==0:
            node.kgen_forced_line = False
        elif len(entity_names)!=len(stmt.entity_decls):
            attrspec = get_attrs(stmt.attrspec, ['pointer', 'allocatable', 'dimension', 'public', 'target'])
            entity_decls = get_decls(entity_names, stmt.entity_decls)

            attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                'selector':stmt.selector, 'entity_decls': entity_decls}

            if stmt.is_derived():
                node.type_spec = 'TYPE'
            else:
                node.type_spec = stmt.__class__.__name__.upper()
            node.attrspec = attrspec
            node.selector = stmt.selector
            node.entity_decls = entity_decls
            node.kgen_use_tokgen = True
            #part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)
            #part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)

        if len(out_entity_names)>0:
            attrspec = get_attrs(stmt.attrspec, ['pointer', 'allocatable', 'dimension'])

            entity_decls = get_decls(out_entity_names, stmt.entity_decls, prefix='kgenref_')

            attrs = {'type_spec': stmt.__class__.__name__.upper(), 'attrspec': attrspec, \
                'selector':stmt.selector, 'entity_decls': entity_decls}
            part_append_genknode(node.kgen_parent, DECL_PART, stmt.__class__, attrs=attrs)

        is_class_derived = check_class_derived(stmt)

        for entity_name, entity_decl in zip(entity_names, stmt.entity_decls):
            if node.kgen_parent.name+entity_name in self.kernel_extern_reads: continue

            if is_remove_state(entity_name, stmt): continue

            self.kernel_extern_reads.append(node.kgen_parent.name+entity_name)

            var = stmt.get_variable(entity_name)
            subrname = get_typedecl_readname(stmt, entity_name)

            if var.is_array():
                if is_zero_array(var, stmt): continue
                if stmt.is_derived() or is_class_derived:
                    self.create_read_call(self.kernel_externs_subrs[node.kgen_parent][0], subrname, entity_name, stmt, var)
                    if entity_name in out_entity_names:
                        self.create_read_call(self.kernel_externs_subrs[node.kgen_parent][1], subrname, entity_name, stmt, var, prefix='kgenref_')
                    if subrname not in self.kernel_created_subrs:
                        create_read_subr(subrname, entity_name, parent, var, stmt)
                        self.kernel_created_subrs.append(subrname)
                else: # intrinsic type
                    if var.is_explicit_shape_array():
                        self.create_read_intrinsic(self.kernel_externs_subrs[node.kgen_parent][0], entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_read_intrinsic(self.kernel_externs_subrs[node.kgen_parent][1], entity_name, stmt, var, prefix='kgenref_')
                    else: # implicit array
                        self.create_read_call(self.kernel_externs_subrs[node.kgen_parent][0], subrname, entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_read_call(self.kernel_externs_subrs[node.kgen_parent][1], subrname, entity_name, stmt, var, prefix='kgenref_')
                        if subrname not in self.kernel_created_subrs:
                            create_read_subr(subrname, entity_name, parent, var, stmt)
                            self.kernel_created_subrs.append(subrname)
            else: # scalar
                if stmt.is_derived() or is_class_derived:
                    if var.is_allocatable() or var.is_pointer():
                        self.create_read_call(self.kernel_externs_subrs[node.kgen_parent][0], subrname, entity_name, stmt, var)
                        if entity_name in out_entity_names:
                            self.create_read_call(self.kernel_externs_subrs[node.kgen_parent][1], subrname, entity_name, stmt, var, prefix='kgenref_')
                        if subrname not in self.kernel_created_subrs:
                            create_read_subr(subrname, entity_name, parent, var, stmt)
                            self.kernel_created_subrs.append(subrname)
                    else:
                        subrname = None
                        for uname, req in stmt.unknowns.iteritems():
                            if ( is_class_derived and uname.firstpartname()==stmt.selector[1]) or uname.firstpartname()==stmt.name:
                            #if uname.firstpartname()==stmt.name:
                                if len(req.res_stmts)>0:
                                    res = req.res_stmts[0]
                                    subrname = get_dtype_readname(res)
                                    break
                        if subrname is None:
                            print 'WARNING: Can not find Type resolver for %s'%stmt.name
                            namedpart_append_comment(self.kernel_externs_subrs[node.kgen_parent][0], EXEC_PART, \
                                'ERROR: "%s" is not resolved. Call statements to read "%s" is not created here.'%\
                                (stmt.name, stmt.name))
                        else:
                            self.create_read_call(self.kernel_externs_subrs[node.kgen_parent][0], subrname, entity_name, stmt, var)
                            if entity_name in out_entity_names:
                                self.create_read_call(self.kernel_externs_subrs[node.kgen_parent][1], subrname, entity_name, stmt, var, prefix='kgenref_')
                else: # intrinsic type
                    self.create_read_intrinsic(self.kernel_externs_subrs[node.kgen_parent][0], entity_name, stmt, var)
                    if entity_name in out_entity_names:
                        self.create_read_intrinsic(self.kernel_externs_subrs[node.kgen_parent][1], entity_name, stmt, var, prefix='kgenref_')