COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
ptest-eigen-vs-matrixt.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2011 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Some benchmarkings for vector operations"
9 
10 #include <boost/test/unit_test.hpp>
11 
13 #include <Eigen/Dense>
14 
15 #include "common/CF.hpp"
17 
18 using namespace std;
19 using namespace Eigen;
20 
21 using namespace cf3;
22 using namespace cf3::common;
23 
24 using namespace Tools::Testing;
25 
27 
28 struct nat
29 {
30  double * cf_restrict data;
31 };
32 
33 #define MSIZE 1000000
34 #define LSIZE 4
35 
36 BOOST_FIXTURE_TEST_SUITE( VectorBenchmarkSuite, TimedTestFixture )
37 
38 
40 BOOST_AUTO_TEST_CASE( dgemv_eigen_dynamic )
41 {
42  std::vector< MatrixXd > ma;
43  ma.resize( MSIZE );
44  for ( int i = 0; i < MSIZE; ++i )
45  ma[i] = MatrixXd::Constant( LSIZE, LSIZE, 2.0);
46 
47  std::vector< VectorXd > vb;
48  vb.resize( MSIZE );
49  for ( int i = 0; i < MSIZE; ++i )
50  vb[i] = VectorXd::Constant( LSIZE, 5.0);
51 
52  std::vector< VectorXd > vc;
53  vc.resize( MSIZE );
54  for ( int i = 0; i < MSIZE; ++i )
55  vc[i] = VectorXd::Constant( LSIZE, 0.0);
56 
57  restart_timer();
58 
59  for ( int i = 0; i < MSIZE; ++i )
60  vc[i].noalias() = ma[i] * vb[i];
61 }
62 
64 
65 BOOST_AUTO_TEST_CASE( dgemv_eigen_fixed )
66 {
67  typedef Matrix< double, LSIZE, LSIZE > MatrixSd;
68  typedef Matrix< double, LSIZE, 1 > VectorSd;
69 
70  std::vector< MatrixSd > ma;
71  ma.resize( MSIZE );
72  for ( int i = 0; i < MSIZE; ++i )
73  ma[i] = MatrixSd::Constant( LSIZE, LSIZE, 2.0);
74 
75  std::vector< VectorSd > vb;
76  vb.resize( MSIZE );
77  for ( int i = 0; i < MSIZE; ++i )
78  vb[i] = VectorSd::Constant( LSIZE, 5.0 );
79 
80  std::vector< VectorSd > vc;
81  vc.resize( MSIZE );
82  for ( int i = 0; i < MSIZE; ++i )
83  vc[i] = VectorSd::Constant( LSIZE, 0.0 );
84 
85  restart_timer();
86 
87  for ( int i = 0; i < MSIZE; ++i )
88  vc[i].noalias() = ma[i] * vb[i];
89 }
90 
92 
93 // BOOST_AUTO_TEST_CASE( dgemv_matrixt )
94 // {
95 // std::vector< RealMatrix > ma;
96 // ma.resize( MSIZE );
97 // for ( int i = 0; i < MSIZE; ++i )
98 // {
99 // ma[i].resize( LSIZE, LSIZE );
100 // ma[i] = 2.0;
101 // }
102 //
103 // std::vector< RealVector > vb;
104 // vb.resize( MSIZE );
105 // for ( int i = 0; i < MSIZE; ++i )
106 // {
107 // vb[i].resize( LSIZE );
108 // vb[i] = 5.0;
109 // }
110 //
111 // std::vector< RealVector > vc;
112 // vc.resize( MSIZE );
113 // for ( int i = 0; i < MSIZE; ++i )
114 // {
115 // vc[i].resize( LSIZE );
116 // vc[i] = 0.0;
117 // }
118 //
119 // restart_timer();
120 //
121 // for ( int i = 0; i < MSIZE; ++i )
122 // vc[i] = ma[i] * vb[i];
123 // }
124 
126 
127 BOOST_AUTO_TEST_CASE( dgemv_native )
128 {
129  std::vector< nat > ma;
130  ma.resize( MSIZE );
131  for ( int i = 0; i < MSIZE; ++i )
132  {
133  ma[i].data = new double [ LSIZE * LSIZE ];
134  for ( int j = 0; j < LSIZE*LSIZE; ++j ) ma[i].data[j] = 2.0;
135  }
136 
137  std::vector< nat > vb;
138  vb.resize( MSIZE );
139  for ( int i = 0; i < MSIZE; ++i )
140  {
141  vb[i].data = new double [ LSIZE ];
142  for ( int j = 0; j < LSIZE; ++j ) vb[i].data[j] = 5.0;
143  }
144 
145  std::vector< nat > vc;
146  vc.resize( MSIZE );
147  for ( int i = 0; i < MSIZE; ++i )
148  {
149  vc[i].data = new double [ LSIZE ];
150  for ( int j = 0; j < LSIZE; ++j ) vc[i].data[j] = 0.0;
151  }
152 
153  restart_timer();
154 
155  for ( int e = 0; e < MSIZE; ++e )
156  for ( int i = 0; i < LSIZE; ++i )
157  {
158  const unsigned n = LSIZE;
159  for (Uint j = 0, k = i*n; j < n; ++j, ++k)
160  vc[e].data[i] += ma[e].data[k] * vb[e].data[j];
161  }
162 }
163 
165 
166 BOOST_AUTO_TEST_CASE( dgemm_eigen_dynamic )
167 {
168  std::vector< MatrixXd > ma;
169  ma.resize( MSIZE );
170  for ( int i = 0; i < MSIZE; ++i )
171  ma[i] = MatrixXd::Constant( LSIZE, LSIZE, 2.0);
172 
173  std::vector< MatrixXd > mb;
174  mb.resize( MSIZE );
175  for ( int i = 0; i < MSIZE; ++i )
176  mb[i] = MatrixXd::Constant( LSIZE, LSIZE, 7.0);
177 
178  std::vector< MatrixXd > mc;
179  mc.resize( MSIZE );
180  for ( int i = 0; i < MSIZE; ++i )
181  mc[i] = MatrixXd::Constant( LSIZE, LSIZE, 0.0);
182 
183  restart_timer();
184 
185  for ( int i = 0; i < MSIZE; ++i )
186  mc[i].noalias() = ma[i] * mb[i];
187 }
188 
190 
191 BOOST_AUTO_TEST_CASE( dgemm_eigen_fixed )
192 {
193  typedef Matrix< double, LSIZE, LSIZE > MatrixSd;
194 
195  std::vector< MatrixSd > ma;
196  ma.resize( MSIZE );
197  for ( int i = 0; i < MSIZE; ++i )
198  ma[i] = MatrixSd::Constant( LSIZE, LSIZE, 2.0);
199 
200  std::vector< MatrixSd > mb;
201  mb.resize( MSIZE );
202  for ( int i = 0; i < MSIZE; ++i )
203  mb[i] = MatrixSd::Constant( LSIZE, LSIZE, 7.0);
204 
205  std::vector< MatrixSd > mc;
206  mc.resize( MSIZE );
207  for ( int i = 0; i < MSIZE; ++i )
208  mc[i] = MatrixSd::Constant( LSIZE, LSIZE, 0.0);
209 
210  restart_timer();
211 
212  for ( int i = 0; i < MSIZE; ++i )
213  mc[i].noalias() = ma[i] * mb[i];
214 }
215 
217 
218 // BOOST_AUTO_TEST_CASE( dgemm_matrixt )
219 // {
220 // std::vector< RealMatrix > ma;
221 // ma.resize( MSIZE );
222 // for ( int i = 0; i < MSIZE; ++i )
223 // {
224 // ma[i].resize( LSIZE, LSIZE );
225 // ma[i] = 2.0;
226 // }
227 //
228 // std::vector< RealMatrix > mb;
229 // mb.resize( MSIZE );
230 // for ( int i = 0; i < MSIZE; ++i )
231 // {
232 // mb[i].resize( LSIZE, LSIZE );
233 // mb[i] = 7.0;
234 // }
235 //
236 // std::vector< RealMatrix > mc;
237 // mc.resize( MSIZE );
238 // for ( int i = 0; i < MSIZE; ++i )
239 // {
240 // mc[i].resize( LSIZE, LSIZE );
241 // mc[i] = 0.0;
242 // }
243 //
244 // restart_timer();
245 //
246 // for ( int i = 0; i < MSIZE; ++i )
247 // mc[i] = ma[i] * mb[i];
248 // }
249 
251 
252 BOOST_AUTO_TEST_CASE( dgemm_native )
253 {
254  std::vector< nat > ma;
255  ma.resize( MSIZE );
256  for ( int i = 0; i < MSIZE; ++i )
257  {
258  ma[i].data = new double [ LSIZE * LSIZE ];
259  for ( int j = 0; j < LSIZE*LSIZE; ++j ) ma[i].data[j] = 2.0;
260  }
261 
262  std::vector< nat > mb;
263  mb.resize( MSIZE );
264  for ( int i = 0; i < MSIZE; ++i )
265  {
266  mb[i].data = new double [ LSIZE * LSIZE ];
267  for ( int j = 0; j < LSIZE*LSIZE; ++j ) mb[i].data[j] = 7.0;
268  }
269 
270  std::vector< nat > mc;
271  mc.resize( MSIZE );
272  for ( int i = 0; i < MSIZE; ++i )
273  {
274  mc[i].data = new double [ LSIZE * LSIZE ];
275  for ( int j = 0; j < LSIZE*LSIZE; ++j ) mc[i].data[j] = 0.0;
276  }
277 
278  restart_timer();
279 
280  for ( int e = 0; e < MSIZE; ++e )
281  {
282  const size_t nc = LSIZE;
283  const size_t m = LSIZE;
284  const size_t n = LSIZE;
285 
286  const nat& A = ma[e];
287  const nat& B = mb[e];
288  nat& C = mc[e];
289 
290  for (size_t i = 0; i < m; ++i)
291  {
292  const size_t jstart = i*n;
293  const size_t jmax = jstart + n;
294  const size_t kstart = i*nc;
295  size_t count = 0;
296  size_t k = 0;
297  while(count < nc)
298  {
299  double sum = 0.;
300  for(size_t j = jstart; j < jmax; ++j)
301  {
302  sum += A.data[j] * B.data[k];
303  k += nc;
304  }
305  C.data[kstart + count] = sum;
306  k = ++count;
307  }
308  }
309  }
310 }
311 
313 
314 BOOST_AUTO_TEST_SUITE_END()
#define LSIZE
#define MSIZE
STL namespace.
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
double *cf_restrict data
BOOST_AUTO_TEST_CASE(dgemv_eigen_dynamic)
Top-level namespace for coolfluid.
Definition: Action.cpp:18
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
coolfluid3 header, included almost everywhere
Most basic kernel library.
Definition: Action.cpp:19
Uint count(const RangeT &range)
Count the elements in a range.
Send comments to:
COOLFluiD Web Admin