[petsc-users] Error handling in petsc4py

Barry Smith bsmith at petsc.dev
Mon Nov 27 09:41:49 CST 2023


  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 <mailto: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
>> 
>>  
>> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20231127/4c23c735/attachment.html>


More information about the petsc-users mailing list