Ejemplo n.º 1
0
def test_external_call(ctx_factory):
    cl_ctx = ctx_factory()
    queue = cl.CommandQueue(cl_ctx)
    actx = PyOpenCLArrayContext(queue)

    def double(queue, x):
        return 2 * x

    from meshmode.mesh.generation import generate_regular_rect_mesh

    dims = 2

    mesh = generate_regular_rect_mesh(a=(0, ) * dims,
                                      b=(1, ) * dims,
                                      n=(4, ) * dims)
    discr = DGDiscretizationWithBoundaries(actx, mesh, order=1)

    ones = sym.Ones(sym.DD_VOLUME)
    op = (ones * 3 + sym.FunctionSymbol("double")(ones))

    from grudge.function_registry import (base_function_registry,
                                          register_external_function)

    freg = register_external_function(base_function_registry,
                                      "double",
                                      implementation=double,
                                      dd=sym.DD_VOLUME)

    bound_op = bind(discr, op, function_registry=freg)

    result = bound_op(actx, double=double)
    assert actx.to_numpy(flatten(result) == 5).all()
Ejemplo n.º 2
0
def test_external_call(actx_factory):
    actx = actx_factory()

    def double(queue, x):
        return 2 * x

    dims = 2

    mesh = mgen.generate_regular_rect_mesh(a=(0, ) * dims,
                                           b=(1, ) * dims,
                                           nelements_per_axis=(4, ) * dims)
    discr = DiscretizationCollection(actx, mesh, order=1)

    ones = sym.Ones(dof_desc.DD_VOLUME)
    op = (ones * 3 + sym.FunctionSymbol("double")(ones))

    from grudge.function_registry import (base_function_registry,
                                          register_external_function)

    freg = register_external_function(base_function_registry,
                                      "double",
                                      implementation=double,
                                      dd=dof_desc.DD_VOLUME)

    bound_op = bind(discr, op, function_registry=freg)

    result = bound_op(actx, double=double)
    assert actx.to_numpy(flatten(result) == 5).all()
Ejemplo n.º 3
0
    def __init__(self,
                 queue,
                 discr,
                 field_var_name,
                 grudge_bound_op,
                 num_fields,
                 component_getter,
                 exec_mapper_factory=ExecutionMapper):
        """Arguments:

            field_var_name: The name of the simulation variable

            grudge_bound_op: The BoundExpression for the right-hand side

            num_fields: The number of components in the simulation variable

            component_getter: A function, which, given an object array
               representing the simulation variable, splits the array into
               its components

        """
        super().__init__(queue, component_getter)

        # Construct sym_rhs to have the effect of replacing the RHS calls in the
        # dagrt code with calls of the grudge operator.
        from grudge.symbolic.primitives import FunctionSymbol, Variable
        call = sym.cse(
            FunctionSymbol("grudge_op")(
                *((Variable("t", dd=sym.DD_SCALAR), ) + tuple(
                    Variable(field_var_name, dd=sym.DD_VOLUME)[i]
                    for i in range(num_fields)))))

        from pytools.obj_array import join_fields
        sym_rhs = join_fields(*(call[i] for i in range(num_fields)))

        self.queue = queue
        self.grudge_bound_op = grudge_bound_op

        from grudge.function_registry import register_external_function

        freg = register_external_function(base_function_registry,
                                          "grudge_op",
                                          implementation=self._bound_op,
                                          dd=sym.DD_VOLUME)

        self.set_up_stepper(discr, field_var_name, sym_rhs, num_fields, freg,
                            exec_mapper_factory)

        self.component_getter = component_getter