Iteration über eine Liste, in parallele mit Cython

Wie macht man iteriert parallel über ein (Python -) Liste in Cython?

Betrachten Sie die folgende einfache Funktion:

def sumList():
    cdef int n = 1000
    cdef int sum = 0

    ls = [i for i in range(n)]

    cdef Py_ssize_t i
    for i in prange(n, nogil=True):
        sum += ls[i]

    return sum

Dies gibt eine Menge von compiler-Fehler, da einem parallelen Abschnitt ohne die GIL anscheinend nicht die Arbeit mit Python-Objekt:

Error compiling Cython file:
------------------------------------------------------------
...

    ls = [i for i in range(n)]

    cdef Py_ssize_t i
    for i in prange(n, nogil=True):
        sum += ls[i]
     ^
------------------------------------------------------------

src/parallel.pyx:42:6: Coercion from Python not allowed without the GIL

Error compiling Cython file:
------------------------------------------------------------
...

    ls = [i for i in range(n)]

    cdef Py_ssize_t i
    for i in prange(n, nogil=True):
        sum += ls[i]
     ^
------------------------------------------------------------

src/parallel.pyx:42:6: Operation not allowed without gil

Error compiling Cython file:
------------------------------------------------------------
...

    ls = [i for i in range(n)]

    cdef Py_ssize_t i
    for i in prange(n, nogil=True):
        sum += ls[i]
     ^
------------------------------------------------------------

src/parallel.pyx:42:6: Converting to Python object not allowed without gil

Error compiling Cython file:
------------------------------------------------------------
...

    ls = [i for i in range(n)]

    cdef Py_ssize_t i
    for i in prange(n, nogil=True):
        sum += ls[i]
          ^
------------------------------------------------------------

src/parallel.pyx:42:11: Indexing Python object not allowed without gil
  • Das ist vielleicht nicht unmittelbar nützlich, aber haben Sie jemals versucht, D??? Es ist c-syntax, und Sachen, wie die parallele iteration über Listen (arrays oder Bereiche in D) ist fast so einfach, wie es zu tun in MATLAB, wenn Sie vertraut sind. Check-out die Beispiel hier: . std.Parallelität. Ich fand es sehr leicht von einem Python-hintergrund.
  • Nie versucht, D, werfen Sie einen Blick.
  • Experimentiert mit D, mochte die pythonic syntax. Aber hier ist ein Problem, das ich gestoßen bin: stackoverflow.com/questions/17837098/...
InformationsquelleAutor clstaudt | 2013-07-23
Schreibe einen Kommentar