<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div><br></div> Ok, thanks. Definitely more than I expected.<div><br></div><div> It is easy to add the support you requested. I'll push a branch latter today.</div><div><br></div><div> Barry</div><div><br><div><br><blockquote type="cite"><div>On Aug 21, 2023, at 3:28 PM, Jonas Lundgren <jonas.lundgren@liu.se> wrote:</div><br class="Apple-interchange-newline"><div><meta charset="UTF-8"><div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);">Dear Barry,<o:p></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);"><o:p> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);">I have tried what you suggested on a (not too large) test example on a bigger cluster that I have access to, using -redistribute_ksp_initial_guess_nonzero 0 and 1, respectively. The average timings during the 5 first (design) iterations are 8.7 s (state) and 7.9 s (adjoint) for the case with zero initial guesses and 5.0 s (state) and 5.7 s (adjoint) for the cases with nonzero initial guesses. These solvings are the bottleneck of my program, accounting for about 60-90% of the total computational time, depending on various parameters. The program is basically consisting of the loop: solve state > solve adjoint > update design > repeat. This is repeated for a couple of hundred iterations.<o:p></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);"><o:p> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);">From my experience, the number of iterations to convergence in each state/adjoint solve will decrease when increasing the (design) iterative counter (i.e. the longer the process has gone on for) IF the initial guess is the solution to the previous solve. This is because the design update is smaller in the end of the process than in the beginning, and a smaller design update leads to smaller changes in state/adjoint solution between subsequent (design) iterations. This means that the numbers provided above are on the low side: most likely the savings can be even more in the end of the design process.<o:p></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);"><o:p> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);">Best regards,<o:p></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);">Jonas Lundgren<o:p></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: rgb(31, 73, 125);"><o:p> </o:p></span></div><div><div style="border-width: 1pt medium medium; border-style: solid none none; border-color: rgb(225, 225, 225) currentcolor currentcolor; border-image: none; padding: 3pt 0cm 0cm;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><b><span lang="SV" style="font-size: 11pt; font-family: Calibri, sans-serif;">Från:</span></b><span lang="SV" style="font-size: 11pt; font-family: Calibri, sans-serif;"><span class="Apple-converted-space"> </span>Barry Smith <<a href="mailto:bsmith@petsc.dev" style="color: purple; text-decoration: underline;">bsmith@petsc.dev</a>><span class="Apple-converted-space"> </span><br><b>Skickat:</b><span class="Apple-converted-space"> </span>den 21 augusti 2023 20:13<br><b>Till:</b><span class="Apple-converted-space"> </span>Jonas Lundgren <<a href="mailto:jonas.lundgren@liu.se" style="color: purple; text-decoration: underline;">jonas.lundgren@liu.se</a>><br><b>Kopia:</b><span class="Apple-converted-space"> </span><a href="mailto:petsc-users@mcs.anl.gov" style="color: purple; text-decoration: underline;">petsc-users@mcs.anl.gov</a><br><b>Ämne:</b><span class="Apple-converted-space"> </span>Re: [petsc-users] (Sub) KSP initial guess with PCREDISTRIBUTE<o:p></o:p></span></div></div></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"> When you use 2 KSP so that you can use the previous "solution" as the initial guess for the next problem, how much savings do you get? In iterations inside <span style="font-family: Calibri, sans-serif;">PCREDISTRIBUTE and in time (relative to the entire linear solver time and relative to the entire application run)? You can get this information running with -log_view</span><o:p></o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-family: Calibri, sans-serif;"> That is, run the 2 KSP simulation twice, once with the inner KSPSetNonzeroInitialGuess() on and once with it off and compare the times for the two cases.</span><o:p></o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-family: Calibri, sans-serif;"> Thanks</span><o:p></o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-family: Calibri, sans-serif;"> Barry</span><o:p></o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-family: Calibri, sans-serif;"> Using KSPSetNonzeroInitialGuess() requires an extra matrix-vector product and preconditioner application, so I would like to verify that you have a measurable performance improvement with the initial guess.</span><o:p></o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-family: Calibri, sans-serif;"> </span><o:p></o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"><br><br></span><o:p></o:p></div><blockquote style="margin-top: 5pt; margin-bottom: 5pt;"><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;">On Aug 21, 2023, at 7:06 AM, Jonas Lundgren via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov" style="color: purple; text-decoration: underline;">petsc-users@mcs.anl.gov</a>> wrote:<o:p></o:p></div></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><o:p> </o:p></div><div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span lang="SV" style="font-size: 11pt; font-family: Calibri, sans-serif;">Dear PETSc users,</span><span style="font-size: 11pt; font-family: Calibri, sans-serif;"><o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span lang="SV" style="font-size: 11pt; font-family: Calibri, sans-serif;"> </span><span style="font-size: 11pt; font-family: Calibri, sans-serif;"><o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">I have a problem regarding the setting of initial guess to KSP when using PCREDISTRIBUTE as the preconditioner. (The reason to why I use PCREDISTRIBUTE is because I have a lot of fixed DOF in my problem, and PCREDISTRIBUTE successfully reduces the problem size and therefore speeds up the solving).<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">First, some details:<o:p></o:p></span></div></div><div style="margin-left: 36pt;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif; text-indent: -18pt;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">-</span><span style="font-size: 7pt;"> <span class="apple-converted-space"> </span></span><span style="font-size: 11pt; font-family: Calibri, sans-serif;">I use a version of PETSc 3.19.1<o:p></o:p></span></div></div><div style="margin-left: 36pt;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif; text-indent: -18pt;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">-</span><span style="font-size: 7pt;"> <span class="apple-converted-space"> </span></span><span style="font-size: 11pt; font-family: Calibri, sans-serif;">The KSP I use is KSPPREONLY, as suggested in the manual pages of PCREDISTRIBUTE:<a href="https://petsc.org/release/manualpages/PC/PCREDISTRIBUTE/" style="color: purple; text-decoration: underline;"><span style="color: rgb(149, 79, 114);">https://petsc.org/release/manualpages/PC/PCREDISTRIBUTE/</span></a><o:p></o:p></span></div></div><div style="margin-left: 36pt;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif; text-indent: -18pt;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">-</span><span style="font-size: 7pt;"> <span class="apple-converted-space"> </span></span><span style="font-size: 11pt; font-family: Calibri, sans-serif;">I use KSPBCGSL as sub-KSP. I can perfectly well solve my problem using this as my main KSP, but the performance is much worse than when using it as my sub-KSP (under KSPPREONLY+PCREDISTRIBUTE) due to the amount of fixed DOF<o:p></o:p></span></div></div><div style="margin-left: 36pt;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif; text-indent: -18pt;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">-</span><span style="font-size: 7pt;"> <span class="apple-converted-space"> </span></span><span style="font-size: 11pt; font-family: Calibri, sans-serif;">I am first solving a state problem, then an adjoint problem using the same linear operator.<o:p></o:p></span></div></div><div style="margin-left: 36pt;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif; text-indent: -18pt;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">-</span><span style="font-size: 7pt;"> <span class="apple-converted-space"> </span></span><span style="font-size: 11pt; font-family: Calibri, sans-serif;">The adjoint vector is used as sensitivity information to update a design. After the design update, the state+adjoint problems are solved again with a slightly updated linear operator. This is done for hundreds of (design) iteration steps<o:p></o:p></span></div></div><div style="margin-left: 36pt;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif; text-indent: -18pt;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">-</span><span style="font-size: 7pt;"> <span class="apple-converted-space"> </span></span><span style="font-size: 11pt; font-family: Calibri, sans-serif;">I want the initial guess for the state problem to be the state solution from the previous (design) iteration, and same for the adjoint problem<o:p></o:p></span></div></div><div style="margin-left: 36pt;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif; text-indent: -18pt;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">-</span><span style="font-size: 7pt;"> <span class="apple-converted-space"> </span></span><span style="font-size: 11pt; font-family: Calibri, sans-serif;">I am aware of the default way of setting a custom initial guess: KSPSetInitialGuessNonzero(ksp, PETSC_TRUE) together with providing the actual guess in the x vector in the call to KSPSolve(ksp, b, x)<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">The main problem is that PCREDISTRIBUTE internally doesn’t use the input solution vector (x) when calling KSPSolve() for the sub-KSP. It zeroes out the solution vector (x) when starting to build x = diag(A)^{-1} b in the beginning of PCApply_Redistribute(), and uses red->x as the solution vector/initial guess when calling KSPSolve(). Therefore, I cannot reach the sub-KSP with an initial guess.<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">Additionally, KSPPREONLY prohibits the use of having a nonzero initial guess (the error message says “it doesn’t make sense”). I guess I can remove the line raising this error and recompile the PETSc libraries, but it still won’t solve the previously mentioned problem, which seems to be the hard nut to crack.<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">So far, I have found out that if I create 2 KSP object, one each for the state and adjoint problems, it is enough with calling KSPSetInitialGuessNonzero(subksp, PETSC_TRUE) on the subksp. It seems as if the variable red->x in PCApply_Redistribute() is kept untouched in memory between calls to the main KSP and therefore is used as (non-zero) initial guess to the sub-KSP. This has been verified by introducing PetscCall(PetscObjectCompose((PetscObject)pc,"redx",(PetscObject)red->x)); in PCApply_Redistribute(), recompiling the PETSc library, and then inserting a corresponding PetscObjectQuery((PetscObject)pc, "redx", (PetscObject *)&redx); in my own program source file.<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">However, I would like to only create 1 KSP to be used with both the state and adjoint problems (same linear operator), for memory reasons. When I do this, the initial guesses are mixed up between the two problems: the initial guess for the adjoint problem is the solution to the state problem in the current design iteration, and the initial guess for the state problem is the solution to the adjoint problem in the previous design iteration. These are very bad guesses that increases the time to convergence in each state/adjoint solve.<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">So, the core of the problem (as far as I can understand) is that I want to control the initial guess red->x in PCApply_Redistribute().<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">The only solution I can think of is to include a call to PetscObjectQuery() in PCApply_Redistribute() to obtain a vector with the initial guess from my main program. And then I need to keep track of the initial guesses for my both problems in my main program myself (minor problem). This is maybe not the neatest way, and I do not know if this approach affects the performance negatively? Maybe one call each to PetscObjectQuery() and PetscObjectCompose() per call to PCApply_Redistribute() is negligible?<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">Is there another (and maybe simpler) solution to this problem? Maybe I can SCATTER_FORWARD the input x vector in PCApply_Redistribute() before it is zeroed out, together with allowing for non-zero initial guess in KSPPREONLY?<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">Any help is welcome!<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">Best regards,<o:p></o:p></span></div></div><div><div style="margin: 0cm 0cm 0.0001pt; font-size: 12pt; font-family: "Times New Roman", serif;"><span style="font-size: 11pt; font-family: Calibri, sans-serif;">Jonas Lundgren</span></div></div></div></blockquote></div></div></div></blockquote></div><br></div></body></html>