I'm getting puzzling results while doing fairly simple tasks to compare the performance of:
- Eigen::Matrix
- boost::multi_array
- boost::multi_array mapped to Eigen::Matrix using Eigen::Map
This is an abridged version of my test code; a fuller version can be found at: http://pastebin.com/faZ7TvJG.
boost::multi_array<double, 2, Eigen::aligned_allocator<double> > boost_multi_array;
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> eigen_matrix;
Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > boost_multi_array_mapped(boost_multi_array.data(), rows, cols);
double
tmp_sum = 0,
tmp_time = omp_get_wtime();
for(size_t i=0; i<iterations; i++)
{
for(size_t j=0; j<rows; j++)
{
for(size_t k=0; k<cols; k++)
{
//if(k%2==0)
//{ // commented out are the different options
//tmp_sum += boost_multi_array[j][k];
//tmp_sum += boost_multi_array_mapped(j,k);
tmp_sum += eigen_matrix(j,k);
//}
}
}
}
const double sequential_access_time = omp_get_wtime() - tmp_time;
The results are as follows:
Sequential Access:
BOOST (MAPPED) : 1.45763s
EIGEN : 1.45736s
BOOST : 2.58971s
If I use an if-statement to skip every second element, I then get the following results:
Alternating Access:
BOOST (MAPPED) : 1.67301s
EIGEN : 2.08834s
BOOST : 2.35295s
Inspecting the assembly shows that in the sequential access case, Eigen is faster because the sum becomes vectorized, while it does not when using raw boost::multi_array.
My questions then are:
- Why is boost::multi_array not vectorized, while Eigen::Matrix is?
- Why would a multi_array mapped to Eigen be faster than a "native" Eigen data structure?
For compilation I use the following:
g++ -I /usr/include/eigen3 test.cpp -Wall -O3 -DNDEBUG -DBOOST_DISABLE_ASSERTS -fopenmp -ffast-math -funroll-loops -march=native -mtune=native -o array_test
Thanks for your answers.