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.

95 lines
3.5 KiB

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