DMatNormExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_DMATNORMEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_DMATNORMEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <utility>
44 #include <blaze/math/Aliases.h>
63 #include <blaze/math/SIMD.h>
71 #include <blaze/util/Assert.h>
72 #include <blaze/util/FalseType.h>
74 #include <blaze/util/mpl/And.h>
75 #include <blaze/util/mpl/Bool.h>
76 #include <blaze/util/mpl/If.h>
78 #include <blaze/util/TrueType.h>
79 #include <blaze/util/TypeList.h>
80 #include <blaze/util/Types.h>
83 
84 
85 namespace blaze {
86 
87 //=================================================================================================
88 //
89 // CLASS DEFINITION
90 //
91 //=================================================================================================
92 
93 //*************************************************************************************************
98 template< typename MT // Type of the dense matrix
99  , typename Abs // Type of the abs operation
100  , typename Power > // Type of the power operation
101 struct DMatNormHelper
102 {
103  //**Type definitions****************************************************************************
105  using ET = ElementType_t<MT>;
106 
108  using CT = RemoveReference_t< CompositeType_t<MT> >;
109  //**********************************************************************************************
110 
111  //**********************************************************************************************
112  static constexpr bool value =
113  ( useOptimizedKernels &&
114  CT::simdEnabled &&
115  If_t< HasSIMDEnabled_v<Abs> && HasSIMDEnabled_v<Power>
116  , And< GetSIMDEnabled<Abs,ET>, GetSIMDEnabled<Power,ET> >
117  , And< HasLoad<Abs>, HasLoad<Power> > >::value &&
118  HasSIMDAdd_v< ElementType_t<CT>, ElementType_t<CT> > );
119  //**********************************************************************************************
120 };
122 //*************************************************************************************************
123 
124 
125 
126 
127 //=================================================================================================
128 //
129 // GLOBAL FUNCTIONS
130 //
131 //=================================================================================================
132 
133 //*************************************************************************************************
148 template< typename MT // Type of the dense matrix
149  , typename Abs // Type of the abs opertaion
150  , typename Power // Type of the power operation
151  , typename Root > // Type of the root operation
152 inline decltype(auto) norm_backend( const DenseMatrix<MT,false>& dm, Abs abs, Power power, Root root, FalseType )
153 {
154  using CT = CompositeType_t<MT>;
155  using ET = ElementType_t<MT>;
156  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
157 
158  if( (~dm).rows() == 0UL || (~dm).columns() == 0UL ) return RT();
159 
160  CT tmp( ~dm );
161 
162  const size_t M( tmp.rows() );
163  const size_t N( tmp.columns() );
164 
165  ET norm( power( abs( tmp(0UL,0UL) ) ) );
166 
167  {
168  size_t j( 1UL );
169 
170  for( ; (j+4UL) <= N; j+=4UL ) {
171  norm += power( abs( tmp(0UL,j ) ) ) + power( abs( tmp(0UL,j+1UL) ) ) +
172  power( abs( tmp(0UL,j+2UL) ) ) + power( abs( tmp(0UL,j+3UL) ) );
173  }
174  for( ; (j+2UL) <= N; j+=2UL ) {
175  norm += power( abs( tmp(0UL,j) ) ) + power( abs( tmp(0UL,j+1UL) ) );
176  }
177  for( ; j<N; ++j ) {
178  norm += power( abs( tmp(0UL,j) ) );
179  }
180  }
181 
182  for( size_t i=1UL; i<M; ++i )
183  {
184  size_t j( 0UL );
185 
186  for( ; (j+4UL) <= N; j+=4UL ) {
187  norm += power( abs( tmp(i,j ) ) ) + power( abs( tmp(i,j+1UL) ) ) +
188  power( abs( tmp(i,j+2UL) ) ) + power( abs( tmp(i,j+3UL) ) );
189  }
190  for( ; (j+2UL) <= N; j+=2UL ) {
191  norm += power( abs( tmp(i,j) ) ) + power( abs( tmp(i,j+1UL) ) );
192  }
193  for( ; j<N; ++j ) {
194  norm += power( abs( tmp(i,j) ) );
195  }
196  }
197 
198  return evaluate( root( norm ) );
199 }
201 //*************************************************************************************************
202 
203 
204 //*************************************************************************************************
219 template< typename MT // Type of the dense matrix
220  , typename Abs // Type of the abs operation
221  , typename Power // Type of the power operation
222  , typename Root > // Type of the root operation
223 inline decltype(auto) norm_backend( const DenseMatrix<MT,true>& dm, Abs abs, Power power, Root root, FalseType )
224 {
225  using CT = CompositeType_t<MT>;
226  using ET = ElementType_t<MT>;
227  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
228 
229  if( (~dm).rows() == 0UL || (~dm).columns() == 0UL ) return RT();
230 
231  CT tmp( ~dm );
232 
233  const size_t M( tmp.rows() );
234  const size_t N( tmp.columns() );
235 
236  ET norm( power( abs( tmp(0UL,0UL) ) ) );
237 
238  {
239  size_t i( 1UL );
240 
241  for( ; (i+4UL) <= M; i+=4UL ) {
242  norm += power( abs( tmp(i ,0UL) ) ) + power( abs( tmp(i+1UL,0UL) ) ) +
243  power( abs( tmp(i+2UL,0UL) ) ) + power( abs( tmp(i+3UL,0UL) ) );
244  }
245  for( ; (i+2UL) <= M; i+=2UL ) {
246  norm += power( abs( tmp(i,0UL) ) ) + power( abs( tmp(i+1UL,0UL) ) );
247  }
248  for( ; i<M; ++i ) {
249  norm += power( abs( tmp(i,0UL) ) );
250  }
251  }
252 
253  for( size_t j=1UL; j<N; ++j )
254  {
255  size_t i( 0UL );
256 
257  for( ; (i+4UL) <= M; i+=4UL ) {
258  norm += power( abs( tmp(i ,j) ) ) + power( abs( tmp(i+1UL,j) ) ) +
259  power( abs( tmp(i+2UL,j) ) ) + power( abs( tmp(i+3UL,j) ) );
260  }
261  for( ; (i+2UL) <= M; i+=2UL ) {
262  norm += power( abs( tmp(i,j) ) ) + power( abs( tmp(i+1UL,j) ) );
263  }
264  for( ; i<M; ++i ) {
265  norm += power( abs( tmp(i,j) ) );
266  }
267  }
268 
269  return evaluate( root( norm ) );
270 }
272 //*************************************************************************************************
273 
274 
275 //*************************************************************************************************
290 template< typename MT // Type of the dense matrix
291  , typename Abs // Type of the abs operation
292  , typename Power // Type of the power operation
293  , typename Root > // Type of the root operation
294 inline decltype(auto) norm_backend( const DenseMatrix<MT,false>& dm, Abs abs, Power power, Root root, TrueType )
295 {
296  using CT = CompositeType_t<MT>;
297  using ET = ElementType_t<MT>;
298  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
299 
300  static constexpr size_t SIMDSIZE = SIMDTrait<ET>::size;
301 
302  if( (~dm).rows() == 0UL || (~dm).columns() == 0UL ) return RT();
303 
304  CT tmp( ~dm );
305 
306  const size_t M( tmp.rows() );
307  const size_t N( tmp.columns() );
308 
309  constexpr bool remainder( !usePadding || !IsPadded_v< RemoveReference_t<CT> > );
310 
311  const size_t jpos( ( remainder )?( N & size_t(-SIMDSIZE) ):( N ) );
312  BLAZE_INTERNAL_ASSERT( !remainder || ( N - ( N % SIMDSIZE ) ) == jpos, "Invalid end calculation" );
313 
314  SIMDTrait_t<ET> xmm1, xmm2, xmm3, xmm4;
315  ET norm{};
316 
317  for( size_t i=0UL; i<M; ++i )
318  {
319  size_t j( 0UL );
320 
321  for( ; (j+SIMDSIZE*3UL) < jpos; j+=SIMDSIZE*4UL ) {
322  xmm1 += power( abs( tmp.load(i,j ) ) );
323  xmm2 += power( abs( tmp.load(i,j+SIMDSIZE ) ) );
324  xmm3 += power( abs( tmp.load(i,j+SIMDSIZE*2UL) ) );
325  xmm4 += power( abs( tmp.load(i,j+SIMDSIZE*3UL) ) );
326  }
327  for( ; (j+SIMDSIZE) < jpos; j+=SIMDSIZE*2UL ) {
328  xmm1 += power( abs( tmp.load(i,j ) ) );
329  xmm2 += power( abs( tmp.load(i,j+SIMDSIZE) ) );
330  }
331  for( ; j<jpos; j+=SIMDSIZE ) {
332  xmm1 += power( abs( tmp.load(i,j) ) );
333  }
334  for( ; remainder && j<N; ++j ) {
335  norm += power( abs( tmp(i,j) ) );
336  }
337  }
338 
339  norm += sum( xmm1 + xmm2 + xmm3 + xmm4 );
340 
341  return evaluate( root( norm ) );
342 }
344 //*************************************************************************************************
345 
346 
347 //*************************************************************************************************
362 template< typename MT // Type of the dense matrix
363  , typename Abs // Type of the abs operation
364  , typename Power // Type of the power operation
365  , typename Root > // Type of the root operation
366 inline decltype(auto) norm_backend( const DenseMatrix<MT,true>& dm, Abs abs, Power power, Root root, TrueType )
367 {
368  using CT = CompositeType_t<MT>;
369  using ET = ElementType_t<MT>;
370  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
371 
372  static constexpr size_t SIMDSIZE = SIMDTrait<ET>::size;
373 
374  if( (~dm).rows() == 0UL || (~dm).columns() == 0UL ) return RT();
375 
376  CT tmp( ~dm );
377 
378  const size_t M( tmp.rows() );
379  const size_t N( tmp.columns() );
380 
381  constexpr bool remainder( !usePadding || !IsPadded_v< RemoveReference_t<CT> > );
382 
383  const size_t ipos( ( remainder )?( M & size_t(-SIMDSIZE) ):( M ) );
384  BLAZE_INTERNAL_ASSERT( !remainder || ( M - ( M % SIMDSIZE ) ) == ipos, "Invalid end calculation" );
385 
386  SIMDTrait_t<ET> xmm1, xmm2, xmm3, xmm4;
387  ET norm{};
388 
389  for( size_t j=0UL; j<N; ++j )
390  {
391  size_t i( 0UL );
392 
393  for( ; (i+SIMDSIZE*3UL) < ipos; i+=SIMDSIZE*4UL ) {
394  xmm1 += power( abs( tmp.load(i ,j) ) );
395  xmm2 += power( abs( tmp.load(i+SIMDSIZE ,j) ) );
396  xmm3 += power( abs( tmp.load(i+SIMDSIZE*2UL,j) ) );
397  xmm4 += power( abs( tmp.load(i+SIMDSIZE*3UL,j) ) );
398  }
399  for( ; (i+SIMDSIZE) < ipos; i+=SIMDSIZE*2UL ) {
400  xmm1 += power( abs( tmp.load(i ,j) ) );
401  xmm2 += power( abs( tmp.load(i+SIMDSIZE,j) ) );
402  }
403  for( ; i<ipos; i+=SIMDSIZE ) {
404  xmm1 += power( abs( tmp.load(i,j) ) );
405  }
406  for( ; remainder && i<M; ++i ) {
407  norm += power( abs( tmp(i,j) ) );
408  }
409  }
410 
411  norm += sum( xmm1 + xmm2 + xmm3 + xmm4 );
412 
413  return evaluate( root( norm ) );
414 }
416 //*************************************************************************************************
417 
418 
419 //*************************************************************************************************
440 template< typename MT // Type of the dense matrix
441  , bool SO // Storage order
442  , typename Abs // Type of the abs operation
443  , typename Power // Type of the power operation
444  , typename Root > // Type of the root operation
445 decltype(auto) norm_backend( const DenseMatrix<MT,SO>& dm, Abs abs, Power power, Root root )
446 {
447  return norm_backend( ~dm, abs, power, root, Bool< DMatNormHelper<MT,Abs,Power>::value >() );
448 }
450 //*************************************************************************************************
451 
452 
453 //*************************************************************************************************
468 template< typename MT // Type of the dense matrix
469  , bool SO > // Storage order
470 decltype(auto) norm( const DenseMatrix<MT,SO>& dm )
471 {
473 
474  return norm_backend( ~dm, SqrAbs(), Noop(), Sqrt() );
475 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
494 template< typename MT // Type of the dense matrix
495  , bool SO > // Storage order
496 decltype(auto) sqrNorm( const DenseMatrix<MT,SO>& dm )
497 {
499 
500  return norm_backend( ~dm, SqrAbs(), Noop(), Noop() );
501 }
502 //*************************************************************************************************
503 
504 
505 //*************************************************************************************************
520 template< typename MT // Type of the dense matrix
521  , bool SO > // Storage order
522 decltype(auto) l1Norm( const DenseMatrix<MT,SO>& dm )
523 {
525 
526  return norm_backend( ~dm, Abs(), Noop(), Noop() );
527 }
528 //*************************************************************************************************
529 
530 
531 //*************************************************************************************************
546 template< typename MT // Type of the dense matrix
547  , bool SO > // Storage order
548 decltype(auto) l2Norm( const DenseMatrix<MT,SO>& dm )
549 {
551 
552  return norm_backend( ~dm, SqrAbs(), Noop(), Sqrt() );
553 }
554 //*************************************************************************************************
555 
556 
557 //*************************************************************************************************
572 template< typename MT // Type of the dense matrix
573  , bool SO > // Storage order
574 decltype(auto) l3Norm( const DenseMatrix<MT,SO>& dm )
575 {
577 
578  return norm_backend( ~dm, Abs(), Pow3(), Cbrt() );
579 }
580 //*************************************************************************************************
581 
582 
583 //*************************************************************************************************
598 template< typename MT // Type of the dense matrix
599  , bool SO > // Storage order
600 decltype(auto) l4Norm( const DenseMatrix<MT,SO>& dm )
601 {
603 
604  return norm_backend( ~dm, SqrAbs(), Pow2(), Qdrt() );
605 }
606 //*************************************************************************************************
607 
608 
609 //*************************************************************************************************
629 template< typename MT // Type of the dense matrix
630  , bool SO // Storage order
631  , typename ST > // Type of the norm parameter
632 decltype(auto) lpNorm( const DenseMatrix<MT,SO>& dm, ST p )
633 {
635 
636  BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
637 
638  using ScalarType = MultTrait_t< UnderlyingBuiltin_t<MT>, decltype( inv( p ) ) >;
639  return norm_backend( ~dm, Abs(), UnaryPow<ScalarType>( p ), UnaryPow<ScalarType>( inv( p ) ) );
640 }
641 //*************************************************************************************************
642 
643 
644 //*************************************************************************************************
663 template< size_t P // Compile time norm parameter
664  , typename MT // Type of the dense matrix
665  , bool SO > // Storage order
666 inline decltype(auto) lpNorm( const DenseMatrix<MT,SO>& dm )
667 {
668  BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
669 
671  using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
672 
673  return Norm()( ~dm );
674 }
675 //*************************************************************************************************
676 
677 
678 //*************************************************************************************************
693 template< typename MT // Type of the dense matrix
694  , bool SO > // Storage order
695 decltype(auto) maxNorm( const DenseMatrix<MT,SO>& dm )
696 {
698 
699  return max( abs( ~dm ) );
700 }
701 //*************************************************************************************************
702 
703 } // namespace blaze
704 
705 #endif
decltype(auto) sqrNorm(const DenseMatrix< MT, SO > &dm)
Computes the squared L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:496
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: FalseType.h:61
Header file for the Sqrt functor.
Header file for auxiliary alias declarations.
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_USER_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERT flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:117
Generic wrapper for the cbrt() function.
Definition: Cbrt.h:80
Generic wrapper for the squared abs() function.
Definition: SqrAbs.h:84
Header file for the HasLoad type trait.
Header file for basic type definitions.
decltype(auto) l3Norm(const DenseMatrix< MT, SO > &dm)
Computes the L3 norm for the given dense matrix.
Definition: DMatNormExpr.h:574
Header file for the Pow2 functor.
decltype(auto) l2Norm(const DenseMatrix< MT, SO > &dm)
Computes the L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:548
Header file for the FalseType type/value trait base class.
decltype(auto) l4Norm(const DenseMatrix< MT, SO > &dm)
Computes the L4 norm for the given dense matrix.
Definition: DMatNormExpr.h:600
Header file for the isZero shim.
Header file for the And class template.
Generic wrapper for the pow() function with fixed exponent.
Definition: Forward.h:129
decltype(auto) lpNorm(const DenseMatrix< MT, SO > &dm, ST p)
Computes the Lp norm for the given dense matrix.
Definition: DMatNormExpr.h:632
Header file for the invert shim.
Generic wrapper for the qdrt() function.
Definition: Qdrt.h:81
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
System settings for performance optimizations.
#define BLAZE_STATIC_ASSERT_MSG(expr, msg)
Compile time assertion macro.In case of an invalid compile time expression, a compilation error is cr...
Definition: StaticAssert.h:123
const MT::ResultType evaluate(const Matrix< MT, SO > &matrix)
Evaluates the given matrix expression.
Definition: Matrix.h:912
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:514
Header file for the Cbrt functor.
Header file for the LpNorm functor.
Base class for dense matrices.The DenseMatrix class is a base class for all dense matrix classes...
Definition: DenseMatrix.h:80
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:673
Generic wrapper for the abs() function.
Definition: Abs.h:82
IntegralConstant< bool, C > Bool
Compile time integral constant wrapper for bool.The Bool class template represents an integral wrappe...
Definition: Bool.h:70
Generic wrapper for the sqrt() function.
Definition: Sqrt.h:82
Header file for the multiplication trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the L4Norm functor.
Header file for the If class template.
Compile time assertion.
Header file for the SqrAbs functor.
Header file for the UnderlyingBuiltin type trait.
decltype(auto) l1Norm(const DenseMatrix< MT, SO > &dm)
Computes the L1 norm for the given dense matrix.
Definition: DMatNormExpr.h:522
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1147
decltype(auto) sum(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of addition.
Definition: DMatReduceExpr.h:2146
Header file for the HasSIMDAdd type trait.
Header file for the DenseMatrix base class.
decltype(auto) inv(const DenseMatrix< MT, SO > &dm)
Calculation of the inverse of the given dense matrix.
Definition: DMatInvExpr.h:423
Header file for all SIMD functionality.
Header file for the L2Norm functor.
Implementation of a type list.The TypeList class template represents a list of data types of arbitrar...
Definition: TypeList.h:119
Generic wrapper for the null function.
Definition: Noop.h:59
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:1179
Header file for the Abs functor.
Header file for the IsPadded type trait.
decltype(auto) abs(const DenseMatrix< MT, SO > &dm)
Applies the abs() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1152
decltype(auto) maxNorm(const DenseMatrix< MT, SO > &dm)
Computes the maximum norm for the given dense matrix.
Definition: DMatNormExpr.h:695
typename MultTrait< T1, T2 >::Type MultTrait_t
Auxiliary alias declaration for the MultTrait class template.The MultTrait_t alias declaration provid...
Definition: MultTrait.h:240
Header file for the evaluate shim.
Header file for the IsSIMDEnabled type trait.
Generic wrapper for the pow2() function.
Definition: Pow2.h:77
Header file for run time assertion macros.
Indexing a type list.The TypeAt class can be used to access a type list at a specified position to qu...
Definition: TypeAt.h:76
Header file for the Unique class template.
Header file for the UnaryPow functor.
constexpr bool IsPadded_v
Auxiliary variable template for the IsPadded type trait.The IsPadded_v variable template provides a c...
Definition: IsPadded.h:135
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:94
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Header file for the HasMember type traits.
Header file for the Noop functor.
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
Header file for the RemoveReference type trait.
decltype(auto) norm(const DenseMatrix< MT, SO > &dm)
Computes the L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:470
Generic wrapper for the pow3() function.
Definition: Pow3.h:77
Header file for the L3Norm functor.
Header file for the L1Norm functor.
Header file for the Qdrt functor.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the Bool class template.
Header file for the Pow3 functor.
Header file for the TrueType type/value trait base class.
Header file for the function trace functionality.