Skip to content

Instantly share code, notes, and snippets.

@michaelchughes
Last active December 12, 2015 08:59
Show Gist options
  • Save michaelchughes/4747789 to your computer and use it in GitHub Desktop.
Save michaelchughes/4747789 to your computer and use it in GitHub Desktop.
Multiplies two random matrices using Eigen, and displays the elapsed time for the multiplication only (not the creation of random matrices). Recommended compiliation (using g++): >> g++-mp-4.7 MatProdEigen.cpp -o MatProdEigen -I/path/to/eigen/ -O3 -DNDEBUG To multiply a 10x3 and 3x55 matrix: >> ./MatProdEigen 10 3 55
#include "Eigen/Dense"
#include <iostream>
#include <time.h>
using namespace Eigen;
using namespace std;
double diffclock(clock_t clock1,clock_t clock2)
{
double diffticks=clock1-clock2;
double diffms=(diffticks)/CLOCKS_PER_SEC;
return diffms;
}
int main( int argc, char *argv[] ) {
if ( argc != 4 ) {
printf("ERROR: Needs 3 arguments -- N, M, D\n");
return -1;
}
int N = atoi( argv[1] );
int D = atoi( argv[2] );
int M = atoi( argv[3] );
MatrixXd X = MatrixXd::Random( N, D);
MatrixXd Y = MatrixXd::Random( D, M);
clock_t starttime = clock();
MatrixXd R = X * Y;
clock_t endtime = clock();
cout << "Time elapsed: " << diffclock( endtime, starttime) \
<< " sec." << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment