You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.4 KiB

  1. #include "gtest/gtest.h"
  2. #include "src/storage/SparseMatrix.h"
  3. TEST(SparseMatrix, Iteration) {
  4. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  5. for (uint_fast64_t row = 0; row < 10000; ++row) {
  6. for (uint_fast64_t column = 0; column < row; ++column) {
  7. ASSERT_NO_THROW(matrixBuilder.addNextValue(row, column, row+column));
  8. }
  9. }
  10. storm::storage::SparseMatrix<double> matrix;
  11. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  12. for (uint_fast64_t row = 0; row < matrix.getRowCount(); ++row) {
  13. for (auto const& entry : matrix.getRow(row)) {
  14. // The following can never be true, but prevents the compiler from optimizing away the loop.
  15. if (entry.getColumn() > matrix.getColumnCount()) {
  16. ASSERT_TRUE(false);
  17. }
  18. }
  19. }
  20. }
  21. TEST(SparseMatrix, SparseMultiplication) {
  22. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  23. for (uint_fast64_t row = 0; row < 100000; ++row) {
  24. ASSERT_NO_THROW(matrixBuilder.addNextValue(row, 0, storm::utility::constantOne<double>()));
  25. ASSERT_NO_THROW(matrixBuilder.addNextValue(row, 1, storm::utility::constantOne<double>()));
  26. }
  27. storm::storage::SparseMatrix<double> matrix;
  28. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  29. std::vector<double> x(matrix.getColumnCount(), 1.0);
  30. std::vector<double> result(matrix.getRowCount());
  31. for (uint_fast64_t i = 0; i < 30000; ++i) {
  32. matrix.multiplyWithVector(x, result);
  33. // The following can never be true, but prevents the compiler from optimizing away the loop.
  34. if (result.size() > matrix.getRowCount()) {
  35. ASSERT_TRUE(false);
  36. }
  37. }
  38. }
  39. TEST(SparseMatrix, HalfSparseMultiplication) {
  40. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  41. for (uint_fast64_t row = 0; row < 2000; ++row) {
  42. for (uint_fast64_t column = 0; column < row; ++column) {
  43. ASSERT_NO_THROW(matrixBuilder.addNextValue(row, column, row+column));
  44. }
  45. }
  46. storm::storage::SparseMatrix<double> matrix;
  47. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  48. std::vector<double> x(matrix.getColumnCount(), 1.0);
  49. std::vector<double> result(matrix.getRowCount());
  50. for (uint_fast64_t i = 0; i < 5000; ++i) {
  51. matrix.multiplyWithVector(x, result);
  52. // The following can never be true, but prevents the compiler from optimizing away the loop.
  53. if (result.size() > matrix.getRowCount()) {
  54. ASSERT_TRUE(false);
  55. }
  56. }
  57. }
  58. TEST(SparseMatrix, DenseMultiplication) {
  59. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  60. uint_fast64_t const size = 2000;
  61. for (uint_fast64_t row = 0; row < size; ++row) {
  62. for (uint_fast64_t column = 0; column < size; ++column) {
  63. ASSERT_NO_THROW(matrixBuilder.addNextValue(row, column, storm::utility::constantOne<double>()));
  64. }
  65. }
  66. storm::storage::SparseMatrix<double> matrix;
  67. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  68. std::vector<double> x(matrix.getColumnCount(), 1.0);
  69. std::vector<double> result(matrix.getRowCount());
  70. for (uint_fast64_t i = 0; i < 1000; ++i) {
  71. matrix.multiplyWithVector(x, result);
  72. // The following can never be true, but prevents the compiler from optimizing away the loop.
  73. if (result.size() > matrix.getRowCount()) {
  74. ASSERT_TRUE(false);
  75. }
  76. }
  77. }