SmartPetscObj<PC> functions

10 views
Skip to first unread message
Assigned to lik...@wp.pl by me

Karol Lewandowski

unread,
Dec 5, 2021, 5:52:34 AM12/5/21
to MoFEM Q&A
How to make the following snippet work? 
Petsc function is expecting a raw pointer here. 
SmartPetscObj<PC> pC;
CHKERR KSPGetPC(ksp, pC)
​Pet

Lukasz Kaczmraczyk

unread,
Dec 5, 2021, 6:18:51 AM12/5/21
to MoFEM Q&A

Karol,

When you look here
https://petsc.org/release/docs/manualpages/KSP/KSPGetPC.html
according to PETSc documentation KSPGetPC returns a pointer, not an object, which you have to destroy when is no longer needed. So, you do not need to manage it with a smart pointer, and if you do, that will create a segmentation fault, once you go out of the scope, and smart petsc pointer will try to destroy the object. This is a typical case.

Converting petsc pointer to smart pointer

If you like to see how to convert a pointer to a smart petsc pointer, for pets object which you have to destroy, look here
http://mofem.eng.gla.ac.uk/mofem/html/petsc_smart_ptr_objects_8cpp-example.html

For example,

Mat m; CHKERR MatCreate(PETSC_COMM_SELF, &m); auto   m_ptr = SmartPetscObj<Mat>(m, false);

however, in such a case, os better to use

auto m_ptr = createSmartVectorMPI(PETSC_COMM_WORLD, nb_loc, nb_glob);
`

see here doc for createSmartVectorMPI
http://mofem.eng.gla.ac.uk/mofem/html/namespace_mo_f_e_m.html#a80dca487877a37c15c740c9c8258ce40

Managing existence of PC

You could anyway write a code such that it prevents the destruction of PC when KSP is destroyed, and bum use reference of it. In such case you would do:

PC pc_raw; CHKERR KSPGetPC(ksp, pc_raw)
auto pc_smart = SmartPetscObj<PC>(pc_raw, true);
`

Note the second argument in reference,  i.e. "true", it bumps reference of the object. This will guarantee that the object exists until pc_smart gets out of the scope.

Regards,
Lukasz

Reply all
Reply to author
Forward
0 new messages