perf_matrix_mul_vector.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #define GLM_FORCE_INLINE
  2. #include <glm/ext/matrix_float4x4.hpp>
  3. #include <glm/ext/matrix_transform.hpp>
  4. #include <glm/ext/vector_float4.hpp>
  5. #if GLM_CONFIG_SIMD == GLM_ENABLE
  6. #include <glm/gtc/type_aligned.hpp>
  7. #include <vector>
  8. #include <chrono>
  9. #include <cstdio>
  10. template <typename matType, typename vecType>
  11. static void test_mat_mul_vec(matType const& M, std::vector<vecType> const& I, std::vector<vecType>& O)
  12. {
  13. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  14. O[i] = M * I[i];
  15. }
  16. template <typename matType, typename vecType>
  17. static int launch_mat_mul_vec(std::size_t Samples)
  18. {
  19. typedef typename vecType::value_type T;
  20. static const matType Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  21. {
  22. std::vector<vecType> I(Samples);
  23. std::vector<vecType> O(Samples);
  24. for(std::size_t i = 0; i < Samples; ++i)
  25. I[i] = vecType(static_cast<T>(i)) * vecType(0.01, 0.02, 0.03, 0.05);
  26. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  27. test_mat_mul_vec<matType, vecType>(Transform, I, O);
  28. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  29. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  30. }
  31. }
  32. template <typename matType, typename vecType>
  33. static void test_vec_mul_mat(matType const& M, std::vector<vecType> const& I, std::vector<vecType>& O)
  34. {
  35. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  36. O[i] = I[i] * M;
  37. }
  38. template <typename matType, typename vecType>
  39. static int launch_vec_mul_mat(std::size_t Samples)
  40. {
  41. typedef typename vecType::value_type T;
  42. static const matType Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  43. std::vector<vecType> I(Samples);
  44. std::vector<vecType> O(Samples);
  45. for(std::size_t i = 0; i < Samples; ++i)
  46. I[i] = vecType(static_cast<T>(i)) * vecType(0.01, 0.02, 0.03, 0.05);
  47. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  48. test_vec_mul_mat<matType, vecType>(Transform, I, O);
  49. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  50. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  51. }
  52. template <typename matType>
  53. static void test_mat_mul_mat(matType const& M, std::vector<matType> const& I, std::vector<matType>& O)
  54. {
  55. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  56. O[i] = M * I[i];
  57. }
  58. template <typename matType>
  59. static int launch_mat_mul_mat(std::size_t Samples)
  60. {
  61. typedef typename matType::value_type T;
  62. static const matType Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  63. std::vector<matType> I(Samples);
  64. std::vector<matType> O(Samples);
  65. for(std::size_t i = 0; i < Samples; ++i)
  66. I[i] = matType(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) * static_cast<T>(i);
  67. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  68. test_mat_mul_mat<matType>(Transform, I, O);
  69. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  70. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  71. }
  72. template <typename matType>
  73. static void test_mat_div_mat(matType const& M, std::vector<matType> const& I, std::vector<matType>& O)
  74. {
  75. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  76. O[i] = M / I[i];
  77. }
  78. template <typename matType>
  79. static int launch_mat_div_mat(std::size_t Samples)
  80. {
  81. typedef typename matType::value_type T;
  82. static const matType Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  83. std::vector<matType> I(Samples);
  84. std::vector<matType> O(Samples);
  85. for(std::size_t i = 0; i < Samples; ++i)
  86. I[i] = matType(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) * static_cast<T>(i);
  87. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  88. test_mat_div_mat<matType>(Transform, I, O);
  89. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  90. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  91. }
  92. int main()
  93. {
  94. std::size_t const Samples = 50000;
  95. printf("\nmat4 * vec4\n");
  96. printf("- dmat4 * dvec4 duration %d us\n", launch_mat_mul_vec<glm::dmat4, glm::dvec4>(Samples));
  97. printf("- dmat4 * dvec4 (SIMD) duration %d us\n", launch_mat_mul_vec<glm::aligned_dmat4, glm::aligned_dvec4>(Samples));
  98. printf("- mat4 * vec4 duration %d us\n", launch_mat_mul_vec<glm::mat4, glm::vec4>(Samples));
  99. printf("- mat4 * vec4 (SIMD) duration %d us\n", launch_mat_mul_vec<glm::aligned_mat4, glm::aligned_vec4>(Samples));
  100. printf("\nvec4 * mat4\n");
  101. printf("- dvec4 * dmat4 duration %d us\n", launch_vec_mul_mat<glm::dmat4, glm::dvec4>(Samples));
  102. printf("- dvec4 * dmat4 (SIMD) duration %d us\n", launch_vec_mul_mat<glm::aligned_dmat4, glm::aligned_dvec4>(Samples));
  103. printf("- vec4 * mat4 duration %d us\n", launch_vec_mul_mat<glm::mat4, glm::vec4>(Samples));
  104. printf("- vec4 * mat4 (SIMD) duration %d us\n", launch_vec_mul_mat<glm::aligned_mat4, glm::aligned_vec4>(Samples));
  105. printf("\nmat4 * mat4\n");
  106. printf("- dmat4 * dmat4 duration %d us\n", launch_mat_mul_mat<glm::dmat4>(Samples));
  107. printf("- dmat4 * dmat4 (SIMD) duration %d us\n", launch_mat_mul_mat<glm::aligned_dmat4>(Samples));
  108. printf("- mat4 * mat4 duration %d us\n", launch_mat_mul_mat<glm::mat4>(Samples));
  109. printf("- mat4 * mat4 (SIMD) duration %d us\n", launch_mat_mul_mat<glm::aligned_mat4>(Samples));
  110. printf("\nmat4 / mat4\n");
  111. printf("- dmat4 / dmat4 duration %d us\n", launch_mat_div_mat<glm::dmat4>(Samples));
  112. printf("- dmat4 / dmat4 (SIMD) duration %d us\n", launch_mat_div_mat<glm::aligned_dmat4>(Samples));
  113. printf("- mat4 / mat4 duration %d us\n", launch_mat_div_mat<glm::mat4>(Samples));
  114. printf("- mat4 / mat4 (SIMD) duration %d us\n", launch_mat_div_mat<glm::aligned_mat4>(Samples));
  115. return 0;
  116. }
  117. #else
  118. int main()
  119. {
  120. return 0;
  121. }
  122. #endif