Re: [petsc-users] Efficiently build a matrix from two asymmetric diagonal block matrices

2022-07-21 Thread Dave May
On Thu 21. Jul 2022 at 14:06, Matthew Knepley  wrote:

> On Thu, Jul 21, 2022 at 6:28 AM Emile Soutter 
> wrote:
>
>> Dear all,
>>
>> I am struggling with the simple following problem : Having a first matrix
>> B1 of size n1xm1, a second matrix B2 of size n2 x m2, build a matrix M of
>> size (n1+n2)x(m1+m2) where the blocks B1 and B2 are the "diagonal" of M
>> (M[0:n1,0:m1]=B1, M[n1:(n1+n2),m1:(m1+m2)]=B2). In my case, the blocks B1
>> and B2 are obtained from another routine, directly in the petsc matrix form
>> (or pyop2.Sparsity form). However the blocks are not squared (n1,n2,m1,m2
>> are all different integers). The operation is easy to do with the SetValues
>> option. However, it takes a large amount of time (too much) when the system
>> becomes large. I struggle to do it efficiently and in parallel. What method
>> do you recommend to use to do this as fast as possible?
>>
>> Thanks you for any tips,
>>
>
> I think it depends on what you want to do with the final matrix. If you
> only want MatMult, then I think you can just use MatNest
>
>   https://petsc.org/main/docs/manualpages/Mat/MatCreateNest/
>
> which will wrap up the submatrices.
>


As an assembled matrix is sought, a follow up suggestion might be to first
create a MatNest representation and pass this to MatConvert to convert the
Nest Mat into an MPIAIJ Mat.

The MatNest object is pretty light weight and doesn’t use much memory as it
refers (via a pointer) to the original matrices. Hence this two step
approach might be appropriate.

Cheers
Dave

However, if you want to manipulate the values (factorization, relaxation,
> etc) then you need
> to assemble a monolithic matrix. For this you could create the global
> matrix, and then use
>
>   https://petsc.org/main/docs/manualpages/Mat/MatCreateLocalRef/
>
> to get a submatrix to assemble directly into, which you pass to your
> assembly routine. Clearly this is more complicated, but
> sometimes necessary.
>
>   Thanks,
>
>   Matt
>
>
>> Emile
>>
>
>
> --
> What most experimenters take for granted before they begin their
> experiments is infinitely more interesting than any results to which their
> experiments lead.
> -- Norbert Wiener
>
> https://www.cse.buffalo.edu/~knepley/
> 
>


Re: [petsc-users] Efficiently build a matrix from two asymmetric diagonal block matrices

2022-07-21 Thread Emile Soutter
Thanks a lot for the reply. The context is that the matrix itself will be
used as a matrix projection in the context of multigrid algorithm. So the
final matrix,P, will be used as the interpolation matrix in PCMG routine (
https://www.mcs.anl.gov/petsc/petsc-3.6/docs/manualpages/PC/PCMGSetInterpolation.html
), and then it will be used in matrix multiplication ( operation of the
kind A'=PAP^T, A is a matrix on a fine MG level, A' the matrix on a coarser
level, P is the final matrix that I want to build by blocks and P^T its
transpose).

As I am using petsc4py, I will try the first option (that exists in
petsc4py) and if it is not sufficient, I will look into
https://petsc.org/main/docs/manualpages/Mat/MatCreateLocalRef/.

Thanks a lot,

Emile

On Thu, Jul 21, 2022 at 2:03 PM Matthew Knepley  wrote:

> On Thu, Jul 21, 2022 at 6:28 AM Emile Soutter 
> wrote:
>
>> Dear all,
>>
>> I am struggling with the simple following problem : Having a first matrix
>> B1 of size n1xm1, a second matrix B2 of size n2 x m2, build a matrix M of
>> size (n1+n2)x(m1+m2) where the blocks B1 and B2 are the "diagonal" of M
>> (M[0:n1,0:m1]=B1, M[n1:(n1+n2),m1:(m1+m2)]=B2). In my case, the blocks B1
>> and B2 are obtained from another routine, directly in the petsc matrix form
>> (or pyop2.Sparsity form). However the blocks are not squared (n1,n2,m1,m2
>> are all different integers). The operation is easy to do with the SetValues
>> option. However, it takes a large amount of time (too much) when the system
>> becomes large. I struggle to do it efficiently and in parallel. What method
>> do you recommend to use to do this as fast as possible?
>>
>> Thanks you for any tips,
>>
>
> I think it depends on what you want to do with the final matrix. If you
> only want MatMult, then I think you can just use MatNest
>
>   https://petsc.org/main/docs/manualpages/Mat/MatCreateNest/
>
> which will wrap up the submatrices. However, if you want to manipulate the
> values (factorization, relaxation, etc) then you need
> to assemble a monolithic matrix. For this you could create the global
> matrix, and then use
>
>   https://petsc.org/main/docs/manualpages/Mat/MatCreateLocalRef/
>
> to get a submatrix to assemble directly into, which you pass to your
> assembly routine. Clearly this is more complicated, but
> sometimes necessary.
>
>   Thanks,
>
>   Matt
>
>
>> Emile
>>
>
>
> --
> What most experimenters take for granted before they begin their
> experiments is infinitely more interesting than any results to which their
> experiments lead.
> -- Norbert Wiener
>
> https://www.cse.buffalo.edu/~knepley/
> 
>


Re: [petsc-users] Efficiently build a matrix from two asymmetric diagonal block matrices

2022-07-21 Thread Matthew Knepley
On Thu, Jul 21, 2022 at 6:28 AM Emile Soutter 
wrote:

> Dear all,
>
> I am struggling with the simple following problem : Having a first matrix
> B1 of size n1xm1, a second matrix B2 of size n2 x m2, build a matrix M of
> size (n1+n2)x(m1+m2) where the blocks B1 and B2 are the "diagonal" of M
> (M[0:n1,0:m1]=B1, M[n1:(n1+n2),m1:(m1+m2)]=B2). In my case, the blocks B1
> and B2 are obtained from another routine, directly in the petsc matrix form
> (or pyop2.Sparsity form). However the blocks are not squared (n1,n2,m1,m2
> are all different integers). The operation is easy to do with the SetValues
> option. However, it takes a large amount of time (too much) when the system
> becomes large. I struggle to do it efficiently and in parallel. What method
> do you recommend to use to do this as fast as possible?
>
> Thanks you for any tips,
>

I think it depends on what you want to do with the final matrix. If you
only want MatMult, then I think you can just use MatNest

  https://petsc.org/main/docs/manualpages/Mat/MatCreateNest/

which will wrap up the submatrices. However, if you want to manipulate the
values (factorization, relaxation, etc) then you need
to assemble a monolithic matrix. For this you could create the global
matrix, and then use

  https://petsc.org/main/docs/manualpages/Mat/MatCreateLocalRef/

to get a submatrix to assemble directly into, which you pass to your
assembly routine. Clearly this is more complicated, but
sometimes necessary.

  Thanks,

  Matt


> Emile
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ 


[petsc-users] Efficiently build a matrix from two asymmetric diagonal block matrices

2022-07-21 Thread Emile Soutter
Dear all,

I am struggling with the simple following problem : Having a first matrix
B1 of size n1xm1, a second matrix B2 of size n2 x m2, build a matrix M of
size (n1+n2)x(m1+m2) where the blocks B1 and B2 are the "diagonal" of M
(M[0:n1,0:m1]=B1, M[n1:(n1+n2),m1:(m1+m2)]=B2). In my case, the blocks B1
and B2 are obtained from another routine, directly in the petsc matrix form
(or pyop2.Sparsity form). However the blocks are not squared (n1,n2,m1,m2
are all different integers). The operation is easy to do with the SetValues
option. However, it takes a large amount of time (too much) when the system
becomes large. I struggle to do it efficiently and in parallel. What method
do you recommend to use to do this as fast as possible?

Thanks you for any tips,

Emile