Import Error when running fenicsx on colab

Hello,

I installed fenicsx on google colab using

try:
    import dolfinx
except ImportError:
    !wget "https://fem-on-colab.github.io/releases/fenicsx-install-real.sh" -O "/tmp/fenicsx-install.sh" && bash "/tmp/fenicsx-install.sh"
    import dolfinx

everything works well after that. Now if I’m recreating a fem.form repeatedly then I get the following errors after many successful executions

File "/content/gdrive/MyDrive/Github/HJDQN/HJQ/gym_lqr/gym_lqr/envs/lqrNd.py", line 344, in step
    self.linear_form = form(self.L)
  File "/usr/local/lib/python3.10/dist-packages/dolfinx/fem/forms.py", line 189, in form
    return _create_form(form)
  File "/usr/local/lib/python3.10/dist-packages/dolfinx/fem/forms.py", line 184, in _create_form
    return _form(form)
  File "/usr/local/lib/python3.10/dist-packages/dolfinx/fem/forms.py", line 144, in _form
    ufcx_form, module, code = jit.ffcx_jit(mesh.comm, form,
  File "/usr/local/lib/python3.10/dist-packages/dolfinx/jit.py", line 56, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/dolfinx/jit.py", line 204, in ffcx_jit
    r = ffcx.codegeneration.jit.compile_forms([ufl_object], options=p_ffcx, **p_jit)
  File "/usr/local/lib/python3.10/dist-packages/ffcx/codegeneration/jit.py", line 201, in compile_forms
    obj, module = _load_objects(cache_dir, module_name, form_names)
  File "/usr/local/lib/python3.10/dist-packages/ffcx/codegeneration/jit.py", line 319, in _load_objects
    compiled_module = importlib.util.module_from_spec(spec)
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1176, in create_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
ImportError: /root/.cache/fenics/libffcx_forms_9695f0f05d2260182b11169d5c2427ea9b4217f4.cpython-310-x86_64-linux-gnu.so: cannot change memory protections

First I thought the cache memory runs out, so I deleted the fenics folder in the cache regulary but it didn’t work. I also checked if the /tmp folder was set executable but this was also no problem, I tried refreshing the dolfinx module but this also didn’t work. Maybe someone did experience this error before? Any help would be highly appreciated.

The title is misleading: the library imports just fine.

I’ve never seen the error “cannot change memory protections” in my 10+ years of experience with unix, but I suspect it may have to do with how google mounts the drives in the colab runtime instances.

If that is the issue, there may not be much we can do, especially not without a minimal example at hand. You should see if it is really necessary to create the dolfinx.fem.form repeatedly (in a for loop, I guess), or if you can create it once and for all outside of the loop.

2 Likes

@francesco-ballarin Thank you for your reply. The problem got fixed when I removed the creation of the dolfinx.fem.form object outside of my loop. Though for me this was not obvious, because I used my code to solve an optimal control problem which had a RHS depending on the time. What solved my issue was declaring the time dependent variable as fem.Constant as they did here

Helo, Thanks for this answer, because I get the same error in jit code generation during dolfinx.fem.form building under my linux computer (it doesn’t come from colab). I removed all dolfinx.from building inside time and non-linear loop and it works, thanks. We just have to know that dolfinx.fem.form should not be built within loops.