Small optimizations on Matrix::collect(), additional timing capabilities, and now collect() can have dimensions specified to avoid need for lookup.
parent
67878830a6
commit
f577b27f52
|
@ -494,24 +494,26 @@ Matrix stack(size_t nrMatrices, ...)
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
Matrix collect(vector<const Matrix *> matrices)
|
||||
Matrix collect(const std::vector<const Matrix *>& matrices, size_t m, size_t n)
|
||||
{
|
||||
int dimA1 = 0;
|
||||
int dimA2 = 0;
|
||||
BOOST_FOREACH(const Matrix* M, matrices) {
|
||||
dimA1 = M->size1(); // TODO: should check if all the same !
|
||||
dimA2 += M->size2();
|
||||
}
|
||||
Matrix A(dimA1, dimA2);
|
||||
int hindex = 0;
|
||||
BOOST_FOREACH(const Matrix* M, matrices) {
|
||||
for(size_t d1 = 0; d1 < M->size1(); d1++)
|
||||
for(size_t d2 = 0; d2 < M->size2(); d2++)
|
||||
A(d1, d2+hindex) = (*M)(d1, d2);
|
||||
hindex += M->size2();
|
||||
}
|
||||
// if we have known and constant dimensions, use them
|
||||
size_t dimA1 = m;
|
||||
size_t dimA2 = n*matrices.size();
|
||||
if (!m && !n)
|
||||
BOOST_FOREACH(const Matrix* M, matrices) {
|
||||
dimA1 = M->size1(); // TODO: should check if all the same !
|
||||
dimA2 += M->size2();
|
||||
}
|
||||
Matrix A(dimA1, dimA2);
|
||||
size_t hindex = 0;
|
||||
BOOST_FOREACH(const Matrix* M, matrices) {
|
||||
for(size_t d1 = 0; d1 < M->size1(); d1++)
|
||||
for(size_t d2 = 0; d2 < M->size2(); d2++)
|
||||
A(d1, d2+hindex) = (*M)(d1, d2);
|
||||
hindex += M->size2();
|
||||
}
|
||||
|
||||
return A;
|
||||
return A;
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
|
@ -252,9 +252,14 @@ Matrix stack(size_t nrMatrices, ...);
|
|||
/**
|
||||
* create a matrix by concatenating
|
||||
* Given a set of matrices: A1, A2, A3...
|
||||
* If all matrices have the same size, specifying single matrix dimensions
|
||||
* will avoid the lookup of dimensions
|
||||
* @param matrices is a vector of matrices in the order to be collected
|
||||
* @param m is the number of rows of a single matrix
|
||||
* @param n is the number of columns of a single matrix
|
||||
* @return combined matrix [A1 A2 A3]
|
||||
*/
|
||||
Matrix collect(std::vector<const Matrix *> matrices);
|
||||
Matrix collect(const std::vector<const Matrix *>& matrices, size_t m = 0, size_t n = 0);
|
||||
Matrix collect(size_t nrMatrices, ...);
|
||||
|
||||
/**
|
||||
|
|
|
@ -72,23 +72,61 @@ TEST( matrix, row_major )
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
TEST( matrix, collect )
|
||||
TEST( matrix, collect1 )
|
||||
{
|
||||
Matrix A = Matrix_(2,2,
|
||||
-5.0 , 3.0,
|
||||
00.0, -5.0 );
|
||||
Matrix B = Matrix_(2,3,
|
||||
-0.5 , 2.1, 1.1,
|
||||
3.4 , 2.6 , 7.1);
|
||||
Matrix AB = collect(2, &A, &B);
|
||||
Matrix C(2,5);
|
||||
for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) C(i,j) = A(i,j);
|
||||
for(int i = 0; i < 2; i++) for(int j = 0; j < 3; j++) C(i,j+2) = B(i,j);
|
||||
Matrix A = Matrix_(2,2,
|
||||
-5.0 , 3.0,
|
||||
00.0, -5.0 );
|
||||
Matrix B = Matrix_(2,3,
|
||||
-0.5 , 2.1, 1.1,
|
||||
3.4 , 2.6 , 7.1);
|
||||
Matrix AB = collect(2, &A, &B);
|
||||
Matrix C(2,5);
|
||||
for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) C(i,j) = A(i,j);
|
||||
for(int i = 0; i < 2; i++) for(int j = 0; j < 3; j++) C(i,j+2) = B(i,j);
|
||||
|
||||
EQUALITY(C,AB);
|
||||
EQUALITY(C,AB);
|
||||
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
TEST( matrix, collect2 )
|
||||
{
|
||||
Matrix A = Matrix_(2,2,
|
||||
-5.0 , 3.0,
|
||||
00.0, -5.0 );
|
||||
Matrix B = Matrix_(2,3,
|
||||
-0.5 , 2.1, 1.1,
|
||||
3.4 , 2.6 , 7.1);
|
||||
vector<const Matrix*> matrices;
|
||||
matrices.push_back(&A);
|
||||
matrices.push_back(&B);
|
||||
Matrix AB = collect(matrices);
|
||||
Matrix C(2,5);
|
||||
for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) C(i,j) = A(i,j);
|
||||
for(int i = 0; i < 2; i++) for(int j = 0; j < 3; j++) C(i,j+2) = B(i,j);
|
||||
|
||||
EQUALITY(C,AB);
|
||||
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
TEST( matrix, collect3 )
|
||||
{
|
||||
Matrix A, B;
|
||||
A = eye(2,3);
|
||||
B = eye(2,3);
|
||||
vector<const Matrix*> matrices;
|
||||
matrices.push_back(&A);
|
||||
matrices.push_back(&B);
|
||||
Matrix AB = collect(matrices, 2, 3);
|
||||
Matrix exp = Matrix_(2, 6,
|
||||
1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
|
||||
|
||||
EQUALITY(exp,AB);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
TEST( matrix, stack )
|
||||
{
|
||||
|
|
|
@ -5,20 +5,24 @@
|
|||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/progress.hpp>
|
||||
#include <boost/timer.hpp>
|
||||
#include "Matrix.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace gtsam;
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
cout << "Starting Matrix::collect() Timing" << endl;
|
||||
|
||||
/*
|
||||
* Results:
|
||||
* Alex's Machine:
|
||||
* - no pass: 0.1818 sec
|
||||
* - pass : 0.1802 sec
|
||||
*/
|
||||
double timeCollect(size_t p, size_t m, size_t n, bool passDims, size_t reps) {
|
||||
// create a large number of matrices
|
||||
size_t n, m, p;
|
||||
p = 100000; // number of matrices
|
||||
n = 10; // rows per matrix
|
||||
m = 12; // columns per matrix
|
||||
// p = number of matrices
|
||||
// m = rows per matrix
|
||||
// n = columns per matrix
|
||||
// reps = number of repetitions
|
||||
|
||||
// fill the matrices with identities
|
||||
vector<const Matrix *> matrices;
|
||||
|
@ -30,15 +34,37 @@ int main(int argc, char ** argv) {
|
|||
|
||||
// start timing
|
||||
Matrix result;
|
||||
double elapsed;
|
||||
{
|
||||
boost::progress_timer t;
|
||||
result = collect(matrices);
|
||||
}
|
||||
boost::timer t;
|
||||
|
||||
if (passDims)
|
||||
for (int i=0; i<reps; ++i)
|
||||
result = collect(matrices, m, n);
|
||||
else
|
||||
for (int i=0; i<reps; ++i)
|
||||
result = collect(matrices);
|
||||
elapsed = t.elapsed();
|
||||
}
|
||||
// delete the matrices
|
||||
for (int i=0; i<p;++i) {
|
||||
delete matrices[i];
|
||||
}
|
||||
|
||||
return elapsed/reps;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
// Time collect()
|
||||
cout << "Starting Matrix::collect() Timing" << endl;
|
||||
size_t p = 100000; size_t m = 10; size_t n = 12; size_t reps = 50;
|
||||
double collect_time1 = timeCollect(p, m, n, false, reps);
|
||||
double collect_time2 = timeCollect(p, m, n, true, reps);
|
||||
cout << "Elapsed time for collect (no pass) [" << p << " (" << m << ", " << n << ") matrices] : " << collect_time1 << endl;
|
||||
cout << "Elapsed time for collect (pass) [" << p << " (" << m << ", " << n << ") matrices] : " << collect_time2 << endl;
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue