[petsc-users] Error handling in petsc4py

Jose E. Roman jroman at dsic.upv.es
Mon Nov 27 10:55:27 CST 2023


The exception has been caught and the execution reaches the print() statement. I think you just need to disable the PETSc error handler, try with this:

  PETSc.Sys.pushErrorHandler("ignore")

Jose



> El 27 nov 2023, a las 16:41, Barry Smith <bsmith at petsc.dev> escribió:
> 
> 
>   I see
> 
> cdef extern from * nogil:
>   ctypedef enum PetscErrorCode:
>     PETSC_SUCCESS
>     PETSC_ERR_PLIB
>     PETSC_ERR_SUP
>     PETSC_ERR_USER
>     PETSC_ERR_MEM
>     PETSC_ERR_MPI
>     PETSC_ERR_PYTHON
> 
>   ctypedef enum PetscErrorType:
>     PETSC_ERROR_INITIAL
>     PETSC_ERROR_REPEAT
> 
> cdef PetscErrorCode CHKERR(PetscErrorCode) except PETSC_ERR_PYTHON nogil
> 
> in src/binding/petsc4py/src/petsc4py/PETSc.pxd
> 
> I don't know enough about cython to know how it could be accessible from Python code.
> 
> 
> 
>> On Nov 27, 2023, at 3:46 AM, Miguel Angel Salazar de Troya <miguel.salazar at corintis.com> wrote:
>> 
>> Hello,
>> 
>> Is there any way to get the PETSc error codes in the python interface? The test I provided below is just a simple example that I know will run out of memory.
>> 
>> Miguel
>> 
>> On Wed, Nov 15, 2023 at 10:00 AM Miguel Angel Salazar de Troya <miguel.salazar at corintis.com> wrote:
>> Hello,
>> 
>> The following simple petsc4py snippet runs out of memory, but I would like to handle it from python with the usual try-except. Is there any way to do so? How can I get the PETSc error codes in the python interface?
>> 
>> Thanks
>> 
>> from petsc4py import PETSc
>> import sys, petsc4py
>> petsc4py.init(sys.argv)
>> try:
>>     m, n = 1000000, 1000000
>>     A = PETSc.Mat().createAIJ([m, n], nnz=1e6)
>>    
>>     A.assemblyBegin()
>>     A.assemblyEnd()
>> except Exception as e:
>>     print(f"An error occurred: {e}")
>> 
>> An error occurred: error code 55
>> [0] MatSeqAIJSetPreallocation() at /Users/miguel/repos/firedrake-glacierware/src/petsc/src/mat/impls/aij/seq/aij.c:3942
>> [0] MatSeqAIJSetPreallocation_SeqAIJ() at /Users/miguel/repos/firedrake-glacierware/src/petsc/src/mat/impls/aij/seq/aij.c:4008
>> [0] PetscMallocA() at /Users/miguel/repos/firedrake-glacierware/src/petsc/src/sys/memory/mal.c:408
>> [0] PetscMallocAlign() at /Users/miguel/repos/firedrake-glacierware/src/petsc/src/sys/memory/mal.c:53
>> [0] Out of memory. Allocated: 0, Used by process: 59752448
>> [0] Memory requested 18446744064984991744
>> 
>>  
>> 
> 



More information about the petsc-users mailing list