問題描述
我正在使用 Python 的 Anaconda 發行版以及 Numba,并且我編寫了以下 Python 函數,該函數乘以稀疏矩陣 A
(存儲在CSR 格式)由密集向量 x
:
I'm using the Anaconda distribution of Python, together with Numba, and I've written the following Python function that multiplies a sparse matrix A
(stored in a CSR format) by a dense vector x
:
@jit
def csrMult( x, Adata, Aindices, Aindptr, Ashape ):
numRowsA = Ashape[0]
Ax = numpy.zeros( numRowsA )
for i in range( numRowsA ):
Ax_i = 0.0
for dataIdx in range( Aindptr[i], Aindptr[i+1] ):
j = Aindices[dataIdx]
Ax_i += Adata[dataIdx] * x[j]
Ax[i] = Ax_i
return Ax
這里A
是一個很大的scipy
稀疏矩陣,
>>> A.shape
( 56469, 39279 )
# having ~ 142,258,302 nonzero entries (so about 6.4% )
>>> type( A[0,0] )
dtype( 'float32' )
和 x
是一個 numpy
數組.這是調用上述函數的代碼片段:
and x
is a numpy
array. Here is a snippet of code that calls the above function:
x = numpy.random.randn( A.shape[1] )
Ax = A.dot( x )
AxCheck = csrMult( x, A.data, A.indices, A.indptr, A.shape )
注意 @jit
裝飾器,它告訴 Numba 對 csrMult()
進行即時編譯 功能.
Notice the @jit
-decorator that tells Numba to do a just-in-time compilation for the csrMult()
function.
在我的實驗中,我的函數 csrMult()
大約是 scipy
.dot()
方法.這對 Numba 來說是一個非常令人印象深刻的結果.
In my experiments, my function csrMult()
is about twice as fast as the scipy
.dot()
method. That is a pretty impressive result for Numba.
但是,MATLAB 執行矩陣向量乘法的速度仍然比 csrMult()
快 6 倍.我相信這是因為 MATLAB 在執行稀疏矩陣向量乘法時使用了多線程.
However, MATLAB still performs this matrix-vector multiplication about 6 times faster than csrMult()
. I believe that is because MATLAB uses multithreading when performing sparse matrix-vector multiplication.
使用 Numba 時如何并行化外部 for
循環?
How can I parallelize the outer for
-loop when using Numba?
Numba 曾經有一個 prange()
函數,這使得并行化變得簡單,令人尷尬的并行 for
-循環.不幸的是,Numba 不再具有 prange()
[實際上,這是錯誤的,請參閱下面的編輯].那么現在并行化這個 for
循環的正確方法是什么,Numba 的 prange()
函數不見了?
Numba used to have a prange()
function, that made it simple to parallelize embarassingly parallel for
-loops. Unfortunately, Numba no longer has prange()
[actually, that is false, see the edit below]. So what is the correct way to parallelize this for
-loop now, that Numba's prange()
function is gone?
當 prange()
從 Numba 中移除時,Numba 的開發人員想到了哪些替代方案?
When prange()
was removed from Numba, what alternative did the developers of Numba have in mind?
編輯 1:
我更新到 Numba 的最新版本,即 .35,prange()
又回來了!它不包含在我一直使用的版本 .33 中.
這是個好消息,但不幸的是,當我嘗試使用 prange()
并行化我的 for 循環時收到一條錯誤消息.這是 Numba 文檔中的一個并行 for 循環 示例(請參閱第 1.9.2 節顯式并行循環"),下面是我的新代碼:
Edit 1:
I updated to the latest version of Numba, which is .35, andprange()
is back! It was not included in version .33, the version I had been using.
That is good news, but unfortunately I am getting an error message when I attempt to parallelize my for loop usingprange()
. Here is a parallel for loop example from the Numba documentation (see section 1.9.2 "Explicit Parallel Loops"), and below is my new code:
from numba import njit, prange
@njit( parallel=True )
def csrMult_numba( x, Adata, Aindices, Aindptr, Ashape):
numRowsA = Ashape[0]
Ax = np.zeros( numRowsA )
for i in prange( numRowsA ):
Ax_i = 0.0
for dataIdx in range( Aindptr[i],Aindptr[i+1] ):
j = Aindices[dataIdx]
Ax_i += Adata[dataIdx] * x[j]
Ax[i] = Ax_i
return Ax
當我使用上面給出的代碼片段調用此函數時,我收到以下錯誤:
When I call this function, using the code snippet given above, I receive the following error:
AttributeError:在 nopython 處失敗(轉換為 parfors)'SetItem'對象沒有屬性get_targets"
AttributeError: Failed at nopython (convert to parfors) 'SetItem' object has no attribute 'get_targets'
<小時>
鑒于
上述使用 prange
的嘗試崩潰,我的問題是:
正確的方法是什么(使用 prange
或替代方法)并行化這個 Python for
-loop?強>
Given
the above attempt to use prange
crashes, my question stands:
What is the correct way ( using prange
or an alternative method ) to parallelize this Python for
-loop?
如下所述,在 20-omp-threads 上運行類似的 C++ 循環并獲得 8 倍 加速是微不足道的.必須有一種使用 Numba 的方法,因為 for 循環是令人尷尬的并行(并且因為稀疏矩陣向量乘法是科學計算中的基本操作).
As noted below, it was trivial to parallelize a similar for loop in C++ and obtain an 8x speedup, having been run on 20-omp-threads. There must be a way to do it using Numba, since the for loop is embarrassingly parallel (and since sparse matrix-vector multiplication is a fundamental operation in scientific computing).
編輯 2:
這是我的 csrMult()
的 C++ 版本.在 C++ 版本中并行化 for()
循環使我的測試中的代碼快了大約 8 倍.這向我表明,在使用 Numba 時,Python 版本應該可以實現類似的加速.
Edit 2:
Here is my C++ version ofcsrMult()
. Parallelizing thefor()
loop in the C++ version makes the code about 8x faster in my tests. This suggests to me that a similar speedup should be possible for the Python version when using Numba.
void csrMult(VectorXd& Ax, VectorXd& x, vector<double>& Adata, vector<int>& Aindices, vector<int>& Aindptr)
{
// This code assumes that the size of Ax is numRowsA.
#pragma omp parallel num_threads(20)
{
#pragma omp for schedule(dynamic,590)
for (int i = 0; i < Ax.size(); i++)
{
double Ax_i = 0.0;
for (int dataIdx = Aindptr[i]; dataIdx < Aindptr[i + 1]; dataIdx++)
{
Ax_i += Adata[dataIdx] * x[Aindices[dataIdx]];
}
Ax[i] = Ax_i;
}
}
}
推薦答案
Numba 已經更新,prange()
現在可以使用了! (我在回答我自己的問題.)
Numba has been updated and prange()
works now! (I'm answering my own question.)
本博文,日期為 2017 年 12 月 12 日.以下是博客的相關片段:
The improvements to Numba's parallel computing capabilities are discussed in this blog post, dated December 12, 2017. Here is a relevant snippet from the blog:
很久以前(超過 20 個版本!),Numba 曾經支持編寫名為 prange()
的并行循環的習慣用法.大一之后在 2014 年重構代碼庫,這個特性不得不被移除,但它一直是最常被請求的 Numba 功能之一從那之后.英特爾開發人員并行化陣列后表達,他們意識到帶回 prange
將是公平的容易
Long ago (more than 20 releases!), Numba used to have support for an idiom to write parallel for loops called
prange()
. After a major refactoring of the code base in 2014, this feature had to be removed, but it has been one of the most frequently requested Numba features since that time. After the Intel developers parallelized array expressions, they realized that bringing backprange
would be fairly easy
使用 Numba 版本 0.36.1,我可以使用以下簡單代碼并行化我令人尷尬的并行 for
-循環:
Using Numba version 0.36.1, I can parallelize my embarrassingly parallel for
-loop using the following simple code:
@numba.jit(nopython=True, parallel=True)
def csrMult_parallel(x,Adata,Aindices,Aindptr,Ashape):
numRowsA = Ashape[0]
Ax = np.zeros(numRowsA)
for i in numba.prange(numRowsA):
Ax_i = 0.0
for dataIdx in range(Aindptr[i],Aindptr[i+1]):
j = Aindices[dataIdx]
Ax_i += Adata[dataIdx]*x[j]
Ax[i] = Ax_i
return Ax
在我的實驗中,并行化 for
循環使函數的執行速度比我在問題開頭發布的版本快大約八倍,該版本已經使用 Numba,但未并行化.此外,在我的實驗中,并行版本比使用 scipy 的稀疏矩陣向量乘法函數的命令 Ax = A.dot(x)
快大約 5 倍.Numba 已經碾壓了 scipy,我終于有了一個 與 MATLAB 一樣快的 Python 稀疏矩陣向量乘法例程.
In my experiments, parallelizing the for
-loop made the function execute about eight times faster than the version I posted at the beginning of my question, which was already using Numba, but which was not parallelized. Moreover, in my experiments the parallelized version is about 5x faster than the command Ax = A.dot(x)
which uses scipy's sparse matrix-vector multiplication function. Numba has crushed scipy and I finally have a python sparse matrix-vector multiplication routine that is as fast as MATLAB.
這篇關于使用 Numba 時如何并行化此 Python for 循環的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!