ThreadBackend.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SMP_THREADS_THREADBACKEND_H_
36 #define _BLAZE_MATH_SMP_THREADS_THREADBACKEND_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #if BLAZE_CPP_THREADS_PARALLEL_MODE
44 # include <condition_variable>
45 # include <mutex>
46 # include <thread>
47 #elif BLAZE_BOOST_THREADS_PARALLEL_MODE
48 # include <boost/thread/condition.hpp>
49 # include <boost/thread/mutex.hpp>
50 # include <boost/thread/thread.hpp>
51 #endif
52 
53 #include <cstdlib>
55 #include <blaze/system/SMP.h>
59 #include <blaze/util/ThreadPool.h>
60 #include <blaze/util/Types.h>
61 
62 
63 namespace blaze {
64 
65 //=================================================================================================
66 //
67 // CLASS DEFINITION
68 //
69 //=================================================================================================
70 
71 //*************************************************************************************************
82 template< typename TT // Type of the encapsulated thread
83  , typename MT // Type of the synchronization mutex
84  , typename LT // Type of the mutex lock
85  , typename CT > // Type of the condition variable
86 class ThreadBackend
87 {
88  public:
89  //**Utility functions***************************************************************************
92  static inline size_t size ();
93  static inline void resize( size_t n, bool block=false );
94  static inline void wait ();
96  //**********************************************************************************************
97 
98  //**Thread execution functions******************************************************************
101  template< typename Target, typename Source, typename OP >
102  static inline void schedule( Target& target, const Source& source, OP op );
104  //**********************************************************************************************
105 
106  private:
107  //**Private class Assigner**********************************************************************
110  template< typename Target // Type of the target operand
111  , typename Source // Type of the source operand
112  , typename OP > // Type of the assignment operation
113  struct Assigner
114  {
115  //**Constructor******************************************************************************
122  explicit inline Assigner( Target& target, const Source& source, OP op )
123  : target_( target ) // The target operand
124  , source_( source ) // The source operand
125  , op_ ( op ) // The (compound) assignment operation
126  {}
127  //*******************************************************************************************
128 
129  //**Function call operator*******************************************************************
134  inline void operator()() {
135  op_( target_, source_ );
136  }
137  //*******************************************************************************************
138 
139  //**Member variables*************************************************************************
140  Target target_;
141  const Source source_;
142  OP op_;
143  //*******************************************************************************************
144 
145  //**Member variables*************************************************************************
148  //*******************************************************************************************
149  };
150  //**********************************************************************************************
151 
152  //**Initialization functions********************************************************************
155  static inline size_t initPool();
157  //**********************************************************************************************
158 
159  //**Member variables****************************************************************************
162  static ThreadPool<TT,MT,LT,CT> threadpool_;
163 
169  //**********************************************************************************************
170 };
172 //*************************************************************************************************
173 
174 
175 
176 
177 //=================================================================================================
178 //
179 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
180 //
181 //=================================================================================================
182 
183 //*************************************************************************************************
185 template< typename TT, typename MT, typename LT, typename CT >
186 ThreadPool<TT,MT,LT,CT> ThreadBackend<TT,MT,LT,CT>::threadpool_( initPool() );
188 //*************************************************************************************************
189 
190 
191 
192 
193 //=================================================================================================
194 //
195 // UTILITY FUNCTIONS
196 //
197 //=================================================================================================
198 
199 //*************************************************************************************************
205 template< typename TT // Type of the encapsulated thread
206  , typename MT // Type of the synchronization mutex
207  , typename LT // Type of the mutex lock
208  , typename CT > // Type of the condition variable
209 inline size_t ThreadBackend<TT,MT,LT,CT>::size()
210 {
211  return threadpool_.size();
212 }
214 //*************************************************************************************************
215 
216 
217 //*************************************************************************************************
233 template< typename TT // Type of the encapsulated thread
234  , typename MT // Type of the synchronization mutex
235  , typename LT // Type of the mutex lock
236  , typename CT > // Type of the condition variable
237 inline void ThreadBackend<TT,MT,LT,CT>::resize( size_t n, bool block )
238 {
239  return threadpool_.resize( n, block );
240 }
242 //*************************************************************************************************
243 
244 
245 //*************************************************************************************************
253 template< typename TT // Type of the encapsulated thread
254  , typename MT // Type of the synchronization mutex
255  , typename LT // Type of the mutex lock
256  , typename CT > // Type of the condition variable
257 inline void ThreadBackend<TT,MT,LT,CT>::wait()
258 {
259  threadpool_.wait();
260 }
262 //*************************************************************************************************
263 
264 
265 
266 
267 //=================================================================================================
268 //
269 // THREAD EXECUTION FUNCTIONS
270 //
271 //=================================================================================================
272 
273 //*************************************************************************************************
284 template< typename TT // Type of the encapsulated thread
285  , typename MT // Type of the synchronization mutex
286  , typename LT // Type of the mutex lock
287  , typename CT > // Type of the condition variable
288 template< typename Target // Type of the target operand
289  , typename Source // Type of the source operand
290  , typename OP > // Type of the assignment operation
291 inline void ThreadBackend<TT,MT,LT,CT>::schedule( Target& target, const Source& source, OP op )
292 {
294  threadpool_.schedule( Assigner<Target,Source,OP>( target, source, op ) );
295 }
297 //*************************************************************************************************
298 
299 
300 
301 
302 //=================================================================================================
303 //
304 // INITIALIZATION FUNCTIONS
305 //
306 //=================================================================================================
307 
308 //*************************************************************************************************
318 #if (defined _MSC_VER)
319 # pragma warning(push)
320 # pragma warning(disable:4996)
321 #endif
322 template< typename TT // Type of the encapsulated thread
323  , typename MT // Type of the synchronization mutex
324  , typename LT // Type of the mutex lock
325  , typename CT > // Type of the condition variable
326 inline size_t ThreadBackend<TT,MT,LT,CT>::initPool()
327 {
328  const char* env = std::getenv( "BLAZE_NUM_THREADS" );
329 
330  if( env == nullptr )
331  return 1UL;
332  else return max( 1, atoi( env ) );
333 }
334 #if (defined _MSC_VER)
335 # pragma warning(pop)
336 #endif
337 
338 //*************************************************************************************************
339 
340 
341 
342 
343 //=================================================================================================
344 //
345 // TYPE DEFINITIONS
346 //
347 //=================================================================================================
348 
349 //*************************************************************************************************
358 #if BLAZE_CPP_THREADS_PARALLEL_MODE
359 using TheThreadBackend = ThreadBackend< std::thread
360  , std::mutex
361  , std::unique_lock< std::mutex >
362  , std::condition_variable
363  >;
364 #elif BLAZE_BOOST_THREADS_PARALLEL_MODE
365 using TheThreadBackend = ThreadBackend< boost::thread
366  , boost::mutex
367  , boost::unique_lock< boost::mutex >
368  , boost::condition_variable
369  >;
370 #endif
371 
372 //*************************************************************************************************
373 
374 
375 
376 
377 //=================================================================================================
378 //
379 // COMPILE TIME CONSTRAINTS
380 //
381 //=================================================================================================
382 
383 //*************************************************************************************************
385 namespace {
386 
388 
389 }
391 //*************************************************************************************************
392 
393 } // namespace blaze
394 
395 #endif
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.In case the given data type is a const-qualified type, a compilation error is created.
Definition: Const.h:79
Header file of the ThreadPool class.
Header file for basic type definitions.
#define BLAZE_CONSTRAINT_MUST_BE_EXPRESSION_TYPE(T)
Constraint on the data type.In case the given data type T is not an expression (i.e. a type derived from the Expression base class), a compilation error is created.
Definition: Expression.h:61
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
#define BLAZE_BOOST_THREADS_PARALLEL_MODE
Compilation switch for the Boost parallelization.This compilation switch enables/disables the paralle...
Definition: SMP.h:122
Constraint on the data type.
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1950
Headerfile for the generic max algorithm.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Compile time assertion.
System settings for the shared-memory parallelization.
#define BLAZE_CPP_THREADS_PARALLEL_MODE
Compilation switch for the C++11 parallelization.This compilation switch enables/disables the paralle...
Definition: SMP.h:95
BLAZE_ALWAYS_INLINE void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:714
Constraint on the data type.
#define BLAZE_STATIC_ASSERT(expr)
Compile time assertion macro.In case of an invalid compile time expression, a compilation error is cr...
Definition: StaticAssert.h:112