Example #1
0
    def visit_Operator(self, o):
        # Kernel signature and body
        body = flatten(self._visit(i) for i in o.children)
        decls = self._args_decl(o.parameters)
        signature = c.FunctionDeclaration(c.Value(o.retval, o.name), decls)
        retval = [c.Statement("return 0")]
        kernel = c.FunctionBody(signature, c.Block(body + retval))

        # Elemental functions
        esigns = []
        efuncs = [blankline]
        for i in o._func_table.values():
            if i.local:
                esigns.append(
                    c.FunctionDeclaration(c.Value(i.root.retval, i.root.name),
                                          self._args_decl(i.root.parameters)))
                efuncs.extend([i.root.ccode, blankline])

        # Header files, extra definitions, ...
        header = [c.Line(i) for i in o._headers]
        includes = [c.Include(i, system=False) for i in o._includes]
        includes += [blankline]
        cglobals = list(o._globals)
        if o._compiler.src_ext == 'cpp':
            cglobals += [c.Extern('C', signature)]
        cglobals = [i for j in cglobals for i in (j, blankline)]

        return c.Module(header + includes + cglobals + esigns +
                        [blankline, kernel] + efuncs)
Example #2
0
 def execute_function_signature(self):
     return cgen.Extern(
         "C",
         cgen.FunctionDeclaration(cgen.Value('int', 'opesci_execute'), [
             cgen.Pointer(cgen.Value(self._grid_structure_name, "grid")),
             cgen.Pointer(
                 cgen.Value(self._profiling_structure_name, "profiling"))
         ]))
Example #3
0
 def copy_initialise_function(self):
     statements = cgen.Block(self.grid.copy_memory)
     return cgen.FunctionBody(
         cgen.Extern(
             "C",
             cgen.FunctionDeclaration(
                 cgen.Value('void', 'copy_initialise'),
                 [cgen.Pointer(cgen.Value(self.grid.real_t, 'data'))])),
         statements)
Example #4
0
 def freemem(self):
     statements = []
     statements.append(self.grid.free_memory)
     statements.append(cgen.Statement("return 0"))
     return cgen.FunctionBody(
         cgen.Extern(
             "C",
             cgen.FunctionDeclaration(cgen.Value('int', 'opesci_free'), [
                 cgen.Pointer(cgen.Value(self._grid_structure_name, "grid"))
             ])), cgen.Block(statements))
Example #5
0
 def convergence_function(self):
     statements = []
     statements.append(self.grid.define_constants)
     statements.append(self.grid.load_fields)
     statements.append(self.grid.converge_test)
     statements.append(cgen.Statement("return 0"))
     return cgen.FunctionBody(
         cgen.Extern(
             "C",
             cgen.FunctionDeclaration(
                 cgen.Value('int', 'opesci_convergence'), [
                     cgen.Pointer(
                         cgen.Value(self._grid_structure_name, "grid")),
                     cgen.Pointer(
                         cgen.Value(self.__convergence_structure_name,
                                    "conv"))
                 ])), cgen.Block(statements))
Example #6
0
    def visit_Operator(self, o):
        blankline = c.Line("")

        # Kernel signature and body
        body = flatten(self._visit(i) for i in o.children)
        decls = self._args_decl(o.parameters)
        signature = c.FunctionDeclaration(c.Value(o.retval, o.name), decls)
        retval = [c.Line(), c.Statement("return 0")]
        kernel = c.FunctionBody(signature, c.Block(body + retval))

        # Elemental functions
        esigns = []
        efuncs = [blankline]
        for i in o._func_table.values():
            if i.local:
                esigns.append(
                    c.FunctionDeclaration(c.Value(i.root.retval, i.root.name),
                                          self._args_decl(i.root.parameters)))
                efuncs.extend([i.root.ccode, blankline])

        # Header files, extra definitions, ...
        header = [c.Define(*i) for i in o._headers] + [blankline]
        includes = [
            c.Include(i, system=(False if i.endswith('.h') else True))
            for i in o._includes
        ]
        includes += [blankline]
        cdefs = [
            i._C_typedecl for i in o.parameters if i._C_typedecl is not None
        ]
        for i in o._func_table.values():
            if i.local:
                cdefs.extend([
                    j._C_typedecl for j in i.root.parameters
                    if j._C_typedecl is not None
                ])
        cdefs = filter_sorted(cdefs, key=lambda i: i.tpname)
        if o._compiler.src_ext == 'cpp':
            cdefs += [c.Extern('C', signature)]
        cdefs = [i for j in cdefs for i in (j, blankline)]

        return c.Module(header + includes + cdefs + esigns +
                        [blankline, kernel] + efuncs)
Example #7
0
    def ccode(self):
        """Returns the C code generated by this kernel.

        This function generates the internal code block from Iteration
        and Expression objects, and adds the necessary template code
        around it.
        """
        header_vars = [
            c.Pointer(c.POD(v.dtype, '%s_vec' % v.name))
            for v in self.signature
        ]
        header = c.Extern(
            "C", c.FunctionDeclaration(c.Value('int', self.name), header_vars))
        cast_shapes = [(v, ''.join(['[%d]' % d for d in v.shape[1:]]))
                       for v in self.signature]
        casts = [
            c.Initializer(
                c.POD(v.dtype, '(*%s)%s' % (v.name, shape)), '(%s (*)%s) %s' %
                (c.dtype_to_ctype(v.dtype), shape, '%s_vec' % v.name))
            for v, shape in cast_shapes
        ]
        body = [e.ccode for e in self.expressions]
        ret = [c.Statement("return 0")]
        return c.FunctionBody(header, c.Block(casts + body + ret))
Example #8
0
    def visit_Operator(self, o):
        # Kernel signature and body
        body = flatten(self.visit(i) for i in o.children)
        params = runtime_arguments(o.parameters)
        decls = self._args_decl(params)
        casts = self._args_cast(params)
        signature = c.FunctionDeclaration(c.Value(o.retval, o.name), decls)
        retval = [c.Statement("return 0")]
        kernel = c.FunctionBody(signature, c.Block(casts + body + retval))

        # Elemental functions
        efuncs = [i.root.ccode
                  for i in o.func_table.values() if i.local] + [blankline]

        # Header files, extra definitions, ...
        header = [c.Line(i) for i in o._headers]
        includes = [c.Include(i, system=False) for i in o._includes]
        includes += [blankline]
        cglobals = list(o._globals)
        if o._compiler.src_ext == 'cpp':
            cglobals += [c.Extern('C', signature)]
        cglobals = [i for j in cglobals for i in (j, blankline)]

        return c.Module(header + includes + cglobals + efuncs + [kernel])
Example #9
0
 def profiling_function(self):
     return cgen.Extern(
         "C",
         cgen.Struct(self._profiling_structure_name,
                     [self.grid.define_profiling]))
Example #10
0
 def convergence_structure(self):
     return cgen.Extern(
         "C",
         cgen.Struct(self.__convergence_structure_name,
                     [self.grid.define_convergence]))
Example #11
0
 def grid_structure(self):
     return cgen.Extern(
         "C",
         cgen.Struct(self._grid_structure_name, [self.grid.define_fields]))