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

cdef memory _buffer = None

cdef inline int attach_buffer(ob, void **p, MPI_Count *n) except -1:
    global _buffer
    cdef void *bptr = NULL
    cdef MPI_Aint blen = 0
    _buffer = asbuffer_w(ob, &bptr, &blen)
    p[0] = bptr
    n[0] = blen
    return 0

cdef inline object detach_buffer(void *p, MPI_Count n):
    global _buffer
    cdef object ob = None
    try:
        if (_buffer is not None and
            _buffer.view.buf == p and
            _buffer.view.obj != NULL):
            ob = <object>_buffer.view.obj
        else:
            ob = mpibuf(p, n)
    finally:
        _buffer = None
    return ob

# -----------------------------------------------------------------------------

cdef object __UNWEIGHTED__    = <MPI_Aint>MPI_UNWEIGHTED

cdef object __WEIGHTS_EMPTY__ = <MPI_Aint>MPI_WEIGHTS_EMPTY



cdef inline bint is_UNWEIGHTED(object weights):
    return is_constant(weights, __UNWEIGHTED__)

cdef inline bint is_WEIGHTS_EMPTY(object weights):
    return is_constant(weights, __WEIGHTS_EMPTY__)


cdef object asarray_weights(object weights, int nweight, int **iweight):
    if weights is None:
        iweight[0] = MPI_UNWEIGHTED
        return None
    if is_UNWEIGHTED(weights):
        iweight[0] = MPI_UNWEIGHTED
        return None
    if is_WEIGHTS_EMPTY(weights):
        if nweight > 0: raise ValueError("empty weights but nonzero degree")
        iweight[0] = MPI_WEIGHTS_EMPTY
        return None
    return chkarray(weights, nweight, iweight)

# -----------------------------------------------------------------------------

cdef inline int comm_neighbors_count(MPI_Comm comm,
                                     int *incoming,
                                     int *outgoing,
                                     ) except -1:
    cdef int topo = MPI_UNDEFINED
    cdef int size=0, ndims=0, rank=0, nneighbors=0
    cdef int indegree=0, outdegree=0, weighted=0
    CHKERR( MPI_Topo_test(comm, &topo) )
    if topo == MPI_UNDEFINED: # XXX
        CHKERR( MPI_Comm_size(comm, &size) )
        indegree = outdegree = size
    elif topo == MPI_CART:
        CHKERR( MPI_Cartdim_get(comm, &ndims) )
        indegree = outdegree = <int>2*ndims
    elif topo == MPI_GRAPH:
        CHKERR( MPI_Comm_rank(comm, &rank) )
        CHKERR( MPI_Graph_neighbors_count(
                comm, rank, &nneighbors) )
        indegree = outdegree = nneighbors
    elif topo == MPI_DIST_GRAPH:
        CHKERR( MPI_Dist_graph_neighbors_count(
                comm, &indegree, &outdegree, &weighted) )
    if incoming != NULL: incoming[0] = indegree
    if outgoing != NULL: outgoing[0] = outdegree
    return 0

# -----------------------------------------------------------------------------

cdef object Lock = None
try:
    from _thread import allocate_lock as Lock
except ImportError:
    from _dummy_thread import allocate_lock as Lock

cdef int  lock_keyval   = MPI_KEYVAL_INVALID
cdef dict lock_registry = {}

cdef inline int lock_free_cb(MPI_Comm comm) \
    except MPI_ERR_UNKNOWN with gil:
    try: del lock_registry[<Py_uintptr_t>comm]
    except KeyError: pass
    return MPI_SUCCESS

@cython.callspec("MPIAPI")
cdef int lock_free_fn(MPI_Comm comm, int keyval,<commit_msg>refactor: Import built-in _thread module unconditionally under Py>=3.9<commit_after># -----------------------------------------------------------------------------

cdef memory _buffer = None

cdef inline int attach_buffer(ob, void **p, MPI_Count *n) except -1:
    global _buffer
    cdef void *bptr = NULL
    cdef MPI_Aint blen = 0
    _buffer = asbuffer_w(ob, &bptr, &blen)
    p[0] = bptr
    n[0] = blen
    return 0

cdef inline object detach_buffer(void *p, MPI_Count n):
    global _buffer
    cdef object ob = None
    try:
        if (_buffer is not None and
            _buffer.view.buf == p and
            _buffer.view.obj != NULL):
            ob = <object>_buffer.view.obj
        else:
            ob = mpibuf(p, n)
    finally:
        _buffer = None
    return ob

# -----------------------------------------------------------------------------

cdef object __UNWEIGHTED__    = <MPI_Aint>MPI_UNWEIGHTED

cdef object __WEIGHTS_EMPTY__ = <MPI_Aint>MPI_WEIGHTS_EMPTY



cdef inline bint is_UNWEIGHTED(object weights):
    return is_constant(weights, __UNWEIGHTED__)

cdef inline bint is_WEIGHTS_EMPTY(object weights):
    return is_constant(weights, __WEIGHTS_EMPTY__)


cdef object asarray_weights(object weights, int nweight, int **iweight):
    if weights is None:
        iweight[0] = MPI_UNWEIGHTED
        return None
    if is_UNWEIGHTED(weights):
        iweight[0] = MPI_UNWEIGHTED
        return None
    if is_WEIGHTS_EMPTY(weights):
        if nweight > 0: raise ValueError("empty weights but nonzero degree")
        iweight[0] = MPI_WEIGHTS_EMPTY
        return None
    return chkarray(weights, nweight, iweight)

# -----------------------------------------------------------------------------

cdef inline int comm_neighbors_count(MPI_Comm comm,
                                     int *incoming,
                                     int *outgoing,
                                     ) except -1:
    cdef int topo = MPI_UNDEFINED
    cdef int size=0, ndims=0, rank=0, nneighbors=0
    cdef int indegree=0, outdegree=0, weighted=0
    CHKERR( MPI_Topo_test(comm, &topo) )
    if topo == MPI_UNDEFINED: # XXX
        CHKERR( MPI_Comm_size(comm, &size) )
        indegree = outdegree = size
    elif topo == MPI_CART:
        CHKERR( MPI_Cartdim_get(comm, &ndims) )
        indegree = outdegree = <int>2*ndims
    elif topo == MPI_GRAPH:
        CHKERR( MPI_Comm_rank(comm, &rank) )
        CHKERR( MPI_Graph_neighbors_count(
                comm, rank, &nneighbors) )
        indegree = outdegree = nneighbors
    elif topo == MPI_DIST_GRAPH:
        CHKERR( MPI_Dist_graph_neighbors_count(
                comm, &indegree, &outdegree, &weighted) )
    if incoming != NULL: incoming[0] = indegree
    if outgoing != NULL: outgoing[0] = outdegree
    return 0

# -----------------------------------------------------------------------------

cdef object Lock = None

if PY_VERSION_HEX >= 0x030900F0:
    from _thread import allocate_lock as Lock
else:
    try:
        from _thread import allocate_lock as Lock
    except ImportError:
        from _dummy_thread import allocate_lock as Lock

cdef int  lock_keyval   = MPI_KEYVAL_INVALID
cdef dict lock_registry = {}

cdef inline int lock_free_cb(MPI_Comm comm) \
    except MPI_ERR_UNKNOWN with gil:
    try: del lock_registry[<Py_uintptr_t>comm]
    except KeyError: pass
    return MPI_SUCCESS

@cython.callspec("MPIAPI")
cdef int lock_free_fn(MPI_Comm comm, int keyval,
{'fraction_non_alphanumeric': 0.16279069767441862, 'fraction_numerical': 0.007702562583320989, 'mean_word_length': 1.910344827586207, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2732752', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2112, 'n_words': 554}