This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Is matrix multiplication faster without OpenMP?

Tags: None
(comma "," separated)
kde-blabla
Registered Member
Posts
4
Karma
0
I want to use OpenMP together with Eigen. I followed these steps: http://eigen.tuxfamily.org/dox/TopicMultiThreading.html. However, it seems that the built-in parallelization in Eigen is much faster than using OpenMP (0.38sec vs 7.96sec).

Using Eigen:
Code: Select all
#include <iostream>
#include "Eigen/Dense"
#include "Eigen/Core"

int main(int argc, char** argv)
{   
    double beginTime = omp_get_wtime();
    int n = 500;
    int num_threads = Eigen::nbThreads();
    std::cout<<"num_threads: "<<num_threads<<std::endl;
               
    for(int i = 0; i < 5000; i++)
    {
      Eigen::Matrix<double,10,Eigen::Dynamic,Eigen::RowMajor> l(10,n);
      l = Eigen::MatrixXd::Random(10,n);
      Eigen::Matrix<double,10,10> M = l * l.transpose();
   }
   
   double endTime = omp_get_wtime();
   double elapsedTime = endTime - beginTime;
   std::cout<<elapsedTime<<" sec."<<std::endl;

   return 0;
}


Using Eigen & OpenMP:
Code: Select all
#include <iostream>
#include "Eigen/Dense"
#include "Eigen/Core"
#define EIGEN_DONT_PARALLELIZE

int main(int argc, char** argv)
{   
    Eigen::initParallel();
   
    double beginTime = omp_get_wtime();
    int n = 500;
    int num_threads = Eigen::nbThreads();
    std::cout<<"num_threads: "<<num_threads<<std::endl;
   
    #pragma omp parallel for           
    for(int i = 0; i < 5000; i++)
    {
      Eigen::Matrix<double,10,Eigen::Dynamic,Eigen::RowMajor> l(10,n);
      l = Eigen::MatrixXd::Random(10,n);
      Eigen::Matrix<double,10,10> M = l * l.transpose();
   }
   
   double endTime = omp_get_wtime();
   double elapsedTime = endTime - beginTime;
   std::cout<<elapsedTime<<" sec."<<std::endl;

   return 0;
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Since your loop is already multi-threaded you should disable Eigen's multithreading with setNbThreads(1); Also, you should disable hyperthreading or run your executable with OMP_NUM_THREADS=<true number of cores>
kde-blabla
Registered Member
Posts
4
Karma
0
ggael wrote:Since your loop is already multi-threaded you should disable Eigen's multithreading with setNbThreads(1); Also, you should disable hyperthreading or run your executable with OMP_NUM_THREADS=<true number of cores>


Yes, I did so. I just realized that Eigen cannot employ the cores for the matrix multiplication anymore, when they are already used for the distribution of the for loop ... :-\


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]