Skip to content

Conversation

@rfalgout
Copy link
Contributor

@rfalgout rfalgout commented Dec 18, 2025

  • We already have reference counting for the Struct and SStruct matrices, and this adds it to ParCSRMatrix.
  • With some changes in src/krylov, reference counting would allow users to destroy the preconditioning matrix passed in through SetPrecondMatrix(). Add this later, and require the following instead.
  • Require (for now) that users not destroy the matrix passed in through SetPrecondMatrix() until the solver is no longer needed and destroyed.

@rfalgout
Copy link
Contributor Author

Hi @oseikuffuor1 . I made a logic change to pcg.c that occurs to me should be enough to fix the issues people are having. We would just require (for now) that when SetPrecondMatrix() is called, that matrix should not be destroyed prematurely. We can add the reference counting later.

Would you please test out the mixed precision PCG cases to make sure this is working correctly? I have already run some of our standard regression tests and this looks good in the single precision case. If this looks okay, I will go ahead and make similar changes to the other Krylov solvers.

Thanks!

@rfalgout rfalgout requested a review from liruipeng December 19, 2025 17:37
@oseikuffuor1
Copy link
Contributor

Hi @oseikuffuor1 . I made a logic change to pcg.c that occurs to me should be enough to fix the issues people are having. We would just require (for now) that when SetPrecondMatrix() is called, that matrix should not be destroyed prematurely. We can add the reference counting later.

Would you please test out the mixed precision PCG cases to make sure this is working correctly? I have already run some of our standard regression tests and this looks good in the single precision case. If this looks okay, I will go ahead and make similar changes to the other Krylov solvers.

Thanks!

@rfalgout I just tested the mixed-precision PCG on the branch and it all looks good.

Thanks.

@rfalgout rfalgout marked this pull request as ready for review December 26, 2025 22:19
@rfalgout
Copy link
Contributor Author

Hi @oseikuffuor1 and @liruipeng . There were some difficult-to-find memory leaks that I had to fix - ParCSRMatrixCreate is not the only routine that creates matrices, so I needed to set the reference counter there also. I have changed the handling of precond_Mat in the other two Krylov routines that use it, and I did some prototype and casting clean up as well. I'm running regression tests now, but this is ready for you to review. Thanks!

@rfalgout
Copy link
Contributor Author

rfalgout commented Jan 6, 2026

Hi @oseikuffuor1 and @liruipeng . Just following up on this PR. It's ready for final review. Thanks!

void *precond_data = (pcg_data -> precond_data);
// preconditioning matrix
void *precond_Mat = (pcg_data -> precond_Mat) ;
void *precond_Mat = (pcg_data -> precond_Mat) ; // preconditioning matrix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this comment be above the preceding line or did you intend to remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved the comment to the end of the precond_Mat declaration line. I thought it looked better. :)

Copy link
Contributor

@oseikuffuor1 oseikuffuor1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good the me, thank you!

@liruipeng
Copy link
Contributor

Hi @oseikuffuor1 and @liruipeng . There were some difficult-to-find memory leaks that I had to fix - ParCSRMatrixCreate is not the only routine that creates matrices, so I needed to set the reference counter there also. I have changed the handling of precond_Mat in the other two Krylov routines that use it, and I did some prototype and casting clean up as well. I'm running regression tests now, but this is ready for you to review. Thanks!

Hi @rfalgout . I guess when a parcsr matrix is not constructed using the constructor, we have to manually set the ref counter, such as in several places you did

matrix = hypre_CTAlloc(hypre_ParCSRMatrix, 1, HYPRE_MEMORY_HOST);
hypre_ParCSRMatrixRefCount(matrix) = 1;

which makes sense. Just curious if we have covered all of these cases.
Thanks

Copy link
Contributor

@liruipeng liruipeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks @rfalgout

@rfalgout
Copy link
Contributor Author

rfalgout commented Jan 8, 2026

Hi @rfalgout . I guess when a parcsr matrix is not constructed using the constructor, we have to manually set the ref counter, such as in several places you did

matrix = hypre_CTAlloc(hypre_ParCSRMatrix, 1, HYPRE_MEMORY_HOST);
hypre_ParCSRMatrixRefCount(matrix) = 1;

which makes sense. Just curious if we have covered all of these cases. Thanks

I am pretty sure I covered all the cases. I did an appropriately-constructed grep for allocation of the matrix structure, and didn't find anything other than what's been fixed here. Thanks!

@rfalgout rfalgout merged commit af1c957 into master Jan 8, 2026
@rfalgout rfalgout deleted the mat-ref-count branch January 8, 2026 17:09
@nmnobre
Copy link

nmnobre commented Jan 14, 2026

Do we think this could grant a minor 3.0.1 release?

@rfalgout
Copy link
Contributor Author

Do we think this could grant a minor 3.0.1 release?

Yes, I think we can do that. Stay tuned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants