All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/math/Functions.h>
56 #include <blaze/system/SMP.h>
58 #include <blaze/util/ThreadPool.h>
59 
60 
61 namespace blaze {
62 
63 //=================================================================================================
64 //
65 // CLASS DEFINITION
66 //
67 //=================================================================================================
68 
69 //*************************************************************************************************
80 template< typename TT // Type of the encapsulated thread
81  , typename MT // Type of the synchronization mutex
82  , typename LT // Type of the mutex lock
83  , typename CT > // Type of the condition variable
84 class ThreadBackend
85 {
86  public:
87  //**Utility functions***************************************************************************
90  static inline size_t size ();
91  static inline void resize( size_t n, bool block=false );
92  static inline void wait ();
94  //**********************************************************************************************
95 
96  //**Thread execution functions******************************************************************
99  template< typename Target, typename Source >
100  static inline void scheduleAssign( Target& target, const Source& source );
101 
102  template< typename Target, typename Source >
103  static inline void scheduleAddAssign( Target& target, const Source& source );
104 
105  template< typename Target, typename Source >
106  static inline void scheduleSubAssign( Target& target, const Source& source );
107 
108  template< typename Target, typename Source >
109  static inline void scheduleMultAssign( Target& target, const Source& source );
111  //**********************************************************************************************
112 
113  private:
114  //**Private class Assigner**********************************************************************
117  template< typename Target // Type of the target operand
118  , typename Source > // Type of the source operand
119  struct Assigner
120  {
121  //**Constructor******************************************************************************
127  explicit inline Assigner( Target& target, const Source& source )
128  : target_( target ) // The target operand
129  , source_( source ) // The source operand
130  {}
131  //*******************************************************************************************
132 
133  //**Function call operator*******************************************************************
138  inline void operator()() {
139  assign( target_, source_ );
140  }
141  //*******************************************************************************************
142 
143  //**Member variables*************************************************************************
144  Target target_;
145  const Source source_;
146  //*******************************************************************************************
147 
148  //**Member variables*************************************************************************
151  //*******************************************************************************************
152  };
153  //**********************************************************************************************
154 
155  //**Private class AddAssigner*******************************************************************
158  template< typename Target // Type of the target operand
159  , typename Source > // Type of the source operand
160  struct AddAssigner
161  {
162  //**Constructor******************************************************************************
168  explicit inline AddAssigner( Target& target, const Source& source )
169  : target_( target ) // The target operand
170  , source_( source ) // The source operand
171  {}
172  //*******************************************************************************************
173 
174  //**Function call operator*******************************************************************
179  inline void operator()() {
180  addAssign( target_, source_ );
181  }
182  //*******************************************************************************************
183 
184  //**Member variables*************************************************************************
185  Target target_;
186  const Source source_;
187  //*******************************************************************************************
188 
189  //**Member variables*************************************************************************
192  //*******************************************************************************************
193  };
194  //**********************************************************************************************
195 
196  //**Private class SubAssigner*******************************************************************
199  template< typename Target // Type of the target operand
200  , typename Source > // Type of the source operand
201  struct SubAssigner
202  {
203  //**Constructor******************************************************************************
209  explicit inline SubAssigner( Target& target, const Source& source )
210  : target_( target ) // The target operand
211  , source_( source ) // The source operand
212  {}
213  //*******************************************************************************************
214 
215  //**Function call operator*******************************************************************
220  inline void operator()() {
221  subAssign( target_, source_ );
222  }
223  //*******************************************************************************************
224 
225  //**Member variables*************************************************************************
226  Target target_;
227  const Source source_;
228  //*******************************************************************************************
229 
230  //**Member variables*************************************************************************
233  //*******************************************************************************************
234  };
235  //**********************************************************************************************
236 
237  //**Private class MultAssigner******************************************************************
240  template< typename Target // Type of the target operand
241  , typename Source > // Type of the source operand
242  struct MultAssigner
243  {
244  //**Constructor******************************************************************************
250  explicit inline MultAssigner( Target& target, const Source& source )
251  : target_( target ) // The target operand
252  , source_( source ) // The source operand
253  {}
254  //*******************************************************************************************
255 
256  //**Function call operator*******************************************************************
261  inline void operator()() {
262  multAssign( target_, source_ );
263  }
264  //*******************************************************************************************
265 
266  //**Member variables*************************************************************************
267  Target target_;
268  const Source source_;
269  //*******************************************************************************************
270 
271  //**Member variables*************************************************************************
274  //*******************************************************************************************
275  };
276  //**********************************************************************************************
277 
278  //**Initialization functions********************************************************************
281  static inline size_t initPool();
283  //**********************************************************************************************
284 
285  //**Member variables****************************************************************************
288  static ThreadPool<TT,MT,LT,CT> threadpool_;
289 
295  //**********************************************************************************************
296 };
298 //*************************************************************************************************
299 
300 
301 
302 
303 //=================================================================================================
304 //
305 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
306 //
307 //=================================================================================================
308 
309 //*************************************************************************************************
311 template< typename TT, typename MT, typename LT, typename CT >
312 ThreadPool<TT,MT,LT,CT> ThreadBackend<TT,MT,LT,CT>::threadpool_( initPool() );
314 //*************************************************************************************************
315 
316 
317 
318 
319 //=================================================================================================
320 //
321 // UTILITY FUNCTIONS
322 //
323 //=================================================================================================
324 
325 //*************************************************************************************************
331 template< typename TT // Type of the encapsulated thread
332  , typename MT // Type of the synchronization mutex
333  , typename LT // Type of the mutex lock
334  , typename CT > // Type of the condition variable
335 inline size_t ThreadBackend<TT,MT,LT,CT>::size()
336 {
337  return threadpool_.size();
338 }
340 //*************************************************************************************************
341 
342 
343 //*************************************************************************************************
359 template< typename TT // Type of the encapsulated thread
360  , typename MT // Type of the synchronization mutex
361  , typename LT // Type of the mutex lock
362  , typename CT > // Type of the condition variable
363 inline void ThreadBackend<TT,MT,LT,CT>::resize( size_t n, bool block )
364 {
365  return threadpool_.resize( n, block );
366 }
368 //*************************************************************************************************
369 
370 
371 //*************************************************************************************************
379 template< typename TT // Type of the encapsulated thread
380  , typename MT // Type of the synchronization mutex
381  , typename LT // Type of the mutex lock
382  , typename CT > // Type of the condition variable
383 inline void ThreadBackend<TT,MT,LT,CT>::wait()
384 {
385  threadpool_.wait();
386 }
388 //*************************************************************************************************
389 
390 
391 
392 
393 //=================================================================================================
394 //
395 // THREAD EXECUTION FUNCTIONS
396 //
397 //=================================================================================================
398 
399 //*************************************************************************************************
409 template< typename TT // Type of the encapsulated thread
410  , typename MT // Type of the synchronization mutex
411  , typename LT // Type of the mutex lock
412  , typename CT > // Type of the condition variable
413 template< typename Target // Type of the target operand
414  , typename Source > // Type of the source operand
415 inline void ThreadBackend<TT,MT,LT,CT>::scheduleAssign( Target& target, const Source& source )
416 {
418  threadpool_.schedule( Assigner<Target,Source>( target, source ) );
419 }
421 //*************************************************************************************************
422 
423 
424 //*************************************************************************************************
434 template< typename TT // Type of the encapsulated thread
435  , typename MT // Type of the synchronization mutex
436  , typename LT // Type of the mutex lock
437  , typename CT > // Type of the condition variable
438 template< typename Target // Type of the target operand
439  , typename Source > // Type of the source operand
440 inline void ThreadBackend<TT,MT,LT,CT>::scheduleAddAssign( Target& target, const Source& source )
441 {
443  threadpool_.schedule( AddAssigner<Target,Source>( target, source ) );
444 }
446 //*************************************************************************************************
447 
448 
449 //*************************************************************************************************
459 template< typename TT // Type of the encapsulated thread
460  , typename MT // Type of the synchronization mutex
461  , typename LT // Type of the mutex lock
462  , typename CT > // Type of the condition variable
463 template< typename Target // Type of the target operand
464  , typename Source > // Type of the source operand
465 inline void ThreadBackend<TT,MT,LT,CT>::scheduleSubAssign( Target& target, const Source& source )
466 {
468  threadpool_.schedule( SubAssigner<Target,Source>( target, source ) );
469 }
471 //*************************************************************************************************
472 
473 
474 //*************************************************************************************************
484 template< typename TT // Type of the encapsulated thread
485  , typename MT // Type of the synchronization mutex
486  , typename LT // Type of the mutex lock
487  , typename CT > // Type of the condition variable
488 template< typename Target // Type of the target operand
489  , typename Source > // Type of the source operand
490 inline void ThreadBackend<TT,MT,LT,CT>::scheduleMultAssign( Target& target, const Source& source )
491 {
493  threadpool_.schedule( MultAssigner<Target,Source>( target, source ) );
494 }
496 //*************************************************************************************************
497 
498 
499 
500 
501 //=================================================================================================
502 //
503 // INITIALIZATION FUNCTIONS
504 //
505 //=================================================================================================
506 
507 //*************************************************************************************************
517 #if (defined _MSC_VER)
518 # pragma warning(push)
519 # pragma warning(disable:4996)
520 #endif
521 template< typename TT // Type of the encapsulated thread
522  , typename MT // Type of the synchronization mutex
523  , typename LT // Type of the mutex lock
524  , typename CT > // Type of the condition variable
525 inline size_t ThreadBackend<TT,MT,LT,CT>::initPool()
526 {
527  const char* env = std::getenv( "BLAZE_NUM_THREADS" );
528 
529  if( env == NULL )
530  return 1UL;
531  else return max( 1, atoi( env ) );
532 }
533 #if (defined _MSC_VER)
534 # pragma warning(pop)
535 #endif
536 
537 //*************************************************************************************************
538 
539 
540 
541 
542 //=================================================================================================
543 //
544 // TYPE DEFINITIONS
545 //
546 //=================================================================================================
547 
548 //*************************************************************************************************
557 #if BLAZE_CPP_THREADS_PARALLEL_MODE
558 typedef ThreadBackend< std::thread
559  , std::mutex
560  , std::unique_lock< std::mutex >
561  , std::condition_variable
562  > TheThreadBackend;
563 #elif BLAZE_BOOST_THREADS_PARALLEL_MODE
564 typedef ThreadBackend< boost::thread
565  , boost::mutex
566  , boost::unique_lock< boost::mutex >
567  , boost::condition_variable
568  > TheThreadBackend;
569 #endif
570 
571 //*************************************************************************************************
572 
573 
574 
575 
576 //=================================================================================================
577 //
578 // COMPILE TIME CONSTRAINTS
579 //
580 //=================================================================================================
581 
582 //*************************************************************************************************
584 namespace {
585 
587 
588 }
590 //*************************************************************************************************
591 
592 } // namespace blaze
593 
594 #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:116
Header file for mathematical functions.
Header file of the ThreadPool class.
#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:79
#define BLAZE_BOOST_THREADS_PARALLEL_MODE
Compilation switch for the Boost parallelization.This compilation switch enables/disables the paralle...
Definition: SMP.h:122
const blaze::Null NULL
Global NULL pointer.This instance of the Null class replaces the NULL macro to ensure a type-safe NUL...
Definition: Null.h:300
Constraint on the data type.
Compile time assertion.
System settings for the shared-memory parallelization.
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:271
#define BLAZE_CPP_THREADS_PARALLEL_MODE
Compilation switch for the C++11 parallelization.This compilation switch enables/disables the paralle...
Definition: SMP.h:95
void multAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the multiplication assignment of a matrix to a matrix.
Definition: Matrix.h:361
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:301
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:331
#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:143