From b2a13c9dd1cbc487c6ca74324766b361418d39dd Mon Sep 17 00:00:00 2001 From: Charles Schlosser Date: Tue, 23 Aug 2022 21:44:22 +0000 Subject: [PATCH] Sparse Core: Replace malloc/free with conditional_aligned --- Eigen/src/SparseCore/CompressedStorage.h | 38 +++++------------- Eigen/src/SparseCore/SparseAssign.h | 10 ++++- Eigen/src/SparseCore/SparseMatrix.h | 49 ++++++++++-------------- 3 files changed, 38 insertions(+), 59 deletions(-) diff --git a/Eigen/src/SparseCore/CompressedStorage.h b/Eigen/src/SparseCore/CompressedStorage.h index 1f7aeb59c..733b1aa73 100644 --- a/Eigen/src/SparseCore/CompressedStorage.h +++ b/Eigen/src/SparseCore/CompressedStorage.h @@ -71,8 +71,8 @@ class CompressedStorage ~CompressedStorage() { - delete[] m_values; - delete[] m_indices; + conditional_aligned_delete_auto(m_values, m_allocatedSize); + conditional_aligned_delete_auto(m_indices, m_allocatedSize); } void reserve(Index size) @@ -180,24 +180,13 @@ class CompressedStorage { if (m_allocatedSize newValues(m_allocatedSize); - internal::scoped_array newIndices(m_allocatedSize); - - // copy first chunk - internal::smart_copy(m_values, m_values +id, newValues.ptr()); - internal::smart_copy(m_indices, m_indices+id, newIndices.ptr()); - - // copy the rest - if(m_size>id) - { - internal::smart_copy(m_values +id, m_values +m_size, newValues.ptr() +id+1); - internal::smart_copy(m_indices+id, m_indices+m_size, newIndices.ptr()+id+1); - } - std::swap(m_values,newValues.ptr()); - std::swap(m_indices,newIndices.ptr()); + Index newAllocatedSize = 2 * (m_size + 1); + m_values = conditional_aligned_realloc_new_auto(m_values, newAllocatedSize, m_allocatedSize); + m_indices = + conditional_aligned_realloc_new_auto(m_indices, newAllocatedSize, m_allocatedSize); + m_allocatedSize = newAllocatedSize; } - else if(m_size>id) + if(m_size>id) { internal::smart_memmove(m_values +id, m_values +m_size, m_values +id+1); internal::smart_memmove(m_indices+id, m_indices+m_size, m_indices+id+1); @@ -233,15 +222,8 @@ class CompressedStorage EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN #endif eigen_internal_assert(size!=m_allocatedSize); - internal::scoped_array newValues(size); - internal::scoped_array newIndices(size); - Index copySize = (std::min)(size, m_size); - if (copySize>0) { - internal::smart_copy(m_values, m_values+copySize, newValues.ptr()); - internal::smart_copy(m_indices, m_indices+copySize, newIndices.ptr()); - } - std::swap(m_values,newValues.ptr()); - std::swap(m_indices,newIndices.ptr()); + m_values = conditional_aligned_realloc_new_auto(m_values, size, m_allocatedSize); + m_indices = conditional_aligned_realloc_new_auto(m_indices, size, m_allocatedSize); m_allocatedSize = size; } diff --git a/Eigen/src/SparseCore/SparseAssign.h b/Eigen/src/SparseCore/SparseAssign.h index 63d69ad43..29f6af4c1 100644 --- a/Eigen/src/SparseCore/SparseAssign.h +++ b/Eigen/src/SparseCore/SparseAssign.h @@ -80,12 +80,18 @@ void assign_sparse_to_sparse(DstXprType &dst, const SrcXprType &src) const bool transpose = (DstEvaluatorType::Flags & RowMajorBit) != (SrcEvaluatorType::Flags & RowMajorBit); const Index outerEvaluationSize = (SrcEvaluatorType::Flags&RowMajorBit) ? src.rows() : src.cols(); + + Index reserveSize = 0; + for (Index j = 0; j < outerEvaluationSize; ++j) + for (typename SrcEvaluatorType::InnerIterator it(srcEvaluator, j); it; ++it) + reserveSize++; + if ((!transpose) && src.isRValue()) { // eval without temporary dst.resize(src.rows(), src.cols()); dst.setZero(); - dst.reserve((std::min)(src.rows()*src.cols(), (std::max)(src.rows(),src.cols())*2)); + dst.reserve(reserveSize); for (Index j=0; j(std::malloc(m_outerSize * sizeof(StorageIndex))); - if (!m_innerNonZeros) internal::throw_std_bad_alloc(); + m_innerNonZeros = internal::conditional_aligned_new_auto(m_outerSize); // temporarily use m_innerSizes to hold the new starting points. StorageIndex* newOuterIndex = m_innerNonZeros; @@ -336,8 +335,7 @@ class SparseMatrix } else { - StorageIndex* newOuterIndex = static_cast(std::malloc((m_outerSize+1)*sizeof(StorageIndex))); - if (!newOuterIndex) internal::throw_std_bad_alloc(); + StorageIndex* newOuterIndex = internal::conditional_aligned_new_auto(m_outerSize + 1); StorageIndex count = 0; for(Index j=0; j(newOuterIndex, m_outerSize + 1); } } @@ -488,7 +486,7 @@ class SparseMatrix m_outerIndex[j+1] = m_outerIndex[j] + m_innerNonZeros[j]; oldStart = nextOldStart; } - std::free(m_innerNonZeros); + internal::conditional_aligned_delete_auto(m_innerNonZeros, m_outerSize); m_innerNonZeros = 0; m_data.resize(m_outerIndex[m_outerSize]); m_data.squeeze(); @@ -499,7 +497,7 @@ class SparseMatrix { if(m_innerNonZeros != 0) return; - m_innerNonZeros = static_cast(std::malloc(m_outerSize * sizeof(StorageIndex))); + m_innerNonZeros = internal::conditional_aligned_new_auto(m_outerSize); for (Index i = 0; i < m_outerSize; i++) { m_innerNonZeros[i] = m_outerIndex[i+1] - m_outerIndex[i]; @@ -569,9 +567,8 @@ class SparseMatrix if (m_innerNonZeros) { // Resize m_innerNonZeros - StorageIndex *newInnerNonZeros = static_cast(std::realloc(m_innerNonZeros, (m_outerSize + outerChange) * sizeof(StorageIndex))); - if (!newInnerNonZeros) internal::throw_std_bad_alloc(); - m_innerNonZeros = newInnerNonZeros; + m_innerNonZeros = internal::conditional_aligned_realloc_new_auto( + m_innerNonZeros, m_outerSize + outerChange, m_outerSize); for(Index i=m_outerSize; i(std::malloc((m_outerSize + outerChange) * sizeof(StorageIndex))); - if (!m_innerNonZeros) internal::throw_std_bad_alloc(); + m_innerNonZeros = internal::conditional_aligned_new_auto(m_outerSize + outerChange); for(Index i = 0; i < m_outerSize + (std::min)(outerChange, Index(0)); i++) m_innerNonZeros[i] = m_outerIndex[i+1] - m_outerIndex[i]; for(Index i = m_outerSize; i < m_outerSize + outerChange; i++) @@ -604,9 +600,8 @@ class SparseMatrix if (outerChange == 0) return; - StorageIndex *newOuterIndex = static_cast(std::realloc(m_outerIndex, (m_outerSize + outerChange + 1) * sizeof(StorageIndex))); - if (!newOuterIndex) internal::throw_std_bad_alloc(); - m_outerIndex = newOuterIndex; + m_outerIndex = internal::conditional_aligned_realloc_new_auto( + m_outerSize + outerChange + 1, m_outerSize + 1); if (outerChange > 0) { StorageIndex lastIdx = m_outerSize == 0 ? 0 : m_outerIndex[m_outerSize]; @@ -630,15 +625,13 @@ class SparseMatrix m_data.clear(); if (m_outerSize != outerSize || m_outerSize==0) { - std::free(m_outerIndex); - m_outerIndex = static_cast(std::malloc((outerSize + 1) * sizeof(StorageIndex))); - if (!m_outerIndex) internal::throw_std_bad_alloc(); - + m_outerIndex = internal::conditional_aligned_realloc_new_auto(m_outerIndex, outerSize + 1, + m_outerSize + 1); m_outerSize = outerSize; } if(m_innerNonZeros) { - std::free(m_innerNonZeros); + internal::conditional_aligned_delete_auto(m_innerNonZeros, m_outerSize); m_innerNonZeros = 0; } std::fill_n(m_outerIndex, m_outerSize + 1, StorageIndex(0)); @@ -746,7 +739,7 @@ class SparseMatrix Eigen::Map(this->m_data.indexPtr(), rows()).setLinSpaced(0, StorageIndex(rows()-1)); Eigen::Map(this->m_data.valuePtr(), rows()).setOnes(); Eigen::Map(this->m_outerIndex, rows()+1).setLinSpaced(0, StorageIndex(rows())); - std::free(m_innerNonZeros); + internal::conditional_aligned_delete_auto(m_innerNonZeros, m_outerSize); m_innerNonZeros = 0; } inline SparseMatrix& operator=(const SparseMatrix& other) @@ -836,8 +829,8 @@ class SparseMatrix /** Destructor */ inline ~SparseMatrix() { - std::free(m_outerIndex); - std::free(m_innerNonZeros); + internal::conditional_aligned_delete_auto(m_outerIndex, m_outerSize + 1); + internal::conditional_aligned_delete_auto(m_innerNonZeros, m_outerSize); } /** Overloaded for performance */ @@ -855,7 +848,7 @@ protected: resize(other.rows(), other.cols()); if(m_innerNonZeros) { - std::free(m_innerNonZeros); + internal::conditional_aligned_delete_auto(m_innerNonZeros, m_outerSize); m_innerNonZeros = 0; } } @@ -1154,7 +1147,7 @@ void SparseMatrix::collapseDuplicates(DupFunctor m_outerIndex[m_outerSize] = count; // turn the matrix into compressed form - std::free(m_innerNonZeros); + internal::conditional_aligned_delete_auto(m_innerNonZeros, m_outerSize); m_innerNonZeros = 0; m_data.resize(m_outerIndex[m_outerSize]); } @@ -1249,8 +1242,7 @@ typename SparseMatrix::Scalar& SparseMatrix(std::malloc(m_outerSize * sizeof(StorageIndex))); - if(!m_innerNonZeros) internal::throw_std_bad_alloc(); + m_innerNonZeros = internal::conditional_aligned_new_auto(m_outerSize); std::fill(m_innerNonZeros, m_innerNonZeros + m_outerSize, StorageIndex(0)); @@ -1263,8 +1255,7 @@ typename SparseMatrix::Scalar& SparseMatrix(std::malloc(m_outerSize * sizeof(StorageIndex))); - if(!m_innerNonZeros) internal::throw_std_bad_alloc(); + m_innerNonZeros = internal::conditional_aligned_new_auto(m_outerSize); for(Index j=0; j