ThreadMapping.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SMP_THREADMAPPING_H_
36 #define _BLAZE_MATH_SMP_THREADMAPPING_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <utility>
45 #include <blaze/math/Functions.h>
46 #include <blaze/math/shims/Round.h>
47 #include <blaze/math/shims/Sqrt.h>
48 #include <blaze/util/Types.h>
49 
50 
51 namespace blaze {
52 
53 //=================================================================================================
54 //
55 // THREADMAPPING FUNCTIONALITY
56 //
57 //=================================================================================================
58 
59 //*************************************************************************************************
61 using ThreadMapping = std::pair<size_t,size_t>;
63 //*************************************************************************************************
64 
65 
66 //*************************************************************************************************
79 template< typename MT // Type of the matrix
80  , bool SO > // Storage order of the matrix
81 ThreadMapping createThreadMapping( size_t threads, const Matrix<MT,SO>& A )
82 {
83  const size_t M( (~A).rows() );
84  const size_t N( (~A).columns() );
85 
86  if( M > N || ( M == N && !SO ) )
87  {
88  const double ratio( double(M)/double(N) );
89  size_t m = min<size_t>( threads, max<size_t>( 1UL, round( sqrt( threads*ratio ) ) ) );
90  size_t n = threads / m;
91 
92  while( m * n != threads ) {
93  ++m;
94  n = threads / m;
95  }
96 
97  return ThreadMapping( m, n );
98  }
99  else
100  {
101  const double ratio( double(N)/double(M) );
102  size_t n = min<size_t>( threads, max<size_t>( 1UL, round( sqrt( threads*ratio ) ) ) );
103  size_t m = threads / n;
104 
105  while( m * n != threads ) {
106  ++n;
107  m = threads / n;
108  }
109 
110  return ThreadMapping( m, n );
111  }
112 }
114 //*************************************************************************************************
115 
116 } // namespace blaze
117 
118 #endif
Header file for mathematical functions.
Header file for basic type definitions.
Header file for the sqrt shim.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
BLAZE_ALWAYS_INLINE size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:330
const DMatForEachExpr< MT, Sqrt, SO > sqrt(const DenseMatrix< MT, SO > &dm)
Computes the square root of each single element of the dense matrix dm.
Definition: DMatForEachExpr.h:1282
Header file for the Matrix base class.
Header file for the round shim.
BLAZE_ALWAYS_INLINE size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:314