Zeilenvertauschungen mit Numba

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
xmelfoyx
User
Beiträge: 3
Registriert: Mittwoch 24. November 2021, 12:49

Mittwoch 24. November 2021, 13:11

Hallo zusammen,
ist es möglich eine Zeilenvertauschung mit numba zu beschleunigen?

Code: Alles auswählen

from numba import jit
import numpy as np

#Nur ein Beispiel
def swap_rows(A):
    A[[0, 2]] = A[[2, 0]]
    return A

A = np.array([[1,2,3], [4,5,6], [7,8,9]])



t = %timeit -o swap_rows(A)

numba_swap = jit()(swap_rows)
t_numba = %timeit -o numba_swap(A)
dabei bekomme ich diese Ausgabe
3.82 µs ± 14.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

und diese Warnung

<ipython-input-4-91659dcd8c37>:5: NumbaWarning:
Compilation is falling back to object mode WITH looplifting enabled because Function "swap_rows" failed type inference due to: No implementation of function Function(<built-in function getitem>) found for signature:

>>> getitem(array(int64, 2d, C), list(int64)<iv=[2, 0]>)

There are 22 candidate implementations:
- Of which 20 did not match due to:
Overload of function 'getitem': File: <numerous>: Line N/A.
With argument(s): '(array(int64, 2d, C), list(int64)<iv=None>)':
No match.
- Of which 1 did not match due to:
Overload in function 'GetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 162.
With argument(s): '(array(int64, 2d, C), list(int64)<iv=None>)':
Rejected as the implementation raised a specific error:
TypeError: unsupported array index type list(int64)<iv=None> in [list(int64)<iv=None>]
raised from /home/yoflem/anaconda3/lib/python3.8/site-packages/numba/core/typing/arraydecl.py:68
- Of which 1 did not match due to:
Overload in function 'GetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 162.
With argument(s): '(array(int64, 2d, C), list(int64)<iv=[2, 0]>)':
Rejected as the implementation raised a specific error:
TypeError: unsupported array index type list(int64)<iv=[2, 0]> in [list(int64)<iv=[2, 0]>]
raised from /home/yoflem/anaconda3/lib/python3.8/site-packages/numba/core/typing/arraydecl.py:68

During: typing of intrinsic-call at <ipython-input-4-91659dcd8c37> (6)
During: typing of static-get-item at <ipython-input-4-91659dcd8c37> (6)

File "<ipython-input-4-91659dcd8c37>", line 6:
def swap_rows(A):
A[[0, 2]] = A[[2, 0]]
^

def swap_rows(A):
/home/yoflem/anaconda3/lib/python3.8/site-packages/numba/core/object_mode_passes.py:151: NumbaWarning: Function "swap_rows" was compiled in object mode without forceobj=True.

File "<ipython-input-4-91659dcd8c37>", line 5:

def swap_rows(A):
^

warnings.warn(errors.NumbaWarning(warn_msg,
/home/yoflem/anaconda3/lib/python3.8/site-packages/numba/core/object_mode_passes.py:161: NumbaDeprecationWarning:
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.

For more information visit https://numba.pydata.org/numba-doc/late ... -using-jit

File "<ipython-input-4-91659dcd8c37>", line 5:

def swap_rows(A):
^

warnings.warn(errors.NumbaDeprecationWarning(msg,

4.04 µs ± 10.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
__deets__
User
Beiträge: 10711
Registriert: Mittwoch 14. Oktober 2015, 14:29

Mittwoch 24. November 2021, 13:24

Meine lineare Algebra ist etwas rostig, aber kannst du nicht einfach per Matrixmultiplikation Zeilen tauschen?
xmelfoyx
User
Beiträge: 3
Registriert: Mittwoch 24. November 2021, 12:49

Mittwoch 24. November 2021, 13:38

__deets__ hat geschrieben:
Mittwoch 24. November 2021, 13:24
Meine lineare Algebra ist etwas rostig, aber kannst du nicht einfach per Matrixmultiplikation Zeilen tauschen?
Habs grad getestet.
Bringt leider keine Verschnellerung
Sirius3
User
Beiträge: 15274
Registriert: Sonntag 21. Oktober 2012, 17:20

Mittwoch 24. November 2021, 15:14

Für numba muß man manchmal auf primitivere Mittel zurückgreifen:

Code: Alles auswählen

@jit
def numba_swap_rows(A):
    for i in range(A.shape[1]):
        A[0,i], A[2,i] = A[2,i], A[0,i]
    return A
xmelfoyx
User
Beiträge: 3
Registriert: Mittwoch 24. November 2021, 12:49

Mittwoch 24. November 2021, 16:48

Sirius3 hat geschrieben:
Mittwoch 24. November 2021, 15:14
Für numba muß man manchmal auf primitivere Mittel zurückgreifen:

Code: Alles auswählen

@jit
def numba_swap_rows(A):
    for i in range(A.shape[1]):
        A[0,i], A[2,i] = A[2,i], A[0,i]
    return A

Hi, Danke für die Hilfe, das hat geklappt.
hier der Code

Code: Alles auswählen

def numba_swap_rows(A):
    for i in range(A.shape[1]):
        A[0,i], A[2,i] = A[2,i], A[0,i]
    return A

A = np.random.rand(512,512)
numba_swap = jit(nopython=True)(numba_swap_rows)

t = %timeit -o numba_swap_rows(A)
t_numba = %timeit -o numba_swap(A)
print(t.best/t_numba.best)
output ist:
186 µs ± 1.41 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
335 ns ± 0.627 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
547.1658042438614

=> somit 547 mal schneller TOP
Antworten