// // Copyright (c) 2000-2002 // Joerg Walter, Mathias Koch // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // The authors gratefully acknowledge the support of // GeNeSys mbH & Co. KG in producing this work. // #include "bench1.hpp" template struct bench_c_matrix_prod { typedef T value_type; void operator () (int runs) const { try { static typename c_matrix_traits::type m1, m2, m3; initialize_c_matrix () (m1); initialize_c_matrix () (m2); boost::timer t; for (int i = 0; i < runs; ++ i) { for (int j = 0; j < N; ++ j) { for (int k = 0; k < N; ++ k) { m3 [j] [k] = 0; for (int l = 0; l < N; ++ l) { m3 [j] [k] += m1 [j] [l] * m2 [l] [k]; } } } // sink_c_matrix () (m3); } footer () (N * N * N, N * N * (N - 1), runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } } }; template struct bench_my_matrix_prod { typedef typename M::value_type value_type; void operator () (int runs, safe_tag) const { try { static M m1 (N, N), m2 (N, N), m3 (N, N); initialize_matrix (m1); initialize_matrix (m2); boost::timer t; for (int i = 0; i < runs; ++ i) { m3 = ublas::prod (m1, m2); // sink_matrix (m3); } footer () (N * N * N, N * N * (N - 1), runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } } void operator () (int runs, fast_tag) const { try { static M m1 (N, N), m2 (N, N), m3 (N, N); initialize_matrix (m1); initialize_matrix (m2); boost::timer t; for (int i = 0; i < runs; ++ i) { m3.assign (ublas::prod (m1, m2)); // sink_matrix (m3); } footer () (N * N * N, N * N * (N - 1), runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } } }; template struct bench_cpp_matrix_prod { typedef typename M::value_type value_type; void operator () (int runs) const { try { static M m1 (N * N), m2 (N * N), m3 (N * N); initialize_vector (m1); initialize_vector (m2); boost::timer t; for (int i = 0; i < runs; ++ i) { for (int j = 0; j < N; ++ j) { std::valarray row (m1 [std::slice (N * j, N, 1)]); for (int k = 0; k < N; ++ k) { std::valarray column (m2 [std::slice (k, N, N)]); m3 [N * j + k] = (row * column).sum (); } } // sink_vector (m3); } footer () (N * N * N, N * N * (N - 1), runs, t.elapsed ()); } catch (std::exception &e) { std::cout << e.what () << std::endl; } } }; // Benchmark O (n ^ 3) template void bench_3::operator () (int runs) { header ("bench_3"); header ("prod (matrix, matrix)"); header ("C array"); bench_c_matrix_prod () (runs); #ifdef USE_C_ARRAY header ("c_matrix safe"); bench_my_matrix_prod, N> () (runs, safe_tag ()); header ("c_matrix fast"); bench_my_matrix_prod, N> () (runs, fast_tag ()); #endif #ifdef USE_BOUNDED_ARRAY header ("matrix safe"); bench_my_matrix_prod >, N> () (runs, safe_tag ()); header ("matrix fast"); bench_my_matrix_prod >, N> () (runs, fast_tag ()); #endif #ifdef USE_UNBOUNDED_ARRAY header ("matrix safe"); bench_my_matrix_prod >, N> () (runs, safe_tag ()); header ("matrix fast"); bench_my_matrix_prod >, N> () (runs, fast_tag ()); #endif #ifdef USE_STD_VALARRAY header ("matrix safe"); bench_my_matrix_prod >, N> () (runs, safe_tag ()); header ("matrix fast"); bench_my_matrix_prod >, N> () (runs, fast_tag ()); #endif #ifdef USE_STD_VECTOR header ("matrix safe"); bench_my_matrix_prod >, N> () (runs, safe_tag ()); header ("matrix fast"); bench_my_matrix_prod >, N> () (runs, fast_tag ()); #endif #ifdef USE_STD_VALARRAY header ("std::valarray"); bench_cpp_matrix_prod, N> () (runs); #endif } #ifdef USE_FLOAT template struct bench_3; template struct bench_3; template struct bench_3; template struct bench_3; #endif #ifdef USE_DOUBLE template struct bench_3; template struct bench_3; template struct bench_3; template struct bench_3; #endif #ifdef USE_STD_COMPLEX #ifdef USE_FLOAT template struct bench_3, 3>; template struct bench_3, 10>; template struct bench_3, 30>; template struct bench_3, 100>; #endif #ifdef USE_DOUBLE template struct bench_3, 3>; template struct bench_3, 10>; template struct bench_3, 30>; template struct bench_3, 100>; #endif #endif