From a30d6586a3880a0bf9a419de9e7e4b59a4092e09 Mon Sep 17 00:00:00 2001 From: babrodtk Date: Fri, 28 Aug 2015 14:16:05 +0200 Subject: [PATCH] Made fastSparseProduct std::move-friendly --- opm/autodiff/AutoDiffMatrix.hpp | 9 +++----- opm/autodiff/fastSparseProduct.hpp | 34 ++++++++++++++++++------------ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/opm/autodiff/AutoDiffMatrix.hpp b/opm/autodiff/AutoDiffMatrix.hpp index 2bdf177d6..9a98fb03f 100644 --- a/opm/autodiff/AutoDiffMatrix.hpp +++ b/opm/autodiff/AutoDiffMatrix.hpp @@ -462,8 +462,7 @@ namespace Opm retval.type_ = S; retval.rows_ = lhs.rows_; retval.cols_ = rhs.cols_; - retval.sparse_[0] = Sparse(retval.rows_, retval.cols_); - fastDiagSparseProduct(lhs.diag_, rhs.sparse_[0], retval.sparse_[0]); + retval.sparse_[0] = std::move(fastDiagSparseProduct(lhs.diag_, rhs.sparse_[0])); return retval; } @@ -475,8 +474,7 @@ namespace Opm retval.type_ = S; retval.rows_ = lhs.rows_; retval.cols_ = rhs.cols_; - retval.sparse_[0] = Sparse(retval.rows_, retval.cols_); - fastSparseDiagProduct(lhs.sparse_[0], rhs.diag_, retval.sparse_[0]); + retval.sparse_[0] = std::move(fastSparseDiagProduct(lhs.sparse_[0], rhs.diag_)); return retval; } @@ -488,8 +486,7 @@ namespace Opm retval.type_ = S; retval.rows_ = lhs.rows_; retval.cols_ = rhs.cols_; - retval.sparse_[0] = Sparse(retval.rows_, retval.cols_); - fastSparseProduct(lhs.sparse_[0], rhs.sparse_[0], retval.sparse_[0]); + retval.sparse_[0] = std::move(fastSparseProduct(lhs.sparse_[0], rhs.sparse_[0])); return retval; } diff --git a/opm/autodiff/fastSparseProduct.hpp b/opm/autodiff/fastSparseProduct.hpp index c5ba0defe..436809611 100644 --- a/opm/autodiff/fastSparseProduct.hpp +++ b/opm/autodiff/fastSparseProduct.hpp @@ -56,16 +56,16 @@ struct QuickSort< 0 > }; -template -void fastSparseProduct(const Lhs& lhs, const Rhs& rhs, ResultType& res) +template +ResultType fastSparseProduct(const Lhs& lhs, const Rhs& rhs) { // initialize result - res = ResultType(lhs.rows(), rhs.cols()); + ResultType res(lhs.rows(), rhs.cols()); // 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 ) - return; + return res; typedef typename Eigen::internal::remove_all::type::Scalar Scalar; typedef typename Eigen::internal::remove_all::type::Index Index; @@ -137,17 +137,19 @@ void fastSparseProduct(const Lhs& lhs, const Rhs& rhs, ResultType& res) } res.finalize(); + + return res; } -inline void fastDiagSparseProduct(// const Eigen::DiagonalMatrix& lhs, - const std::vector& lhs, - const Eigen::SparseMatrix& rhs, - Eigen::SparseMatrix& res) +inline +Eigen::SparseMatrix +fastDiagSparseProduct(const std::vector& lhs, + const Eigen::SparseMatrix& rhs) { - res = rhs; + Eigen::SparseMatrix res = rhs; // Multiply rows by diagonal lhs. int n = res.cols(); @@ -157,17 +159,19 @@ inline void fastDiagSparseProduct(// const Eigen::DiagonalMatrix& lhs, - // const Eigen::DiagonalMatrix& rhs, - const std::vector& rhs, - Eigen::SparseMatrix& res) +inline +Eigen::SparseMatrix +fastSparseDiagProduct(const Eigen::SparseMatrix& lhs, + const std::vector& rhs) { - res = lhs; + Eigen::SparseMatrix res = lhs; // Multiply columns by diagonal rhs. int n = res.cols(); @@ -177,6 +181,8 @@ inline void fastSparseDiagProduct(const Eigen::SparseMatrix& lhs, it.valueRef() *= rhs[col]; // rhs.diagonal()(col); } } + + return res; }