19 #include <boost/format.hpp> 20 #include <boost/lambda/lambda.hpp> 36 static std::mt19937
rng;
37 static std::uniform_real_distribution<>
uniform(-1.0, 0.0);
46 int main(
int argc,
char* argv[]) {
49 cout <<
"\nTiming matrix_block:" << endl;
53 volatile size_t m=500;
54 volatile size_t n=300;
55 volatile size_t nReps = 1000;
57 std::uniform_int_distribution<size_t> uniform_i(0,m-1);
58 std::uniform_int_distribution<size_t> uniform_j(0,n-1);
64 cout << format(
" Basic: %1%x%2%\n") % (
int)m % (
int)
n;
65 cout << format(
" Full: mat(%1%:%2%, %3%:%4%)\n") % 0 % (
int)m % 0 % (
int)
n;
66 cout << format(
" Top: mat(%1%:%2%, %3%:%4%)\n") % 0 % (
int)n % 0 % (
int)
n;
71 double basicTime, fullTime, topTime, blockTime;
73 cout <<
"Row-major matrix, row-major assignment:" << endl;
76 for(
size_t rep=0; rep<1000; ++rep)
77 for(
size_t i=0;
i<(
size_t)mat.rows(); ++
i)
78 for(
size_t j=0;
j<(
size_t)mat.cols(); ++
j)
82 for(
size_t rep=0; rep<nReps; ++rep)
83 for(
size_t i=0;
i<(
size_t)mat.rows(); ++
i)
84 for(
size_t j=0;
j<(
size_t)mat.cols(); ++
j)
88 basicTime = basicTimeNode->secs();
90 cout << format(
" Basic: %1% mus/element") % double(1000000 * basicTime /
double(mat.rows()*mat.cols()*nReps)) << endl;
93 for(
size_t rep=0; rep<nReps; ++rep)
94 for(
size_t i=0;
i<(
size_t)full.rows(); ++
i)
95 for(
size_t j=0;
j<(
size_t)full.cols(); ++
j)
99 fullTime = fullTimeNode->secs();
101 cout << format(
" Full: %1% mus/element") % double(1000000 * fullTime /
double(full.rows()*full.cols()*nReps)) << endl;
104 for(
size_t rep=0; rep<nReps; ++rep)
105 for(
size_t i=0;
i<(
size_t)top.rows(); ++
i)
106 for(
size_t j=0;
j<(
size_t)top.cols(); ++
j)
110 topTime = topTimeNode->secs();
112 cout << format(
" Top: %1% mus/element") % double(1000000 * topTime /
double(top.rows()*top.cols()*nReps)) << endl;
115 for(
size_t rep=0; rep<nReps; ++rep)
116 for(
size_t i=0;
i<(
size_t)block.rows(); ++
i)
117 for(
size_t j=0;
j<(
size_t)block.cols(); ++
j)
121 blockTime = blockTimeNode->secs();
123 cout << format(
" Block: %1% mus/element") % double(1000000 * blockTime /
double(block.rows()*block.cols()*nReps)) << endl;
129 double basicTime, fullTime, topTime, blockTime;
131 cout <<
"Row-major matrix, column-major assignment:" << endl;
134 for(
size_t rep=0; rep<1000; ++rep)
135 for(
size_t j=0;
j<(
size_t)mat.cols(); ++
j)
136 for(
size_t i=0;
i<(
size_t)mat.rows(); ++
i)
140 for(
size_t rep=0; rep<nReps; ++rep)
141 for(
size_t j=0;
j<(
size_t)mat.cols(); ++
j)
142 for(
size_t i=0;
i<(
size_t)mat.rows(); ++
i)
146 basicTime = basicTimeNode->secs();
148 cout << format(
" Basic: %1% mus/element") % double(1000000 * basicTime /
double(mat.rows()*mat.cols()*nReps)) << endl;
151 for(
size_t rep=0; rep<nReps; ++rep)
152 for(
size_t j=0;
j<(
size_t)full.cols(); ++
j)
153 for(
size_t i=0;
i<(
size_t)full.rows(); ++
i)
157 fullTime = fullTimeNode->secs();
159 cout << format(
" Full: %1% mus/element") % double(1000000 * fullTime /
double(full.rows()*full.cols()*nReps)) << endl;
162 for(
size_t rep=0; rep<nReps; ++rep)
163 for(
size_t j=0;
j<(
size_t)top.cols(); ++
j)
164 for(
size_t i=0;
i<(
size_t)top.rows(); ++
i)
168 topTime = topTimeNode->secs();
170 cout << format(
" Top: %1% mus/element") % double(1000000 * topTime /
double(top.rows()*top.cols()*nReps)) << endl;
173 for(
size_t rep=0; rep<nReps; ++rep)
174 for(
size_t j=0;
j<(
size_t)block.cols(); ++
j)
175 for(
size_t i=0;
i<(
size_t)block.rows(); ++
i)
179 blockTime = blockTimeNode->secs();
181 cout << format(
" Block: %1% mus/element") % double(1000000 * blockTime /
double(block.rows()*block.cols()*nReps)) << endl;
187 double basicTime, fullTime, topTime, blockTime;
188 typedef pair<size_t,size_t> ij_t;
189 vector<ij_t> ijs(100000);
191 cout <<
"Row-major matrix, random assignment:" << endl;
194 for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(
rng),uniform_j(
rng)));
195 for(
size_t rep=0; rep<1000; ++rep)
196 for(
const ij_t& ij: ijs) {
mat(ij.first, ij.second) =
uniform(
rng); }
199 for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(
rng),uniform_j(
rng)));
200 for(
size_t rep=0; rep<1000; ++rep)
201 for(
const ij_t& ij: ijs) {
mat(ij.first, ij.second) =
uniform(
rng); }
204 basicTime = basicTimeNode->secs();
206 cout << format(
" Basic: %1% mus/element") % double(1000000 * basicTime /
double(ijs.size()*nReps)) << endl;
209 for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(
rng),uniform_j(
rng)));
210 for(
size_t rep=0; rep<1000; ++rep)
211 for(
const ij_t& ij: ijs) { full(ij.first, ij.second) =
uniform(
rng); }
214 fullTime = fullTimeNode->secs();
216 cout << format(
" Full: %1% mus/element") % double(1000000 * fullTime /
double(ijs.size()*nReps)) << endl;
219 for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(
rng)%top.rows(),uniform_j(
rng)));
220 for(
size_t rep=0; rep<1000; ++rep)
221 for(
const ij_t& ij: ijs) { top(ij.first, ij.second) =
uniform(
rng); }
224 topTime = topTimeNode->secs();
226 cout << format(
" Top: %1% mus/element") % double(1000000 * topTime /
double(ijs.size()*nReps)) << endl;
229 for_each(ijs.begin(), ijs.end(), _1 = make_pair(uniform_i(
rng)%block.rows(),uniform_j(
rng)%block.cols()));
230 for(
size_t rep=0; rep<1000; ++rep)
231 for(
const ij_t& ij: ijs) {
block(ij.first, ij.second) =
uniform(
rng); }
234 blockTime = blockTimeNode->secs();
236 cout << format(
" Block: %1% mus/element") % double(1000000 * blockTime /
double(ijs.size()*nReps)) << endl;
#define tictoc_getNode(variable, label)
m m block(1, 0, 2, 2)<< 4
static std::uniform_real_distribution uniform(-1.0, 0.0)
int main(int argc, char *argv[])
Expression of a fixed-size or dynamic-size block.