# Calling FFC just-in-time (JIT) compiler, this may take some time

**URL:** <https://fenicsproject.discourse.group/t/calling-ffc-just-in-time-jit-compiler-this-may-take-some-time/2275>\
**Category:** Uncategorized\
**Created:** [January 23, 2020, 9:02am UTC](https://fenicsproject.discourse.group/t/calling-ffc-just-in-time-jit-compiler-this-may-take-some-time/2275 "2020-01-23T09:02:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eloi](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eloi](https://fenicsproject.discourse.group/u/Eloi)\
**Post date:** [January 23, 2020, 9:02am UTC](https://fenicsproject.discourse.group/t/calling-ffc-just-in-time-jit-compiler-this-may-take-some-time/2275/1 "2020-01-23T09:02:58Z")

</div>

Hello,  
As the title say, calling this compiler take some time. Actually, I have to perform it thousand times in my code due to the projection on my FE space. I was wondering if there were a way to make it quicker ? Because what I understood was that it is a “preprocessor” that generates a C++ files containing the variationnal form, and then dolfin read this file to assemble the matrix; isn’t there a way to assemble the matrices directly ?  
Thank’s,  
Eloi.

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [January 23, 2020, 2:37pm UTC](https://fenicsproject.discourse.group/t/calling-ffc-just-in-time-jit-compiler-this-may-take-some-time/2275/2 "2020-01-23T14:37:57Z")

</div>

You can directly manipulate matrices through the `petsc4py` API, but, in most use cases, repeated recompilation of forms can be avoided. In particular, you can prevent recompilation after changing the values of numerical constants by wrapping them as `Constant`s. For example:

```python
from dolfin import *
mesh = UnitIntervalMesh(1)

# Compiles every time (if you start with a clean cache):
print("Not using a Constant.")
for i in range(11,17):
    print(assemble(i*dx(domain=mesh)))
c = Constant(0)

# Compiles only the first time:
print("Using a Constant.")
f = c*dx(domain=mesh)
for i in range(11,17):
    c.assign(i)
    print(assemble(f))

```

---

<div class="post-metadata">

**Author:** ![Eloi](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eloi](https://fenicsproject.discourse.group/u/Eloi)\
**Post date:** [January 29, 2020, 11:22am UTC](https://fenicsproject.discourse.group/t/calling-ffc-just-in-time-jit-compiler-this-may-take-some-time/2275/3 "2020-01-29T11:22:10Z")

</div>

Actually I have a density function that changes at each iteration, not constants. Is it Possible to accelerate that ?

---

<div class="post-metadata">

**Author:** ![Eloi](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eloi](https://fenicsproject.discourse.group/u/Eloi)\
**Post date:** [January 29, 2020, 11:33am UTC](https://fenicsproject.discourse.group/t/calling-ffc-just-in-time-jit-compiler-this-may-take-some-time/2275/4 "2020-01-29T11:33:09Z")

</div>

Let add more precision. The compiler is called when I need to project on my FE space the following function:  
\Nabla mu = \frac{ |\Nabla u|^2 - \mu u^2}{\int\_\Omega \rho u^2}  
with the functions u, rho and the constant \mu changing at each step, where I perform:

```
u_func = Function(V)
u_func.vector().set_local(u)
rho_func = Function(VD)
rho_func.vector().set_local(rho)
dMu = Function(VD)
dMu = (dot(grad(u_func), grad(u_func)) - mu*u_func*u_func)/assemble(rho_func*u_func*u_func*dx)
dMu = project(dMu, VD)

```

How do I allow this to assemble the form just once ?  
Thank’s !

---

<div class="post-metadata">

**Author:** ![bleyerj](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bleyerj/32/6020_2.png) [@bleyerj](https://fenicsproject.discourse.group/u/bleyerj)\
**Post date:** [January 29, 2020, 4:35pm UTC](https://fenicsproject.discourse.group/t/calling-ffc-just-in-time-jit-compiler-this-may-take-some-time/2275/5 "2020-01-29T16:35:57Z")

</div>

Hello,  
can you try by defining before entering the loop

```auto
u_func = Function(VD)
rho_func = Function(VD)
dMu = Function(VD)
c = Constant(1)

```

and then inside the loop do

```auto
u_func.vector().set_local(u)
rho_func.vector().set_local(rho)
c.assign(Constant(assemble(rho_func*u_func*u_func*dx)))
dmu = (dot(grad(u_func), grad(u_func)) - mu*u_func*u_func)/c
dMu.assign(project(dmu, VD)

```

also if mu is changing, define it as a Constant and assign it its new value inside the loop

---

<div class="post-metadata">

**Author:** ![Eloi](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Eloi](https://fenicsproject.discourse.group/u/Eloi)\
**Post date:** [January 30, 2020, 9:05am UTC](https://fenicsproject.discourse.group/t/calling-ffc-just-in-time-jit-compiler-this-may-take-some-time/2275/6 "2020-01-30T09:05:00Z")

</div>

Holy s\*\*\* that works ! Thanks a lot !  
Do you know where is explained the way the JIT compiler works ?
