perf_matrix_mul.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #define GLM_FORCE_INLINE
  2. #include <glm/ext/matrix_float4x4.hpp>
  3. #include <glm/ext/matrix_transform.hpp>
  4. #include <glm/ext/matrix_relational.hpp>
  5. #include <glm/ext/vector_float4.hpp>
  6. #if GLM_CONFIG_SIMD == GLM_ENABLE
  7. #include <glm/gtc/type_aligned.hpp>
  8. #include <vector>
  9. #include <chrono>
  10. #include <cstdio>
  11. template <typename matType>
  12. static void test_mat_mul_mat(matType const& M, std::vector<matType> const& I, std::vector<matType>& O)
  13. {
  14. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  15. O[i] = M * I[i];
  16. }
  17. template <typename matType>
  18. static int launch_mat_mul_mat(std::vector<matType>& O, matType const& Transform, matType const& Scale, std::size_t Samples)
  19. {
  20. typedef typename matType::value_type T;
  21. std::vector<matType> I(Samples);
  22. O.resize(Samples);
  23. for(std::size_t i = 0; i < Samples; ++i)
  24. I[i] = Scale * static_cast<T>(i);
  25. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  26. test_mat_mul_mat<matType>(Transform, I, O);
  27. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  28. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  29. }
  30. static int comp_mat_mul_mat(std::size_t Samples)
  31. {
  32. int Error = 0;
  33. glm::mat4 const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  34. glm::mat4 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);
  35. std::vector<glm::mat4> Mat4SISD;
  36. printf("mat4 * mat4 (SISD) duration %d us\n", launch_mat_mul_mat<glm::mat4>(Mat4SISD, Transform, Scale, Samples));
  37. std::vector<glm::aligned_mat4> Mat4SIMD;
  38. printf("mat4 * mat4 (SIMD) duration %d us\n", launch_mat_mul_mat<glm::aligned_mat4>(Mat4SIMD, Transform, Scale, Samples));
  39. for(std::size_t i = 0; i < Samples; ++i)
  40. Error += glm::all(glm::equal(Mat4SISD[i], Mat4SIMD[i], 0.001)) ? 0 : 1;
  41. return Error;
  42. }
  43. int main()
  44. {
  45. std::size_t const Samples = 100000;
  46. int Error = 0;
  47. Error += comp_mat_mul_mat(Samples);
  48. return Error;
  49. }
  50. #else
  51. int main()
  52. {
  53. return 0;
  54. }
  55. #endif