# How to properly clear and control JIT cache in dolfinx

**URL:** <https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220>\
**Category:** dolfinx\
**Created:** [September 14, 2023, 7:34pm UTC](https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220 "2023-09-14T19:34:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aherrema](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@aherrema](https://fenicsproject.discourse.group/u/aherrema)\
**Post date:** [September 14, 2023, 7:34pm UTC](https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220/1 "2023-09-14T19:34:03Z")

</div>

Can somebody explain how to properly clear the cache at `~/.cache/fenics` when using `dolfinx`? I’m seeing some suggestions ([here](https://fenicsproject.discourse.group/t/dijitso-compilation-error/11496) and [here](https://fenicsproject.discourse.group/t/signature-of-method-gamma-l1-inside-does-not-match-signature-of-the-base-method-in-class-subdomain/6221/3)) to execute `dijitso clean` at the command line but I don’t have the `dijitso` command available within my conda environment–maybe this procedure has changed or I need to install additional utilities?

On a related note, I have a workflow where I’m running a FEniCSx problem repeatedly with an embarrassingly parallel workflow. The inputs (mesh, constants values) are the different each time, but the code/forms remain the same. That said, I noticed that my `~/.cache/fenics` directory contains over 26,000 files (nearly 5 GB worth). Is that expected? I am fighting some issues with I/O overhead and it seems this could be contributing. Does this indicate that something isn’t working as intended with the cache?

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [September 14, 2023, 7:41pm UTC](https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220/2 "2023-09-14T19:41:00Z")

</div>

dijitso is part of legacy FEniCS. The python layer of dolfinx does not use dijitso, instead it uses CFFI. And the default cache directory is the one you’ve referred to.

Regarding many files in your cache. Make sure you’re properly generating `dolfinx.fem.form` objects outside of `for`/`while` loops. See, e.g., [Speeding up time-dependent diffusion equation - #6 by nate](https://fenicsproject.discourse.group/t/speeding-up-time-dependent-diffusion-equation/11856/6)

---

<div class="post-metadata">

**Author:** ![aherrema](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@aherrema](https://fenicsproject.discourse.group/u/aherrema)\
**Post date:** [September 14, 2023, 8:00pm UTC](https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220/3 "2023-09-14T20:00:39Z")

</div>

Thanks for the response, @nate. Regarding taking the `fem.form` call “outside of a loop”, this is frankly not practical in the near-term within my workflow since the “loop” is controlled externally by different software. I see how this is not technically leveraging FEniCS optimally, but I expect I will be stuck with situation for a while. Is there a way to simply accept that I’ll need to recompile each time (it is reasonably fast) and avoid digging into the cache every time?

Helpful to know that dolfinx is using a different approach for the compilation, thanks for that as well. Do you know if there is a “proper” method to clear the cache within CFFI or if I can simply delete those files without issue? A quick look into CFFI documentation was not helpful…

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [September 14, 2023, 8:05pm UTC](https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220/4 "2023-09-14T20:05:31Z")

</div>

> [@aherrema](#):
>
> Thanks for the response, @nate. Regarding taking the `fem.form` call “outside of a loop”, this is frankly not practical in the near-term within my workflow since the “loop” is controlled externally by different software.

I would like to see how you do this, as there are many ways of writing code with external input that would use a single cached form.

> [@aherrema](#):
>
> Helpful to know that dolfinx is using a different approach for the compilation, thanks for that as well. Do you know if there is a “proper” method to clear the cache within CFFI or if I can simply delete those files without issue? A quick look into CFFI documentation was not helpful…

Just remove the files in that folder.

---

<div class="post-metadata">

**Author:** ![aherrema](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@aherrema](https://fenicsproject.discourse.group/u/aherrema)\
**Post date:** [September 14, 2023, 10:09pm UTC](https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220/5 "2023-09-14T22:09:36Z")

</div>

Thanks, I marked your response as the solution as clearing the cache was the first key here! To follow up on the related issues…

> I would like to see how you do this, as there are many ways of writing code with external input that would use a single cached form.

Without getting into too much detail, the workflow is something like (from Matlab):

```matlab
for ii = 1 : nIters
    
    % prep mesh .xdmf file, other inputs
    
    % execute fenics analysis for this iteration
    system('python3 run.py`)

    % read and process outputs

end

```

In this case, `run.py` is a structural analysis that reads the mesh/inputs, constructs the relevant equations, solves, then outputs results, similar to [this](https://github.com/RuruX/shell_analysis_fenicsx/blob/main/examples/scordelis_lo_roof/scordelis_lo_roof.py).

Indeed, clearing the cache drastically improved my issues with I/O overhead. And it appears I do have a new entry in the cache every time I run `run.py`, which explains why the cache had become so bloated over time. Thus, I’d be very curious as to how this workflow could be retrofitted to either (1) use a single cached form for every iteration or (2) simply ignore the cache (accepting that we will need to compile each time) so as to eliminate issues with I/O and cache bloat (obviously not the elegant solution but possibly pragmatic).

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [September 15, 2023, 3:03am UTC](https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220/6 "2023-09-15T03:03:27Z")

</div>

> [@aherrema](#):
>
> (1) use a single cached form for every iteration

If you provide your code it would be easier to point out what to do. It usually boils down to material parameters and discretization parameters (such as theta in time splitting schemes) not being wrapped as Constant.

---

<div class="post-metadata">

**Author:** ![aherrema](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@aherrema](https://fenicsproject.discourse.group/u/aherrema)\
**Post date:** [September 15, 2023, 2:25pm UTC](https://fenicsproject.discourse.group/t/how-to-properly-clear-and-control-jit-cache-in-dolfinx/12220/7 "2023-09-15T14:25:21Z")

</div>

Okay, thanks. I cannot share my current code in its entirety due to IP issues unfortunately, but I may be able to put together a representative example later.

> It usually boils down to material parameters and discretization parameters (such as theta in time splitting schemes) not being wrapped as Constant.

This makes sense–I can review my code and investigate the issue with this in mind.
