Parallelizer.h 4.2 KB
Newer Older
LM's avatar
LM committed
1 2 3 4 5
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
//
Don Gagne's avatar
Don Gagne committed
6 7 8
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
LM's avatar
LM committed
9 10 11 12

#ifndef EIGEN_PARALLELIZER_H
#define EIGEN_PARALLELIZER_H

Don Gagne's avatar
Don Gagne committed
13 14
namespace Eigen { 

LM's avatar
LM committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
namespace internal {

/** \internal */
inline void manage_multi_threading(Action action, int* v)
{
  static EIGEN_UNUSED int m_maxThreads = -1;

  if(action==SetAction)
  {
    eigen_internal_assert(v!=0);
    m_maxThreads = *v;
  }
  else if(action==GetAction)
  {
    eigen_internal_assert(v!=0);
    #ifdef EIGEN_HAS_OPENMP
    if(m_maxThreads>0)
      *v = m_maxThreads;
    else
      *v = omp_get_max_threads();
    #else
    *v = 1;
    #endif
  }
  else
  {
    eigen_internal_assert(false);
  }
}

Don Gagne's avatar
Don Gagne committed
45 46 47 48 49 50 51 52 53 54 55
}

/** Must be call first when calling Eigen from multiple threads */
inline void initParallel()
{
  int nbt;
  internal::manage_multi_threading(GetAction, &nbt);
  std::ptrdiff_t l1, l2;
  internal::manage_caching_sizes(GetAction, &l1, &l2);
}

LM's avatar
LM committed
56 57 58 59 60
/** \returns the max number of threads reserved for Eigen
  * \sa setNbThreads */
inline int nbThreads()
{
  int ret;
Don Gagne's avatar
Don Gagne committed
61
  internal::manage_multi_threading(GetAction, &ret);
LM's avatar
LM committed
62 63 64 65 66 67 68
  return ret;
}

/** Sets the max number of threads reserved for Eigen
  * \sa nbThreads */
inline void setNbThreads(int v)
{
Don Gagne's avatar
Don Gagne committed
69
  internal::manage_multi_threading(SetAction, &v);
LM's avatar
LM committed
70 71
}

Don Gagne's avatar
Don Gagne committed
72 73
namespace internal {

LM's avatar
LM committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
template<typename Index> struct GemmParallelInfo
{
  GemmParallelInfo() : sync(-1), users(0), rhs_start(0), rhs_length(0) {}

  int volatile sync;
  int volatile users;

  Index rhs_start;
  Index rhs_length;
};

template<bool Condition, typename Functor, typename Index>
void parallelize_gemm(const Functor& func, Index rows, Index cols, bool transpose)
{
Don Gagne's avatar
Don Gagne committed
88 89 90
  // TODO when EIGEN_USE_BLAS is defined,
  // we should still enable OMP for other scalar types
#if !(defined (EIGEN_HAS_OPENMP)) || defined (EIGEN_USE_BLAS)
LM's avatar
LM committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  // FIXME the transpose variable is only needed to properly split
  // the matrix product when multithreading is enabled. This is a temporary
  // fix to support row-major destination matrices. This whole
  // parallelizer mechanism has to be redisigned anyway.
  EIGEN_UNUSED_VARIABLE(transpose);
  func(0,rows, 0,cols);
#else

  // Dynamically check whether we should enable or disable OpenMP.
  // The conditions are:
  // - the max number of threads we can create is greater than 1
  // - we are not already in a parallel code
  // - the sizes are large enough

  // 1- are we already in a parallel session?
  // FIXME omp_get_num_threads()>1 only works for openmp, what if the user does not use openmp?
  if((!Condition) || (omp_get_num_threads()>1))
    return func(0,rows, 0,cols);

  Index size = transpose ? cols : rows;

  // 2- compute the maximal number of threads from the size of the product:
  // FIXME this has to be fine tuned
  Index max_threads = std::max<Index>(1,size / 32);

  // 3 - compute the number of threads we are going to use
  Index threads = std::min<Index>(nbThreads(), max_threads);

  if(threads==1)
    return func(0,rows, 0,cols);

Don Gagne's avatar
Don Gagne committed
122
  Eigen::initParallel();
LM's avatar
LM committed
123 124 125 126 127 128 129
  func.initParallelSession();

  if(transpose)
    std::swap(rows,cols);

  GemmParallelInfo<Index>* info = new GemmParallelInfo<Index>[threads];

130
  #pragma omp parallel num_threads(threads)
LM's avatar
LM committed
131
  {
132 133 134 135 136 137 138
    Index i = omp_get_thread_num();
    // Note that the actual number of threads might be lower than the number of request ones.
    Index actual_threads = omp_get_num_threads();
    
    Index blockCols = (cols / actual_threads) & ~Index(0x3);
    Index blockRows = (rows / actual_threads) & ~Index(0x7);
    
LM's avatar
LM committed
139
    Index r0 = i*blockRows;
140
    Index actualBlockRows = (i+1==actual_threads) ? rows-r0 : blockRows;
LM's avatar
LM committed
141 142

    Index c0 = i*blockCols;
143
    Index actualBlockCols = (i+1==actual_threads) ? cols-c0 : blockCols;
LM's avatar
LM committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

    info[i].rhs_start = c0;
    info[i].rhs_length = actualBlockCols;

    if(transpose)
      func(0, cols, r0, actualBlockRows, info);
    else
      func(r0, actualBlockRows, 0,cols, info);
  }

  delete[] info;
#endif
}

} // end namespace internal

Don Gagne's avatar
Don Gagne committed
160 161
} // end namespace Eigen

LM's avatar
LM committed
162
#endif // EIGEN_PARALLELIZER_H