Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OpenMP and Eigen causes infinite loop/deadlock

Tags:

c++

openmp

eigen

I'm solving a much larger problem and have run into a bug when I try to use OpenMP to parallelize some loops. I've reproduced the problem with some simpler code below that mimics my own code.

The problem is that when I run the program, it will randomly go into some sort of infinite loop/deadlock (CPU is 100%, but doesn't do anything). From what I can tell from my testing, one of the threads attempts to compute the matrix-matrix product but never finishes for some reason.

I know that if you enable OpenMP, Eigen will parallelize matrix-matrix products using OpenMP . I'm also adding another parallel loop outside of this. However, this bug still occurs if I disable Eigen's parallelization by defining EIGEN_DONT_PARALLELIZE.

I'm using gcc version 4.6.0 20101127 on MacOS 10.6.8 with Eigen 3.0.4.

I can't figure out what could be going wrong...

#include <iostream>
#include <Eigen/Core>

using namespace std;
using namespace Eigen;

MatrixXd Test(MatrixXd const& F, MatrixXd const& G)
{
  MatrixXd H(F.rows(), G.cols());
  H.noalias() = F*G;

  return H;
}

int main()
{
  MatrixXd F = MatrixXd::Random(2,2);
  MatrixXd G = MatrixXd::Random(2,2);

  #pragma omp parallel for
  for (unsigned int i = 0; i < 10000; ++i)
    MatrixXd H = Test(F,G);

  cout << "Done!" << endl;
}
like image 774
user1144371 Avatar asked Jan 11 '12 23:01

user1144371


1 Answers

After some debugging, I think the problem is located in Eigen. In the file src/Core/products/GeneralBlockPanelKernel.h there is a function called manage_caching_sizes that declares two static variables:

static std::ptrdiff_t m_l1CacheSize = 0;
static std::ptrdiff_t m_l2CacheSize = 0;

Changing this to:

static std::ptrdiff_t m_l1CacheSize = 0;
static std::ptrdiff_t m_l2CacheSize = 0;
#pragma omp threadprivate(m_l1CacheSize, m_l2CacheSize)

fixed my problem.

like image 175
user1144371 Avatar answered Oct 11 '22 14:10

user1144371