-
Notifications
You must be signed in to change notification settings - Fork 341
[LinearAlgebra] Refactor sparse matrix product #4547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fcc5d41
[LinearAlgebra] Refactor sparse matrix product
alxbilger ed0a5c2
Move tests as a template test
alxbilger 09d9abf
Fix all combinations of storage
alxbilger 190459d
Introduce parallel computation
alxbilger cbc49e6
Manage options for more types
alxbilger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
...framework/LinearAlgebra/Testing/src/Sofa.LinearAlgebra.Testing/SparseMatrixProduct_test.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /****************************************************************************** | ||
| * SOFA, Simulation Open-Framework Architecture * | ||
| * (c) 2006 INRIA, USTL, UJF, CNRS, MGH * | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify it * | ||
| * under the terms of the GNU Lesser General Public License as published by * | ||
| * the Free Software Foundation; either version 2.1 of the License, or (at * | ||
| * your option) any later version. * | ||
| * * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT * | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * | ||
| * for more details. * | ||
| * * | ||
| * You should have received a copy of the GNU Lesser General Public License * | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
| ******************************************************************************* | ||
| * Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
| * * | ||
| * Contact information: contact@sofa-framework.org * | ||
| ******************************************************************************/ | ||
| #pragma once | ||
| #include <gtest/gtest.h> | ||
| #include <sofa/testing/NumericTest.h> | ||
| #include <Sofa.LinearAlgebra.Testing/SparseMatrixTest.h> | ||
|
|
||
| #include <sofa/linearalgebra/BaseMatrix.h> | ||
| #include <Eigen/Sparse> | ||
| #include <sofa/helper/random.h> | ||
|
|
||
| namespace sofa::linearalgebra::testing | ||
| { | ||
|
|
||
| template<class T> | ||
| struct SparseMatrixProductInit | ||
| { | ||
| static void init(T& product) | ||
| { | ||
| SOFA_UNUSED(product); | ||
| }; | ||
|
|
||
| static void cleanup(T& product) | ||
| { | ||
| SOFA_UNUSED(product); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Test the class SparseMatrixProduct | ||
| * The class is designed to use the templates defined in TestSparseMatrixProductTraits | ||
| * The type of matrix can be any of the types supported by SparseMatrixProduct. | ||
| */ | ||
| template <class T> | ||
| struct TestSparseMatrixProduct : public sofa::testing::SparseMatrixTest<typename T::LhsScalar> | ||
| { | ||
| using LHSMatrix = typename T::LhsCleaned; | ||
| using RHSMatrix = typename T::RhsCleaned; | ||
| using Real = typename T::LhsScalar; | ||
| using Base = sofa::testing::SparseMatrixTest<Real>; | ||
| using Base::generateRandomSparseMatrix; | ||
| using Base::copyFromEigen; | ||
| using Base::compareSparseMatrix; | ||
|
|
||
| bool checkMatrix(typename LHSMatrix::Index nbRowsA, typename LHSMatrix::Index nbColsA, typename RHSMatrix::Index nbColsB, Real sparsity) | ||
| { | ||
| Eigen::SparseMatrix<Real, Eigen::RowMajor> eigen_a; | ||
| Eigen::SparseMatrix<Real, Eigen::ColMajor> eigen_b; | ||
|
|
||
| generateRandomSparseMatrix(eigen_a, nbRowsA, nbColsA, sparsity); | ||
| generateRandomSparseMatrix(eigen_b, nbColsA, nbColsB, sparsity); | ||
|
|
||
| LHSMatrix A; | ||
| RHSMatrix B; | ||
| copyFromEigen(A, eigen_a); | ||
| copyFromEigen(B, eigen_b); | ||
|
|
||
| EXPECT_GT(eigen_a.outerSize(), 0); | ||
| EXPECT_GT(eigen_b.outerSize(), 0); | ||
|
|
||
| Eigen::SparseMatrix<Real, Eigen::RowMajor> eigen_c = eigen_a * eigen_b; | ||
|
|
||
| EXPECT_EQ(eigen_c.rows(), nbRowsA); | ||
| EXPECT_EQ(eigen_c.cols(), nbColsB); | ||
| EXPECT_GT(eigen_c.outerSize(), 0); //to make sure that there are non-zero values in the result matrix | ||
|
|
||
| T product(&A, &B); | ||
| SparseMatrixProductInit<T>::init(product); | ||
| product.computeProduct(); | ||
|
|
||
| EXPECT_TRUE(compareSparseMatrix(eigen_c, product.getProductResult())); | ||
|
|
||
| //the second time computeProduct is called uses the faster algorithm | ||
| product.computeProduct(); | ||
| EXPECT_TRUE(compareSparseMatrix(eigen_c, product.getProductResult())); | ||
|
|
||
| // force re-computing the intersection | ||
| product.computeProduct(true); | ||
| EXPECT_TRUE(compareSparseMatrix(eigen_c, product.getProductResult())); | ||
|
|
||
| //modify the values of A, but not its pattern | ||
| for (int i = 0; i < eigen_a.nonZeros(); ++i) | ||
| { | ||
| eigen_a.valuePtr()[i] = static_cast<SReal>(sofa::helper::drand(1)); | ||
| } | ||
|
|
||
| eigen_c = eigen_a * eigen_b; //result is updated using the regular matrix product | ||
| copyFromEigen(A, eigen_a); | ||
|
|
||
| product.m_lhs = &A; | ||
| product.computeProduct(); //intersection is already computed: uses the faster algorithm | ||
| EXPECT_TRUE(compareSparseMatrix(eigen_c, product.getProductResult())); | ||
|
|
||
| SparseMatrixProductInit<T>::cleanup(product); | ||
|
|
||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| TYPED_TEST_SUITE_P(TestSparseMatrixProduct); | ||
|
|
||
| TYPED_TEST_P(TestSparseMatrixProduct, squareMatrix ) | ||
| { | ||
| EXPECT_TRUE( this->checkMatrix( 5, 5, 5, 1. / 5. ) ); | ||
| EXPECT_TRUE( this->checkMatrix( 5, 5, 5, 3. / 5. ) ); | ||
|
|
||
| EXPECT_TRUE( this->checkMatrix( 100, 1000, 1000, 1. / 1000. ) ); | ||
| EXPECT_TRUE( this->checkMatrix( 1000, 1000, 1000, 20. / 1000. ) ); | ||
| // EXPECT_TRUE( this->checkMatrix( 1000, 1000, 1000, 150. / 1000. ) ); | ||
|
|
||
| EXPECT_TRUE( this->checkMatrix( 20, 20, 20, 1. ) ); | ||
| } | ||
|
|
||
| TYPED_TEST_P(TestSparseMatrixProduct, rectangularMatrix ) | ||
| { | ||
| EXPECT_TRUE( this->checkMatrix( 5, 10, 7, 1. / 5. ) ); | ||
| EXPECT_TRUE( this->checkMatrix( 5, 10, 7, 3. / 5. ) ); | ||
|
|
||
| EXPECT_TRUE( this->checkMatrix( 10, 5, 7, 1. / 5. ) ); | ||
| EXPECT_TRUE( this->checkMatrix( 10, 5, 7, 3. / 5. ) ); | ||
|
|
||
| EXPECT_TRUE( this->checkMatrix( 1000, 3000, 2000, 1. / 1000. ) ); | ||
| EXPECT_TRUE( this->checkMatrix( 1000, 3000, 2000, 20. / 1000. ) ); | ||
|
|
||
| EXPECT_TRUE( this->checkMatrix( 20, 30, 10, 1. ) ); | ||
| } | ||
|
|
||
| REGISTER_TYPED_TEST_SUITE_P(TestSparseMatrixProduct, | ||
| squareMatrix,rectangularMatrix); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alxbilger is there a sense to keep this empty file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File removed in #4599