blob: fadb0b67333343b09b6a5c4baf4d1c97d6f4ff3d [file] [log] [blame]
Brian Silvermandc6866b2018-08-05 00:18:23 -07001//
2// Copyright (c) 2000-2002
3// Joerg Walter, Mathias Koch
4//
5// Distributed under the Boost Software License, Version 1.0. (See
6// accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// The authors gratefully acknowledge the support of
10// GeNeSys mbH & Co. KG in producing this work.
11//
12
13#include "bench1.hpp"
14
15template<class T, int N>
16struct bench_c_matrix_prod {
17 typedef T value_type;
18
19 void operator () (int runs) const {
20 try {
21 static typename c_matrix_traits<T, N, N>::type m1, m2, m3;
22 initialize_c_matrix<T, N, N> () (m1);
23 initialize_c_matrix<T, N, N> () (m2);
24 boost::timer t;
25 for (int i = 0; i < runs; ++ i) {
26 for (int j = 0; j < N; ++ j) {
27 for (int k = 0; k < N; ++ k) {
28 m3 [j] [k] = 0;
29 for (int l = 0; l < N; ++ l) {
30 m3 [j] [k] += m1 [j] [l] * m2 [l] [k];
31 }
32 }
33 }
34// sink_c_matrix<T, N, N> () (m3);
35 }
36 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
37 }
38 catch (std::exception &e) {
39 std::cout << e.what () << std::endl;
40 }
41 }
42};
43template<class M, int N>
44struct bench_my_matrix_prod {
45 typedef typename M::value_type value_type;
46
47 void operator () (int runs, safe_tag) const {
48 try {
49 static M m1 (N, N), m2 (N, N), m3 (N, N);
50 initialize_matrix (m1);
51 initialize_matrix (m2);
52 boost::timer t;
53 for (int i = 0; i < runs; ++ i) {
54 m3 = ublas::prod (m1, m2);
55// sink_matrix (m3);
56 }
57 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
58 }
59 catch (std::exception &e) {
60 std::cout << e.what () << std::endl;
61 }
62 }
63 void operator () (int runs, fast_tag) const {
64 try {
65 static M m1 (N, N), m2 (N, N), m3 (N, N);
66 initialize_matrix (m1);
67 initialize_matrix (m2);
68 boost::timer t;
69 for (int i = 0; i < runs; ++ i) {
70 m3.assign (ublas::prod (m1, m2));
71// sink_matrix (m3);
72 }
73 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
74 }
75 catch (std::exception &e) {
76 std::cout << e.what () << std::endl;
77 }
78 }
79};
80template<class M, int N>
81struct bench_cpp_matrix_prod {
82 typedef typename M::value_type value_type;
83
84 void operator () (int runs) const {
85 try {
86 static M m1 (N * N), m2 (N * N), m3 (N * N);
87 initialize_vector (m1);
88 initialize_vector (m2);
89 boost::timer t;
90 for (int i = 0; i < runs; ++ i) {
91 for (int j = 0; j < N; ++ j) {
92 std::valarray<value_type> row (m1 [std::slice (N * j, N, 1)]);
93 for (int k = 0; k < N; ++ k) {
94 std::valarray<value_type> column (m2 [std::slice (k, N, N)]);
95 m3 [N * j + k] = (row * column).sum ();
96 }
97 }
98// sink_vector (m3);
99 }
100 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
101 }
102 catch (std::exception &e) {
103 std::cout << e.what () << std::endl;
104 }
105 }
106};
107
108// Benchmark O (n ^ 3)
109template<class T, int N>
110void bench_3<T, N>::operator () (int runs) {
111 header ("bench_3");
112
113 header ("prod (matrix, matrix)");
114
115 header ("C array");
116 bench_c_matrix_prod<T, N> () (runs);
117
118#ifdef USE_C_ARRAY
119 header ("c_matrix safe");
120 bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, safe_tag ());
121
122 header ("c_matrix fast");
123 bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, fast_tag ());
124#endif
125
126#ifdef USE_BOUNDED_ARRAY
127 header ("matrix<bounded_array> safe");
128 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, safe_tag ());
129
130 header ("matrix<bounded_array> fast");
131 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, fast_tag ());
132#endif
133
134#ifdef USE_UNBOUNDED_ARRAY
135 header ("matrix<unbounded_array> safe");
136 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
137
138 header ("matrix<unbounded_array> fast");
139 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
140#endif
141
142#ifdef USE_STD_VALARRAY
143 header ("matrix<std::valarray> safe");
144 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, safe_tag ());
145
146 header ("matrix<std::valarray> fast");
147 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, fast_tag ());
148#endif
149
150#ifdef USE_STD_VECTOR
151 header ("matrix<std::vector> safe");
152 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, safe_tag ());
153
154 header ("matrix<std::vector> fast");
155 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, fast_tag ());
156#endif
157
158#ifdef USE_STD_VALARRAY
159 header ("std::valarray");
160 bench_cpp_matrix_prod<std::valarray<T>, N> () (runs);
161#endif
162}
163
164#ifdef USE_FLOAT
165template struct bench_3<float, 3>;
166template struct bench_3<float, 10>;
167template struct bench_3<float, 30>;
168template struct bench_3<float, 100>;
169#endif
170
171#ifdef USE_DOUBLE
172template struct bench_3<double, 3>;
173template struct bench_3<double, 10>;
174template struct bench_3<double, 30>;
175template struct bench_3<double, 100>;
176#endif
177
178#ifdef USE_STD_COMPLEX
179#ifdef USE_FLOAT
180template struct bench_3<std::complex<float>, 3>;
181template struct bench_3<std::complex<float>, 10>;
182template struct bench_3<std::complex<float>, 30>;
183template struct bench_3<std::complex<float>, 100>;
184#endif
185
186#ifdef USE_DOUBLE
187template struct bench_3<std::complex<double>, 3>;
188template struct bench_3<std::complex<double>, 10>;
189template struct bench_3<std::complex<double>, 30>;
190template struct bench_3<std::complex<double>, 100>;
191#endif
192#endif