<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Barry,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Thank you so much. I'll try these options and see how they work.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Jiannan<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Barry Smith <bsmith@petsc.dev><br>
<b>Sent:</b> Tuesday, August 16, 2022 1:59 PM<br>
<b>To:</b> Tu, Jiannan <Jiannan_Tu@uml.edu><br>
<b>Cc:</b> petsc-users@mcs.anl.gov <petsc-users@mcs.anl.gov><br>
<b>Subject:</b> Re: [petsc-users] Using matrix-free with KSP</font>
<div> </div>
</div>
<div class="" style="word-wrap:break-word; line-break:after-white-space">
<p></p>
<div style="background-color:#FFCACA; border-style:hidden; border-color:#FFCACA; border-width:1pt; padding:2pt; font-size:12pt; line-height:16pt; font-family:Calibri; color:Black; text-align:left">
<span style="font-weight:bold">CAUTION:</span> This email was sent from outside the UMass Lowell network.</div>
<br>
<p></p>
<div>
<div class=""><br class="">
</div>
   Create another matrix B that contains "<span class="" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">Selected parts of the matrix can be pre-calculated and stored to serve as the preconditioner." When you call KSPSetOperators() pass
 in B as the second matrix argument.</span>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class=""><br class="">
</font></div>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class="">   Now if literally application of B is the preconditioner you want to use, then simply use PCMAT as the preconditioner, and it will apply B each time it needs to apply the preconditioner.
 With this, you do not even need a PCShell. </font></div>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class=""><br class="">
</font></div>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class="">   If your preconditioner uses B to construct your own specific data structure needed to apply your preconditioner, then you can use PCShellSetSetUp() and have your routine call
 PCGetOperators() to pull out the B matrix and use it to construct your preconditioner. </font></div>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class=""><br class="">
</font></div>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class="">   If you want PETSc to construct a standard preconditioner from B, then you don't need the PCShell; you simply pass in the B, as above, and use -pc_type type to set the preconditioner
 you want to use. </font></div>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class=""><br class="">
</font></div>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class="">   Barry</font></div>
<div class=""><font face="Calibri, Arial, Helvetica, sans-serif" size="3" class=""><br class="">
</font>
<div><br class="">
<blockquote type="cite" class="">
<div class="">On Aug 16, 2022, at 12:31 PM, Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>> wrote:</div>
<br class="x_Apple-interchange-newline">
<div class="">
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Barry,</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Thank you very much for your instructions. I am sorry I may not ask clearer questions.<br class="">
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
I don't use SNES. What I am trying to solve is a very large linear equation system with dense and ill-conditioned matrix. Selected parts of the matrix can be pre-calculated and stored to serve as the preconditioner. My question is how I can apply this matrix
 as the preconditioner.<span class="x_Apple-converted-space"> </span><br class="">
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Here is the part of code from my linear equations solver. MatrixFreePreconditioner is supposed to be the routine to provide the preconditioner. But I don't understand how to use it. Should I use SNESSetJacobian() even if this is a linear problem?<span class="x_Apple-converted-space"> </span><br class="">
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
    MatCreateMFFD(MPI_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, N, N, &A);
<div class="">    MatMFFDSetFunction(A, formfunction, &params);</div>
<div class="">    MatSetFromOptions(A);</div>
<div class="">    MatMFFDSetBase(A, X, NULL);</div>
<div class=""><br class="">
</div>
<div class="">    KSPCreate(MPI_COMM_WORLD, &ksp);</div>
<div class="">    KSPSetOperators(ksp, A, A);</div>
<div class="">    KSPSetFromOptions(ksp);</div>
<br class="">
<div class="">    PC pc;</div>
<div class="">    KSPGetPC(ksp,&pc);</div>
<div class="">    PCSetType(pc,PCSHELL);</div>
<div class="">    PCShellSetApply(pc,MatrixFreePreconditioner);</div>
<div class=""><br class="">
</div>
<div class="">Thank you,</div>
<div class="">Jiannan<br class="">
</div>
<br class="">
</div>
<div id="x_appendonsend" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
</div>
<hr tabindex="-1" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; display:inline-block; width:1025.078125px">
<span class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; float:none; display:inline!important"></span>
<div id="x_divRplyFwdMsg" dir="ltr" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
<font face="Calibri, sans-serif" class="" style="font-size:11pt"><b class="">From:</b><span class="x_Apple-converted-space"> </span>Barry Smith <<a href="mailto:bsmith@petsc.dev" class="">bsmith@petsc.dev</a>><br class="">
<b class="">Sent:</b><span class="x_Apple-converted-space"> </span>Tuesday, August 16, 2022 10:10 AM<br class="">
<b class="">To:</b><span class="x_Apple-converted-space"> </span>Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>><br class="">
<b class="">Cc:</b><span class="x_Apple-converted-space"> </span><a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a><span class="x_Apple-converted-space"> </span><<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>><br class="">
<b class="">Subject:</b><span class="x_Apple-converted-space"> </span>Re: [petsc-users] Using matrix-free with KSP</font>
<div class=""> </div>
</div>
<div class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; word-wrap:break-word; line-break:after-white-space">
<p class="" style="margin-top:0px; margin-bottom:0px"></p>
<div class="" style="background-color:rgb(255,202,202); border:1pt hidden rgb(255,202,202); padding:2pt; font-size:12pt; line-height:16pt; font-family:Calibri; text-align:left">
<span class="" style="font-weight:bold">CAUTION:</span><span class="x_Apple-converted-space"> </span>This email was sent from outside the UMass Lowell network.</div>
<br class="">
<p class="" style="margin-top:0px; margin-bottom:0px"></p>
<div class="">
<div class=""><br class="">
</div>
  I don't fully understand your question so I will answer questions that I do know the answer to :-)
<div class=""><br class="">
</div>
<div class="">  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.</div>
<div class=""><br class="">
</div>
<div class="">  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.</div>
<div class=""><br class="">
</div>
<div class="">  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.</div>
<div class=""><br class="">
</div>
<div class="">  If this is unclear, feel free to ask additional questions.</div>
<div class=""><br class="">
</div>
<div class="">  Barry</div>
<div class=""><br class="">
</div>
<div class="">
<div class=""><br class="">
<blockquote type="cite" class="">
<div class="">On Aug 16, 2022, at 9:40 AM, Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>> wrote:</div>
<br class="x_x_Apple-interchange-newline">
<div class="">
<div class="x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
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.</div>
<div class="x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Thank you and appreciate your help!</div>
<div class="x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Jiannan<span class="x_x_Apple-converted-space"> </span><br class="">
</div>
<div id="x_x_appendonsend" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
</div>
<hr tabindex="-1" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; display:inline-block; width:1025.078125px">
<span class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; float:none; display:inline!important"></span>
<div id="x_x_divRplyFwdMsg" dir="ltr" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
<font face="Calibri, sans-serif" class="" style="font-size:11pt"><b class="">From:</b><span class="x_x_Apple-converted-space"> </span>Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>><br class="">
<b class="">Sent:</b><span class="x_x_Apple-converted-space"> </span>Wednesday, July 6, 2022 2:34 PM<br class="">
<b class="">To:</b><span class="x_x_Apple-converted-space"> </span>Barry Smith <<a href="mailto:bsmith@petsc.dev" class="">bsmith@petsc.dev</a>><br class="">
<b class="">Cc:</b><span class="x_x_Apple-converted-space"> </span>Jed Brown <<a href="mailto:jed@jedbrown.org" class="">jed@jedbrown.org</a>>;<span class="x_x_Apple-converted-space"> </span><a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a><span class="x_x_Apple-converted-space"> </span><<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>><br class="">
<b class="">Subject:</b><span class="x_x_Apple-converted-space"> </span>Re: [petsc-users] Using matrix-free with KSP</font>
<div class=""> </div>
</div>
<div dir="ltr" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
<div class="x_x_x_elementToProof" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Barry,</div>
<div class="x_x_x_elementToProof" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_x_x_elementToProof" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
VecScatterCreateToAll solves the problem! The code now gives the correct answer with multiple processes.<span class="x_x_Apple-converted-space"> </span><br class="">
</div>
<div class="x_x_x_elementToProof" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_x_x_elementToProof" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Thank you all so much!</div>
<div class="x_x_x_elementToProof" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_x_x_elementToProof" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Jiannan<br class="">
</div>
<div id="x_x_x_appendonsend" class=""></div>
<hr tabindex="-1" class="" style="display:inline-block; width:1025.078125px">
<div id="x_x_x_divRplyFwdMsg" dir="ltr" class=""><font face="Calibri, sans-serif" class="" style="font-size:11pt"><b class="">From:</b><span class="x_x_Apple-converted-space"> </span>Barry Smith <<a href="mailto:bsmith@petsc.dev" class="">bsmith@petsc.dev</a>><br class="">
<b class="">Sent:</b><span class="x_x_Apple-converted-space"> </span>Wednesday, July 6, 2022 2:08 PM<br class="">
<b class="">To:</b><span class="x_x_Apple-converted-space"> </span>Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>><br class="">
<b class="">Cc:</b><span class="x_x_Apple-converted-space"> </span>Jed Brown <<a href="mailto:jed@jedbrown.org" class="">jed@jedbrown.org</a>>;<span class="x_x_Apple-converted-space"> </span><a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a><span class="x_x_Apple-converted-space"> </span><<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>><br class="">
<b class="">Subject:</b><span class="x_x_Apple-converted-space"> </span>Re: [petsc-users] Using matrix-free with KSP</font>
<div class=""> </div>
</div>
<div class="" style="word-wrap:break-word; line-break:after-white-space">
<div class=""><br class="">
</div>
  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.
<div class=""><br class="">
</div>
<div class="">  Barry</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">
<div class=""><br class="">
<blockquote type="cite" class="">
<div class="">On Jul 6, 2022, at 1:39 PM, Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>> wrote:</div>
<br class="x_x_x_x_Apple-interchange-newline">
<div class="">
<div class="x_x_x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Jed, thank you very much for the reply.<span class="x_x_x_x_Apple-converted-space"> </span><br class="">
</div>
<div class="x_x_x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_x_x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
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).<span class="x_x_x_x_Apple-converted-space"> </span><br class="">
</div>
<div class="x_x_x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_x_x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
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.</div>
<div class="x_x_x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<br class="">
</div>
<div class="x_x_x_x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
Jiannan<br class="">
</div>
<div id="x_x_x_x_appendonsend" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
</div>
<hr tabindex="-1" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; display:inline-block; width:1262.234375px">
<span class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; float:none; display:inline!important"></span>
<div id="x_x_x_x_divRplyFwdMsg" dir="ltr" class="" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
<font face="Calibri, sans-serif" class="" style="font-size:11pt"><b class="">From:</b><span class="x_x_x_x_Apple-converted-space"> </span>Jed Brown <<a href="mailto:jed@jedbrown.org" class="">jed@jedbrown.org</a>><br class="">
<b class="">Sent:</b><span class="x_x_x_x_Apple-converted-space"> </span>Wednesday, July 6, 2022 10:09 AM<br class="">
<b class="">To:</b><span class="x_x_x_x_Apple-converted-space"> </span>Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>>; Barry Smith <<a href="mailto:bsmith@petsc.dev" class="">bsmith@petsc.dev</a>><br class="">
<b class="">Cc:</b><span class="x_x_x_x_Apple-converted-space"> </span><a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a><span class="x_x_x_x_Apple-converted-space"> </span><<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>><br class="">
<b class="">Subject:</b><span class="x_x_x_x_Apple-converted-space"> </span>Re: [petsc-users] Using matrix-free with KSP</font>
<div class=""> </div>
</div>
<div class="x_x_x_x_BodyFragment" style="font-family:Helvetica; font-size:18px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
<font size="2" class=""><span class="" style="font-size:11pt">
<div class="x_x_x_x_PlainText">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.<br class="">
<br class="">
"Tu, Jiannan" <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>> writes:<br class="">
<br class="">
> Hi Barry,<br class="">
><br class="">
> 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?<br class="">
><br class="">
> Thanks,<br class="">
> Jiannan<br class="">
> ________________________________<br class="">
> From: Barry Smith <<a href="mailto:bsmith@petsc.dev" class="">bsmith@petsc.dev</a>><br class="">
> Sent: Tuesday, May 24, 2022 2:12 PM<br class="">
> To: Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a>><br class="">
> Cc:<span class="x_x_x_x_Apple-converted-space"> </span><a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a><span class="x_x_x_x_Apple-converted-space"> </span><<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>><br class="">
> Subject: Re: [petsc-users] Using matrix-free with KSP<br class="">
><br class="">
> This e-mail originated from outside the UMass Lowell network.<br class="">
> ________________________________<br class="">
><br class="">
>    You can use MatCreateMFFD<span class="x_x_x_x_Apple-converted-space"> </span><a href="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%7C454a1eb8de0745be453508da7fb114e4%7C4c25b8a617f746f983f054734ab81fb1%7C0%7C0%7C637962695800558712%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2FausyQFMwKFHd4FnNaKAOCGwskUc%2F8jIlSxtWMdIjC4%3D&reserved=0" originalsrc="https://petsc.org/main/docs/manualpages/Mat/MatCreateMFFD/" shash="OXKqaRIGZYnFVjGsEndYF989/HIbwWzM2ToFraIWwrywvhm436sts2PD1KjGZxozjiCm8vXZZy3OBE9D05QF4jpheGbfAcAo3UyqqZThf3Fe6gvSJSY76Ae3rZhL4mgOk0yIXfCgfybNG8E1HRWIn6N8rdS8ThybrS9OLFiezpI=" originalsrc="https://petsc.org/main/docs/manualpages/Mat/MatCreateMFFD/" shash="rN9MZi1to+M0AdFwszqdMc669h2ATU71WiVBYKI7FVBra4QZNn1HaoCs+AUMLnAjbwXM2Yk8G86TFqGLtUg8rKecvVFH8uYIT3O5O3N+xlng6VCHLhjN3RhTrtfQCTCVOS7x06CCj03BNrwcH2eNvKn2wJEoUnjiW3c0WyK8YFw=" class="">https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpetsc.org%2Fmain%2Fdocs%2Fmanualpages%2FMat%2FMatCreateMFFD%2F&amp;data=05%7C01%7CJiannan_Tu%40uml.edu%7Cab0859d8d154471590bc08da5f5918d6%7C4c25b8a617f746f983f054734ab81fb1%7C0%7C0%7C637927133525745809%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=A7wqegTfh94No5BpDiWLK3VxOuR44U2wlWHVm2k7l60%3D&amp;reserved=0<https://urldefense.com/v3/__https://petsc.org/main/docs/manualpages/Mat/MatCreateMFFD/__;!!PVKG_VDCxu5g!stxq2NWGVjUihvHBQC9Rlk0aHVoowcy0PTQYMcQxQ4DqOEC05KSw6TmstSXKckiUgelHzy4ue-d10-zlXkw$></a><span class="x_x_x_x_Apple-converted-space"> </span>MatMFFDSetFunction
 MatSetFromOptions MatMFFDSetBase and provide the matrix to KSP. Note you will need to use -pc_type none or provide your own custom preconditioner with<a href="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%7C454a1eb8de0745be453508da7fb114e4%7C4c25b8a617f746f983f054734ab81fb1%7C0%7C0%7C637962695800558712%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2F5%2BLn2AdyPMZYfBS11Dd87S5anXtl3QPPNEst%2BjDGE4%3D&reserved=0" originalsrc="https://petsc.org/main/docs/manualpages/PC/PCSHELL/" shash="ADEpSAS/BwgX9WKCk/y0Q5yPp6bKDatP1G54pPLniX3N5mc9jpY4ddxZFe9QvEz/gZELGFq+F5PgPsTo6WyqamxripRXW18P99tWZAFK4bKNZwdv2q25Q4SD6/dV5G1ykXFlBQtu19c3QOF6MYnOxHZXjpmomqq53my6z9U1EjE=" originalsrc="https://petsc.org/main/docs/manualpages/PC/PCSHELL/" shash="j1TIaelvOpa8OGECHYDvi+RzvWKqquL+CSwpKOt/CvHOYPARc8Er/z1XRlplSkrNgfsmTA/KmQA1lB6KqjlGSArIRaHS1qtslXnO27AW8+zvtBytOWwaJIUeoo13A7EjjNwxX4TwNW3gg0P1PJ6pW87OPNKrEKMfPo2nLKz2hko=" class="">https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpetsc.org%2Fmain%2Fdocs%2Fmanualpages%2FPC%2FPCSHELL%2F&amp;data=05%7C01%7CJiannan_Tu%40uml.edu%7Cab0859d8d154471590bc08da5f5918d6%7C4c25b8a617f746f983f054734ab81fb1%7C0%7C0%7C637927133525745809%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=Gb%2F9uFG3jp%2FbfWaSh2cSEQLItHS9CuLwarzN0KcNiJY%3D&amp;reserved=0<https://urldefense.com/v3/__https://petsc.org/main/docs/manualpages/PC/PCSHELL/__;!!PVKG_VDCxu5g!stxq2NWGVjUihvHBQC9Rlk0aHVoowcy0PTQYMcQxQ4DqOEC05KSw6TmstSXKckiUgelHzy4ue-d1cA4fKGw$></a><br class="">
><br class="">
><br class="">
><br class="">
> On May 24, 2022, at 1:21 PM, Tu, Jiannan <<a href="mailto:Jiannan_Tu@uml.edu" class="">Jiannan_Tu@uml.edu</a><<a href="mailto:Jiannan_Tu@uml.edu" class="">mailto:Jiannan_Tu@uml.edu</a>>> wrote:<br class="">
><br class="">
> 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.<br class="">
><br class="">
> I greatly appreciate your help for me to understand this topic.<br class="">
><br class="">
> Jiannan Tu</div>
</span></font></div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div>
</body>
</html>