def ffc_nonaffine_compile_form(form, parameters=None): if parameters is None: parameters = ffc.default_parameters() else: _ = ffc.default_parameters() _.update(parameters) parameters = _ # Firedrake settings parameters["write_file"] = False parameters["format"] = 'pyop2' parameters["representation"] = 'quadrature' parameters["pyop2-ir"] = True tic = time() kernel, = ffc.compile_form([form], parameters=parameters) code = kernel.gencode() T1 = time() - tic print('#include <math.h>\n', code.replace('static inline', '', 1), file=open("Form.c", 'w')) tic = time() subprocess.check_call(["cc", "-pipe", "-c", "-O2", "-std=c99", "Form.c"]) T2 = time() - tic inst = int(subprocess.check_output("objdump -d Form.o | wc -l", shell=True)) return T1 + T2, inst
def ffc_jit(ufl_object, form_compiler_parameters=None): # Prepare form compiler parameters with overrides from dolfin p = ffc.default_parameters() p["scalar_type"] = "double complex" if common.has_petsc_complex else "double" p.update(form_compiler_parameters or {}) # CFFI compiler options/flags extra_compile_args = ['-g0', '-O3', '-march=native'] user_cflags = os.getenv('DOLFIN_JIT_CFLAGS') if user_cflags is not None: extra_compile_args = user_cflags.split(" ") cffi_options = dict(cffi_extra_compile_args=extra_compile_args, cffi_verbose=False, cffi_debug=False) # Set FFC cache location cache_dir = "~/.cache/fenics" cache_dir = os.getenv('FENICS_CACHE_DIR', cache_dir) cache_dir = Path(cache_dir).expanduser() # Switch on type and compile, returning cffi object if isinstance(ufl_object, ufl.Form): r = ffc.codegeneration.jit.compile_forms([ufl_object], parameters=p, cache_dir=cache_dir, **cffi_options) elif isinstance(ufl_object, ufl.FiniteElementBase): r = ffc.codegeneration.jit.compile_elements([ufl_object], parameters=p, cache_dir=cache_dir, **cffi_options) elif isinstance(ufl_object, ufl.Mesh): r = ffc.codegeneration.jit.compile_coordinate_maps( [ufl_object], parameters=p, cache_dir=cache_dir, **cffi_options) else: raise TypeError(type(ufl_object)) return r[0][0]
def jit(ufl_object, form_compiler_parameters=None, mpi_comm=None): """Just-in-time compile any provided UFL Form or FiniteElement using FFC. Default parameters from FFC are overridden by parameters["form_compiler"] first and then the form_compiler_parameters argument to this function. """ # Check that form is not empty (workaround for bug in UFL where # bilinear form 0*u*v*dx becomes the functional 0*dx) if isinstance(ufl_object, ufl.Form) and ufl_object.empty(): cpp.dolfin_error("jit.py", "perform just-in-time compilation of form", "Form is empty. Cannot pass to JIT compiler") # Compatibility checks on ffc interface if not (hasattr(ffc, 'default_parameters') and hasattr(ffc, 'jit') and hasattr(ffc, 'ufc_signature')): cpp.dolfin_error( "jit.py", "perform just-in-time compilation of form", "Form compiler must implement 'default_parameters()', " "'jit(ufl_object, parameters=None)' " "and 'ufc_signature()' functions") # Check DOLFIN build time UFC matches currently loaded UFC if cpp.__ufcsignature__ != ffc.ufc_signature(): cpp.dolfin_error( "jit.py", "perform just-in-time compilation of form", "DOLFIN was not compiled against matching" "UFC from current FFC installation.") # Prepare form compiler parameters with overrides from dolfin and kwargs p = ffc.default_parameters() p.update(parameters["form_compiler"]) p.update(form_compiler_parameters or {}) # Execute! try: result = ffc.jit(ufl_object, parameters=p) except Exception as e: tb_text = ''.join(traceback.format_exception(*sys.exc_info())) cpp.dolfin_error("jit.py", "perform just-in-time compilation of form", "ffc.jit failed with message:\n%s" % (tb_text, )) if isinstance(ufl_object, ufl.Form): compiled_form, module, prefix = result compiled_form = cpp.make_ufc_form(compiled_form) return compiled_form, module, prefix elif isinstance(ufl_object, ufl.FiniteElementBase): ufc_element, ufc_dofmap = result ufc_element = cpp.make_ufc_finite_element(ufc_element) ufc_dofmap = cpp.make_ufc_dofmap(ufc_dofmap) return ufc_element, ufc_dofmap elif isinstance(ufl_object, ufl.Mesh): ufc_coordinate_mapping = result ufc_coordinate_mapping = cpp.make_ufc_coordinate_mapping( ufc_coordinate_mapping) return ufc_coordinate_mapping
parameters["coffee"].set_update_function( lambda k, v: Citations().register("Luporini2015")) # Default to the values of PyOP2 configuration dictionary pyop2_opts = Parameters("pyop2_options", **configuration) pyop2_opts.set_update_function( lambda k, v: configuration.unsafe_reconfigure(**{k: v})) # Override values pyop2_opts["type_check"] = True pyop2_opts["log_level"] = "INFO" parameters.add(pyop2_opts) ffc_parameters = default_parameters() ffc_parameters['write_file'] = False ffc_parameters['format'] = 'pyop2' ffc_parameters['representation'] = 'quadrature' ffc_parameters['pyop2-ir'] = True parameters.add(Parameters("form_compiler", **ffc_parameters)) parameters["reorder_meshes"] = True parameters["matnest"] = True parameters["type_check_safe_par_loops"] = False def disable_performance_optimisations(): """Switches off performance optimisations in Firedrake.
parameters.add(Parameters("coffee", O2=True)) # Default to the values of PyOP2 configuration dictionary pyop2_opts = Parameters("pyop2_options", **configuration) pyop2_opts.set_update_function(lambda k, v: configuration.unsafe_reconfigure(**{k: v})) # Override values pyop2_opts["type_check"] = True pyop2_opts["log_level"] = "INFO" parameters.add(pyop2_opts) ffc_parameters = default_parameters() ffc_parameters['write_file'] = False ffc_parameters['format'] = 'pyop2' ffc_parameters['representation'] = 'quadrature' ffc_parameters['pyop2-ir'] = True parameters.add(Parameters("form_compiler", **ffc_parameters)) parameters["reorder_meshes"] = True parameters["matnest"] = True parameters["type_check_safe_par_loops"] = False def disable_performance_optimisations(): """Switches off performance optimisations in Firedrake.
from forms import Forms from dolfin import * from ffc import compile_form, default_parameters params = default_parameters() params["optimize"] = True params["cpp_optimize"] = True params["representation"] = "quadrature" params["output_dir"] = "kernels" # Form compiler options parameters["form_compiler"]["optimize"] = True parameters["form_compiler"]["cpp_optimize"] = True parameters["form_compiler"]["representation"] = "quadrature" meshes = {2: UnitSquareMesh(31, 31), 3: UnitCubeMesh(9, 9, 9)} def mass(q, p, dim, mesh, nf=0): V = FunctionSpace(mesh, 'CG', q) P = FunctionSpace(mesh, 'CG', p) u = TrialFunction(V) v = TestFunction(V) it = dot(v, u) f = [Function(P) for _ in range(nf)] for f_ in f: f_.interpolate(Expression('1.0')) return reduce(inner, f + [it])*dx def elasticity(q, p, dim, mesh, nf=0):
from forms import Forms from dolfin import * from ffc import compile_form, default_parameters params = default_parameters() params["optimize"] = True params["cpp_optimize"] = True params["representation"] = "quadrature" params["output_dir"] = "kernels" # Form compiler options parameters["form_compiler"]["optimize"] = True parameters["form_compiler"]["cpp_optimize"] = True parameters["form_compiler"]["representation"] = "quadrature" meshes = {2: UnitSquareMesh(31, 31), 3: UnitCubeMesh(9, 9, 9)} def mass(q, p, dim, mesh, nf=0): V = FunctionSpace(mesh, 'CG', q) P = FunctionSpace(mesh, 'CG', p) u = TrialFunction(V) v = TestFunction(V) it = dot(v, u) f = [Function(P) for _ in range(nf)] for f_ in f: f_.interpolate(Expression('1.0')) return reduce(inner, f + [it]) * dx def elasticity(q, p, dim, mesh, nf=0):