Skip to content
Snippets Groups Projects
Commit 3df660ad authored by Joan Solà Ortega's avatar Joan Solà Ortega
Browse files

Improve matrix product benchmark

parent e832f22b
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,50 @@
//std includes
#include <ctime>
#include <iostream>
#include <iomanip>
#define INIT_ALL\
R1.setRandom(s, s);\
R2.setRandom(s, s);\
C1.setRandom(s, s);\
C2.setRandom(s, s);
#define LOOP(N,Mo,M1,M2) \
for (int i = 0; i < N; i++) \
{ \
Mo = M1 * M2; \
M1(2,2) += 0.000001; \
M2(0,0) *= 1.000001; \
/*M2 = Mo; */\
}
#define EVALUATE(N,Mo,M1,M2) \
t0 = clock(); \
LOOP(N,Mo,M1,M2) \
t1 = clock(); \
std::cout << std::setw(15) << Mo(2,2) << "\t";
#define EVALUATE_ALL \
EVALUATE(N, Ro, R1, R2)\
std::cout << "Time Ro = R * R: " << (long double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;\
EVALUATE(N, Ro, R1, C2)\
std::cout << "Time Ro = R * C: " << (long double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;\
EVALUATE(N, Ro, C1, R2)\
std::cout << "Time Ro = C * R: " << (long double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;\
EVALUATE(N, Ro, C1, C2)\
std::cout << "Time Ro = C * C: " << (long double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;\
EVALUATE(N, Co, R1, R2)\
std::cout << "Time Co = R * R: " << (long double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;\
EVALUATE(N, Co, R1, C2)\
std::cout << "Time Co = R * C: " << (long double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;\
EVALUATE(N, Co, C1, R2)\
std::cout << "Time Co = C * R: " << (long double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;\
EVALUATE(N, Co, C1, C2)\
std::cout << "Time Co = C * C: " << (long double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N \
<< "us <-- this is the Eigen default!" << std::endl;
/**
* We multiply matrices and see how long it takes.
......@@ -20,10 +64,10 @@ int main()
{
using namespace Eigen;
int N = 1000000;
int N = 100000;
const int S = 6;
Matrix<double, 16, S - 3 + 1> results;
clock_t t0;
clock_t t0, t1;
{
Matrix<double, Dynamic, Dynamic, RowMajor> R1, R2, Ro;
......@@ -31,152 +75,24 @@ int main()
for (int s = 3; s <= S; s++)
{
R1.setRandom(s, s);
R2.setRandom(s, s);
C1.setRandom(s, s);
C2.setRandom(s, s);
std::cout << "Timings for dynamic matrix product. R: row major matrix. C: column major matrix. " << s << "x"
<< s << " matrices." << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
INIT_ALL
EVALUATE_ALL
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * R2;
R2.setRandom();
}
std::cout << "Time Co = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * C2;
C2.setRandom();
}
std::cout << "Time Co = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * R2;
R2.setRandom();
}
std::cout << "Time Co = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * C2;
C2.setRandom();
}
std::cout << "Time Co = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6
<< "us <-- this is the Eigen default!" << std::endl;
}
}
N *= 10;
// N *= 1000;
{
const int s = 3;
std::cout << "Timings for static matrix product. R: row major matrix. C: column major matrix. " << s << "x" << s
<< " matrices." << std::endl;
Matrix<double, s, s, RowMajor> R1, R2, Ro;
Matrix<double, s, s, ColMajor> C1, C2, Co;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * R2;
R2.setRandom();
}
std::cout << "Time Co = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * C2;
C2.setRandom();
}
std::cout << "Time Co = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * R2;
R2.setRandom();
}
std::cout << "Time Co = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * C2;
C2.setRandom();
}
std::cout << "Time Co = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6
<< "us <-- this is the Eigen default!" << std::endl;
INIT_ALL
EVALUATE_ALL
}
{
const int s = 4;
......@@ -184,141 +100,20 @@ int main()
<< " matrices." << std::endl;
Matrix<double, s, s, RowMajor> R1, R2, Ro;
Matrix<double, s, s, ColMajor> C1, C2, Co;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * R2;
R2.setRandom();
}
std::cout << "Time Co = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * C2;
C2.setRandom();
}
std::cout << "Time Co = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * R2;
R2.setRandom();
}
std::cout << "Time Co = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * C2;
C2.setRandom();
}
std::cout << "Time Co = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6
<< "us <-- this is the Eigen default!" << std::endl;
INIT_ALL
EVALUATE_ALL
}
// N /= 100000;
{
const int s = 5;
std::cout << "Timings for static matrix product. R: row major matrix. C: column major matrix. " << s << "x" << s
<< " matrices." << std::endl;
Matrix<double, s, s, RowMajor> R1, R2, Ro;
Matrix<double, s, s, ColMajor> C1, C2, Co;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * R2;
R2.setRandom();
}
std::cout << "Time Co = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * C2;
C2.setRandom();
}
std::cout << "Time Co = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * R2;
R2.setRandom();
}
std::cout << "Time Co = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * C2;
C2.setRandom();
}
std::cout << "Time Co = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6
<< "us <-- this is the Eigen default!" << std::endl;
INIT_ALL
EVALUATE_ALL
}
{
const int s = 6;
......@@ -326,97 +121,34 @@ int main()
<< " matrices." << std::endl;
Matrix<double, s, s, RowMajor> R1, R2, Ro;
Matrix<double, s, s, ColMajor> C1, C2, Co;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = R1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * R2;
R2.setRandom();
}
std::cout << "TimeRo = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Ro = C1 * C2;
C2.setRandom();
}
std::cout << "TimeRo = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * R2;
R2.setRandom();
}
std::cout << "Time Co = R * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = R1 * C2;
C2.setRandom();
}
std::cout << "Time Co = R * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * R2;
R2.setRandom();
}
std::cout << "Time Co = C * R: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
Co = C1 * C2;
C2.setRandom();
}
std::cout << "Time Co = C * C: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6
<< "us <-- this is the Eigen default!" << std::endl;
INIT_ALL
EVALUATE_ALL
}
std::cout << "Test q and R rotations" << std::endl;
Eigen::Quaterniond q(Eigen::Vector4d::Random().normalized());
Eigen::Matrix3d R = q.matrix();
Eigen::Vector3d v((Eigen::Vector3d() << 1, 2, 3).finished());
Eigen::Vector3d w;
Eigen::Vector3d v; v << 1,2,3; double vn = v.norm();
N *= 10;
N *= 100;
t0 = clock();
for (int i = 0; i < N; i++)
{
w = R * v;
v.setRandom();
v = R * v;
}
std::cout << "Time w = R * v: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t1 = clock();
std::cout << "Time w = R * v: " << (double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;
t0 = clock();
for (int i = 0; i < N; i++)
{
w = q * v;
v.setRandom();
v = q * v;
}
std::cout << "Time w = q * v: " << ((double)clock() - t0) / CLOCKS_PER_SEC / N * 1e6 << "us" << std::endl;
t1 = clock();
std::cout << "Time w = q * v: " << (double)(t1 - t0) * 1e9 / CLOCKS_PER_SEC / N << "ns" << std::endl;
std::cout << "v norm change: " << 10*log((long double)v.norm()/(long double)vn) << " dB" << std::endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment