# Matrix free petsc solver with multigrid (MG or GAMG) preconditioner

**URL:** <https://fenicsproject.discourse.group/t/matrix-free-petsc-solver-with-multigrid-mg-or-gamg-preconditioner/13569>\
**Category:** Linear Algebra\
**Created:** [February 9, 2024, 5:30pm UTC](https://fenicsproject.discourse.group/t/matrix-free-petsc-solver-with-multigrid-mg-or-gamg-preconditioner/13569 "2024-02-09T17:30:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbookwala](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mbookwala/32/8230_2.png) [@mbookwala](https://fenicsproject.discourse.group/u/mbookwala)\
**Post date:** [February 9, 2024, 5:30pm UTC](https://fenicsproject.discourse.group/t/matrix-free-petsc-solver-with-multigrid-mg-or-gamg-preconditioner/13569/1 "2024-02-09T17:30:46Z")

</div>

Hi.  
I want to use petsc `MG` or `GAMG` preconditioner in conjunction with matrix free approach given [here](https://github.com/juliusgh/dolfinx/blob/demo-poisson-matrix-free/python/demo/demo_poisson_matrix_free.py).  
So in line 346, I want  
`solver.getPC().setType(PETSc.PC.Type.GAMG)`  
instead of  
`solver.getPC().setType(PETSc.PC.Type.NONE)`  
Is it possible to use petsc preconditioners directly or do I need to define my own preconditioner? I am presuming something like PCSHELL

---

<div class="post-metadata">

**Author:** ![pietrobenedusi](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pietrobenedusi](https://fenicsproject.discourse.group/u/pietrobenedusi)\
**Post date:** [February 25, 2025, 10:32am UTC](https://fenicsproject.discourse.group/t/matrix-free-petsc-solver-with-multigrid-mg-or-gamg-preconditioner/13569/2 "2025-02-25T10:32:09Z")

</div>

Hi,

you will need to build a matrix-free preconditioner within PCSHELL, PETSc built-in preconditioners need access to the matrix entries. Algebraic MG, in particular, needs the assembled matrix, but you can construct your own geometric MG matrix-free. You can also build simpler matrix-free preconditioners (e.g. SOR) or physics-based ones using PCSHELL, depending on the problem you are trying to solve.
