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/shims/Round.h>
46 #include <blaze/math/shims/Sqrt.h>
49 #include <blaze/util/Types.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // THREADMAPPING FUNCTIONALITY
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
62 using ThreadMapping = std::pair<size_t,size_t>;
64 //*************************************************************************************************
65 
66 
67 //*************************************************************************************************
80 template< typename MT // Type of the matrix
81  , bool SO > // Storage order of the matrix
82 ThreadMapping createThreadMapping( size_t threads, const Matrix<MT,SO>& A )
83 {
84  const size_t M( (~A).rows() );
85  const size_t N( (~A).columns() );
86 
87  if( M > N || ( M == N && !SO ) )
88  {
89  const double ratio( double(M)/double(N) );
90  size_t m = min( threads, max( 1UL, static_cast<size_t>( round( sqrt( threads*ratio ) ) ) ) );
91  size_t n = threads / m;
92 
93  while( m * n != threads ) {
94  ++m;
95  n = threads / m;
96  }
97 
98  return ThreadMapping( m, n );
99  }
100  else
101  {
102  const double ratio( double(N)/double(M) );
103  size_t n = min( threads, max( 1UL, static_cast<size_t>( round( sqrt( threads*ratio ) ) ) ) );
104  size_t m = threads / n;
105 
106  while( m * n != threads ) {
107  ++n;
108  m = threads / n;
109  }
110 
111  return ThreadMapping( m, n );
112  }
113 }
115 //*************************************************************************************************
116 
117 } // namespace blaze
118 
119 #endif
Headerfile for the generic min algorithm.
Header file for basic type definitions.
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1809
Header file for the sqrt shim.
Headerfile for the generic max algorithm.
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:340
decltype(auto) round(const DenseMatrix< MT, SO > &dm)
Applies the round() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1292
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:324
decltype(auto) sqrt(const DenseMatrix< MT, SO > &dm)
Computes the square root of each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1448