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)
def _check_parameters(parameters): "Initial check of parameters." if parameters is None: parameters = default_parameters() if "blas" in parameters: warning("BLAS mode unavailable (will return in a future version).") if "quadrature_points" in parameters: warning("Option 'quadrature_points' has been replaced by 'quadrature_degree'.") # HACK import os r = os.environ.get("FFC_FORCE_REPRESENTATION") if r: parameters["representation"] = r return parameters
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)
def main(args=None): """Commandline tool for FFC.""" xargs = parser.parse_args(args) parameters = default_parameters() ffc_logger = logging.getLogger("ffc") if xargs.debug: ffc_logger.setLevel(logging.DEBUG) if xargs.verbose: ffc_logger.setLevel(logging.INFO) parameters["representation"] = xargs.representation parameters["quadrature_rule"] = xargs.quadrature_rule parameters["quadrature_degree"] = xargs.quadrature_degree if xargs.output_directory: parameters["output_dir"] = xargs.output_directory for p in xargs.f: assert len(p) == 2 if p[0] not in parameters: raise RuntimeError( "Command parameter set with -f does not exist in parameters system." ) parameters[p[0]] = p[1] for p in xargs.u: assert len(p) == 2 if p[0] in parameters: raise RuntimeError( "Command parameter set with -u already exists in parameters system. Use -f." ) parameters[p[0]] = p[1] # FIXME: This is terrible! # Set UFL precision # ufl.constantvalue.precision = int(parameters["precision"]) # Call parser and compiler for each file resultcode = _compile_files(xargs.ufl_file, parameters, xargs.profile) return resultcode
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
def main(args=None): """This is the commandline tool for the python module ffc.""" if args is None: args = sys.argv[1:] # Get command-line arguments try: if "-O" in args: args[args.index("-O")] = "-O2" opts, args = getopt.getopt(args, "hIVSdvsl:r:f:O:o:q:ep", ["help", "includes", "version", "signature", "debug", "verbose", "silent", "language=", "representation=", "optimize=", "output-directory=", "quadrature-rule=", "error-control", "profile"]) except getopt.GetoptError: info_usage() print_error("Illegal command-line arguments.") return 1 # Check for --help if ("-h", "") in opts or ("--help", "") in opts: info_usage() return 0 # Check for --includes if ("-I", "") in opts or ("--includes", "") in opts: print(get_include_path()) return 0 # Check for --version if ("-V", "") in opts or ("--version", "") in opts: info_version() return 0 # Check for --signature if ("-S", "") in opts or ("--signature", "") in opts: print(get_ufc_signature()) return 0 # Check that we get at least one file if len(args) == 0: print_error("Missing file.") return 1 # Get parameters parameters = default_parameters() # Choose WARNING as default for script parameters["log_level"] = WARNING # Set default value (not part of in parameters[]) enable_profile = False # Parse command-line parameters for opt, arg in opts: if opt in ("-v", "--verbose"): parameters["log_level"] = INFO elif opt in ("-d", "--debug"): parameters["log_level"] = DEBUG elif opt in ("-s", "--silent"): parameters["log_level"] = ERROR elif opt in ("-l", "--language"): parameters["format"] = arg elif opt in ("-r", "--representation"): parameters["representation"] = arg elif opt in ("-q", "--quadrature-rule"): parameters["quadrature_rule"] = arg elif opt == "-f": if len(arg.split("=")) == 2: (key, value) = arg.split("=") default = parameters.get(key) if isinstance(default, int): value = int(value) elif isinstance(default, float): value = float(value) parameters[key] = value elif len(arg.split("==")) == 1: key = arg.split("=")[0] if key.startswith("no-"): key = key[3:] value = False else: value = True parameters[key] = value else: info_usage() return 1 elif opt in ("-O", "--optimize"): parameters["optimize"] = bool(int(arg)) elif opt in ("-o", "--output-directory"): parameters["output_dir"] = arg elif opt in ("-e", "--error-control"): parameters["error_control"] = True elif opt in ("-p", "--profile"): enable_profile = True # Set log_level push_level(parameters["log_level"]) # FIXME: This is terrible! # Set UFL precision #ufl.constantvalue.precision = int(parameters["precision"]) # Print a versioning message if verbose output was requested if parameters["log_level"] <= INFO: info_version() # Call parser and compiler for each file resultcode = 0 init_indent = ffc_logger._indent_level try: resultcode = _compile_files(args, parameters, enable_profile) finally: # Reset logging level and indent pop_level() set_indent(init_indent) return resultcode
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
def main(args=None): """This is the commandline tool for the python module ffc.""" if args is None: args = sys.argv[1:] # Get command-line arguments try: if "-O" in args: args[args.index("-O")] = "-O2" opts, args = getopt.getopt(args, "hIVSdvsl:r:f:O:o:q:ep", [ "help", "includes", "version", "signature", "debug", "verbose", "silent", "language=", "representation=", "optimize=", "output-directory=", "quadrature-rule=", "error-control", "profile" ]) except getopt.GetoptError: info_usage() print_error("Illegal command-line arguments.") return 1 # Check for --help if ("-h", "") in opts or ("--help", "") in opts: info_usage() return 0 # Check for --includes if ("-I", "") in opts or ("--includes", "") in opts: print(get_include_path()) return 0 # Check for --version if ("-V", "") in opts or ("--version", "") in opts: info_version() return 0 # Check for --signature if ("-S", "") in opts or ("--signature", "") in opts: print(get_ufc_signature()) return 0 # Check that we get at least one file if len(args) == 0: print_error("Missing file.") return 1 # Get parameters parameters = default_parameters() # Choose WARNING as default for script parameters["log_level"] = WARNING # Set default value (not part of in parameters[]) enable_profile = False # Parse command-line parameters for opt, arg in opts: if opt in ("-v", "--verbose"): parameters["log_level"] = INFO elif opt in ("-d", "--debug"): parameters["log_level"] = DEBUG elif opt in ("-s", "--silent"): parameters["log_level"] = ERROR elif opt in ("-l", "--language"): parameters["format"] = arg elif opt in ("-r", "--representation"): parameters["representation"] = arg elif opt in ("-q", "--quadrature-rule"): parameters["quadrature_rule"] = arg elif opt == "-f": if len(arg.split("=")) == 2: (key, value) = arg.split("=") default = parameters.get(key) if isinstance(default, int): value = int(value) elif isinstance(default, float): value = float(value) parameters[key] = value elif len(arg.split("==")) == 1: key = arg.split("=")[0] if key.startswith("no-"): key = key[3:] value = False else: value = True parameters[key] = value else: info_usage() return 1 elif opt in ("-O", "--optimize"): parameters["optimize"] = bool(int(arg)) elif opt in ("-o", "--output-directory"): parameters["output_dir"] = arg elif opt in ("-e", "--error-control"): parameters["error_control"] = True elif opt in ("-p", "--profile"): enable_profile = True # Set log_level push_level(parameters["log_level"]) # FIXME: This is terrible! # Set UFL precision #ufl.constantvalue.precision = int(parameters["precision"]) # Print a versioning message if verbose output was requested if parameters["log_level"] <= INFO: info_version() # Call parser and compiler for each file resultcode = 0 init_indent = ffc_logger._indent_level try: resultcode = _compile_files(args, parameters, enable_profile) finally: # Reset logging level and indent pop_level() set_indent(init_indent) return resultcode
from ffc.log import log from ffc.log import info from ffc.log import warning from ffc.log import debug from ffc.log import error from ffc.log import set_level from ffc.log import set_prefix from ffc.log import INFO from ffc.parameters import default_parameters from ffc.mixedelement import MixedElement from ffc.compiler import compile_form from ffc.jitobject import JITObject from ffc.quadratureelement import default_quadrature_degree # Special Options for JIT-compilation FFC_PARAMETERS_JIT = default_parameters() FFC_PARAMETERS_JIT["no-evaluate_basis_derivatives"] = True # Set debug level for Instant instant.set_log_level("warning") def jit(ufl_object, parameters=None): """Just-in-time compile the given form or element Parameters: ufl_object : The UFL object to be compiled parameters : A set of parameters """ # Check if we get an element or a form