Blaze 3.9
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>
60#include <blaze/util/Types.h>
61
62
63namespace blaze {
64
65//=================================================================================================
66//
67// CLASS DEFINITION
68//
69//=================================================================================================
70
71//*************************************************************************************************
82template< 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
86class 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 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_;
169 //**********************************************************************************************
170};
172//*************************************************************************************************
173
174
175
176
177//=================================================================================================
178//
179// DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
180//
181//=================================================================================================
182
183//*************************************************************************************************
185template< typename TT, typename MT, typename LT, typename CT >
186ThreadPool<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//*************************************************************************************************
205template< 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
210{
211 return threadpool_.size();
212}
214//*************************************************************************************************
215
216
217//*************************************************************************************************
233template< 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
237inline void ThreadBackend<TT,MT,LT,CT>::resize( size_t n, bool block )
238{
239 return threadpool_.resize( n, block );
240}
242//*************************************************************************************************
243
244
245//*************************************************************************************************
253template< 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
257inline 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//*************************************************************************************************
284template< 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
288template< typename Target // Type of the target operand
289 , typename Source // Type of the source operand
290 , typename OP > // Type of the assignment operation
291inline 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
322template< 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
326inline 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
338//*************************************************************************************************
339
340
341
342
343//=================================================================================================
344//
345// TYPE DEFINITIONS
346//
347//=================================================================================================
348
349//*************************************************************************************************
358#if BLAZE_CPP_THREADS_PARALLEL_MODE
359using 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
365using TheThreadBackend = ThreadBackend< boost::thread
366 , boost::mutex
367 , boost::unique_lock< boost::mutex >
368 , boost::condition_variable
369 >;
370#endif
372//*************************************************************************************************
373
374
375
376
377//=================================================================================================
378//
379// COMPILE TIME CONSTRAINTS
380//
381//=================================================================================================
382
383//*************************************************************************************************
385namespace {
386
388
389}
391//*************************************************************************************************
392
393} // namespace blaze
394
395#endif
Constraint on the data type.
Compile time assertion.
Header file of the ThreadPool class.
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.
Definition: Const.h:79
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1375
#define BLAZE_CONSTRAINT_MUST_BE_EXPRESSION_TYPE(T)
Constraint on the data type.
Definition: Expression.h:61
void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:1108
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:676
#define BLAZE_STATIC_ASSERT(expr)
Compile time assertion macro.
Definition: StaticAssert.h:112
#define BLAZE_CPP_THREADS_PARALLEL_MODE
Compilation switch for the C++11 parallelization.
Definition: SMP.h:124
#define BLAZE_BOOST_THREADS_PARALLEL_MODE
Compilation switch for the Boost parallelization.
Definition: SMP.h:152
System settings for the shared-memory parallelization.
Header file for basic type definitions.
Header file for the generic max algorithm.