# Adding MatDiagonalSet, MatAXPY and MatAYPX

**URL:** <https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345>\
**Category:** Feature Request\
**Created:** [November 22, 2021, 3:40pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345 "2021-11-22T15:40:57Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [November 26, 2021, 1:33pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/21 "2021-11-26T13:33:23Z")

</div>

You are missing a `-ksp_reuse_preconditioner false` when you want to compute a new factorization.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [November 26, 2021, 1:45pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/22 "2021-11-26T13:45:37Z")

</div>

Wow, such a stupid mistake 😀 Now everything work perfectly, thank you!

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 20, 2022, 9:37am UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/23 "2022-01-20T09:37:12Z")

</div>

Dear @prj ,

I would like to try out reusing LU factorization when solving a polynomial eigenvalue problem with `PEPSolve` in a similar way that you recommanded above. Something like first solving the problem with `-st_type sinvert -st_pc_type lu`, then slightly change the coefficient matrices, and use an iterative method also with reusing the previous factorization to precondition the new system. The naive way would be setting `-st_ksp_type fgmres -st_pc_type lu -st_ksp_reuse_preconditioner`; however, I suppose the internal `KSP` object created by `PEPSolve` (or `EPSSolve`) is destroyed after the call. Is there any easy way to do such a thing? If so, where should I start looking?

Thanks for the help in advance!

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 20, 2022, 2:58pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/24 "2022-01-20T14:58:29Z")

</div>

We could do exactly as for `EPS` ([FreeFem-sources/SLEPc-code.hpp at develop · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/SLEPc-code.hpp#L280-L282)), i.e., call `PEPGetST()`, and if there is a `KSP` attached to one of the `Mat`, use it via `STSetKSP()`. But I’m not sure of what you want to do exactly, so if you could share a small example (possibly in private), it would be easier for me to help you out.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 20, 2022, 5:26pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/25 "2022-01-20T17:26:44Z")

</div>

Thanks for the quick reply. Based on the info you provided, I modified the Navier-Stokes SLEPc example, which hopefully illustrated what I have in mind. I seem to be able to access the `KSP` object. However, running the script with the ` -log_view` option shows `MatLUFactorNum 2`, which indicates that the LU factorization is computed once again (or I also managed to crash the program).  
I would really appreciate if you could take a look at the code.  
[navier-stokes-2d-SLEPc-complex.edp](https://community.freefem.org/uploads/short-url/o4RE8RQkApnVJRPuNVbkjIgRw2Z.edp) (3.4 KB)

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 20, 2022, 5:30pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/26 "2022-01-20T17:30:46Z")

</div>

You are not attaching any `KSP` information (`set(J, sparams = "...");`) to `J` prior to the _first_ call to `EPSSolve()`, so it is expected that there are two numerical factorizations.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 20, 2022, 10:26pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/27 "2022-01-20T22:26:48Z")

</div>

Interestingly, setting the same option for both the matrix `J` and `EPSSolve` before the first call does not change the fact that LU factorization is performed twice. Furthermore, I find it odd that even if I do not do `set(J, sparams = "...");` before the first `EPSSolve` call, `set(J, sparams = " -ksp_monitor"); ` changes the behavior of the program.  
[navier-stokes-2d-SLEPc-complex.edp](https://community.freefem.org/uploads/short-url/ltCTnOF2GdkyONvjQucIHSe54J4.edp) (2.5 KB)

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 21, 2022, 6:58am UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/28 "2022-01-21T06:58:36Z")

</div>

I don’t understand why the MUMPS instance is being destroyed at the end of the first `EPSSolve()`, I’ll need to investigate this in the upcoming days (busy schedule). I’ll keep you posted ASAP.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 21, 2022, 7:54am UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/29 "2022-01-21T07:54:24Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 24, 2022, 2:25pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/30 "2022-01-24T14:25:35Z")

</div>

This should be fixed in [Fixes reuse of KSP in ST. · FreeFem/FreeFem-sources@6c1db49 · GitHub](https://github.com/FreeFem/FreeFem-sources/commit/6c1db49cbba53bd7e59b36bbcec99dfd6af1cf71), could you please give it a go? Then, we will move to `PEPSolve()`, if that’s OK for you.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 24, 2022, 2:28pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/31 "2022-01-24T14:28:10Z")

</div>

Thanks for the quick fix. I will try it out in the evening and let you know about the results.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 24, 2022, 7:04pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/32 "2022-01-24T19:04:57Z")

</div>

I compiled FreeFem with the fix, it works perfectly. Not sure why, but setting only `set(J, sparams = " -ksp_type fgmres -pc_type lu -ksp_reuse_preconditioner true ");` achieves the desired effect, while changing only `-st_ksp_type fgmres -st_pc_type lu -st_ksp_reuse_preconditioner true` does not do the job (but I guess this does not matter - it works 🙂 ).

I would like it very much if we could try this out with `PEPSolve`. I think I could rewrite my polynomial eigenvalue problem with the companion matrix method into a normal eigenvalue problem, assemble the system matrix and use `EPSSolve`; however, I prefer SLEPc to handle all that.

Furthermore, I have some other questions which I think are important if I want to try out iterative solvers.

1. The [following](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/SLEPc-code.hpp#L331-L351) code suggests me that if I give `EPSSolve` (and `PEPSolve`?) nonzero vectors, those are used as a guess for the following `EPSSolve` call? Because the SLEPc documentation suggests me that this might not be a good idea for the Krylov-Schur method.

2. `EPSSetDeflationSpace` can be accessed with passing the input `deflation=...` to the eigenvalue solver? How does this work - I am struggling to figure this out on my own.

Thank you very much for the help!

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 25, 2022, 6:20am UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/33 "2022-01-25T06:20:26Z")

</div>

> Not sure why, but setting only […]

The `st_` prefix is added when the `KSP` is created by SLEPc. If you create it yourself with `set(...)`, the option should not be prefixed, unless you add the flag `set(..., prefix = "st_")`.

> […] however, I prefer SLEPc to handle all that.

Would it be possible to ask you for a piece of code that calls `PEPSolve()` and where I could try the `-ksp_reuse_preconditioner` option? You can share that in private if it’s best for you.

> […] nonzero vectors, those are used as a guess […]

I’ve never seen tremendous improvements using this feature.

> How does this work - I am struggling to figure this out on my own.

Please see [Slepc ESPSetDeflationSpace function](https://community.freefem.org/t/slepc-espsetdeflationspace-function/508) andd let me know if you need more details.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 25, 2022, 9:02am UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/34 "2022-01-25T09:02:22Z")

</div>

Thanks for the detailed answers.  
I slightly modified the [blasius-stability-1d-SLEPc-complex.edp](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/blasius-stability-1d-SLEPc-complex.edp) example with a second `PEPSolve` call - this is very similar to what I am trying to achieve, yet it is close to an MWE.  
[blasius-stability-1d-SLEPc-complex.edp](https://community.freefem.org/uploads/short-url/jv8yVfgANBHYUfuYR9JmCxp58wH.edp) (5.6 KB)

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 25, 2022, 3:52pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/35 "2022-01-25T15:52:34Z")

</div>

I think this works with [Gets a ST from a PEP (stored in a Mat). · FreeFem/FreeFem-sources@cc9ba13 · GitHub](https://github.com/FreeFem/FreeFem-sources/commit/cc9ba13fc16b3df11424e5c0eb276b29815ff3ec) and the updated  
[blasius-stability-1d-SLEPc-complex.edp](https://community.freefem.org/uploads/short-url/inDLej0ogeqM8xRLPJ8bzzRf4cC.edp) (5.7 KB). I don’t like the code very much, so it’s currently in its own branch. If this works OK for you, I’ll try to tidy this up. Please let me know.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 25, 2022, 10:03pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/36 "2022-01-25T22:03:13Z")

</div>

Thank you very much. I will test this feature on my problem tomorrow and let you know about the results I got.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [January 28, 2022, 1:44pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/37 "2022-01-28T13:44:17Z")

</div>

Dear @prj ,

I tested reusing the LU factorization of the polynomial eigenvalue problem for my main problem. It works well; unfortunately, only a small speedup could be achieved and only in the case of using a small krylov subspace. Therefore it is unlikely that I will use this feature for my research. Nevertheless, thank you very much for the implementation!

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 28, 2022, 4:40pm UTC](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345/38 "2022-01-28T16:40:24Z")

</div>

Thanks for letting me know. I’ll babysit the `pep` branch for a while until we find out if it’s really not worth it.

[Previous page](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345.md?page=1)
