numba guvectorize target = 'parallel' langsamer als target = 'cpu'

Ich habe versucht, einen Teil des Python-Codes zu optimieren, der umfangreiche mehrdimensionale Array-Berechnungen umfasst. Ich erhalte kontraproduktive Ergebnisse mit Numba. Ich laufe auf einem MBP, Mitte 2015, 2,5 GHz i7 Quadcore, OS 10.10.5, Python 2.7.11. Folgendes berücksichtigen

 import numpy as np
 from numba import jit, vectorize, guvectorize
 import numexpr as ne
 import timeit

 def add_two_2ds_naive(A,B,res):
     for i in range(A.shape[0]):
         for j in range(B.shape[1]):
             res[i,j] = A[i,j]+B[i,j]

 @jit
 def add_two_2ds_jit(A,B,res):
     for i in range(A.shape[0]):
         for j in range(B.shape[1]):
             res[i,j] = A[i,j]+B[i,j]

 @guvectorize(['float64[:,:],float64[:,:],float64[:,:]'],
    '(n,m),(n,m)->(n,m)',target='cpu')
 def add_two_2ds_cpu(A,B,res):
     for i in range(A.shape[0]):
         for j in range(B.shape[1]):
             res[i,j] = A[i,j]+B[i,j]

 @guvectorize(['(float64[:,:],float64[:,:],float64[:,:])'],
    '(n,m),(n,m)->(n,m)',target='parallel')
 def add_two_2ds_parallel(A,B,res):
     for i in range(A.shape[0]):
         for j in range(B.shape[1]):
             res[i,j] = A[i,j]+B[i,j]

 def add_two_2ds_numexpr(A,B,res):
     res = ne.evaluate('A+B')

 if __name__=="__main__":
     np.random.seed(69)
     A = np.random.rand(10000,100)
     B = np.random.rand(10000,100)
     res = np.zeros((10000,100))

Ich kann jetzt timeit für die verschiedenen Funktionen ausführen:

%timeit add_two_2ds_jit(A,B,res)
1000 loops, best of 3: 1.16 ms per loop

%timeit add_two_2ds_cpu(A,B,res)
1000 loops, best of 3: 1.19 ms per loop

%timeit add_two_2ds_parallel(A,B,res)
100 loops, best of 3: 6.9 ms per loop

%timeit add_two_2ds_numexpr(A,B,res)
1000 loops, best of 3: 1.62 ms per loop

Es scheint, dass 'parallel' nicht einmal die Mehrheit eines einzelnen Kerns nutzt, wie es in @ verwendet wirtop zeigt, dass Python ~ 40% CPU für 'Parallel', ~ 100% für 'CPU' und numexpr Treffer ~ 300% erreicht.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage