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 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. 4
0
def compile_ufl_objects(ufl_objects: typing.Union[typing.List, typing.Tuple],
                        object_names: typing.Dict = {},
                        prefix: str = None,
                        parameters: typing.Dict = None):
    """Generate UFC code for a given UFL objects.

    Parameters
    ----------
    @param ufl_objects:
        Objects to be compiled. Accepts elements, forms, integrals or coordinate mappings.

    """
    logger.info("Compiling {}\n".format(prefix))
    if prefix != os.path.basename(prefix):
        raise RuntimeError(
            "Invalid prefix, looks like a full path? prefix='{}'.".format(
                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
    parameters = validate_parameters(parameters)

    # Convert single input arguments to tuple. All types should be the same.
    if not isinstance(ufl_objects, (list, tuple)):
        ufl_objects = (ufl_objects, )

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

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

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

    # Stage 3.1: generate convenience wrappers, e.g. for DOLFIN
    cpu_time = time()

    # FIXME: Simplify and make robist w.r.t. naming
    # Extract class names from the IR and add to a dict
    # ir_finite_elements, ir_dofmaps, ir_coordinate_mappings, ir_integrals, ir_forms = ir
    if len(object_names) > 0:
        classnames = defaultdict(list)
        comp = ["elements", "dofmaps", "coordinate_maps", "integrals", "forms"]
        for ir_comp, e_name in zip(ir, comp):
            try:
                for e in ir_comp:
                    classnames[e_name].append(e["classname"])
            except TypeError:
                for e in ir_comp:
                    classnames[e_name].append(e.classname)
        wrapper_code = generate_wrapper_code(analysis, prefix, object_names,
                                             classnames, parameters)
    else:
        wrapper_code = None

    _print_timing(4.1, time() - cpu_time)

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

    logger.info("FFC finished in {} seconds.".format(time() - cpu_time_0))

    return code_h, code_c
Esempio n. 5
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. 6
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