Eigen  3.2.92
Parallelizer.h
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 namespace Eigen {
14 
15 namespace internal {
16 
18 inline void manage_multi_threading(Action action, int* v)
19 {
20  static EIGEN_UNUSED int m_maxThreads = -1;
21 
22  if(action==SetAction)
23  {
24  eigen_internal_assert(v!=0);
25  m_maxThreads = *v;
26  }
27  else if(action==GetAction)
28  {
29  eigen_internal_assert(v!=0);
30  #ifdef EIGEN_HAS_OPENMP
31  if(m_maxThreads>0)
32  *v = m_maxThreads;
33  else
34  *v = omp_get_max_threads();
35  #else
36  *v = 1;
37  #endif
38  }
39  else
40  {
41  eigen_internal_assert(false);
42  }
43 }
44 
45 }
46 
48 inline void initParallel()
49 {
50  int nbt;
51  internal::manage_multi_threading(GetAction, &nbt);
52  std::ptrdiff_t l1, l2, l3;
53  internal::manage_caching_sizes(GetAction, &l1, &l2, &l3);
54 }
55 
58 inline int nbThreads()
59 {
60  int ret;
61  internal::manage_multi_threading(GetAction, &ret);
62  return ret;
63 }
64 
67 inline void setNbThreads(int v)
68 {
69  internal::manage_multi_threading(SetAction, &v);
70 }
71 
72 namespace internal {
73 
74 template<typename Index> struct GemmParallelInfo
75 {
76  GemmParallelInfo() : sync(-1), users(0), lhs_start(0), lhs_length(0) {}
77 
78  int volatile sync;
79  int volatile users;
80 
81  Index lhs_start;
82  Index lhs_length;
83 };
84 
85 template<bool Condition, typename Functor, typename Index>
86 void parallelize_gemm(const Functor& func, Index rows, Index cols, bool transpose)
87 {
88  // TODO when EIGEN_USE_BLAS is defined,
89  // we should still enable OMP for other scalar types
90 #if !(defined (EIGEN_HAS_OPENMP)) || defined (EIGEN_USE_BLAS)
91  // FIXME the transpose variable is only needed to properly split
92  // the matrix product when multithreading is enabled. This is a temporary
93  // fix to support row-major destination matrices. This whole
94  // parallelizer mechanism has to be redisigned anyway.
95  EIGEN_UNUSED_VARIABLE(transpose);
96  func(0,rows, 0,cols);
97 #else
98 
99  // Dynamically check whether we should enable or disable OpenMP.
100  // The conditions are:
101  // - the max number of threads we can create is greater than 1
102  // - we are not already in a parallel code
103  // - the sizes are large enough
104 
105  // compute the maximal number of threads from the size of the product:
106  // FIXME this has to be fine tuned
107  Index size = transpose ? rows : cols;
108  Index pb_max_threads = std::max<Index>(1,size / 32);
109  // compute the number of threads we are going to use
110  Index threads = std::min<Index>(nbThreads(), pb_max_threads);
111 
112  // if multi-threading is explicitely disabled, not useful, or if we already are in a parallel session,
113  // then abort multi-threading
114  // FIXME omp_get_num_threads()>1 only works for openmp, what if the user does not use openmp?
115  if((!Condition) || (threads==1) || (omp_get_num_threads()>1))
116  return func(0,rows, 0,cols);
117 
118  Eigen::initParallel();
119  func.initParallelSession(threads);
120 
121  if(transpose)
122  std::swap(rows,cols);
123 
124  ei_declare_aligned_stack_constructed_variable(GemmParallelInfo<Index>,info,threads,0);
125 
126  #pragma omp parallel num_threads(threads)
127  {
128  Index i = omp_get_thread_num();
129  // Note that the actual number of threads might be lower than the number of request ones.
130  Index actual_threads = omp_get_num_threads();
131 
132  Index blockCols = (cols / actual_threads) & ~Index(0x3);
133  Index blockRows = (rows / actual_threads);
134  blockRows = (blockRows/Functor::Traits::mr)*Functor::Traits::mr;
135 
136  Index r0 = i*blockRows;
137  Index actualBlockRows = (i+1==actual_threads) ? rows-r0 : blockRows;
138 
139  Index c0 = i*blockCols;
140  Index actualBlockCols = (i+1==actual_threads) ? cols-c0 : blockCols;
141 
142  info[i].lhs_start = r0;
143  info[i].lhs_length = actualBlockRows;
144 
145  if(transpose) func(c0, actualBlockCols, 0, rows, info);
146  else func(0, rows, c0, actualBlockCols, info);
147  }
148 #endif
149 }
150 
151 } // end namespace internal
152 
153 } // end namespace Eigen
154 
155 #endif // EIGEN_PARALLELIZER_H
Definition: LDLT.h:16
Definition: Eigen_Colamd.h:54