From 007fe0ff6565fe8df60509b6a644c2998db6c6ba Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Sun, 19 Aug 2018 13:36:31 +0200 Subject: [PATCH] Fixed perf mat div test to avoid NaN --- test/perf/CMakeLists.txt | 2 + test/perf/perf_matrix_div.cpp | 7 +- test/perf/perf_matrix_mul_vector.cpp | 203 +++++++++++++-------------- test/perf/perf_vector_mul_matrix.cpp | 154 ++++++++++++++++++++ 4 files changed, 260 insertions(+), 106 deletions(-) create mode 100644 test/perf/perf_vector_mul_matrix.cpp diff --git a/test/perf/CMakeLists.txt b/test/perf/CMakeLists.txt index 3c12ef5e..10da5ffc 100644 --- a/test/perf/CMakeLists.txt +++ b/test/perf/CMakeLists.txt @@ -1,2 +1,4 @@ glmCreateTestGTC(perf_matrix_mul) +glmCreateTestGTC(perf_matrix_mul_vector) glmCreateTestGTC(perf_matrix_div) +glmCreateTestGTC(perf_vector_mul_matrix) diff --git a/test/perf/perf_matrix_div.cpp b/test/perf/perf_matrix_div.cpp index 913fc5d0..ab41349e 100644 --- a/test/perf/perf_matrix_div.cpp +++ b/test/perf/perf_matrix_div.cpp @@ -26,7 +26,7 @@ static int launch_mat_div_mat(std::vector& O, matType const& Transform, O.resize(Samples); for(std::size_t i = 0; i < Samples; ++i) - I[i] = Scale * static_cast(i); + I[i] = Scale * static_cast(i) + Scale; std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); test_mat_div_mat(Transform, I, O); @@ -56,6 +56,7 @@ static int comp_mat2_div_mat2(std::size_t Samples) packedMatType const A = SISD[i]; packedMatType const B = SIMD[i]; Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + assert(!Error); } return Error; @@ -82,6 +83,7 @@ static int comp_mat3_div_mat3(std::size_t Samples) packedMatType const A = SISD[i]; packedMatType const B = SIMD[i]; Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + assert(!Error); } return Error; @@ -95,7 +97,7 @@ static int comp_mat4_div_mat4(std::size_t Samples) int Error = 0; packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); - 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); + packedMatType const Scale(0.01, 0.02, 0.05, 0.04, 0.02, 0.08, 0.05, 0.01, 0.08, 0.03, 0.05, 0.06, 0.02, 0.03, 0.07, 0.05); std::vector SISD; printf("- SISD: %d us\n", launch_mat_div_mat(SISD, Transform, Scale, Samples)); @@ -108,6 +110,7 @@ static int comp_mat4_div_mat4(std::size_t Samples) packedMatType const A = SISD[i]; packedMatType const B = SIMD[i]; Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + assert(!Error); } return Error; diff --git a/test/perf/perf_matrix_mul_vector.cpp b/test/perf/perf_matrix_mul_vector.cpp index 72b1327d..3dc9d74c 100644 --- a/test/perf/perf_matrix_mul_vector.cpp +++ b/test/perf/perf_matrix_mul_vector.cpp @@ -1,6 +1,12 @@ #define GLM_FORCE_INLINE +#include +#include +#include +#include #include +#include #include +#include #include #if GLM_CONFIG_SIMD == GLM_ENABLE #include @@ -16,137 +22,126 @@ static void test_mat_mul_vec(matType const& M, std::vector const& I, st } template -static int launch_mat_mul_vec(std::size_t Samples) +static int launch_mat_mul_vec(std::vector& O, matType const& Transform, vecType const& Scale, std::size_t Samples) { - typedef typename vecType::value_type T; - - static const matType Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); - - { - std::vector I(Samples); - std::vector O(Samples); - - for(std::size_t i = 0; i < Samples; ++i) - I[i] = vecType(static_cast(i)) * vecType(0.01, 0.02, 0.03, 0.05); - - std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); - test_mat_mul_vec(Transform, I, O); - std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); - - return static_cast(std::chrono::duration_cast(t2 - t1).count()); - } -} - -template -static void test_vec_mul_mat(matType const& M, std::vector const& I, std::vector& O) -{ - for (std::size_t i = 0, n = I.size(); i < n; ++i) - O[i] = I[i] * M; -} - -template -static int launch_vec_mul_mat(std::size_t Samples) -{ - typedef typename vecType::value_type T; - - static const matType Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); + typedef typename matType::value_type T; std::vector I(Samples); - std::vector O(Samples); + O.resize(Samples); for(std::size_t i = 0; i < Samples; ++i) - I[i] = vecType(static_cast(i)) * vecType(0.01, 0.02, 0.03, 0.05); + I[i] = Scale * static_cast(i); std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); - test_vec_mul_mat(Transform, I, O); + test_mat_mul_vec(Transform, I, O); std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); return static_cast(std::chrono::duration_cast(t2 - t1).count()); } -template -static void test_mat_mul_mat(matType const& M, std::vector const& I, std::vector& O) +template +static int comp_mat2_mul_vec2(std::size_t Samples) { - for (std::size_t i = 0, n = I.size(); i < n; ++i) - O[i] = M * I[i]; -} - -template -static int launch_mat_mul_mat(std::size_t Samples) -{ - typedef typename matType::value_type T; - - static const matType Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); - - std::vector I(Samples); - std::vector O(Samples); - - for(std::size_t i = 0; i < Samples; ++i) - 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(i); - - std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); - test_mat_mul_mat(Transform, I, O); - std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); - - return static_cast(std::chrono::duration_cast(t2 - t1).count()); -} - -template -static void test_mat_div_mat(matType const& M, std::vector const& I, std::vector& O) -{ - for (std::size_t i = 0, n = I.size(); i < n; ++i) - O[i] = M / I[i]; -} - -template -static int launch_mat_div_mat(std::size_t Samples) -{ - typedef typename matType::value_type T; + typedef typename packedMatType::value_type T; - static const matType Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); + int Error = 0; - std::vector I(Samples); - std::vector O(Samples); + packedMatType const Transform(1, 2, 3, 4); + packedVecType const Scale(0.01, 0.02); + + std::vector SISD; + printf("- SISD: %d us\n", launch_mat_mul_vec(SISD, Transform, Scale, Samples)); + + std::vector SIMD; + printf("- SIMD: %d us\n", launch_mat_mul_vec(SIMD, Transform, Scale, Samples)); for(std::size_t i = 0; i < Samples; ++i) - 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(i); + { + packedVecType const A = SISD[i]; + packedVecType const B = packedVecType(SIMD[i]); + Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + } + + return Error; +} - std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); - test_mat_div_mat(Transform, I, O); - std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); +template +static int comp_mat3_mul_vec3(std::size_t Samples) +{ + typedef typename packedMatType::value_type T; + + int Error = 0; - return static_cast(std::chrono::duration_cast(t2 - t1).count()); + packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9); + packedVecType const Scale(0.01, 0.02, 0.05); + + std::vector SISD; + printf("- SISD: %d us\n", launch_mat_mul_vec(SISD, Transform, Scale, Samples)); + + std::vector SIMD; + printf("- SIMD: %d us\n", launch_mat_mul_vec(SIMD, Transform, Scale, Samples)); + + for(std::size_t i = 0; i < Samples; ++i) + { + packedVecType const A = SISD[i]; + packedVecType const B = SIMD[i]; + Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + } + + return Error; +} + +template +static int comp_mat4_mul_vec4(std::size_t Samples) +{ + typedef typename packedMatType::value_type T; + + int Error = 0; + + packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); + packedVecType const Scale(0.01, 0.02, 0.03, 0.05); + + std::vector SISD; + printf("- SISD: %d us\n", launch_mat_mul_vec(SISD, Transform, Scale, Samples)); + + std::vector SIMD; + printf("- SIMD: %d us\n", launch_mat_mul_vec(SIMD, Transform, Scale, Samples)); + + for(std::size_t i = 0; i < Samples; ++i) + { + packedVecType const A = SISD[i]; + packedVecType const B = SIMD[i]; + Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + } + + return Error; } int main() { - std::size_t const Samples = 50000; + std::size_t const Samples = 100000; + + int Error = 0; - printf("\nmat4 * vec4\n"); - printf("- dmat4 * dvec4 duration %d us\n", launch_mat_mul_vec(Samples)); - printf("- dmat4 * dvec4 (SIMD) duration %d us\n", launch_mat_mul_vec(Samples)); - printf("- mat4 * vec4 duration %d us\n", launch_mat_mul_vec(Samples)); - printf("- mat4 * vec4 (SIMD) duration %d us\n", launch_mat_mul_vec(Samples)); + printf("mat2 * vec2:\n"); + Error += comp_mat2_mul_vec2(Samples); + + printf("dmat2 * dvec2:\n"); + Error += comp_mat2_mul_vec2(Samples); - printf("\nvec4 * mat4\n"); - printf("- dvec4 * dmat4 duration %d us\n", launch_vec_mul_mat(Samples)); - printf("- dvec4 * dmat4 (SIMD) duration %d us\n", launch_vec_mul_mat(Samples)); - printf("- vec4 * mat4 duration %d us\n", launch_vec_mul_mat(Samples)); - printf("- vec4 * mat4 (SIMD) duration %d us\n", launch_vec_mul_mat(Samples)); + printf("mat3 * vec3:\n"); + Error += comp_mat3_mul_vec3(Samples); + + printf("dmat3 * dvec3:\n"); + Error += comp_mat3_mul_vec3(Samples); - printf("\nmat4 * mat4\n"); - printf("- dmat4 * dmat4 duration %d us\n", launch_mat_mul_mat(Samples)); - printf("- dmat4 * dmat4 (SIMD) duration %d us\n", launch_mat_mul_mat(Samples)); - printf("- mat4 * mat4 duration %d us\n", launch_mat_mul_mat(Samples)); - printf("- mat4 * mat4 (SIMD) duration %d us\n", launch_mat_mul_mat(Samples)); + printf("mat4 * vec4:\n"); + Error += comp_mat4_mul_vec4(Samples); + + printf("dmat4 * dvec4:\n"); + Error += comp_mat4_mul_vec4(Samples); - printf("\nmat4 / mat4\n"); - printf("- dmat4 / dmat4 duration %d us\n", launch_mat_div_mat(Samples)); - printf("- dmat4 / dmat4 (SIMD) duration %d us\n", launch_mat_div_mat(Samples)); - printf("- mat4 / mat4 duration %d us\n", launch_mat_div_mat(Samples)); - printf("- mat4 / mat4 (SIMD) duration %d us\n", launch_mat_div_mat(Samples)); - - return 0; + return Error; } #else diff --git a/test/perf/perf_vector_mul_matrix.cpp b/test/perf/perf_vector_mul_matrix.cpp new file mode 100644 index 00000000..f38bd2f8 --- /dev/null +++ b/test/perf/perf_vector_mul_matrix.cpp @@ -0,0 +1,154 @@ +#define GLM_FORCE_INLINE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if GLM_CONFIG_SIMD == GLM_ENABLE +#include +#include +#include +#include + +template +static void test_vec_mul_mat(matType const& M, std::vector const& I, std::vector& O) +{ + for (std::size_t i = 0, n = I.size(); i < n; ++i) + O[i] = I[i] * M; +} + +template +static int launch_vec_mul_mat(std::vector& O, matType const& Transform, vecType const& Scale, std::size_t Samples) +{ + typedef typename matType::value_type T; + + std::vector I(Samples); + O.resize(Samples); + + for(std::size_t i = 0; i < Samples; ++i) + I[i] = Scale * static_cast(i); + + std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); + test_vec_mul_mat(Transform, I, O); + std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); + + return static_cast(std::chrono::duration_cast(t2 - t1).count()); +} + +template +static int comp_vec2_mul_mat2(std::size_t Samples) +{ + typedef typename packedMatType::value_type T; + + int Error = 0; + + packedMatType const Transform(1, 2, 3, 4); + packedVecType const Scale(0.01, 0.02); + + std::vector SISD; + printf("- SISD: %d us\n", launch_vec_mul_mat(SISD, Transform, Scale, Samples)); + + std::vector SIMD; + printf("- SIMD: %d us\n", launch_vec_mul_mat(SIMD, Transform, Scale, Samples)); + + for(std::size_t i = 0; i < Samples; ++i) + { + packedVecType const A = SISD[i]; + packedVecType const B = packedVecType(SIMD[i]); + Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + } + + return Error; +} + +template +static int comp_vec3_mul_mat3(std::size_t Samples) +{ + typedef typename packedMatType::value_type T; + + int Error = 0; + + packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9); + packedVecType const Scale(0.01, 0.02, 0.05); + + std::vector SISD; + printf("- SISD: %d us\n", launch_vec_mul_mat(SISD, Transform, Scale, Samples)); + + std::vector SIMD; + printf("- SIMD: %d us\n", launch_vec_mul_mat(SIMD, Transform, Scale, Samples)); + + for(std::size_t i = 0; i < Samples; ++i) + { + packedVecType const A = SISD[i]; + packedVecType const B = SIMD[i]; + Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + } + + return Error; +} + +template +static int comp_vec4_mul_mat4(std::size_t Samples) +{ + typedef typename packedMatType::value_type T; + + int Error = 0; + + packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); + packedVecType const Scale(0.01, 0.02, 0.03, 0.05); + + std::vector SISD; + printf("- SISD: %d us\n", launch_vec_mul_mat(SISD, Transform, Scale, Samples)); + + std::vector SIMD; + printf("- SIMD: %d us\n", launch_vec_mul_mat(SIMD, Transform, Scale, Samples)); + + for(std::size_t i = 0; i < Samples; ++i) + { + packedVecType const A = SISD[i]; + packedVecType const B = SIMD[i]; + Error += glm::all(glm::equal(A, B, static_cast(0.001))) ? 0 : 1; + } + + return Error; +} + +int main() +{ + std::size_t const Samples = 100000; + + int Error = 0; + + printf("vec2 * mat2:\n"); + Error += comp_vec2_mul_mat2(Samples); + + printf("dvec2 * dmat2:\n"); + Error += comp_vec2_mul_mat2(Samples); + + printf("vec3 * mat3:\n"); + Error += comp_vec3_mul_mat3(Samples); + + printf("dvec3 * dmat3:\n"); + Error += comp_vec3_mul_mat3(Samples); + + printf("vec4 * mat4:\n"); + Error += comp_vec4_mul_mat4(Samples); + + printf("dvec4 * dmat4:\n"); + Error += comp_vec4_mul_mat4(Samples); + + return Error; +} + +#else + +int main() +{ + return 0; +} + +#endif