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>
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 >
102  static inline void scheduleAssign( Target& target, const Source& source );
103 
104  template< typename Target, typename Source >
105  static inline void scheduleAddAssign( Target& target, const Source& source );
106 
107  template< typename Target, typename Source >
108  static inline void scheduleSubAssign( Target& target, const Source& source );
109 
110  template< typename Target, typename Source >
111  static inline void scheduleMultAssign( Target& target, const Source& source );
113  //**********************************************************************************************
114 
115  private:
116  //**Private class Assigner**********************************************************************
119  template< typename Target // Type of the target operand
120  , typename Source > // Type of the source operand
121  struct Assigner
122  {
123  //**Constructor******************************************************************************
129  explicit inline Assigner( Target& target, const Source& source )
130  : target_( target ) // The target operand
131  , source_( source ) // The source operand
132  {}
133  //*******************************************************************************************
134 
135  //**Function call operator*******************************************************************
140  inline void operator()() {
141  assign( target_, source_ );
142  }
143  //*******************************************************************************************
144 
145  //**Member variables*************************************************************************
146  Target target_;
147  const Source source_;
148  //*******************************************************************************************
149 
150  //**Member variables*************************************************************************
153  //*******************************************************************************************
154  };
155  //**********************************************************************************************
156 
157  //**Private class AddAssigner*******************************************************************
160  template< typename Target // Type of the target operand
161  , typename Source > // Type of the source operand
162  struct AddAssigner
163  {
164  //**Constructor******************************************************************************
170  explicit inline AddAssigner( Target& target, const Source& source )
171  : target_( target ) // The target operand
172  , source_( source ) // The source operand
173  {}
174  //*******************************************************************************************
175 
176  //**Function call operator*******************************************************************
181  inline void operator()() {
182  addAssign( target_, source_ );
183  }
184  //*******************************************************************************************
185 
186  //**Member variables*************************************************************************
187  Target target_;
188  const Source source_;
189  //*******************************************************************************************
190 
191  //**Member variables*************************************************************************
194  //*******************************************************************************************
195  };
196  //**********************************************************************************************
197 
198  //**Private class SubAssigner*******************************************************************
201  template< typename Target // Type of the target operand
202  , typename Source > // Type of the source operand
203  struct SubAssigner
204  {
205  //**Constructor******************************************************************************
211  explicit inline SubAssigner( Target& target, const Source& source )
212  : target_( target ) // The target operand
213  , source_( source ) // The source operand
214  {}
215  //*******************************************************************************************
216 
217  //**Function call operator*******************************************************************
222  inline void operator()() {
223  subAssign( target_, source_ );
224  }
225  //*******************************************************************************************
226 
227  //**Member variables*************************************************************************
228  Target target_;
229  const Source source_;
230  //*******************************************************************************************
231 
232  //**Member variables*************************************************************************
235  //*******************************************************************************************
236  };
237  //**********************************************************************************************
238 
239  //**Private class MultAssigner******************************************************************
242  template< typename Target // Type of the target operand
243  , typename Source > // Type of the source operand
244  struct MultAssigner
245  {
246  //**Constructor******************************************************************************
252  explicit inline MultAssigner( Target& target, const Source& source )
253  : target_( target ) // The target operand
254  , source_( source ) // The source operand
255  {}
256  //*******************************************************************************************
257 
258  //**Function call operator*******************************************************************
263  inline void operator()() {
264  multAssign( target_, source_ );
265  }
266  //*******************************************************************************************
267 
268  //**Member variables*************************************************************************
269  Target target_;
270  const Source source_;
271  //*******************************************************************************************
272 
273  //**Member variables*************************************************************************
276  //*******************************************************************************************
277  };
278  //**********************************************************************************************
279 
280  //**Initialization functions********************************************************************
283  static inline size_t initPool();
285  //**********************************************************************************************
286 
287  //**Member variables****************************************************************************
290  static ThreadPool<TT,MT,LT,CT> threadpool_;
291 
297  //**********************************************************************************************
298 };
300 //*************************************************************************************************
301 
302 
303 
304 
305 //=================================================================================================
306 //
307 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
308 //
309 //=================================================================================================
310 
311 //*************************************************************************************************
313 template< typename TT, typename MT, typename LT, typename CT >
314 ThreadPool<TT,MT,LT,CT> ThreadBackend<TT,MT,LT,CT>::threadpool_( initPool() );
316 //*************************************************************************************************
317 
318 
319 
320 
321 //=================================================================================================
322 //
323 // UTILITY FUNCTIONS
324 //
325 //=================================================================================================
326 
327 //*************************************************************************************************
333 template< typename TT // Type of the encapsulated thread
334  , typename MT // Type of the synchronization mutex
335  , typename LT // Type of the mutex lock
336  , typename CT > // Type of the condition variable
337 inline size_t ThreadBackend<TT,MT,LT,CT>::size()
338 {
339  return threadpool_.size();
340 }
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
361 template< typename TT // Type of the encapsulated thread
362  , typename MT // Type of the synchronization mutex
363  , typename LT // Type of the mutex lock
364  , typename CT > // Type of the condition variable
365 inline void ThreadBackend<TT,MT,LT,CT>::resize( size_t n, bool block )
366 {
367  return threadpool_.resize( n, block );
368 }
370 //*************************************************************************************************
371 
372 
373 //*************************************************************************************************
381 template< typename TT // Type of the encapsulated thread
382  , typename MT // Type of the synchronization mutex
383  , typename LT // Type of the mutex lock
384  , typename CT > // Type of the condition variable
385 inline void ThreadBackend<TT,MT,LT,CT>::wait()
386 {
387  threadpool_.wait();
388 }
390 //*************************************************************************************************
391 
392 
393 
394 
395 //=================================================================================================
396 //
397 // THREAD EXECUTION FUNCTIONS
398 //
399 //=================================================================================================
400 
401 //*************************************************************************************************
411 template< typename TT // Type of the encapsulated thread
412  , typename MT // Type of the synchronization mutex
413  , typename LT // Type of the mutex lock
414  , typename CT > // Type of the condition variable
415 template< typename Target // Type of the target operand
416  , typename Source > // Type of the source operand
417 inline void ThreadBackend<TT,MT,LT,CT>::scheduleAssign( Target& target, const Source& source )
418 {
420  threadpool_.schedule( Assigner<Target,Source>( target, source ) );
421 }
423 //*************************************************************************************************
424 
425 
426 //*************************************************************************************************
436 template< typename TT // Type of the encapsulated thread
437  , typename MT // Type of the synchronization mutex
438  , typename LT // Type of the mutex lock
439  , typename CT > // Type of the condition variable
440 template< typename Target // Type of the target operand
441  , typename Source > // Type of the source operand
442 inline void ThreadBackend<TT,MT,LT,CT>::scheduleAddAssign( Target& target, const Source& source )
443 {
445  threadpool_.schedule( AddAssigner<Target,Source>( target, source ) );
446 }
448 //*************************************************************************************************
449 
450 
451 //*************************************************************************************************
461 template< typename TT // Type of the encapsulated thread
462  , typename MT // Type of the synchronization mutex
463  , typename LT // Type of the mutex lock
464  , typename CT > // Type of the condition variable
465 template< typename Target // Type of the target operand
466  , typename Source > // Type of the source operand
467 inline void ThreadBackend<TT,MT,LT,CT>::scheduleSubAssign( Target& target, const Source& source )
468 {
470  threadpool_.schedule( SubAssigner<Target,Source>( target, source ) );
471 }
473 //*************************************************************************************************
474 
475 
476 //*************************************************************************************************
486 template< typename TT // Type of the encapsulated thread
487  , typename MT // Type of the synchronization mutex
488  , typename LT // Type of the mutex lock
489  , typename CT > // Type of the condition variable
490 template< typename Target // Type of the target operand
491  , typename Source > // Type of the source operand
492 inline void ThreadBackend<TT,MT,LT,CT>::scheduleMultAssign( Target& target, const Source& source )
493 {
495  threadpool_.schedule( MultAssigner<Target,Source>( target, source ) );
496 }
498 //*************************************************************************************************
499 
500 
501 
502 
503 //=================================================================================================
504 //
505 // INITIALIZATION FUNCTIONS
506 //
507 //=================================================================================================
508 
509 //*************************************************************************************************
519 #if (defined _MSC_VER)
520 # pragma warning(push)
521 # pragma warning(disable:4996)
522 #endif
523 template< typename TT // Type of the encapsulated thread
524  , typename MT // Type of the synchronization mutex
525  , typename LT // Type of the mutex lock
526  , typename CT > // Type of the condition variable
527 inline size_t ThreadBackend<TT,MT,LT,CT>::initPool()
528 {
529  const char* env = std::getenv( "BLAZE_NUM_THREADS" );
530 
531  if( env == NULL )
532  return 1UL;
533  else return max( 1, atoi( env ) );
534 }
535 #if (defined _MSC_VER)
536 # pragma warning(pop)
537 #endif
538 
539 //*************************************************************************************************
540 
541 
542 
543 
544 //=================================================================================================
545 //
546 // TYPE DEFINITIONS
547 //
548 //=================================================================================================
549 
550 //*************************************************************************************************
559 #if BLAZE_CPP_THREADS_PARALLEL_MODE
560 typedef ThreadBackend< std::thread
561  , std::mutex
562  , std::unique_lock< std::mutex >
563  , std::condition_variable
564  > TheThreadBackend;
565 #elif BLAZE_BOOST_THREADS_PARALLEL_MODE
566 typedef ThreadBackend< boost::thread
567  , boost::mutex
568  , boost::unique_lock< boost::mutex >
569  , boost::condition_variable
570  > TheThreadBackend;
571 #endif
572 
573 //*************************************************************************************************
574 
575 
576 
577 
578 //=================================================================================================
579 //
580 // COMPILE TIME CONSTRAINTS
581 //
582 //=================================================================================================
583 
584 //*************************************************************************************************
586 namespace {
587 
589 
590 }
592 //*************************************************************************************************
593 
594 } // namespace blaze
595 
596 #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
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:994
Header file for mathematical functions.
BLAZE_ALWAYS_INLINE 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:879
Header file of the ThreadPool class.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:258
#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
Constraint on the data type.
Compile time assertion.
System settings for the shared-memory parallelization.
BLAZE_ALWAYS_INLINE 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:635
#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:535
BLAZE_ALWAYS_INLINE 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:742
Constraint on the data type.
Header file for basic type definitions.
#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
BLAZE_ALWAYS_INLINE 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:849