带有mpmath的Numpy使用单线程进行矩阵乘法

时间:2018-12-22 12:01:47

标签: python numpy scipy sparse-matrix mpmath

我正在研究一个numpy矩阵 adj ,该矩阵表示某些networkx图的邻接矩阵。当我按以下方式构造 adj 时:

adj = sparse.csr_matrix(nx.adjacency_matrix(graph), dtype='longdouble').todense()

然后运行adj = adj ** 2,然后在htop中可以看到numpy使用了所有可用线程。

但是,由于精度损失,我试图在两者之间的某个位置集成mpmath

我是这样做的:

mp.dps = 120
adj = sparse.csr_matrix(nx.adjacency_matrix(graph), dtype='longdouble').todense()
# ... just like before
adjmp = mp.matrix(adj)
# this casts all values to mpf
adj = np.matrix(adjmp, dtype=object)
# and get back the np matrix, now with mpfs inside

生成的 adj 看起来像这样

matrix([[mpf('0.0'), mpf('0.0'), mpf('0.0'), ..., mpf('0.0'), mpf('0.0'),
     mpf('0.125')], #  [...]

这是我的期望。

计算包括两个步骤:第一步是对 adj 求平方,第二步是实际计算。从结果中,我可以看到精度要高得多,但是htop表明由于某种原因,平方步骤仅在一个线程上运行。

当我运行np.show_config()时,我得到:

blas_mkl_info:
  NOT AVAILABLE
blis_info:
  NOT AVAILABLE
openblas_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
  NOT AVAILABLE
openblas_lapack_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]

0 个答案:

没有答案