コード例 #1
0
ファイル: expressions.py プロジェクト: michalhabera/ffcx
def generator(ir, parameters):
    """Generate UFC code for an expression."""

    logger.info("Generating code for expression:")
    logger.info("--- points: {}".format(ir.points))
    logger.info("--- name: {}".format(ir.name))

    factory_name = ir.name

    # Format declaration
    declaration = expressions_template.declaration.format(
        factory_name=factory_name)

    backend = FFCXBackend(ir, parameters)
    eg = ExpressionGenerator(ir, backend)

    code = {}
    code["name"] = "{}_expression".format(ir.name)
    parts = eg.generate()

    body = format_indented_lines(parts.cs_format(), 1)
    code["tabulate_expression"] = body

    code["original_coefficient_positions"] = format_indented_lines(
        eg.generate_original_coefficient_positions().cs_format(), 1)

    code["points"] = format_indented_lines(eg.generate_points().cs_format(), 1)
    code["value_shape"] = format_indented_lines(
        eg.generate_value_shape().cs_format(), 1)

    # Format implementation code
    implementation = expressions_template.factory.format(
        factory_name=factory_name,
        tabulate_expression=code["tabulate_expression"],
        original_coefficient_positions=code["original_coefficient_positions"],
        num_coefficients=len(ir.coefficient_numbering),
        num_points=ir.points.shape[0],
        topological_dimension=ir.points.shape[1],
        num_components=len(ir.expression_shape),
        points=code["points"],
        value_shape=code["value_shape"])

    return declaration, implementation
コード例 #2
0
ファイル: cnodes.py プロジェクト: jpdean/ffcx
 def __str__(self):
     try:
         s = self.cs_format()
     except Exception:
         if CNode.debug:
             logger.error("Error in CStatement string formatting. Inspect self.")
             import IPython
             IPython.embed()
         raise
     return format_indented_lines(s)
コード例 #3
0
ファイル: integrals.py プロジェクト: jpdean/ffcx
def generator(ir, parameters):
    logger.info("Generating code for integral:")
    logger.info(f"--- type: {ir.integral_type}")
    logger.info(f"--- name: {ir.name}")
    """Generate code for an integral."""
    factory_name = ir.name
    integral_type = ir.integral_type

    # Format declaration
    if integral_type == "custom":
        declaration = ufc_integrals.custom_declaration.format(
            factory_name=factory_name)
    else:
        declaration = ufc_integrals.declaration.format(
            factory_name=factory_name)

    # Create FFCx C backend
    backend = FFCXBackend(ir, parameters)

    # Configure kernel generator
    ig = IntegralGenerator(ir, backend)

    # Generate code ast for the tabulate_tensor body
    parts = ig.generate()

    # Format code as string
    body = format_indented_lines(parts.cs_format(ir.precision), 1)

    # Generate generic FFCx code snippets and add specific parts
    code = {}
    code["class_type"] = ir.integral_type + "_integral"
    code["name"] = ir.name
    code["members"] = ""
    code["constructor"] = ""
    code["constructor_arguments"] = ""
    code["initializer_list"] = ""
    code["destructor"] = ""

    L = backend.language
    if len(ir.enabled_coefficients) > 0:
        code["enabled_coefficients_init"] = L.ArrayDecl(
            "bool",
            f"enabled_coefficients_{ir.name}",
            values=ir.enabled_coefficients,
            sizes=len(ir.enabled_coefficients))
        code["enabled_coefficients"] = f"enabled_coefficients_{ir.name}"
    else:
        code["enabled_coefficients_init"] = ""
        code["enabled_coefficients"] = L.Null()

    code["additional_includes_set"] = set()  # FIXME: Get this out of code[]
    code["tabulate_tensor"] = body

    if parameters["tabulate_tensor_void"]:
        code["tabulate_tensor"] = ""

    implementation = ufc_integrals.factory.format(
        factory_name=factory_name,
        enabled_coefficients=code["enabled_coefficients"],
        enabled_coefficients_init=code["enabled_coefficients_init"],
        tabulate_tensor=code["tabulate_tensor"],
        needs_facet_permutations="true"
        if ir.needs_facet_permutations else "false")

    return declaration, implementation
コード例 #4
0
ファイル: integrals.py プロジェクト: mrambausek/ffcx
def generator(ir, parameters):

    logger.info("Generating code for integral:")
    logger.info("--- type: {}".format(ir.integral_type))
    logger.info("--- name: {}".format(ir.name))
    """Generate code for an integral."""
    factory_name = ir.name
    integral_type = ir.integral_type

    # Format declaration
    if integral_type == "custom":
        declaration = ufc_integrals.custom_declaration.format(
            factory_name=factory_name)
    else:
        declaration = ufc_integrals.declaration.format(
            factory_name=factory_name)

    # Create FFCX C backend
    backend = FFCXBackend(ir, parameters)

    # Configure kernel generator
    ig = IntegralGenerator(ir, backend)

    # Generate code ast for the tabulate_tensor body
    parts = ig.generate()

    # Format code as string
    body = format_indented_lines(parts.cs_format(ir.precision), 1)

    # Generate generic ffcx code snippets and add specific parts
    code = {}
    code["class_type"] = ir.integral_type + "_integral"
    code["name"] = ir.name
    code["members"] = ""
    code["constructor"] = ""
    code["constructor_arguments"] = ""
    code["initializer_list"] = ""
    code["destructor"] = ""

    # TODO: I don't know how to implement this using the format dict,
    # this will do for now:
    initializer_list = ", ".join("true" if enabled else "false"
                                 for enabled in ir.enabled_coefficients)
    if ir.enabled_coefficients:
        enabled_coeffs_code = '\n'.join([
            "[{}] = {{ {} }};".format(len(ir.enabled_coefficients),
                                      initializer_list)
        ])
    else:
        enabled_coeffs_code = "[1] = {false};  /* No coefficients, but C does not permit zero-sized arrays */"

    code["enabled_coefficients"] = enabled_coeffs_code
    code["additional_includes_set"] = set()  # FIXME: Get this out of code[]
    code["tabulate_tensor"] = body

    if parameters["tabulate_tensor_void"]:
        code["tabulate_tensor"] = ""

    # Format tabulate tensor body
    tabulate_tensor_declaration = ufc_integrals.tabulate_implementation[
        integral_type]
    tabulate_tensor_fn = tabulate_tensor_declaration.format(
        factory_name=factory_name, tabulate_tensor=code["tabulate_tensor"])

    # Format implementation code

    if integral_type == "custom":
        implementation = ufc_integrals.custom_factory.format(
            factory_name=factory_name,
            enabled_coefficients=code["enabled_coefficients"],
            tabulate_tensor=tabulate_tensor_fn,
            needs_permutation_data=ir.needs_permutation_data)
    else:
        implementation = ufc_integrals.factory.format(
            factory_name=factory_name,
            enabled_coefficients=code["enabled_coefficients"],
            tabulate_tensor=tabulate_tensor_fn,
            needs_permutation_data=ir.needs_permutation_data)

    return declaration, implementation
コード例 #5
0
ファイル: expressions.py プロジェクト: jpdean/ffcx
def generator(ir, parameters):
    """Generate UFC code for an expression."""
    logger.info("Generating code for expression:")
    logger.info(f"--- points: {ir.points}")
    logger.info(f"--- name: {ir.name}")

    factory_name = ir.name

    # Format declaration
    declaration = expressions_template.declaration.format(
        factory_name=factory_name)

    backend = FFCXBackend(ir, parameters)
    L = backend.language
    eg = ExpressionGenerator(ir, backend)

    d = {}
    d["factory_name"] = ir.name
    parts = eg.generate()

    body = format_indented_lines(parts.cs_format(), 1)
    d["tabulate_expression"] = body

    if len(ir.original_coefficient_positions) > 0:
        d["original_coefficient_positions"] = f"original_coefficient_positions_{ir.name}"
        d["original_coefficient_positions_init"] = L.ArrayDecl(
            "static int",
            f"original_coefficient_positions_{ir.name}",
            values=ir.original_coefficient_positions,
            sizes=len(ir.original_coefficient_positions))
    else:
        d["original_coefficient_positions"] = L.Null()
        d["original_coefficient_positions_init"] = ""

    d["points_init"] = L.ArrayDecl("static double",
                                   f"points_{ir.name}",
                                   values=ir.points.flatten(),
                                   sizes=ir.points.size)
    d["points"] = L.Symbol(f"points_{ir.name}")

    if len(ir.expression_shape) > 0:
        d["value_shape_init"] = L.ArrayDecl("static int",
                                            f"value_shape_{ir.name}",
                                            values=ir.expression_shape,
                                            sizes=len(ir.expression_shape))
        d["value_shape"] = f"value_shape_{ir.name}"
    else:
        d["value_shape_init"] = ""
        d["value_shape"] = L.Null()

    d["num_components"] = len(ir.expression_shape)
    d["num_coefficients"] = len(ir.coefficient_numbering)
    d["num_points"] = ir.points.shape[0]
    d["topological_dimension"] = ir.points.shape[1]
    d["needs_facet_permutations"] = "true" if ir.needs_facet_permutations else "false"

    # Check that no keys are redundant or have been missed
    from string import Formatter
    fields = [
        fname
        for _, fname, _, _ in Formatter().parse(expressions_template.factory)
        if fname
    ]

    assert set(fields) == set(
        d.keys()), "Mismatch between keys in template and in formattting dict"

    # Format implementation code
    implementation = expressions_template.factory.format_map(d)

    return declaration, implementation