<commit_before># --------------------------------------------------------------------

cdef class SF(Object):

    def __cinit__(self):
        if PETSC_VERSION_GT(3,3,0):
            self.obj = <PetscObject*> &self.sf
        self.sf  = NULL

    def __dealloc__(self):
        CHKERR( PetscSFDestroy(&self.sf) )
        self.sf = NULL

    def view(self, Viewer viewer=None):
        cdef PetscViewer vwr = NULL
        if viewer is not None: vwr = viewer.vwr
        CHKERR( PetscSFView(self.sf, vwr) )

    def destroy(self):
        CHKERR( PetscSFDestroy(&self.sf) )
        return self

    def create(self, comm=None):
        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
        cdef PetscSF newsf = NULL
        CHKERR( PetscSFCreate(ccomm, &newsf) )
        PetscCLEAR(self.obj); self.sf = newsf
        return self

    def setUp(self):
        CHKERR( PetscSFSetUp(self.sf) )

    def reset(self):
        CHKERR( PetscSFReset(self.sf) )

    def getGraph(self):
        cdef PetscInt nroots = 0, nleaves = 0
        cdef const_PetscInt *ilocal = NULL
        cdef const_PetscSFNode *iremote
        CHKERR( PetscSFGetGraph(self.sf, &nroots, &nleaves, &ilocal, &iremote) )
        local = array_i(nleaves, ilocal)
        remote = []
        for i in range(toInt(nleaves)):
            sfnode = (toInt(iremote[asInt(i)].rank), toInt(iremote[asInt(i)].index))
            remote.append( sfnode )
        return asInt(nroots), asInt(nleaves), local, remote

    def setGraph(self, nroots, nleaves, local, remote):
        cdef PetscInt cnroots = asInt(nroots)
        cdef PetscInt cnleaves = asInt(nleaves)
        cdef PetscInt nlocal = 0
        cdef PetscInt *ilocal = NULL
        local = iarray_i(local, &nlocal, &ilocal)
        cdef PetscSFNode* iremote = NULL
        CHKERR( PetscMalloc(nleaves*sizeof(PetscSFNode), &iremote) )
        cdef int i = 0
        for rank, index in remote:
            iremote[i].rank = rank
            iremote[i].index = index
            i += 1
        CHKERR( PetscSFSetGraph(self.sf, cnroots, cnleaves, ilocal, PETSC_COPY_VALUES, iremote, PETSC_COPY_VALUES) )<commit_msg>DMPlex: Fix memory leak in SF.setGraph()<commit_after># --------------------------------------------------------------------

cdef class SF(Object):

    def __cinit__(self):
        if PETSC_VERSION_GT(3,3,0):
            self.obj = <PetscObject*> &self.sf
        self.sf  = NULL

    def __dealloc__(self):
        CHKERR( PetscSFDestroy(&self.sf) )
        self.sf = NULL

    def view(self, Viewer viewer=None):
        cdef PetscViewer vwr = NULL
        if viewer is not None: vwr = viewer.vwr
        CHKERR( PetscSFView(self.sf, vwr) )

    def destroy(self):
        CHKERR( PetscSFDestroy(&self.sf) )
        return self

    def create(self, comm=None):
        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
        cdef PetscSF newsf = NULL
        CHKERR( PetscSFCreate(ccomm, &newsf) )
        PetscCLEAR(self.obj); self.sf = newsf
        return self

    def setUp(self):
        CHKERR( PetscSFSetUp(self.sf) )

    def reset(self):
        CHKERR( PetscSFReset(self.sf) )

    def getGraph(self):
        cdef PetscInt nroots = 0, nleaves = 0
        cdef const_PetscInt *ilocal = NULL
        cdef const_PetscSFNode *iremote
        CHKERR( PetscSFGetGraph(self.sf, &nroots, &nleaves, &ilocal, &iremote) )
        local = array_i(nleaves, ilocal)
        remote = []
        for i in range(toInt(nleaves)):
            sfnode = (toInt(iremote[asInt(i)].rank), toInt(iremote[asInt(i)].index))
            remote.append( sfnode )
        return asInt(nroots), asInt(nleaves), local, remote

    def setGraph(self, nroots, nleaves, local, remote):
        cdef PetscInt cnroots = asInt(nroots)
        cdef PetscInt cnleaves = asInt(nleaves)
        cdef PetscInt nlocal = 0
        cdef PetscInt *ilocal = NULL
        local = iarray_i(local, &nlocal, &ilocal)
        cdef PetscSFNode* iremote = NULL
        CHKERR( PetscMalloc(nleaves*sizeof(PetscSFNode), &iremote) )
        cdef int i = 0
        for rank, index in remote:
            iremote[i].rank = rank
            iremote[i].index = index
            i += 1
        CHKERR( PetscSFSetGraph(self.sf, cnroots, cnleaves, ilocal, PETSC_COPY_VALUES, iremote, PETSC_OWN_POINTER) )
{'fraction_non_alphanumeric': 0.13339530560074367, 'fraction_numerical': 0.0037183360446200325, 'mean_word_length': 2.153113553113553, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1951430', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1500, 'n_words': 337}