简体   繁体   English

用本征进行有效的块稀疏矩阵乘法

[英]Efficient block-sparse matrix multiplication with Eigen

Let's say I have a matrix that is sparse, except for blocks along the diagonal (of a fixed size). 假设我有一个稀疏的矩阵,除了沿对角线的块(固定大小)外。

Eigen::SparseMatrix<float> lhs;

lhs is approximately 2% non-sparse, but may be very large. lhs大约是2%的稀疏状态,但是可能非常大。 Then, let's say I have a vector: 然后,假设我有一个向量:

Eigen::MatrixXf rhs = Eigen::MatrixXf::Random(SomeSz, 1);

For the moment, let's assume it's dense. 目前,让我们假设它是密集的。

I want to efficiently compute: 我想有效地计算:

result.noalias() = lhs * rhs;

If I was to compile with -O3 -march=native -mtune=native (with Clang), would this produce an optimal result? 如果我要使用-O3 -march = native -mtune = native(使用Clang)进行编译,这会产生最佳结果吗?

Also, what if rhs was sparse: 另外,如果rhs稀疏怎么办:

Eigen::SparseMatrix<float> rhs; rhs.resize(SomeSz, 1); rhs.reserve(SomeSz/SomeFactor);

Is: 是:

result = lhs * rhs;

still optimal/suboptimal? 还是最优/次优?

I guess what I'm asking is whether or not Eigen will take advantage of the block-sparse structure and only perform the necessary computations. 我想我要问的是Eigen是否将利用块稀疏结构的优势,仅执行必要的计算。

First of all, in the dense case for the rhs, if rhs is a vector, then please tell it to Eigen by using VectorXf . 首先,在rhs的密集情况下,如果rhs是向量,则请使用VectorXf其告知Eigen。 Then, with Eigen 3.3, you can take advantage of multi-threading by compiling with -fopenmp and using a row-major storage of the lhs . 然后,在Eigen 3.3中,可以通过使用-fopenmp进行编译并使用lhs的行主存储来利用多线程。

In the sparse case, yes Eigen will take into consideration the sparsity of both the lhs and rhs. 在稀疏情况下,是的,Eigen将考虑lhs和rhs的稀疏性。 The complexity will really be rhs.nonZeros()*average_nnz_per_col_of_lhs , in contrast to rhs.size()*average_nnz_per_col_of_lhs with a dense rhs. 与具有密集rhs的rhs.size()*average_nnz_per_col_of_lhs相比,复杂度实际上是rhs.nonZeros()*average_nnz_per_col_of_lhs So if rhs is really sparse, then it might be worth a try. 因此,如果rhs真的很稀疏,那么可能值得尝试。 Only the useful column of lhs will be considered. 仅考虑lhs的有用列。 In this case, better keep the lhs column-major. 在这种情况下,最好保持lhs列为主要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM