<div dir="ltr">Thanks, all! That makes sense (and the code worked without the initializations in question).<div><br></div><div>BTW, my original email to the PETSc mailing list was held up for review by a moderator yesterday. So that's probably why you haven't seen it there yet.</div><div><br></div><div>Best,</div><div><br></div><div>Robert </div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, Jul 21, 2025 at 9:10 AM Barry Smith <<a href="mailto:bsmith@petsc.dev">bsmith@petsc.dev</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="line-break:after-white-space"><div><br></div> One passes PETSC_NULL_XXX as an argument that one does not want to get filled up, an optional argument. Similarly in PETSc C one passes NULL as an optional argument. <div><br></div><div> You should not be setting variables to PETSC_NULL_XXX. When they are declared in your code they are automatically correctly initialized to an appropriate default value (which is not PETSC_NULL_XXX). </div><div><br></div><div> You can use code such as</div><div><br></div><div> if (PetscObjectIsNull(vec)) then </div><div><br></div><div> to check if a PetscObject is currently validly set. Do not use</div><div><br></div><div> if (vec == PETSC_NULL_VEC) then</div><div><br></div><div> So, for example,</div><div><br></div><div> VecCreate(..., vec,ierr)</div><div> ....</div><div> VecDestroy(vec,ierr)</div><div> if (PetscObjectIsNull(vec)) then </div><div> print*, 'vector was appropriately freed')</div><div> endif</div><div> VecCreate(..., vec,ierr)</div><div> ....</div><div><br></div><div> Any further questions, please ask on petsc-maint or petsc-users</div><div><br></div><div> Barry <br><div><br></div><div> <br><div><br><blockquote type="cite"><div>On Jul 21, 2025, at 8:49 AM, Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank">knepley@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div dir="ltr">On Mon, Jul 21, 2025 at 7:49 AM Mark Adams <<a href="mailto:mfadams@lbl.gov" target="_blank">mfadams@lbl.gov</a>> wrote:</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Robert, [I don't see this message to PETSc users in my email so cc'ing manually]<div><br></div><div>This is odd, something is messed up. </div><div>These NULL things are certainly 0 and the initial value of these objects is not defined by the compiler, and they could be 0 in some cases.</div></div></blockquote><div><br></div><div>Hi Robert,</div><div><br></div><div>I am not the Fortran expert, but I believe that passing PETSC_NULL_XXX to a creation routine is not correct. Here is my logic.</div><div><br></div><div>In the wrapper, PETSC_NULL_XXX is converted to a NULL pointer (_not_ a pointer whose value is NULL). Thus KSPCreate would try to set a NULL pointer and you get the SEGV. I think it may have worked before because this was not automated, so these routines did not check for PETSC_NULL_XXX since it is not a case handled by the C function.</div><div><br></div><div>Could you try without initializing? Barry would have to tell you what they should be initialized to.</div><div><br></div><div> Thanks,</div><div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Others are more up on the new Fortran stuff, but I would do a clean build of PETSc (rm -fr arch...), and turn debugging on. </div><div>You will get a stack trace and it might find something and give a useful error message, or "fix" the problem mysteriously.</div><div><br></div><div>You could put a print statement in before these are initialized to see what *was* in it that you are clobbering.</div><div><br></div><div>Mark</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jul 20, 2025 at 10:33 AM Robert Hager <<a href="mailto:rhager@pppl.gov" target="_blank">rhager@pppl.gov</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">FYI<br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">---------- Forwarded message ---------<br>From: <strong class="gmail_sendername" dir="auto">Robert Hager</strong> <span dir="auto"><<a href="mailto:rhager@pppl.gov" target="_blank">rhager@pppl.gov</a>></span><br>Date: Sun, Jul 20, 2025 at 10:32 AM<br>Subject: Segfaults when calling some PETSc functions in Fortran with variables initialized to PETSC_NULL_XX<br>To: <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a>><br></div><br><br><div dir="ltr">Hello,<div><br></div><div>I am in the process of updating my code to use PETSc versions >=3.22. (I am using v3.22.3 compiled with GCC on Perlmutter-CPU at NERSC for debugging at this time.) After updating all the Fortran function calls that have changed from v3.21 to v3.22 I am now getting segfaults in some PETSc routines. In the two instances I was able to identify, the problem seems to be calling the PETSc function with a PETSc variable that has been initialized to PETSC_NULL_XX:</div><div><span style="font-family:Menlo;font-size:12px"><br></span></div><blockquote style="margin:0px 0px 0px 40px;border:medium;padding:0px"><div><span style="font-family:Menlo;font-size:12px">solver%rhs</span><span style="font-family:Menlo;font-size:12px;color:rgb(28,0,207)">2</span><span style="font-family:Menlo;font-size:12px">_mat = PETSC_NULL_MAT</span></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal">[...]</div></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal"><span style="color:rgb(155,35,147)"><b>call</b></span> MatDuplicate(solver%Amat,MAT_DO_NOT_COPY_VALUES,solver%rhs<span style="color:rgb(28,0,207)">2</span>_mat,ierr)</div></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal">--> Segfault</div></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal"><br></div></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal">solver%ksp<span> </span>= PETSC_NULL_KSP</div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal">[...]</div></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal"><span style="color:rgb(155,35,147)"><b>call</b></span> KSPCreate(solver%comm,solver%ksp,ierr)</div></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal">--> Segfault</div></div></blockquote><div><font face="Menlo"><span style="font-size:12px"><br></span></font></div><div><span style="font-size:12px"><font face="arial, sans-serif">When I comment out the assignments to PETSC_NULL_XX in the above examples, the code works just fine.</font></span></div><div><span style="font-size:12px"><font face="arial, sans-serif"><br></font></span></div><div><span style="font-size:12px"><font face="arial, sans-serif">Is this the intended behavior or a bug that you might have fixed by now?</font></span></div><div><span style="font-size:12px"><font face="arial, sans-serif"><br></font></span></div><div><span style="font-size:12px"><font face="arial, sans-serif">Best,</font></span></div><div><span style="font-size:12px"><font face="arial, sans-serif"><br></font></span></div><div><span style="font-size:12px"><font face="arial, sans-serif">Robert</font></span></div></div>
</div></div>
</blockquote></div>
</blockquote></div><div><br clear="all"></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!Ylz6mzmFkBJe1SAiy6sraNQ79TbZP3WWJtJY0kJSoeydhbohH8fH8uPd-f_ig_mjlKa2S7MME6rnPWQlLV-_$" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>
</div></blockquote></div><br></div></div></div></blockquote></div>