1 /*
2    Copyright (c) 2009-2014, Jack Poulson
3    All rights reserved.
4 
5    This file is part of Elemental and is under the BSD 2-Clause License,
6    which can be found in the LICENSE file in the root directory, or at
7    http://opensource.org/licenses/BSD-2-Clause
8 */
9 #pragma once
10 #ifndef ELEM_NORM_ZERO_HPP
11 #define ELEM_NORM_ZERO_HPP
12 
13 // The number of nonzeros in a matrix isn't really a norm...but it's useful
14 
15 namespace elem {
16 
17 template<typename F>
18 inline Int
ZeroNorm(const Matrix<F> & A)19 ZeroNorm( const Matrix<F>& A )
20 {
21     DEBUG_ONLY(CallStackEntry cse("ZeroNorm"))
22     Int numNonzeros = 0;
23     const Int height = A.Height();
24     const Int width = A.Width();
25     for( Int j=0; j<width; ++j )
26         for( Int i=0; i<height; ++i )
27             if( Abs(A.Get(i,j)) > 0 )
28                 ++numNonzeros;
29     return numNonzeros;
30 }
31 
32 template<typename F,Dist U,Dist V>
33 inline Int
ZeroNorm(const DistMatrix<F,U,V> & A)34 ZeroNorm( const DistMatrix<F,U,V>& A )
35 {
36     DEBUG_ONLY(CallStackEntry cse("ZeroNorm"))
37     Int numNonzeros;
38     if( A.Participating() )
39     {
40         const Int numLocalNonzeros = ZeroNorm( A.LockedMatrix() );
41         numNonzeros = mpi::AllReduce( numLocalNonzeros, A.DistComm() );
42     }
43     mpi::Broadcast( numNonzeros, A.Root(), A.CrossComm() );
44     return numNonzeros;
45 }
46 
47 } // namespace elem
48 
49 #endif // ifndef ELEM_NORM_ZERO_HPP
50