I want to perform a lot of computations over externally allocated data, especially matrix multiplications. It can be done via Eigen::Map
. Unfortunately I'm not an expert in vectorized computations, but as far as i can see it's possible to specify Aligned
flag for Map
.
I decided to check performance difference between matrix multiplications via Eigen::MatrixXf
and 'Eigen::Map':
void testMatProduct(
const Eigen::MatrixXf &a,
const Eigen::MatrixXf &b,
Eigen::MatrixXf &res)
{
const auto startTime = std::chrono::high_resolution_clock::now();
res.noalias() = a * b;
const auto endTime = std::chrono::high_resolution_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::microseconds>( endTime - startTime ).count();
std::cout << "Mat product elapsed " << duration / 1.0e6 << std::endl;
}
using EigenMap = Eigen::Map<Eigen::MatrixXf, Eigen::Unaligned>;
void testMapProduct(
const EigenMap &a,
const EigenMap &b,
EigenMap &res)
{
const auto startTime = std::chrono::high_resolution_clock::now();
res.noalias() = a * b;
const auto endTime = std::chrono::high_resolution_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::microseconds>( endTime - startTime ).count();
std::cout << "Map product elapsed " << duration / 1.0e6 << std::endl;
}
int main(int, char **)
{
srand(42);
const int64_t N = 7000;
const int64_t K = 6000;
const int64_t M = 100;
Eigen::MatrixXf mat1 = Eigen::MatrixXf::Random(N, K);
Eigen::MatrixXf mat2 = Eigen::MatrixXf::Random(K, M);
Eigen::MatrixXf matRes = Eigen::MatrixXf::Zero(N, M);
// Copy data from mats to vecs
Eigen::VectorXf vec1 = Eigen::Map<Eigen::MatrixXf>(mat1.data(), mat1.rows() * mat1.cols(), 1);
Eigen::VectorXf vec2 = Eigen::Map<Eigen::MatrixXf>(mat2.data(), mat2.rows() * mat2.cols(), 1);
Eigen::VectorXf vecRes = Eigen::VectorXf::Zero(N * M);
EigenMap map1 = EigenMap(vec1.data(), mat1.rows(), mat1.cols());
EigenMap map2 = EigenMap(vec2.data(), mat2.rows(), mat2.cols());
EigenMap mapRes = EigenMap(vecRes.data(), matRes.rows(), matRes.cols());
for(int i = 0; i < 10; ++i){
testMapProduct(map1, map2, mapRes);
testMatProduct(mat1, mat2, matRes);
matRes.setZero();
vecRes.setZero();
}
return 0;
}
I'm pretty sure this is not a valid benchmark but it should give me some intuition. I compile it with -march=native
and it prints following output:
Map product elapsed 0.102751
Mat product elapsed 0.10224
Map product elapsed 0.10022
Mat product elapsed 0.100726
Map product elapsed 0.09963
Mat product elapsed 0.100697
Map product elapsed 0.099673
Mat product elapsed 0.100809
Map product elapsed 0.100195
.......
So it's seems to me that there are no big difference between map product and matrix product.
My questions are:
1) What the difference between Map<MatrixXf, Unaligned>
and Map<MatrixXf, Aligned>
in terms of performance? Should i care about Map
alignment for other operations like dot products, elementwise addition, etc
2) Is my comparison correct?
PS Sorry for my poor English