Esempio n. 1
0
def compile_element(elements,
                    prefix="Element",
                    parameters=default_parameters()):
    """This function generates UFC code for a given UFL element or
    list of UFL elements."""

    info("Compiling element %s\n" % prefix)

    # Reset timing
    cpu_time_0 = time()

    # Check input arguments
    elements = _check_elements(elements)
    parameters = _check_parameters(parameters)
    if not elements: return

    # Stage 1: analysis
    cpu_time = time()
    analysis = analyze_elements(elements, parameters)
    _print_timing(1, time() - cpu_time)

    # Stage 2: intermediate representation
    cpu_time = time()
    ir = compute_ir(analysis, parameters)
    _print_timing(2, time() - cpu_time)

    # Stage 3: optimization
    cpu_time = time()
    oir = optimize_ir(ir, parameters)
    _print_timing(3, time() - cpu_time)

    # Stage 4: code generation
    cpu_time = time()
    code = generate_code(oir, prefix, parameters)
    _print_timing(4, time() - cpu_time)

    # Stage 4.1: generate wrappers
    cpu_time = time()
    object_names = {}
    wrapper_code = generate_wrapper_code(analysis, prefix, object_names,
                                         parameters)
    _print_timing(4.1, time() - cpu_time)

    # Stage 5: format code
    cpu_time = time()
    format_code(code, wrapper_code, prefix, parameters)
    _print_timing(5, time() - cpu_time)

    info_green("FFC finished in %g seconds.", time() - cpu_time_0)
Esempio n. 2
0
def compile_form(forms, object_names={}, prefix="Form",\
                 parameters=default_parameters()):
    """This function generates UFC code for a given UFL form or list
    of UFL forms."""

    info("Compiling form %s\n" % prefix)

    # Reset timing
    cpu_time_0 = time()

    # Check input arguments
    forms = _check_forms(forms)
    parameters = _check_parameters(parameters)
    if not forms: return

    # Stage 1: analysis
    cpu_time = time()
    analysis = analyze_forms(forms, object_names, parameters)
    _print_timing(1, time() - cpu_time)

    # Stage 2: intermediate representation
    cpu_time = time()
    ir = compute_ir(analysis, parameters)
    _print_timing(2, time() - cpu_time)

    # Stage 3: optimization
    cpu_time = time()
    oir = optimize_ir(ir, parameters)
    _print_timing(3, time() - cpu_time)

    # Stage 4: code generation
    cpu_time = time()
    code = generate_code(oir, prefix, parameters)
    _print_timing(4, time() - cpu_time)

    # Stage 4.1: generate wrappers
    cpu_time = time()
    wrapper_code = generate_wrapper_code(analysis, prefix, parameters)
    _print_timing(4.1, time() - cpu_time)

    # Stage 5: format code
    cpu_time = time()
    format_code(code, wrapper_code, prefix, parameters)
    _print_timing(5, time() - cpu_time)

    info_green("FFC finished in %g seconds.", time() - cpu_time_0)
Esempio n. 3
0
def compute_tensor_representation(form):
    """Compute tensor representation for given form. This function may
    be useful for those (Hi Matt!) that want to access the FFC tensor
    representation from outside FFC."""

    # Set parameters
    parameters = default_parameters()
    parameters["representation"] = "tensor"
    #parameters["optimize"] = "optimize"

    # The below steps basically duplicate the compiler process but
    # skip the code formatting step. Instead, we extract the relevant
    # portions for tabulate_tensor.

    # Stage 1: analysis
    cpu_time = time()
    analysis = analyze_forms([form], {}, parameters)
    _print_timing(1, time() - cpu_time)

    # Stage 2: intermediate representation
    cpu_time = time()
    ir = compute_ir(analysis, parameters)
    _print_timing(2, time() - cpu_time)

    # Stage 3: optimization
    cpu_time = time()
    oir = optimize_ir(ir, parameters)
    _print_timing(3, time() - cpu_time)

    # Stage 4: code generation
    cpu_time = time()
    code = generate_code(oir, "foo", parameters)
    _print_timing(4, time() - cpu_time)

    # Extract representations
    ir_elements, ir_dofmaps, ir_integrals, ir_forms = ir

    # Extract entries in reference tensor
    reference_tensors = []
    for i in ir_integrals:
        if i["integral_type"] == "cell":
            t = [A0.A0 for (A0, GK, dummy) in i["AK"]]
            if len(t) == 1: t = t[0]
        elif i["integral_type"] == "exterior_facet":
            t = [A0.A0 for j in i["AK"] for (A0, GK, dummy) in j]
            if len(t) == 1: t = t[0]
        elif i["integral_type"] == "interior_facet":
            t = [A0.A0 for j in i["AK"] for k in j for (A0, GK, dummy) in k]
            if len(t) == 1: t = t[0]
        else:
            raise RuntimeError("Unhandled domain type: %s" % str(i["integral_type"]))
        reference_tensors.append(t)

    # Extract code
    code_elements, code_dofmaps, code_integrals, code_forms = code

    # Extract code for computing the geometry tensor
    geometry_tensor_codes = [c["tabulate_tensor"].split("// Compute element tensor")[0] for c in code_integrals]

    # Simplify return values when there is just one term
    if len(reference_tensors) == 1:
        reference_tensors = reference_tensors[0]
    if len(geometry_tensor_codes) == 1:
        geometry_tensor_codes = geometry_tensor_codes[0]

    return reference_tensors, geometry_tensor_codes
Esempio n. 4
0
def compile_ufl_objects(ufl_objects, kind, object_names=None,
                        prefix=None, parameters=None, jit=False):
    """This function generates UFC code for a given UFL form or list of UFL forms."""
    info("Compiling %s %s\n" % (kind, prefix))

    # Reset timing
    cpu_time_0 = time()

    # Note that jit will always pass validated parameters so 
    # this is only for commandline and direct call from python
    if not jit:
        parameters = validate_parameters(parameters)

    # Check input arguments
    if not isinstance(ufl_objects, (list, tuple)):
        ufl_objects = (ufl_objects,)
    if not ufl_objects:
        return "", ""
    if prefix != os.path.basename(prefix):
        error("Invalid prefix, looks like a full path? prefix='{}'.".format(prefix))
    if object_names is None:
        object_names = {}

    # Stage 1: analysis
    cpu_time = time()
    analysis = analyze_ufl_objects(ufl_objects, kind, parameters)
    _print_timing(1, time() - cpu_time)

    # Stage 2: intermediate representation
    cpu_time = time()
    ir = compute_ir(analysis, prefix, parameters, jit)
    _print_timing(2, time() - cpu_time)

    # Stage 3: optimization
    cpu_time = time()
    oir = optimize_ir(ir, parameters)
    _print_timing(3, time() - cpu_time)

    # Stage 4: code generation
    cpu_time = time()
    code = generate_code(oir, parameters)
    _print_timing(4, time() - cpu_time)

    # Stage 4.1: generate wrappers
    cpu_time = time()
    wrapper_code = generate_wrapper_code(analysis, prefix, object_names, parameters)
    _print_timing(4.1, time() - cpu_time)

    # Stage 5: format code
    cpu_time = time()
    code_h, code_c = format_code(code, wrapper_code, prefix, parameters, jit)
    _print_timing(5, time() - cpu_time)

    info_green("FFC finished in %g seconds.", time() - cpu_time_0)

    if jit:
        # Must use processed elements from analysis here
        form_datas, unique_elements, element_numbers, unique_coordinate_elements = analysis

        # Wrap coordinate elements in Mesh object to represent that
        # we want a ufc::coordinate_mapping not a ufc::finite_element
        unique_meshes = [ufl.Mesh(element, ufl_id=0)
                         for element in unique_coordinate_elements]

        # Avoid returning self as dependency for infinite recursion
        unique_elements = tuple(element for element in unique_elements
                                if element not in ufl_objects)
        unique_meshes = tuple(mesh for mesh in unique_meshes
                              if mesh not in ufl_objects)

        # Setup dependencies (these will be jitted before continuing to compile ufl_objects)
        dependent_ufl_objects = {
            "element": unique_elements,
            "coordinate_mapping": unique_meshes,
            }
        return code_h, code_c, dependent_ufl_objects
    else:
        return code_h, code_c
Esempio n. 5
0
def compute_tensor_representation(form):
    """Compute tensor representation for given form. This function may
    be useful for those (Hi Matt!) that want to access the FFC tensor
    representation from outside FFC."""

    # Set parameters
    parameters = default_parameters()
    parameters["representation"] = "tensor"
    #parameters["optimize"] = "optimize"

    # The below steps basically duplicate the compiler process but
    # skip the code formatting step. Instead, we extract the relevant
    # portions for tabulate_tensor.

    # Stage 1: analysis
    cpu_time = time()
    analysis = analyze_forms([form], {}, parameters)
    _print_timing(1, time() - cpu_time)

    # Stage 2: intermediate representation
    cpu_time = time()
    ir = compute_ir(analysis, parameters)
    _print_timing(2, time() - cpu_time)

    # Stage 3: optimization
    cpu_time = time()
    oir = optimize_ir(ir, parameters)
    _print_timing(3, time() - cpu_time)

    # Stage 4: code generation
    cpu_time = time()
    code = generate_code(oir, "foo", parameters)
    _print_timing(4, time() - cpu_time)

    # Extract representations
    ir_elements, ir_dofmaps, ir_integrals, ir_forms = ir

    # Extract entries in reference tensor
    reference_tensors = []
    for i in ir_integrals:
        if i["integral_type"] == "cell":
            t = [A0.A0 for (A0, GK, dummy) in i["AK"]]
            if len(t) == 1: t = t[0]
        elif i["integral_type"] == "exterior_facet":
            t = [A0.A0 for j in i["AK"] for (A0, GK, dummy) in j]
            if len(t) == 1: t = t[0]
        elif i["integral_type"] == "interior_facet":
            t = [A0.A0 for j in i["AK"] for k in j for (A0, GK, dummy) in k]
            if len(t) == 1: t = t[0]
        else:
            raise RuntimeError("Unhandled domain type: %s" %
                               str(i["integral_type"]))
        reference_tensors.append(t)

    # Extract code
    code_elements, code_dofmaps, code_integrals, code_forms = code

    # Extract code for computing the geometry tensor
    geometry_tensor_codes = [
        c["tabulate_tensor"].split("// Compute element tensor")[0]
        for c in code_integrals
    ]

    # Simplify return values when there is just one term
    if len(reference_tensors) == 1:
        reference_tensors = reference_tensors[0]
    if len(geometry_tensor_codes) == 1:
        geometry_tensor_codes = geometry_tensor_codes[0]

    return reference_tensors, geometry_tensor_codes
Esempio n. 6
0
def compile_form(forms, object_names=None, prefix="Form", parameters=None):
    """This function generates UFC code for a given UFL form or list
    of UFL forms."""

    info("Compiling form %s\n" % prefix)

    # Reset timing
    cpu_time_0 = time()

    # Check input arguments
    forms = _check_forms(forms)
    if not forms:
        return
    if prefix != os.path.basename(prefix):
        prefix = os.path.basename(prefix)
        warning("Invalid prefix, modified to {}.".format(prefix))
    if object_names is None:
        object_names = {}
    parameters = _check_parameters(parameters)

    # Stage 1: analysis
    cpu_time = time()
    analysis = analyze_forms(forms, parameters)
    _print_timing(1, time() - cpu_time)

    # Stage 2: intermediate representation
    cpu_time = time()
    ir = compute_ir(analysis, prefix, parameters, object_names=object_names)
    _print_timing(2, time() - cpu_time)

    # Stage 3: optimization
    cpu_time = time()
    oir = optimize_ir(ir, parameters)
    _print_timing(3, time() - cpu_time)

    # Return IR (PyOP2 mode) or code string (otherwise)
    if parameters["pyop2-ir"]:
        try:
            from ffc.quadrature.quadraturepyop2ir import generate_pyop2_ir
        except ImportError:
            raise ImportError("Format pyop2-ir depends on PyOP2, which is not available.")
        # Stage 4: build PyOP2 intermediate representation

        cpu_time = time()
        #FIXME: need a cleaner interface
        pyop2_ir = [generate_pyop2_ir(ir, prefix, parameters) for ir in oir[3]]
        _print_timing(4, time() - cpu_time)

        info_green("FFC finished in %g seconds.", time() - cpu_time_0)
        return pyop2_ir

    else:
        # Stage 4: code generation
        cpu_time = time()
        code = generate_code(oir, prefix, parameters)
        _print_timing(4, time() - cpu_time)

        # Stage 4.1: generate wrappers
        cpu_time = time()
        wrapper_code = generate_wrapper_code(analysis, prefix, object_names, parameters)
        _print_timing(4.1, time() - cpu_time)

        # Stage 5: format code
        cpu_time = time()
        code_h, code_c = format_code(code, wrapper_code, prefix, parameters)
        write_code(code_h, code_c, prefix, parameters) # FIXME: Don't write to file in this function (issue #72)
        _print_timing(5, time() - cpu_time)

        info_green("FFC finished in %g seconds.", time() - cpu_time_0)

        return code
Esempio n. 7
0
def compile_ufl_objects(ufl_objects,
                        kind,
                        object_names=None,
                        prefix=None,
                        parameters=None,
                        jit=False):
    """This function generates UFC code for a given UFL form or list of UFL forms."""
    info("Compiling %s %s\n" % (kind, prefix))

    # Reset timing
    cpu_time_0 = time()

    # Note that jit will always pass validated parameters so
    # this is only for commandline and direct call from python
    if not jit:
        parameters = validate_parameters(parameters)

    # Check input arguments
    if not isinstance(ufl_objects, (list, tuple)):
        ufl_objects = (ufl_objects, )
    if not ufl_objects:
        return "", ""
    if prefix != os.path.basename(prefix):
        error("Invalid prefix, looks like a full path? prefix='{}'.".format(
            prefix))
    if object_names is None:
        object_names = {}

    # Stage 1: analysis
    cpu_time = time()
    analysis = analyze_ufl_objects(ufl_objects, kind, parameters)
    _print_timing(1, time() - cpu_time)

    # Stage 2: intermediate representation
    cpu_time = time()
    ir = compute_ir(analysis, prefix, parameters, jit)
    _print_timing(2, time() - cpu_time)

    # Stage 3: optimization
    cpu_time = time()
    oir = optimize_ir(ir, parameters)
    _print_timing(3, time() - cpu_time)

    # Stage 4: code generation
    cpu_time = time()
    code = generate_code(oir, parameters)
    _print_timing(4, time() - cpu_time)

    # Stage 4.1: generate wrappers
    cpu_time = time()
    wrapper_code = generate_wrapper_code(analysis, prefix, object_names,
                                         parameters)
    _print_timing(4.1, time() - cpu_time)

    # Stage 5: format code
    cpu_time = time()
    code_h, code_c = format_code(code, wrapper_code, prefix, parameters, jit)
    _print_timing(5, time() - cpu_time)

    info_green("FFC finished in %g seconds.", time() - cpu_time_0)

    if jit:
        # Must use processed elements from analysis here
        form_datas, unique_elements, element_numbers, unique_coordinate_elements = analysis

        # Wrap coordinate elements in Mesh object to represent that
        # we want a ufc::coordinate_mapping not a ufc::finite_element
        unique_meshes = [
            ufl.Mesh(element, ufl_id=0)
            for element in unique_coordinate_elements
        ]

        # Avoid returning self as dependency for infinite recursion
        unique_elements = tuple(element for element in unique_elements
                                if element not in ufl_objects)
        unique_meshes = tuple(mesh for mesh in unique_meshes
                              if mesh not in ufl_objects)

        # Setup dependencies (these will be jitted before continuing to compile ufl_objects)
        dependent_ufl_objects = {
            "element": unique_elements,
            "coordinate_mapping": unique_meshes,
        }
        return code_h, code_c, dependent_ufl_objects
    else:
        return code_h, code_c