Parallelizer.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_PARALLELIZER_H
11 #define EIGEN_PARALLELIZER_H
12 
13 #include "../InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
20 inline void manage_multi_threading(Action action, int* v)
21 {
22  static int m_maxThreads = -1;
23  EIGEN_UNUSED_VARIABLE(m_maxThreads)
24 
25  if(action==SetAction)
26  {
28  m_maxThreads = *v;
29  }
30  else if(action==GetAction)
31  {
33  #ifdef EIGEN_HAS_OPENMP
34  if(m_maxThreads>0)
35  *v = m_maxThreads;
36  else
37  *v = omp_get_max_threads();
38  #else
39  *v = 1;
40  #endif
41  }
42  else
43  {
44  eigen_internal_assert(false);
45  }
46 }
47 
48 }
49 
51 inline void initParallel()
52 {
53  int nbt;
55  std::ptrdiff_t l1, l2, l3;
57 }
58 
61 inline int nbThreads()
62 {
63  int ret;
65  return ret;
66 }
67 
70 inline void setNbThreads(int v)
71 {
73 }
74 
75 namespace internal {
76 
77 template<typename Index> struct GemmParallelInfo
78 {
79 
80 #ifdef EIGEN_HAS_OPENMP
81  GemmParallelInfo() : sync(-1), users(0), lhs_start(0), lhs_length(0) {}
82  std::atomic<Index> sync;
83  std::atomic<int> users;
84 #else
85  GemmParallelInfo() : lhs_start(0), lhs_length(0) {}
86 #endif
87 
88  Index lhs_start;
89  Index lhs_length;
90 };
91 
92 template<bool Condition, typename Functor, typename Index>
93 void parallelize_gemm(const Functor& func, Index rows, Index cols, Index depth, bool transpose)
94 {
95  // TODO when EIGEN_USE_BLAS is defined,
96  // we should still enable OMP for other scalar types
97  // Without C++11, we have to disable GEMM's parallelization on
98  // non x86 architectures because there volatile is not enough for our purpose.
99  // See bug 1572.
100 #if (! defined(EIGEN_HAS_OPENMP)) || defined(EIGEN_USE_BLAS)
101  // FIXME the transpose variable is only needed to properly split
102  // the matrix product when multithreading is enabled. This is a temporary
103  // fix to support row-major destination matrices. This whole
104  // parallelizer mechanism has to be redesigned anyway.
105  EIGEN_UNUSED_VARIABLE(depth);
106  EIGEN_UNUSED_VARIABLE(transpose);
107  func(0,rows, 0,cols);
108 #else
109 
110  // Dynamically check whether we should enable or disable OpenMP.
111  // The conditions are:
112  // - the max number of threads we can create is greater than 1
113  // - we are not already in a parallel code
114  // - the sizes are large enough
115 
116  // compute the maximal number of threads from the size of the product:
117  // This first heuristic takes into account that the product kernel is fully optimized when working with nr columns at once.
118  Index size = transpose ? rows : cols;
119  Index pb_max_threads = std::max<Index>(1,size / Functor::Traits::nr);
120 
121  // compute the maximal number of threads from the total amount of work:
122  double work = static_cast<double>(rows) * static_cast<double>(cols) *
123  static_cast<double>(depth);
124  double kMinTaskSize = 50000; // FIXME improve this heuristic.
125  pb_max_threads = std::max<Index>(1, std::min<Index>(pb_max_threads, static_cast<Index>( work / kMinTaskSize ) ));
126 
127  // compute the number of threads we are going to use
128  Index threads = std::min<Index>(nbThreads(), pb_max_threads);
129 
130  // if multi-threading is explicitly disabled, not useful, or if we already are in a parallel session,
131  // then abort multi-threading
132  // FIXME omp_get_num_threads()>1 only works for openmp, what if the user does not use openmp?
133  if((!Condition) || (threads==1) || (omp_get_num_threads()>1))
134  return func(0,rows, 0,cols);
135 
137  func.initParallelSession(threads);
138 
139  if(transpose)
141 
142  ei_declare_aligned_stack_constructed_variable(GemmParallelInfo<Index>,info,threads,0);
143 
144  #pragma omp parallel num_threads(threads)
145  {
146  Index i = omp_get_thread_num();
147  // Note that the actual number of threads might be lower than the number of request ones.
148  Index actual_threads = omp_get_num_threads();
149 
150  Index blockCols = (cols / actual_threads) & ~Index(0x3);
151  Index blockRows = (rows / actual_threads);
152  blockRows = (blockRows/Functor::Traits::mr)*Functor::Traits::mr;
153 
154  Index r0 = i*blockRows;
155  Index actualBlockRows = (i+1==actual_threads) ? rows-r0 : blockRows;
156 
157  Index c0 = i*blockCols;
158  Index actualBlockCols = (i+1==actual_threads) ? cols-c0 : blockCols;
159 
160  info[i].lhs_start = r0;
161  info[i].lhs_length = actualBlockRows;
162 
163  if(transpose) func(c0, actualBlockCols, 0, rows, info);
164  else func(0, rows, c0, actualBlockCols, info);
165  }
166 #endif
167 }
168 
169 } // end namespace internal
170 
171 } // end namespace Eigen
172 
173 #endif // EIGEN_PARALLELIZER_H
Array< int, Dynamic, 1 > v
#define eigen_internal_assert(x)
Definition: Macros.h:908
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:957
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
Definition: Memory.h:847
void parallelize_gemm(const Functor &func, Index rows, Index cols, Index depth, bool transpose)
Definition: Parallelizer.h:93
void manage_multi_threading(Action action, int *v)
Definition: Parallelizer.h:20
void manage_caching_sizes(Action action, std::ptrdiff_t *l1, std::ptrdiff_t *l2, std::ptrdiff_t *l3)
void swap(scoped_array< T > &a, scoped_array< T > &b)
Definition: Memory.h:788
: InteropHeaders
Definition: Core:139
@ GetAction
Definition: Constants.h:508
@ SetAction
Definition: Constants.h:508
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
void initParallel()
Definition: Parallelizer.h:51
int nbThreads()
Definition: Parallelizer.h:61
void setNbThreads(int v)
Definition: Parallelizer.h:70