perf_matrix_mul_vector.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #define GLM_FORCE_INLINE
  2. #include <glm/ext/matrix_float2x2.hpp>
  3. #include <glm/ext/matrix_double2x2.hpp>
  4. #include <glm/ext/matrix_float3x3.hpp>
  5. #include <glm/ext/matrix_double3x3.hpp>
  6. #include <glm/ext/matrix_float4x4.hpp>
  7. #include <glm/ext/matrix_double4x4.hpp>
  8. #include <glm/ext/matrix_transform.hpp>
  9. #include <glm/ext/matrix_relational.hpp>
  10. #include <glm/ext/vector_float4.hpp>
  11. #if GLM_CONFIG_SIMD == GLM_ENABLE
  12. #include <glm/gtc/type_aligned.hpp>
  13. #include <vector>
  14. #include <chrono>
  15. #include <cstdio>
  16. template <typename matType, typename vecType>
  17. static void test_mat_mul_vec(matType const& M, std::vector<vecType> const& I, std::vector<vecType>& O)
  18. {
  19. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  20. O[i] = M * I[i];
  21. }
  22. template <typename matType, typename vecType>
  23. static int launch_mat_mul_vec(std::vector<vecType>& O, matType const& Transform, vecType const& Scale, std::size_t Samples)
  24. {
  25. typedef typename matType::value_type T;
  26. std::vector<vecType> I(Samples);
  27. O.resize(Samples);
  28. for(std::size_t i = 0; i < Samples; ++i)
  29. I[i] = Scale * static_cast<T>(i);
  30. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  31. test_mat_mul_vec<matType, vecType>(Transform, I, O);
  32. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  33. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  34. }
  35. template <typename packedMatType, typename packedVecType, typename alignedMatType, typename alignedVecType>
  36. static int comp_mat2_mul_vec2(std::size_t Samples)
  37. {
  38. typedef typename packedMatType::value_type T;
  39. int Error = 0;
  40. packedMatType const Transform(1, 2, 3, 4);
  41. packedVecType const Scale(0.01, 0.02);
  42. std::vector<packedVecType> SISD;
  43. std::printf("- SISD: %d us\n", launch_mat_mul_vec<packedMatType, packedVecType>(SISD, Transform, Scale, Samples));
  44. std::vector<alignedVecType> SIMD;
  45. std::printf("- SIMD: %d us\n", launch_mat_mul_vec<alignedMatType, alignedVecType>(SIMD, Transform, Scale, Samples));
  46. for(std::size_t i = 0; i < Samples; ++i)
  47. {
  48. packedVecType const A = SISD[i];
  49. packedVecType const B = packedVecType(SIMD[i]);
  50. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  51. }
  52. return Error;
  53. }
  54. template <typename packedMatType, typename packedVecType, typename alignedMatType, typename alignedVecType>
  55. static int comp_mat3_mul_vec3(std::size_t Samples)
  56. {
  57. typedef typename packedMatType::value_type T;
  58. int Error = 0;
  59. std::vector<packedVecType> SISD;
  60. {
  61. packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9);
  62. packedVecType const Scale(0.01, 0.02, 0.05);
  63. std::printf("- SISD: %d us\n", launch_mat_mul_vec<packedMatType, packedVecType>(SISD, Transform, Scale, Samples));
  64. }
  65. std::vector<alignedVecType> SIMD;
  66. {
  67. alignedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9);
  68. alignedVecType const Scale(0.01, 0.02, 0.05);
  69. std::printf("- SIMD: %d us\n", launch_mat_mul_vec<alignedMatType, alignedVecType>(SIMD, Transform, Scale, Samples));
  70. }
  71. for(std::size_t i = 0; i < Samples; ++i)
  72. {
  73. packedVecType const A = SISD[i];
  74. packedVecType const B = SIMD[i];
  75. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  76. }
  77. return Error;
  78. }
  79. template <typename packedMatType, typename packedVecType, typename alignedMatType, typename alignedVecType>
  80. static int comp_mat4_mul_vec4(std::size_t Samples)
  81. {
  82. typedef typename packedMatType::value_type T;
  83. int Error = 0;
  84. packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  85. packedVecType const Scale(0.01, 0.02, 0.03, 0.05);
  86. std::vector<packedVecType> SISD;
  87. std::printf("- SISD: %d us\n", launch_mat_mul_vec<packedMatType, packedVecType>(SISD, Transform, Scale, Samples));
  88. std::vector<alignedVecType> SIMD;
  89. std::printf("- SIMD: %d us\n", launch_mat_mul_vec<alignedMatType, alignedVecType>(SIMD, Transform, Scale, Samples));
  90. for(std::size_t i = 0; i < Samples; ++i)
  91. {
  92. packedVecType const A = SISD[i];
  93. packedVecType const B = SIMD[i];
  94. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  95. }
  96. return Error;
  97. }
  98. int main()
  99. {
  100. std::size_t const Samples = 1000;
  101. int Error = 0;
  102. std::printf("mat2 * vec2:\n");
  103. Error += comp_mat2_mul_vec2<glm::mat2, glm::vec2, glm::aligned_mat2, glm::aligned_vec2>(Samples);
  104. std::printf("dmat2 * dvec2:\n");
  105. Error += comp_mat2_mul_vec2<glm::dmat2, glm::dvec2, glm::aligned_dmat2, glm::aligned_dvec2>(Samples);
  106. std::printf("mat3 * vec3:\n");
  107. Error += comp_mat3_mul_vec3<glm::mat3, glm::vec3, glm::aligned_mat3, glm::aligned_vec3>(Samples);
  108. std::printf("dmat3 * dvec3:\n");
  109. Error += comp_mat3_mul_vec3<glm::dmat3, glm::dvec3, glm::aligned_dmat3, glm::aligned_dvec3>(Samples);
  110. std::printf("mat4 * vec4:\n");
  111. Error += comp_mat4_mul_vec4<glm::mat4, glm::vec4, glm::aligned_mat4, glm::aligned_vec4>(Samples);
  112. std::printf("dmat4 * dvec4:\n");
  113. Error += comp_mat4_mul_vec4<glm::dmat4, glm::dvec4, glm::aligned_dmat4, glm::aligned_dvec4>(Samples);
  114. return Error;
  115. }
  116. #else
  117. int main()
  118. {
  119. return 0;
  120. }
  121. #endif