[petsc-users] Calculating inertias

Jose E. Roman jroman at dsic.upv.es
Tue Mar 24 12:32:33 CDT 2020


MatDestroy(&B) should not be called, for the same reason as the PC - it is owned by the PC object, it just gives you a pointer. This is stated in the manpage of PCFactorGetMatrix:

   Notes:
    Does not increase the reference count for the matrix so DO NOT destroy it

As a general rule, objects returned by functions XXXGetYYY() should not be destroyed, while objects returned by functions XXXCreateYYY() should.

Jose


> El 24 mar 2020, a las 18:26, Perceval Desforges <perceval.desforges at polytechnique.edu> escribió:
> 
> Thank you but I am still confused, I still get the same error message when calling KSPDestroy(). My code looks something like this:
> 
> 
> 
> ierr = MatShift(M,-E);
> ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);
> ierr = KSPSetOperators(ksp,M,M);
> ierr = KSPSetType(ksp,KSPPREONLY);
> ierr = KSPGetPC(ksp,&pc);
> ierr = PCSetType(pc,PCCHOLESKY);
> ierr = KSPSetUp(ksp);
> ierr = PCFactorGetMatrix(pc,&B);
> ierr = MatGetInertia(B,&nneg,&nzero,&npos);
> ierr = PetscPrintf(PETSC_COMM_WORLD,"nneg: %D    nzero: %D    npos: %D   \n",nneg,nzero,npos);
> ierr = MatDestroy(&B);
> ierr = MatDestroy(&M);
> ierr = KSPDestroy(&ksp);
> 
> where I've defined the matrix M beforehand. What am I doing wrong?
> 
> Thanks again,
> 
> Best regards,
> 
> Perceval,
> 
> 
> 
>> If you obtained the PC object with KSPGetPC() then you do not have to destroy it, only the KSP object. KSPGetPC() only gives a pointer to the internal object, which is managed by the KSP.
>> Jose
>> 
>> 
>>> El 24 mar 2020, a las 17:18, Perceval Desforges <perceval.desforges at polytechnique.edu> escribió:
>>> 
>>> Thank you very much, this seems to work well.
>>> 
>>> I have another question linked to this which is probably a bit basic so I apologize.
>>> 
>>> At the end of my program, I want to destroy all the petsc objects. I start by destroying the matrixes, then the ksp and pc objects similarly to how it's done in the examples. However I get an error when attempting to destroy the KSP and PC objects :
>>> 
>>> [0]PETSC ERROR: Invalid argument
>>> 
>>> [0]PETSC ERROR: Wrong type of object: Parameter # 1
>>> 
>>> I've tried switching the order around, but I still get the same errors. 
>>> 
>>> And if I don't destroy these objects I get a memory leak, which I'd like to avoid.
>>> 
>>> My question is I don't really understand when and how I'm supposed to destroy the KSP and PC objects? 
>>> 
>>> Thanks again,
>>> 
>>> Best regards,
>>> 
>>> Perceval,
>>> 
>>> 
>>> 
>>> 
>>> 
>>>> You can do this directly in PETSc. Create a KSP object with PREONLY and PCCHOLESKY (or just a PC object). Then call KSPSetUp (or PCSetUp) and extract the factored matrix with PCFactorGetMatrix(). Then call MatGetInertia() on the factored matrix. Repeat this for each value of E.
>>>> 
>>>> I guess it can be even shorter if you call MatCholeskyFactor() directly.
>>>> 
>>>> Jose
>>>> 
>>>> 
>>>>> El 24 mar 2020, a las 11:07, Perceval Desforges <perceval.desforges at polytechnique.edu> escribió:
>>>>> 
>>>>> Dear petsc developers,
>>>>> 
>>>>> I am interested in calculating the inertias of matrixes. Specifically, for a certain matrix A, and for different real numbers E, I want to calculate the inertias of (A - E * I), in order to get the number of eigenvalues less than E. 
>>>>> 
>>>>> In order to do this I have been setting up a slepc EPS object with spectrum slicing, and using EPSKrylovSchurGetInertias. I realize this is a bit convoluted, and was wondering if there is a better way to do this?
>>>>> 
>>>>> Best regards,
>>>>> 
>>>>> Perceval,
>>>>> 
>>>>> P.S. my last email seems to not have been sent (I couldn't find it in the archives) so I am trying again...
>>>>> 
>>> 
>>> 
> 
> 



More information about the petsc-users mailing list