I have a problem in using multi-threading in Eigen
library.
This is my code:
#include <QCoreApplication>
#include <iostream>
#include "Eigen/Core"
#include <QDebug>
using namespace Eigen;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Eigen::setNbThreads(6);
qDebug() << Eigen::nbThreads( );
int n = 1000;
MatrixXd A = MatrixXd::Ones(n,n);
MatrixXd B = MatrixXd::Ones(n,n);
MatrixXd C = MatrixXd::Ones(n,n);
C.noalias() += A*B;
std::cout << C.sum() << "\n";
return a.exec();
}
Whatever I did Eigen::nbThreads( )
always returns 1 no matter what number I used in Eigen::setNbThreads(6)
!
I read here but it actually doesn't say clearly about how can we actually run Eigen
in parallel mode when OpenMP
is not present!
I also did a lot of searches but all of them are used with OpenMP
!
What just happened? Is Eigen
only supports OpenMP
for multi-threading or it has a built-in multi-threading too?
Thanks in advance!
Eigen's built-in multithreading only works with activated OpenMP. If you did not compile with OpenMP, then Eigen::setNbThreads(6);
does essentially nothing, otherwise it is essentially equivalent to calling omp_set_num_threads
(from Eigen's point of view).
You can use Eigen in an application which itself is multi-threaded (the main caveat to be careful with are calls to setRandom()
and related, as described on the page you linked to).
Also, if your own multi-threading is based on OpenMP, but you don't want Eigen to exploit multithreading, you can disable it, at compile time by defining EIGEN_DONT_PARALLELIZE
, or locally by setting Eigen::setNbThreads(1);
.