This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Efficient integration and the "just-in-time" compiler

+3 votes

Hi,

I have a collection of functions f and scalars c. All the functions are defined on the same mesh and FunctionSpace. I integrate the functions using lines similar to the following

for i in range(100):
    for j in range(100):
        result = assemble(f[i]*f[j]*c[i]*dx)

Unfortunately this often result is the just in time compiler being called in each iteration. Is there a way to have the compiler called once for all cases, or is a compilation at each iteration inevitable?

I also notice that, if I run the script a second time, the jit compiler is not called again. Are the compilations saved somewhere?

Thanks

asked Sep 11, 2014 by sixtysymbols FEniCS User (2,280 points)

If I wrote my code in C++ rather than python, would there still be on-the-fly compilation?

1 Answer

+5 votes
 
Best answer

You need to wrap the scalars as Constants, e.g.,

for i in range(100):
    c_i  = Constant(c[i])
    for j in range(100):
        result = assemble(f[i]*f[j]*c_i*dx)

The above will avoid the JIT compiler being called more than once. You can make it even more efficient via lower-level interfaces where you pre-process the form to avoid UFL processing the form each time. To do this, you need to turn a UFL form into a DOLFIN Form object. Something like:

# UFL form
M = fi*fj*c*dx

# Attach coefficients to form
...

# Create a DOLFIN form
M = Form(M)

Caveat: the above snippet has not been tested. See site-packages/dolfin/fem/form.py for details.

answered Sep 13, 2014 by Garth N. Wells FEniCS Expert (35,930 points)
edited Sep 14, 2014 by Garth N. Wells

Thanks for the reply. This helped a lot! Is there any link or reference for reading up on lower-level interfaces and 'pre-JIT' techniques? Thanks again.

I've edited my answer to add some more detail on pre-JIT.

...