[petsc-users] Using matrix-free with KSP
Barry Smith
bsmith at petsc.dev
Tue Aug 16 09:10:49 CDT 2022
I don't fully understand your question so I will answer questions that I do know the answer to :-)
The function you provide to PCShellSetApply() applies the preconditioner to the input vector and puts the result in the output vector. This input vector changes at every application of the preconditioner so should not be used in constructing the preconditioner.
If you are using the standard PETSc matrix-free matrix-vector product via finite difference, -snes_mf_operator or via MatCreateSNESMF() or you are using a MatShell, the matrix-vector product routines input changes for each multiply and should not be used in constructing the preconditioner.
The function you provide with SNESSetJacobian() is where you can compute anything you need to help build your preconditioner. The input vector to that function is the location at which the Jacobian is needed and is what you should use to build your approximate/part of Jacobian. You can store your approximate/part of Jacobian that you need to compute in the pmat and then in a PCShellSetUp() function that you provide you will compute what you will need latter to apply the preconditioner in your PCShellSetApply() function. Note that the values you put in the pmat do not need to be the full Jacobian; they can be anything you want since this pmat is not used to apply the matrix vector product.
If this is unclear, feel free to ask additional questions.
Barry
> On Aug 16, 2022, at 9:40 AM, Tu, Jiannan <Jiannan_Tu at uml.edu> wrote:
>
> I am trying to apply a user-defined preconditioner to speed up the convergency of matrix-free KSP solver. The user's manual provides an example of how to call the user-defined preconditioner routine. The routine has PC, input Vec and output Vec as arguments. I want to use part of the Jacobian as preconditioner matrix, the elements of which can be calculated using input Vec. My question is what the role of output Vec from the routine is in matrix-vector product? Or I just have the preconditioner matrix elements calculated in the routine and then Petsc handles applying the preconditioner to matrix-vector product? I used PCSetType(pc, PCSHELL) and PCShellSetApply.
>
> Thank you and appreciate your help!
>
> Jiannan
> From: Tu, Jiannan <Jiannan_Tu at uml.edu <mailto:Jiannan_Tu at uml.edu>>
> Sent: Wednesday, July 6, 2022 2:34 PM
> To: Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>>
> Cc: Jed Brown <jed at jedbrown.org <mailto:jed at jedbrown.org>>; petsc-users at mcs.anl.gov <mailto:petsc-users at mcs.anl.gov> <petsc-users at mcs.anl.gov <mailto:petsc-users at mcs.anl.gov>>
> Subject: Re: [petsc-users] Using matrix-free with KSP
>
> Barry,
>
> VecScatterCreateToAll solves the problem! The code now gives the correct answer with multiple processes.
>
> Thank you all so much!
>
> Jiannan
> From: Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>>
> Sent: Wednesday, July 6, 2022 2:08 PM
> To: Tu, Jiannan <Jiannan_Tu at uml.edu <mailto:Jiannan_Tu at uml.edu>>
> Cc: Jed Brown <jed at jedbrown.org <mailto:jed at jedbrown.org>>; petsc-users at mcs.anl.gov <mailto:petsc-users at mcs.anl.gov> <petsc-users at mcs.anl.gov <mailto:petsc-users at mcs.anl.gov>>
> Subject: Re: [petsc-users] Using matrix-free with KSP
>
>
> So your operator is a "dense" operator in that its matrix representation would be essentially dense. In that case, you can use specialized routines to do this efficiently. You can use VecScatterCreateToAll() and then VecScatterBegin/End to do the communication.
>
> Barry
>
>
>
>> On Jul 6, 2022, at 1:39 PM, Tu, Jiannan <Jiannan_Tu at uml.edu <mailto:Jiannan_Tu at uml.edu>> wrote:
>>
>> Jed, thank you very much for the reply.
>>
>> I'm not sure GlobaltoLocal will work. Say the solution vector is of length 10. Two processes then each process can see 5 elements of the vector. For A x = b (10 equations), each process performs matrix-vector product for five equations. And for each equation i , sum_j A_ij * x_j = b_i requires the process has access to all 10 unknows x_i (i=0,1,...,9).
>>
>> Can VecScatter and PetscSF make the entire vector accessible to each process? I am reading the manual and trying to understand how VecScatter and PetscSF work.
>>
>> Jiannan
>> From: Jed Brown <jed at jedbrown.org <mailto:jed at jedbrown.org>>
>> Sent: Wednesday, July 6, 2022 10:09 AM
>> To: Tu, Jiannan <Jiannan_Tu at uml.edu <mailto:Jiannan_Tu at uml.edu>>; Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>>
>> Cc: petsc-users at mcs.anl.gov <mailto:petsc-users at mcs.anl.gov> <petsc-users at mcs.anl.gov <mailto:petsc-users at mcs.anl.gov>>
>> Subject: Re: [petsc-users] Using matrix-free with KSP
>>
>> You'll usually have a GlobalToLocal operation for each rank to get the halo data it needs, then either a LocalToGlobal to collect the result (e.g., finite element methods) or the local compute will write directly into the owned portion of the global vector. If you're doing the communication "raw", then you may find VecScatter or PetscSF useful to perform the necessary communication.
>>
>> "Tu, Jiannan" <Jiannan_Tu at uml.edu <mailto:Jiannan_Tu at uml.edu>> writes:
>>
>> > Hi Barry,
>> >
>> > Following your instructions I implemented the matrix-free method to solve a large linear equation system resulted from a surface integration equation. The KSP solver works fine for single process, but it is problematic with multiple processes. The problem is that each process only can access its own part of solution vector so that each process only conducts part of matrix-vector multiplication. MPI can be used to assemble these partial matrix-vector multiplication together. Does Petsc provide any ways to implement multi-process matrix-free method?
>> >
>> > Thanks,
>> > Jiannan
>> > ________________________________
>> > From: Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>>
>> > Sent: Tuesday, May 24, 2022 2:12 PM
>> > To: Tu, Jiannan <Jiannan_Tu at uml.edu <mailto:Jiannan_Tu at uml.edu>>
>> > Cc: petsc-users at mcs.anl.gov <mailto:petsc-users at mcs.anl.gov> <petsc-users at mcs.anl.gov <mailto:petsc-users at mcs.anl.gov>>
>> > Subject: Re: [petsc-users] Using matrix-free with KSP
>> >
>> > This e-mail originated from outside the UMass Lowell network.
>> > ________________________________
>> >
>> > You can use MatCreateMFFD https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpetsc.org%2Fmain%2Fdocs%2Fmanualpages%2FMat%2FMatCreateMFFD%2F&data=05%7C01%7CJiannan_Tu%40uml.edu%7Cab0859d8d154471590bc08da5f5918d6%7C4c25b8a617f746f983f054734ab81fb1%7C0%7C0%7C637927133525745809%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=A7wqegTfh94No5BpDiWLK3VxOuR44U2wlWHVm2k7l60%3D&reserved=0<https://urldefense.com/v3/__https://petsc.org/main/docs/manualpages/Mat/MatCreateMFFD/__;!!PVKG_VDCxu5g!stxq2NWGVjUihvHBQC9Rlk0aHVoowcy0PTQYMcQxQ4DqOEC05KSw6TmstSXKckiUgelHzy4ue-d10-zlXkw$> <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpetsc.org%2Fmain%2Fdocs%2Fmanualpages%2FMat%2FMatCreateMFFD%2F&data=05%7C01%7CJiannan_Tu%40uml.edu%7C60675432e0b5409f004908da5f7a8b29%7C4c25b8a617f746f983f054734ab81fb1%7C0%7C0%7C637927277198462552%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=7SEmNO9TybDxwnhwcmyW1vFVc2eo7ys8JGBv9%2F%2FR2eI%3D&reserved=0> MatMFFDSetFunction MatSetFromOptions MatMFFDSetBase and provide the matrix to KSP. Note you will need to use -pc_type none or provide your own custom preconditioner withhttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpetsc.org%2Fmain%2Fdocs%2Fmanualpages%2FPC%2FPCSHELL%2F&data=05%7C01%7CJiannan_Tu%40uml.edu%7Cab0859d8d154471590bc08da5f5918d6%7C4c25b8a617f746f983f054734ab81fb1%7C0%7C0%7C637927133525745809%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Gb%2F9uFG3jp%2FbfWaSh2cSEQLItHS9CuLwarzN0KcNiJY%3D&reserved=0<https://urldefense.com/v3/__https://petsc.org/main/docs/manualpages/PC/PCSHELL/__;!!PVKG_VDCxu5g!stxq2NWGVjUihvHBQC9Rlk0aHVoowcy0PTQYMcQxQ4DqOEC05KSw6TmstSXKckiUgelHzy4ue-d1cA4fKGw$> <https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpetsc.org%2Fmain%2Fdocs%2Fmanualpages%2FPC%2FPCSHELL%2F&data=05%7C01%7CJiannan_Tu%40uml.edu%7C60675432e0b5409f004908da5f7a8b29%7C4c25b8a617f746f983f054734ab81fb1%7C0%7C0%7C637927277198462552%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=FDGqXk2UbO%2FYef5n21SIAUYrte093iKgL2p%2FwoA3qec%3D&reserved=0>
>> >
>> >
>> >
>> > On May 24, 2022, at 1:21 PM, Tu, Jiannan <Jiannan_Tu at uml.edu <mailto:Jiannan_Tu at uml.edu><mailto:Jiannan_Tu at uml.edu <mailto:Jiannan_Tu at uml.edu>>> wrote:
>> >
>> > I want to use a matrix-free matrix to solve a large linear equation system because the matrix is too large to be stored. Petsc user manual describes matrix-free method for SNES with examples. The matrix-free matrices section explains how to set up such a matrix, but I can't find any example of matrix-free method with KSP. I am wondering how to apply the method to KSP iterative solver in parallel.
>> >
>> > I greatly appreciate your help for me to understand this topic.
>> >
>> > Jiannan Tu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20220816/14884696/attachment-0001.html>
More information about the petsc-users
mailing list