# Matrix-vector product in parallel

**URL:** <https://community.freefem.org/t/matrix-vector-product-in-parallel/2189>\
**Category:** General Discussion\
**Created:** [December 15, 2022, 10:00am UTC](https://community.freefem.org/t/matrix-vector-product-in-parallel/2189 "2022-12-15T10:00:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![edelgado](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@edelgado](https://community.freefem.org/u/edelgado)\
**Post date:** [December 15, 2022, 10:00am UTC](https://community.freefem.org/t/matrix-vector-product-in-parallel/2189/1 "2022-12-15T10:00:40Z")

</div>

Dear FreeFem users,

I’m interested in perform some matrix-vector matrix-matrix products in parallel. The matrices, in general, come from varf and interpolate, and vectors are the solutions of FE problems. Right now it seems that all the products are performed in serial, but since I’m dealing with 3D problems, at some point, some products get stuck due to large dimensions of matrices and vectors.

Is there any way to parallelize the way of compute products matrix(sparse)-vector, or matrix(sparse)-matrix(sparse)?

Thank you in advance,  
Enrique

---

<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:** [December 15, 2022, 10:11am UTC](https://community.freefem.org/t/matrix-vector-product-in-parallel/2189/2 "2022-12-15T10:11:50Z")

</div>

Yes, you can use PETSc, which does all this efficiently, see for example this line [FreeFem-sources/diffusion-2d-PETSc.edp at master · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/diffusion-2d-PETSc.edp#L29) which computes a distributed (sparse) matrix-vector product.

---

<div class="post-metadata">

**Author:** ![edelgado](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@edelgado](https://community.freefem.org/u/edelgado)\
**Post date:** [December 15, 2022, 10:21am UTC](https://community.freefem.org/t/matrix-vector-product-in-parallel/2189/3 "2022-12-15T10:21:53Z")

</div>

And for the case of matrix-matrix product? There is where I’m having more problems, since I’m trying to multiply to FE sparse matrices and, when the dof are too large, it does not work.

Best,  
Enrique

---

<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:** [December 15, 2022, 10:37am UTC](https://community.freefem.org/t/matrix-vector-product-in-parallel/2189/4 "2022-12-15T10:37:39Z")

</div>

You can, for example, call `MatPtAP`. It depends on what kind of distributed matrices you want to multiply (same `fespace` or not, same mesh or not, etc.).

---

<div class="post-metadata">

**Author:** ![edelgado](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@edelgado](https://community.freefem.org/u/edelgado)\
**Post date:** [December 16, 2022, 8:28am UTC](https://community.freefem.org/t/matrix-vector-product-in-parallel/2189/5 "2022-12-16T08:28:35Z")

</div>

I’m interested in compute a matrix-matrix, M1\* M2, where M1 comes from a varf with same fespaces, and M2 comes from a interpolate(Vh1, Vh2).  
Then I should use this M=M1\* M2 to compute, M\*u, with u a FE solution.  
Is this MatPtAP useful for this case?

Thanks,  
Enrique

---

<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:** [December 16, 2022, 9:36am UTC](https://community.freefem.org/t/matrix-vector-product-in-parallel/2189/6 "2022-12-16T09:36:13Z")

</div>

Why do you need to compute M explicitly? Why not do two successive matrix-vector products? Also, you did not answer to my previous question, are the fespace defined on the same mesh?

---

<div class="post-metadata">

**Author:** ![edelgado](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@edelgado](https://community.freefem.org/u/edelgado)\
**Post date:** [December 16, 2022, 12:46pm UTC](https://community.freefem.org/t/matrix-vector-product-in-parallel/2189/7 "2022-12-16T12:46:31Z")

</div>

All the fespace are defined on the same mesh. I would try to think if I can do it without matrix-matrix products.
