Repository navigation
Open
Description
Benchmark results:
STATS FOR OPENBLAS
---------------------------------------------------------------
NUMBER OF THREADS: 1
NUMBER OF PROCESSORS: 6
CONFIG: OpenBLAS 0.3.13 NO_LAPACKE DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=64
CORE NAME: Haswell
PARALLEL: 1
2021-07-10T17:29:27+00:00
Running ./eigen_performance_test
Run on (6 X 2900 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x6)
L1 Instruction 32 KiB (x6)
L2 Unified 256 KiB (x6)
L3 Unified 12288 KiB (x6)
Load Average: 1.39, 0.97, 0.65
-------------------------------------------------------------------------------
Benchmark Time CPU Iterations
-------------------------------------------------------------------------------
eigen_matrix_vector_mult_test/2 52.0 ns 51.9 ns 10896682
eigen_matrix_vector_mult_test/4 45.3 ns 45.1 ns 15167031
eigen_matrix_vector_mult_test/8 58.9 ns 58.7 ns 11416469
eigen_matrix_vector_mult_test/16 97.1 ns 97.0 ns 6808424
eigen_matrix_vector_mult_test/32 225 ns 224 ns 3104947
eigen_matrix_vector_mult_test/64 708 ns 707 ns 982545
eigen_matrix_vector_mult_test/128 3247 ns 3241 ns 215232
eigen_matrix_vector_mult_test/256 17878 ns 17854 ns 40561
eigen_matrix_vector_mult_test/512 79920 ns 79654 ns 8723
eigen_matrix_vector_mult_test/1024 400534 ns 399784 ns 1756
eigen_matrix_matrix_mult_test/2 10.0 ns 9.98 ns 68861889
eigen_matrix_matrix_mult_test/4 7.77 ns 7.76 ns 88185892
eigen_matrix_matrix_mult_test/8 33.3 ns 33.3 ns 20416419
eigen_matrix_matrix_mult_test/16 221 ns 221 ns 3159655
eigen_matrix_matrix_mult_test/32 484 ns 484 ns 1371857
eigen_matrix_matrix_mult_test/64 2112 ns 2112 ns 318067
eigen_matrix_matrix_mult_test/128 14355 ns 14352 ns 48288
eigen_matrix_matrix_mult_test/256 61533 ns 61529 ns 11485
eigen_matrix_matrix_mult_test/512 276762 ns 276711 ns 2525
eigen_matrix_matrix_mult_test/1024 1389020 ns 1388564 ns 523
xtensor_matrix_matrix_mult_test/2 403 ns 403 ns 1692067
xtensor_matrix_matrix_mult_test/4 402 ns 402 ns 1746090
xtensor_matrix_matrix_mult_test/8 418 ns 417 ns 1674838
xtensor_matrix_matrix_mult_test/16 436 ns 436 ns 1562858
xtensor_matrix_matrix_mult_test/32 562 ns 561 ns 1202666
xtensor_matrix_matrix_mult_test/64 1056 ns 1056 ns 667352
xtensor_matrix_matrix_mult_test/128 3066 ns 3066 ns 222012
xtensor_matrix_matrix_mult_test/256 14641 ns 14595 ns 47534
xtensor_matrix_matrix_mult_test/512 56908 ns 56907 ns 11483
xtensor_matrix_matrix_mult_test/1024 352145 ns 351940 ns 2301
Benchmark code:
#include <benchmark/benchmark.h>
#include <Eigen/Dense>
#include <xtensor-blas/xlinalg.hpp>
#include <xtensor/xnoalias.hpp>
#include <xtensor/xtensor.hpp>
void blas_stats() {
std::cout << "\nSTATS FOR "
"OPENBLAS\n-----------------------------------------------------"
"----------\n\n";
openblas_set_num_threads(1);
std::cout << "NUMBER OF THREADS: " << openblas_get_num_threads()
<< std::endl;
// Get the number of physical processors (cores)
std::cout << "NUMBER OF PROCESSORS: " << openblas_get_num_procs()
<< std::endl;
// Get the build configure on runtime.
std::cout << "CONFIG: " << openblas_get_config()
<< std::endl;
/*Get the CPU corename on runtime.*/
std::cout << "CORE NAME: " << openblas_get_corename()
<< std::endl;
// Get the parallelization type which is used by OpenBLAS
std::cout << "PARALLEL: " << openblas_get_parallel()
<< std::endl;
std::cout << "\n\n";
}
static void eigen_matrix_vector_mult_test(benchmark::State &state) {
int size = state.range(0);
Eigen::MatrixXd m = Eigen::MatrixXd::Random(size, size);
Eigen::VectorXd v = Eigen::VectorXd::Random(size);
Eigen::MatrixXd r = Eigen::MatrixXd::Random(size, 1);
for (auto _ : state) {
r.noalias() = m * v;
}
}
BENCHMARK(eigen_matrix_vector_mult_test)->RangeMultiplier(2)->Range(2, 8 << 7);
static void eigen_matrix_matrix_mult_test(benchmark::State &state) {
int size = state.range(0);
Eigen::MatrixXd m = Eigen::MatrixXd::Random(size, size);
Eigen::MatrixXd v = Eigen::MatrixXd::Random(size, 1);
Eigen::MatrixXd r = Eigen::MatrixXd::Random(size, 1);
for (auto _ : state) {
r.noalias() = m * v;
}
}
BENCHMARK(eigen_matrix_matrix_mult_test)->RangeMultiplier(2)->Range(2, 8 << 7);
static void xtensor_matrix_matrix_mult_test(benchmark::State &state) {
uint64_t size = state.range(0);
xt::xarray<double>::shape_type shape_m = {size, size};
xt::xarray<double>::shape_type shape_v = {size};
xt::xarray<double> m(shape_m);
xt::xarray<double> v(shape_v);
xt::xarray<double> r(shape_v);
for (auto _ : state) {
xt::noalias(r) = xt::linalg::dot(m, v);
}
}
BENCHMARK(xtensor_matrix_matrix_mult_test)
->RangeMultiplier(2)
->Range(2, 8 << 7);
int main(int argc, char **argv) {
blas_stats();
benchmark::Initialize(&argc, argv);
if (benchmark::ReportUnrecognizedArguments(argc, argv))
return 1;
benchmark::RunSpecifiedBenchmarks();
}
Any thoughts why small arrays for xtensor incur so much overhead? what should I do if most of my arrays are small (but not fixed size).
Metadata
Metadata
Assignees
Labels
No labels
Activity
wolfv commentedon Jul 11, 2021
I think there are two differences with eigen:
dotoperation, there is some overhead to check if you're broadcastingIt could be interesting to make a 2D dot function that works faster on small matrices / matrix-vector combination. We could even use Eigen as a "backend" for that :)
ansar-sa commentedon Jul 11, 2021
This is an interesting idea. First, two thoughts:
add_definitions(-DHAVE_CBLAS=1 -DWITH_OPENBLAS=1 -DXTENSOR_ENABLE_XSIMD=1)set(LINK_LIBRARIES ${CONAN_LIBS} ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES} xtensor xtensor::optimize xtensor::use_xsimd)set(RELEASE_FLAGS "-Ofast -DNDEBUG -march=native ${WARNING_FLAGS}")MKLor do you think that will be just as slow for small matrices?In case both options are a no go, yes I would love to see
xtensorhandle small matrix / matrix, matrix / vec dot products fast either via handrolled simd code or via eigen backend.wolfv commentedon Jul 11, 2021
As I mentioned before, the dot product is more general than Eigen's matrix / matrix-vector multiplication as it performs broadcasting. Without broadcasting checks you'd probably already be 2x faster for small matrices.
You could try using
xtensoras a class. It has a compile time dimension (just as MatrixXd is always 2-dimensionla) you can usextensor<double, 2>.wolfv commentedon Jul 11, 2021
MKL won't help much I am afraid :)
wolfv commentedon Jul 11, 2021
calling into BLAS functions has some intrinsic overhead (since even a function call vs. inlined cost has some cost attached to it).
ansar-sa commentedon Jul 11, 2021
Using xtensor class does not help either. As you say, the call into BLAS and broadcast checks is probably dominating the time with small matrices. Do you have some example code on how I can use xsimd to multiply a small matrix with a vector please?
ansar-sa commentedon Jul 12, 2021
Actually the problem is in other kinds of operations as well. Even simple array ops such as element wise array sums for xtensor based code is very slow compared to eigen when it comes to small arrays. Can we please look what bounds / broadcast checks are the culprit. Benchmark results:
wolfv commentedon Jul 12, 2021
comparing
MatrixXdandxt::xarray<double>is never a fair comparison. Quite different containers (since xarray is dynamically ndimensional, MatrixXd is statically 2d).ansar-sa commentedon Jul 12, 2021
I'll update benchmark results using
xtensor<double, 2>ansar-sa commentedon Jul 12, 2021
wolfv commentedon Jul 12, 2021
you can shave off some more ns by doing
xt::noalias(r) = a + b;There might be some similar trick for eigen.