2014-12-05 05:56:15 -06:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
// This file has been modified for use in the OPM project codebase.
|
|
|
|
|
|
|
|
#ifndef OPM_FASTSPARSEPRODUCT_HEADER_INCLUDED
|
|
|
|
#define OPM_FASTSPARSEPRODUCT_HEADER_INCLUDED
|
|
|
|
|
|
|
|
#include <Eigen/Sparse>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <Eigen/Core>
|
|
|
|
|
|
|
|
namespace Opm {
|
|
|
|
|
|
|
|
template < unsigned int depth >
|
|
|
|
struct QuickSort
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
static inline void sort(T begin, T end)
|
|
|
|
{
|
|
|
|
if (begin != end)
|
|
|
|
{
|
|
|
|
T middle = std::partition (begin, end,
|
|
|
|
std::bind2nd(std::less<typename std::iterator_traits<T>::value_type>(), *begin)
|
|
|
|
);
|
|
|
|
QuickSort< depth-1 >::sort(begin, middle);
|
|
|
|
|
|
|
|
// std::sort (max(begin + 1, middle), end);
|
|
|
|
T new_middle = begin;
|
|
|
|
QuickSort< depth-1 >::sort(++new_middle, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct QuickSort< 0 >
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
static inline void sort(T begin, T end)
|
|
|
|
{
|
|
|
|
// fall back to standard insertion sort
|
|
|
|
std::sort( begin, end );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-28 08:41:51 -05:00
|
|
|
template<typename Lhs, typename Rhs, typename ResultType>
|
|
|
|
void fastSparseProduct(const Lhs& lhs, const Rhs& rhs, ResultType& res)
|
2014-12-05 05:56:15 -06:00
|
|
|
{
|
2014-12-05 07:48:16 -06:00
|
|
|
// initialize result
|
2015-08-28 08:41:51 -05:00
|
|
|
res = ResultType(lhs.rows(), rhs.cols());
|
2014-12-05 07:48:16 -06:00
|
|
|
|
2014-12-05 05:56:15 -06:00
|
|
|
// if one of the matrices does not contain non zero elements
|
|
|
|
// the result will only contain an empty matrix
|
|
|
|
if( lhs.nonZeros() == 0 || rhs.nonZeros() == 0 )
|
2015-08-28 08:41:51 -05:00
|
|
|
return;
|
2014-12-05 05:56:15 -06:00
|
|
|
|
|
|
|
typedef typename Eigen::internal::remove_all<Lhs>::type::Scalar Scalar;
|
|
|
|
typedef typename Eigen::internal::remove_all<Lhs>::type::Index Index;
|
|
|
|
|
|
|
|
// make sure to call innerSize/outerSize since we fake the storage order.
|
|
|
|
Index rows = lhs.innerSize();
|
|
|
|
Index cols = rhs.outerSize();
|
|
|
|
eigen_assert(lhs.outerSize() == rhs.innerSize());
|
|
|
|
|
|
|
|
std::vector<bool> mask(rows,false);
|
2014-12-05 07:48:16 -06:00
|
|
|
Eigen::Matrix<Scalar,Eigen::Dynamic,1> values(rows);
|
|
|
|
Eigen::Matrix<Index, Eigen::Dynamic,1> indices(rows);
|
2014-12-05 05:56:15 -06:00
|
|
|
|
|
|
|
// estimate the number of non zero entries
|
|
|
|
// given a rhs column containing Y non zeros, we assume that the respective Y columns
|
|
|
|
// of the lhs differs in average of one non zeros, thus the number of non zeros for
|
|
|
|
// the product of a rhs column with the lhs is X+Y where X is the average number of non zero
|
|
|
|
// per column of the lhs.
|
|
|
|
// Therefore, we have nnz(lhs*rhs) = nnz(lhs) + nnz(rhs)
|
|
|
|
Index estimated_nnz_prod = lhs.nonZeros() + rhs.nonZeros();
|
|
|
|
|
|
|
|
res.setZero();
|
|
|
|
res.reserve(Index(estimated_nnz_prod));
|
|
|
|
|
|
|
|
//const Scalar epsilon = std::numeric_limits< Scalar >::epsilon();
|
|
|
|
const Scalar epsilon = 0.0;
|
|
|
|
|
|
|
|
// we compute each column of the result, one after the other
|
|
|
|
for (Index j=0; j<cols; ++j)
|
|
|
|
{
|
|
|
|
Index nnz = 0;
|
|
|
|
for (typename Rhs::InnerIterator rhsIt(rhs, j); rhsIt; ++rhsIt)
|
|
|
|
{
|
|
|
|
const Scalar y = rhsIt.value();
|
|
|
|
for (typename Lhs::InnerIterator lhsIt(lhs, rhsIt.index()); lhsIt; ++lhsIt)
|
|
|
|
{
|
|
|
|
const Scalar val = lhsIt.value() * y;
|
|
|
|
if( std::abs( val ) > epsilon )
|
|
|
|
{
|
2014-12-05 06:10:33 -06:00
|
|
|
const Index i = lhsIt.index();
|
2014-12-05 05:56:15 -06:00
|
|
|
if(!mask[i])
|
|
|
|
{
|
|
|
|
mask[i] = true;
|
|
|
|
values[i] = val;
|
|
|
|
indices[nnz] = i;
|
|
|
|
++nnz;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
values[i] += val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( nnz > 1 )
|
|
|
|
{
|
|
|
|
// sort indices for sorted insertion to avoid later copying
|
2014-12-05 07:48:16 -06:00
|
|
|
QuickSort< 1 >::sort( indices.data(), indices.data()+nnz );
|
2014-12-05 05:56:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
res.startVec(j);
|
|
|
|
// ordered insertion
|
|
|
|
// still using insertBackByOuterInnerUnordered since we know what we are doing
|
|
|
|
for(Index k=0; k<nnz; ++k)
|
|
|
|
{
|
|
|
|
const Index i = indices[k];
|
|
|
|
res.insertBackByOuterInnerUnordered(j,i) = values[i];
|
|
|
|
mask[i] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
res.finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-26 08:31:48 -05:00
|
|
|
|
2015-09-03 05:04:27 -05:00
|
|
|
inline void fastDiagSparseProduct(const std::vector<double>& lhs,
|
2015-08-28 08:41:51 -05:00
|
|
|
const Eigen::SparseMatrix<double>& rhs,
|
|
|
|
Eigen::SparseMatrix<double>& res)
|
2015-08-26 08:31:48 -05:00
|
|
|
{
|
2015-08-28 08:41:51 -05:00
|
|
|
res = rhs;
|
2015-08-26 08:31:48 -05:00
|
|
|
|
|
|
|
// Multiply rows by diagonal lhs.
|
|
|
|
int n = res.cols();
|
|
|
|
for (int col = 0; col < n; ++col) {
|
2015-08-27 03:03:59 -05:00
|
|
|
typedef Eigen::SparseMatrix<double>::InnerIterator It;
|
|
|
|
for (It it(res, col); it; ++it) {
|
2015-09-03 05:04:27 -05:00
|
|
|
it.valueRef() *= lhs[it.row()];
|
2015-08-27 03:03:59 -05:00
|
|
|
}
|
2015-08-26 08:31:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-28 08:41:51 -05:00
|
|
|
inline void fastSparseDiagProduct(const Eigen::SparseMatrix<double>& lhs,
|
|
|
|
const std::vector<double>& rhs,
|
|
|
|
Eigen::SparseMatrix<double>& res)
|
2015-08-26 08:31:48 -05:00
|
|
|
{
|
2015-08-28 08:41:51 -05:00
|
|
|
res = lhs;
|
2015-08-26 08:31:48 -05:00
|
|
|
|
|
|
|
// Multiply columns by diagonal rhs.
|
|
|
|
int n = res.cols();
|
|
|
|
for (int col = 0; col < n; ++col) {
|
2015-08-27 03:03:59 -05:00
|
|
|
typedef Eigen::SparseMatrix<double>::InnerIterator It;
|
|
|
|
for (It it(res, col); it; ++it) {
|
2015-09-03 05:04:27 -05:00
|
|
|
it.valueRef() *= rhs[col];
|
2015-08-27 03:03:59 -05:00
|
|
|
}
|
2015-08-26 08:31:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 09:00:24 -06:00
|
|
|
template<typename Lhs, typename Rhs>
|
2016-02-13 09:02:42 -06:00
|
|
|
inline bool
|
|
|
|
equalSparsityPattern(const Lhs& lhs, const Rhs& rhs)
|
2016-02-12 09:00:24 -06:00
|
|
|
{
|
2016-02-16 08:24:52 -06:00
|
|
|
// if both matrices have equal storage and non zeros match, we can check sparsity pattern
|
|
|
|
bool equal = (Lhs::IsRowMajor == Rhs::IsRowMajor) && (lhs.nonZeros() == rhs.nonZeros());
|
2016-02-12 09:00:24 -06:00
|
|
|
|
2016-02-16 08:24:52 -06:00
|
|
|
// check complete sparsity pattern
|
|
|
|
if( equal )
|
|
|
|
{
|
|
|
|
typedef typename Eigen::internal::remove_all<Lhs>::type::Index Index;
|
|
|
|
const Index outerSize = lhs.outerSize();
|
|
|
|
if( outerSize != rhs.outerSize() )
|
|
|
|
{
|
2016-02-13 09:02:42 -06:00
|
|
|
return false;
|
2016-02-16 08:24:52 -06:00
|
|
|
}
|
|
|
|
|
2016-02-16 10:18:04 -06:00
|
|
|
// outer indices
|
|
|
|
const Index* rhsOuter = rhs.outerIndexPtr();
|
|
|
|
const Index* lhsOuter = lhs.outerIndexPtr();
|
2016-02-17 08:59:34 -06:00
|
|
|
for(Index i=0; i<=outerSize; ++i )
|
|
|
|
{
|
|
|
|
if( lhsOuter[ i ] != rhsOuter[ i ] ) {
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 10:18:04 -06:00
|
|
|
|
|
|
|
// inner indices
|
|
|
|
const Index* rhsInner = rhs.innerIndexPtr();
|
|
|
|
const Index* lhsInner = lhs.innerIndexPtr();
|
|
|
|
|
2016-02-17 08:59:34 -06:00
|
|
|
const Index nnz = lhs.nonZeros();
|
|
|
|
for( Index i=0; i<nnz; ++i)
|
2016-02-16 08:24:52 -06:00
|
|
|
{
|
2016-02-17 08:59:34 -06:00
|
|
|
if( lhsInner[ i ] != rhsInner[ i ] ) {
|
|
|
|
return false;
|
2016-02-16 08:24:52 -06:00
|
|
|
}
|
|
|
|
}
|
2016-02-12 09:00:24 -06:00
|
|
|
}
|
|
|
|
|
2016-02-16 08:24:52 -06:00
|
|
|
return equal;
|
2016-02-12 09:00:24 -06:00
|
|
|
}
|
|
|
|
|
2016-02-13 09:02:42 -06:00
|
|
|
// this function substracts two sparse matrices
|
|
|
|
// if the sparsity pattern is the same a faster add/substract is performed
|
2016-02-12 09:00:24 -06:00
|
|
|
template<typename Lhs, typename Rhs>
|
2016-02-13 09:02:42 -06:00
|
|
|
inline void
|
|
|
|
fastSparseAdd(Lhs& lhs, const Rhs& rhs)
|
2016-02-12 09:00:24 -06:00
|
|
|
{
|
2016-02-13 09:02:42 -06:00
|
|
|
if( equalSparsityPattern( lhs, rhs ) )
|
|
|
|
{
|
|
|
|
typedef typename Eigen::internal::remove_all<Lhs>::type::Scalar Scalar;
|
|
|
|
typedef typename Eigen::internal::remove_all<Lhs>::type::Index Index;
|
|
|
|
|
|
|
|
const Index nnz = lhs.nonZeros();
|
|
|
|
|
|
|
|
// fast add using only the data pointers
|
|
|
|
const Scalar* rhsV = rhs.valuePtr();
|
|
|
|
Scalar* lhsV = lhs.valuePtr();
|
|
|
|
|
|
|
|
for(Index i=0; i<nnz; ++i )
|
|
|
|
{
|
|
|
|
lhsV[ i ] += rhsV[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-15 04:38:09 -06:00
|
|
|
// default Eigen operator+=
|
2016-02-18 08:31:35 -06:00
|
|
|
lhs += rhs;
|
2016-02-13 09:02:42 -06:00
|
|
|
}
|
2016-02-12 09:00:24 -06:00
|
|
|
}
|
|
|
|
|
2016-02-13 09:02:42 -06:00
|
|
|
// this function substracts two sparse matrices
|
|
|
|
// if the sparsity pattern is the same a faster add/substract is performed
|
2016-02-12 09:00:24 -06:00
|
|
|
template<typename Lhs, typename Rhs>
|
2016-02-13 09:02:42 -06:00
|
|
|
inline void
|
|
|
|
fastSparseSubstract(Lhs& lhs, const Rhs& rhs)
|
2016-02-12 09:00:24 -06:00
|
|
|
{
|
2016-02-13 09:02:42 -06:00
|
|
|
if( equalSparsityPattern( lhs, rhs ) )
|
|
|
|
{
|
|
|
|
typedef typename Eigen::internal::remove_all<Lhs>::type::Scalar Scalar;
|
|
|
|
typedef typename Eigen::internal::remove_all<Lhs>::type::Index Index;
|
|
|
|
|
|
|
|
const Index nnz = lhs.nonZeros();
|
|
|
|
|
|
|
|
// fast add using only the data pointers
|
|
|
|
const Scalar* rhsV = rhs.valuePtr();
|
|
|
|
Scalar* lhsV = lhs.valuePtr();
|
|
|
|
|
|
|
|
for(Index i=0; i<nnz; ++i )
|
|
|
|
{
|
|
|
|
lhsV[ i ] -= rhsV[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-15 04:38:09 -06:00
|
|
|
// default Eigen operator-=
|
2016-02-18 08:31:35 -06:00
|
|
|
lhs -= rhs;
|
2016-02-13 09:02:42 -06:00
|
|
|
}
|
2016-02-12 09:00:24 -06:00
|
|
|
}
|
2015-08-26 08:31:48 -05:00
|
|
|
|
2014-12-05 05:56:15 -06:00
|
|
|
} // end namespace Opm
|
|
|
|
|
|
|
|
#endif // OPM_FASTSPARSEPRODUCT_HEADER_INCLUDED
|