Optimise assignment into a Block<SparseMatrix> by using Ref and avoiding useless updates in non-compressed mode. This make row-by-row filling of a row-major sparse matrix very efficient.

This commit is contained in:
Gael Guennebaud 2015-10-06 11:59:08 +02:00
parent 945b80c83e
commit f25bdc707f

View File

@ -114,7 +114,8 @@ public:
// and/or it is not at the end of the nonzeros of the underlying matrix. // and/or it is not at the end of the nonzeros of the underlying matrix.
// 1 - eval to a temporary to avoid transposition and/or aliasing issues // 1 - eval to a temporary to avoid transposition and/or aliasing issues
SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, StorageIndex> tmp(other); Ref<const SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, StorageIndex> > tmp(other.derived());
eigen_internal_assert(tmp.outerSize()==m_outerSize.value());
// 2 - let's check whether there is enough allocated memory // 2 - let's check whether there is enough allocated memory
Index nnz = tmp.nonZeros(); Index nnz = tmp.nonZeros();
@ -127,6 +128,7 @@ public:
? Index(matrix.data().allocatedSize()) + block_size ? Index(matrix.data().allocatedSize()) + block_size
: block_size; : block_size;
bool update_trailing_pointers = false;
if(nnz>free_size) if(nnz>free_size)
{ {
// realloc manually to reduce copies // realloc manually to reduce copies
@ -135,8 +137,8 @@ public:
internal::smart_copy(&m_matrix.data().value(0), &m_matrix.data().value(0) + start, &newdata.value(0)); internal::smart_copy(&m_matrix.data().value(0), &m_matrix.data().value(0) + start, &newdata.value(0));
internal::smart_copy(&m_matrix.data().index(0), &m_matrix.data().index(0) + start, &newdata.index(0)); internal::smart_copy(&m_matrix.data().index(0), &m_matrix.data().index(0) + start, &newdata.index(0));
internal::smart_copy(&tmp.data().value(0), &tmp.data().value(0) + nnz, &newdata.value(start)); internal::smart_copy(tmp.valuePtr(), tmp.valuePtr() + nnz, &newdata.value(start));
internal::smart_copy(&tmp.data().index(0), &tmp.data().index(0) + nnz, &newdata.index(start)); internal::smart_copy(tmp.innerIndexPtr(), tmp.innerIndexPtr() + nnz, &newdata.index(start));
internal::smart_copy(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &newdata.value(start+nnz)); internal::smart_copy(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &newdata.value(start+nnz));
internal::smart_copy(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &newdata.index(start+nnz)); internal::smart_copy(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &newdata.index(start+nnz));
@ -144,8 +146,12 @@ public:
newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz); newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
matrix.data().swap(newdata); matrix.data().swap(newdata);
update_trailing_pointers = true;
} }
else else
{
if(m_matrix.isCompressed())
{ {
// no need to realloc, simply copy the tail at its respective position and insert tmp // no need to realloc, simply copy the tail at its respective position and insert tmp
matrix.data().resize(start + nnz + tail_size); matrix.data().resize(start + nnz + tail_size);
@ -153,27 +159,41 @@ public:
internal::smart_memmove(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &matrix.data().value(start + nnz)); internal::smart_memmove(&matrix.data().value(end), &matrix.data().value(end) + tail_size, &matrix.data().value(start + nnz));
internal::smart_memmove(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &matrix.data().index(start + nnz)); internal::smart_memmove(&matrix.data().index(end), &matrix.data().index(end) + tail_size, &matrix.data().index(start + nnz));
internal::smart_copy(&tmp.data().value(0), &tmp.data().value(0) + nnz, &matrix.data().value(start)); update_trailing_pointers = true;
internal::smart_copy(&tmp.data().index(0), &tmp.data().index(0) + nnz, &matrix.data().index(start));
} }
// update innerNonZeros internal::smart_copy(tmp.valuePtr(), tmp.valuePtr() + nnz, &matrix.data().value(start));
if(!m_matrix.isCompressed()) internal::smart_copy(tmp.innerIndexPtr(), tmp.innerIndexPtr() + nnz, &matrix.data().index(start));
for(Index j=0; j<m_outerSize.value(); ++j) }
matrix.innerNonZeroPtr()[m_outerStart+j] = StorageIndex(tmp.innerVector(j).nonZeros());
// update outer index pointers // update outer index pointers and innerNonZeros
if(IsVectorAtCompileTime)
{
if(!m_matrix.isCompressed())
matrix.innerNonZeroPtr()[m_outerStart] = StorageIndex(nnz);
matrix.outerIndexPtr()[m_outerStart] = StorageIndex(start);
}
else
{
StorageIndex p = StorageIndex(start); StorageIndex p = StorageIndex(start);
for(Index k=0; k<m_outerSize.value(); ++k) for(Index k=0; k<m_outerSize.value(); ++k)
{ {
Index nnz_k = tmp.innerVector(k).nonZeros();
if(!m_matrix.isCompressed())
matrix.innerNonZeroPtr()[m_outerStart+k] = StorageIndex(nnz_k);
matrix.outerIndexPtr()[m_outerStart+k] = p; matrix.outerIndexPtr()[m_outerStart+k] = p;
p += tmp.innerVector(k).nonZeros(); p += nnz_k;
} }
}
if(update_trailing_pointers)
{
StorageIndex offset = internal::convert_index<StorageIndex>(nnz - block_size); StorageIndex offset = internal::convert_index<StorageIndex>(nnz - block_size);
for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k) for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
{ {
matrix.outerIndexPtr()[k] += offset; matrix.outerIndexPtr()[k] += offset;
} }
}
return derived(); return derived();
} }