perf_matrix_mul.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. inline bool
  17. is_aligned(const void* ptr, std::uintptr_t alignment) noexcept {
  18. auto iptr = reinterpret_cast<std::uintptr_t>(ptr);
  19. return !(iptr % alignment);
  20. }
  21. template <typename matType>
  22. static void align_check(matType const& M, std::vector<matType> const& I, std::vector<matType>& O)
  23. {
  24. if (matType::col_type::is_aligned::value)
  25. {
  26. if (!is_aligned(&M, 16))
  27. abort();
  28. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  29. {
  30. if (!is_aligned(&I[i], 16))
  31. abort();
  32. if (!is_aligned(&O[i], 16))
  33. abort();
  34. }
  35. }
  36. }
  37. template <typename matType>
  38. static void test_mat_mul_mat(matType const& M, std::vector<matType> const& I, std::vector<matType>& O)
  39. {
  40. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  41. O[i] = M * I[i];
  42. }
  43. template <typename matType>
  44. static int launch_mat_mul_mat(std::vector<matType>& O, matType const& Transform, matType const& Scale, std::size_t Samples)
  45. {
  46. typedef typename matType::value_type T;
  47. std::vector<matType> I(Samples);
  48. O.resize(Samples);
  49. for(std::size_t i = 0; i < Samples; ++i)
  50. I[i] = Scale * static_cast<T>(i);
  51. align_check<matType>(Transform, I, O);
  52. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  53. test_mat_mul_mat<matType>(Transform, I, O);
  54. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  55. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  56. }
  57. template <typename packedMatType, typename alignedMatType>
  58. static int comp_mat2_mul_mat2(std::size_t Samples)
  59. {
  60. typedef typename packedMatType::value_type T;
  61. int Error = 0;
  62. packedMatType const Transform(1, 2, 3, 4);
  63. packedMatType const Scale(0.01, 0.02, 0.03, 0.05);
  64. std::vector<packedMatType> SISD;
  65. std::printf("- SISD: %d us\n", launch_mat_mul_mat<packedMatType>(SISD, Transform, Scale, Samples));
  66. std::vector<alignedMatType> SIMD;
  67. std::printf("- SIMD: %d us\n", launch_mat_mul_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  68. for(std::size_t i = 0; i < Samples; ++i)
  69. {
  70. packedMatType const A = SISD[i];
  71. packedMatType const B = SIMD[i];
  72. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  73. }
  74. return Error;
  75. }
  76. template<typename T1, typename T2>
  77. bool percent_error(const T1& a, const T2& b, float percentThreshold)
  78. {
  79. typedef typename T1::value_type value_type;
  80. for (int i = 0; i < a.length(); ++i)
  81. for (int j = 0; j < a[i].length(); ++j)
  82. {
  83. value_type v;
  84. if (glm::abs(a[i][j] - value_type(0)) < glm::epsilon<value_type>())
  85. v = ((b[i][j] - a[i][j]) / a[i][j]) * value_type(100);
  86. else
  87. v = b[i][j] * value_type(100);
  88. if (v > value_type(percentThreshold))
  89. return false;
  90. }
  91. return true;
  92. }
  93. template <typename packedMatType, typename alignedMatType>
  94. static int comp_mat3_mul_mat3(std::size_t Samples)
  95. {
  96. int Error = 0;
  97. std::vector<packedMatType> SISD;
  98. {
  99. packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9);
  100. packedMatType const Scale(0.01, 0.02, 0.03, 0.05, 0.01, 0.02, 0.03, 0.05, 0.01);
  101. std::printf("- SISD: %d us\n", launch_mat_mul_mat<packedMatType>(SISD, Transform, Scale, Samples));
  102. }
  103. std::vector<alignedMatType> SIMD;
  104. {
  105. alignedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9);
  106. alignedMatType const Scale(0.01, 0.02, 0.03, 0.05, 0.01, 0.02, 0.03, 0.05, 0.01);
  107. std::printf("- SIMD: %d us\n", launch_mat_mul_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  108. }
  109. for(std::size_t i = 0; i < Samples; ++i)
  110. {
  111. packedMatType const A = SISD[i];
  112. packedMatType const B = SIMD[i];
  113. Error += percent_error(A, B, 0.01f) ? 0 : 1;
  114. }
  115. return Error;
  116. }
  117. template <typename packedMatType, typename alignedMatType>
  118. static int comp_mat4_mul_mat4(std::size_t Samples)
  119. {
  120. int Error = 0;
  121. packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  122. packedMatType const Scale(0.01, 0.02, 0.03, 0.05, 0.01, 0.02, 0.03, 0.05, 0.01, 0.02, 0.03, 0.05, 0.01, 0.02, 0.03, 0.05);
  123. std::vector<packedMatType> SISD;
  124. std::printf("- SISD: %d us\n", launch_mat_mul_mat<packedMatType>(SISD, Transform, Scale, Samples));
  125. std::vector<alignedMatType> SIMD;
  126. std::printf("- SIMD: %d us\n", launch_mat_mul_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  127. for(std::size_t i = 0; i < Samples; ++i)
  128. {
  129. packedMatType const A = SISD[i];
  130. packedMatType const B = SIMD[i];
  131. Error += percent_error(A, B, 0.01f) ? 0 : 1;
  132. }
  133. return Error;
  134. }
  135. int main()
  136. {
  137. std::size_t const Samples = 1000;
  138. int Error = 0;
  139. std::printf("mat2 * mat2:\n");
  140. Error += comp_mat2_mul_mat2<glm::mat2, glm::aligned_mat2>(Samples);
  141. std::printf("dmat2 * dmat2:\n");
  142. Error += comp_mat2_mul_mat2<glm::dmat2, glm::aligned_dmat2>(Samples);
  143. std::printf("mat3 * mat3:\n");
  144. Error += comp_mat3_mul_mat3<glm::mat3, glm::aligned_mat3>(Samples);
  145. std::printf("dmat3 * dmat3:\n");
  146. Error += comp_mat3_mul_mat3<glm::dmat3, glm::aligned_dmat3>(Samples);
  147. std::printf("mat4 * mat4:\n");
  148. Error += comp_mat4_mul_mat4<glm::mat4, glm::aligned_mat4>(Samples);
  149. std::printf("dmat4 * dmat4:\n");
  150. Error += comp_mat4_mul_mat4<glm::dmat4, glm::aligned_dmat4>(Samples);
  151. return Error;
  152. }
  153. #else
  154. int main()
  155. {
  156. return 0;
  157. }
  158. #endif