使用MKL BLAS时,scipy是否支持稀疏矩阵乘法的多线程?

4jb9z9bj  于 2023-10-20  发布在  其他
关注(0)|答案(2)|浏览(158)

根据MKL BLAS文档,“所有矩阵-矩阵操作(第3级)都是密集和稀疏BLAS的线程。”http://software.intel.com/en-us/articles/parallelism-in-the-intel-math-kernel-library
我用MKL BLAS构建了Scipy。使用下面的测试代码,我看到了密集矩阵乘法的预期多线程加速,但稀疏矩阵乘法没有。Scipy是否有任何更改以启用多线程稀疏操作?

# test dense matrix multiplication
from numpy import *
import time    
x = random.random((10000,10000))
t1 = time.time()
foo = dot(x.T, x)
print time.time() - t1

# test sparse matrix multiplication
from scipy import sparse
x = sparse.rand(10000,10000)
t1 = time.time()
foo = dot(x.T, x)
print time.time() - t1
kgsdhlau

kgsdhlau1#

据我所知,答案是否定的。但是,您可以围绕MKL稀疏乘法例程构建自己的 Package 器。你问了两个稀疏矩阵相乘的问题。下面是我用来将一个稀疏矩阵乘以一个密集向量的一些 Package 器代码,因此应该不难修改(请参阅mkl_cspiral_dcsrgemm的英特尔MKL参考)。另外,请注意scipy数组是如何存储的:默认为COO,但CSR(或CSC)可能是更好的选择。我选择了csr,但MKL支持大多数类型(只需调用适当的例程)。
据我所知,scipy的default和MKL都是多线程的。通过改变OMP_NUM_THREADS,我可以看到性能上的差异。
要使用下面的函数,如果你有一个最新版本的MKL,只要确保你有LD_LIBRARY_PATHS设置为包括相关的MKL目录。对于旧版本,您需要构建一些特定的库。我从IntelMKL in python得到的信息

def SpMV_viaMKL( A, x ):
 """
 Wrapper to Intel's SpMV
 (Sparse Matrix-Vector multiply)
 For medium-sized matrices, this is 4x faster
 than scipy's default implementation
 Stephen Becker, April 24 2014
 [email protected]
 """

 import numpy as np
 import scipy.sparse as sparse
 from ctypes import POINTER,c_void_p,c_int,c_char,c_double,byref,cdll
 mkl = cdll.LoadLibrary("libmkl_rt.so")

 SpMV = mkl.mkl_cspblas_dcsrgemv
 # Dissecting the "cspblas_dcsrgemv" name:
 # "c" - for "c-blas" like interface (as opposed to fortran)
 #    Also means expects sparse arrays to use 0-based indexing, which python does
 # "sp"  for sparse
 # "d"   for double-precision
 # "csr" for compressed row format
 # "ge"  for "general", e.g., the matrix has no special structure such as symmetry
 # "mv"  for "matrix-vector" multiply

 if not sparse.isspmatrix_csr(A):
     raise Exception("Matrix must be in csr format")
 (m,n) = A.shape

 # The data of the matrix
 data    = A.data.ctypes.data_as(POINTER(c_double))
 indptr  = A.indptr.ctypes.data_as(POINTER(c_int))
 indices = A.indices.ctypes.data_as(POINTER(c_int))

 # Allocate output, using same conventions as input
 nVectors = 1
 if x.ndim is 1:
    y = np.empty(m,dtype=np.double,order='F')
    if x.size != n:
        raise Exception("x must have n entries. x.size is %d, n is %d" % (x.size,n))
 elif x.shape[1] is 1:
    y = np.empty((m,1),dtype=np.double,order='F')
    if x.shape[0] != n:
        raise Exception("x must have n entries. x.size is %d, n is %d" % (x.size,n))
 else:
    nVectors = x.shape[1]
    y = np.empty((m,nVectors),dtype=np.double,order='F')
    if x.shape[0] != n:
        raise Exception("x must have n entries. x.size is %d, n is %d" % (x.size,n))

 # Check input
 if x.dtype.type is not np.double:
    x = x.astype(np.double,copy=True)
 # Put it in column-major order, otherwise for nVectors > 1 this FAILS completely
 if x.flags['F_CONTIGUOUS'] is not True:
    x = x.copy(order='F')

 if nVectors == 1:
    np_x = x.ctypes.data_as(POINTER(c_double))
    np_y = y.ctypes.data_as(POINTER(c_double))
    # now call MKL. This returns the answer in np_y, which links to y
    SpMV(byref(c_char("N")), byref(c_int(m)),data ,indptr, indices, np_x, np_y ) 
 else:
    for columns in xrange(nVectors):
        xx = x[:,columns]
        yy = y[:,columns]
        np_x = xx.ctypes.data_as(POINTER(c_double))
        np_y = yy.ctypes.data_as(POINTER(c_double))
        SpMV(byref(c_char("N")), byref(c_int(m)),data,indptr, indices, np_x, np_y ) 

 return y
xhv8bpkk

xhv8bpkk2#

(Idea答案复制自https://stackoverflow.com/a/75663143/9569654
有一个Python Package 器,允许使用英特尔MKL库进行多线程稀疏矩阵操作(项目网站:https://pypi.org/project/sparse-dot-mkl/)。它也可以使用conda安装(按照https://anaconda.org/conda-forge/sparse_dot_mkl上的说明)!
使用此包,您的代码将更改为

# test dense matrix multiplication
from numpy import *
import time    
x = random.random((10000, 10000))
t1 = time.time()
foo = dot(x.T, x)
print(time.time() - t1)

# test sparse matrix multiplication
from scipy import sparse
from sparse_dot_mkl import dot_product_mkl
x = sparse.rand(10000, 10000, format="csr")
t1 = time.time()
foo = dot_product_mkl(x.T, x)
print(time.time() - t1)

请注意,您需要将稀疏矩阵的格式指定为CSR或CSC以使用dot_product_mkl。我还将print语句更改为Python 3语法。在我的机器上,numpy代码的时间是3.39秒,稀疏乘法的时间是0.27秒。

相关问题