From 48945d1199a5f7701f427538236f89c8c9dc89ad Mon Sep 17 00:00:00 2001 From: TimQu Date: Thu, 1 Mar 2018 14:29:19 +0100 Subject: [PATCH] improved multiplyRow method --- src/storm/solver/GmmxxMultiplier.cpp | 4 ++-- src/storm/solver/GmmxxMultiplier.h | 2 +- .../IterativeMinMaxLinearEquationSolver.cpp | 10 +--------- src/storm/solver/Multiplier.cpp | 6 ++++++ src/storm/solver/Multiplier.h | 15 +++++++++++++-- src/storm/solver/NativeLinearEquationSolver.cpp | 9 +-------- src/storm/solver/NativeMultiplier.cpp | 15 +++++++++++++-- src/storm/solver/NativeMultiplier.h | 4 +++- 8 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/storm/solver/GmmxxMultiplier.cpp b/src/storm/solver/GmmxxMultiplier.cpp index 759bdba35..9618e2495 100644 --- a/src/storm/solver/GmmxxMultiplier.cpp +++ b/src/storm/solver/GmmxxMultiplier.cpp @@ -103,9 +103,9 @@ namespace storm { } template - ValueType GmmxxMultiplier::multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType const& offset) const { + void GmmxxMultiplier::multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType& value) const { initialize(); - return vect_sp(gmm::mat_const_row(gmmMatrix, rowIndex), x, typename gmm::linalg_traits>::storage_type(), typename gmm::linalg_traits>::storage_type()) + offset; + value += vect_sp(gmm::mat_const_row(gmmMatrix, rowIndex), x, typename gmm::linalg_traits>::storage_type(), typename gmm::linalg_traits>::storage_type()); } template diff --git a/src/storm/solver/GmmxxMultiplier.h b/src/storm/solver/GmmxxMultiplier.h index 9ea4089b9..87bee797e 100644 --- a/src/storm/solver/GmmxxMultiplier.h +++ b/src/storm/solver/GmmxxMultiplier.h @@ -24,7 +24,7 @@ namespace storm { virtual void multiplyGaussSeidel(Environment const& env, std::vector& x, std::vector const* b) const override; virtual void multiplyAndReduce(Environment const& env, OptimizationDirection const& dir, std::vector const& rowGroupIndices, std::vector const& x, std::vector const* b, std::vector& result, std::vector* choices = nullptr) const override; virtual void multiplyAndReduceGaussSeidel(Environment const& env, OptimizationDirection const& dir, std::vector const& rowGroupIndices, std::vector& x, std::vector const* b, std::vector* choices = nullptr) const override; - virtual ValueType multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType const& offset) const override; + virtual void multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType& value) const override; virtual void clearCache() const override; private: void initialize() const; diff --git a/src/storm/solver/IterativeMinMaxLinearEquationSolver.cpp b/src/storm/solver/IterativeMinMaxLinearEquationSolver.cpp index 587aa3169..184c1e027 100644 --- a/src/storm/solver/IterativeMinMaxLinearEquationSolver.cpp +++ b/src/storm/solver/IterativeMinMaxLinearEquationSolver.cpp @@ -667,17 +667,9 @@ namespace storm { } void multiplyRow(uint64_t const& row, storm::storage::SparseMatrix const& A, storm::solver::Multiplier const& multiplier, ValueType const& bi, ValueType& xi, ValueType& yi) { - xi = multiplier.multiplyRow(row, x, bi); - yi = multiplier.multiplyRow(row, y, storm::utility::zero()); - - /* xi = bi; yi = storm::utility::zero(); - for (auto const& entry : A.getRow(row)) { - xi += entry.getValue() * x[entry.getColumn()]; - yi += entry.getValue() * y[entry.getColumn()]; - } - */ + multiplier.multiplyRow2(row, x, xi, y, yi); } template diff --git a/src/storm/solver/Multiplier.cpp b/src/storm/solver/Multiplier.cpp index b2b6dfd5e..26bb4d85b 100644 --- a/src/storm/solver/Multiplier.cpp +++ b/src/storm/solver/Multiplier.cpp @@ -50,6 +50,12 @@ namespace storm { } } + template + void Multiplier::multiplyRow2(uint64_t const& rowIndex, std::vector const& x1, ValueType& val1, std::vector const& x2, ValueType& val2) const { + multiplyRow(rowIndex, x1, val1); + multiplyRow(rowIndex, x2, val2); + } + template std::unique_ptr> MultiplierFactory::create(Environment const& env, storm::storage::SparseMatrix const& matrix) { switch (env.solver().multiplier().getType()) { diff --git a/src/storm/solver/Multiplier.h b/src/storm/solver/Multiplier.h index c420984e7..87d51b2f5 100644 --- a/src/storm/solver/Multiplier.h +++ b/src/storm/solver/Multiplier.h @@ -113,11 +113,22 @@ namespace storm { void repeatedMultiplyAndReduce(Environment const& env, OptimizationDirection const& dir, std::vector& x, std::vector const* b, uint64_t n) const; /*! - * Multiplies the row with the given index with x and adds the given offset + * Multiplies the row with the given index with x and adds the result to the provided value * @param rowIndex The index of the considered row * @param x The input vector with which the row is multiplied + * @param value The multiplication result is added to this value. */ - virtual ValueType multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType const& offset) const = 0; + virtual void multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType& value) const = 0; + + /*! + * Multiplies the row with the given index with x1 and x2 and adds the given offset o1 and o2, respectively + * @param rowIndex The index of the considered row + * @param x1 The first input vector with which the row is multiplied + * @param val1 The first multiplication result is added to this value. + * @param x2 The second input vector with which the row is multiplied + * @param val2 The second multiplication result is added to this value. + */ + virtual void multiplyRow2(uint64_t const& rowIndex, std::vector const& x1, ValueType& val1, std::vector const& x2, ValueType& val2) const; protected: mutable std::unique_ptr> cachedVector; diff --git a/src/storm/solver/NativeLinearEquationSolver.cpp b/src/storm/solver/NativeLinearEquationSolver.cpp index c524513f2..51b8fa3c9 100644 --- a/src/storm/solver/NativeLinearEquationSolver.cpp +++ b/src/storm/solver/NativeLinearEquationSolver.cpp @@ -591,16 +591,9 @@ namespace storm { } void multiplyRow(uint64_t const& row, storm::storage::SparseMatrix const& A, storm::solver::Multiplier const& multiplier, ValueType const& bi, ValueType& xi, ValueType& yi) { - xi = multiplier.multiplyRow(row, x, bi); - yi = multiplier.multiplyRow(row, y, storm::utility::zero()); - /* xi = bi; yi = storm::utility::zero(); - for (auto const& entry : A.getRow(row)) { - xi += entry.getValue() * x[entry.getColumn()]; - yi += entry.getValue() * y[entry.getColumn()]; - } - */ + multiplier.multiplyRow2(row, x, xi, y, yi); } void performIterationStep(storm::storage::SparseMatrix const& A, storm::solver::Multiplier const& multiplier, std::vector const& b) { diff --git a/src/storm/solver/NativeMultiplier.cpp b/src/storm/solver/NativeMultiplier.cpp index 3f84a4a8d..13b27c617 100644 --- a/src/storm/solver/NativeMultiplier.cpp +++ b/src/storm/solver/NativeMultiplier.cpp @@ -83,10 +83,21 @@ namespace storm { } template - ValueType NativeMultiplier::multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType const& offset) const { - return this->matrix.multiplyRowWithVector(rowIndex, x); + void NativeMultiplier::multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType& value) const { + for (auto const& entry : this->matrix.getRow(rowIndex)) { + value += entry.getValue() * x[entry.getColumn()]; + } + } + + template + void NativeMultiplier::multiplyRow2(uint64_t const& rowIndex, std::vector const& x1, ValueType& val1, std::vector const& x2, ValueType& val2) const { + for (auto const& entry : this->matrix.getRow(rowIndex)) { + val1 += entry.getValue() * x1[entry.getColumn()]; + val2 += entry.getValue() * x2[entry.getColumn()]; + } } + template void NativeMultiplier::multAdd(std::vector const& x, std::vector const* b, std::vector& result) const { this->matrix.multiplyWithVector(x, result, b); diff --git a/src/storm/solver/NativeMultiplier.h b/src/storm/solver/NativeMultiplier.h index e1cc4d01a..73d1f84dc 100644 --- a/src/storm/solver/NativeMultiplier.h +++ b/src/storm/solver/NativeMultiplier.h @@ -21,7 +21,9 @@ namespace storm { virtual void multiplyGaussSeidel(Environment const& env, std::vector& x, std::vector const* b) const override; virtual void multiplyAndReduce(Environment const& env, OptimizationDirection const& dir, std::vector const& rowGroupIndices, std::vector const& x, std::vector const* b, std::vector& result, std::vector* choices = nullptr) const override; virtual void multiplyAndReduceGaussSeidel(Environment const& env, OptimizationDirection const& dir, std::vector const& rowGroupIndices, std::vector& x, std::vector const* b, std::vector* choices = nullptr) const override; - virtual ValueType multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType const& offset) const override; + virtual void multiplyRow(uint64_t const& rowIndex, std::vector const& x, ValueType& value) const override; + virtual void multiplyRow2(uint64_t const& rowIndex, std::vector const& x1, ValueType& val1, std::vector const& x2, ValueType& val2) const override; + private: bool parallelize(Environment const& env) const;