コード例 #1
0
ファイル: Gemv.cpp プロジェクト: elemental/Elemental
void TestGemv
( El::Int height,
  El::Int width,
  El::Orientation orientation,
  const El::Grid& grid,
  bool print )
{
    El::DistMatrix<Field> A(grid);
    El::Uniform( A, height, width );

    // Draw the entries of the original x and y from uniform distributions
    // over the complex unit ball
    El::DistMatrix<Field,El::VC,El::STAR> x(grid), y(grid);
    if( orientation == El::NORMAL )
    {
        El::Uniform( x, width, 1 );
        El::Uniform( y, height, 1 );
    }
    else
    {
        El::Uniform( x, height, 1 );
        El::Uniform( y, width, 1 );
    }

    if( print )
    {
        El::Print( A, "A" );
        El::Print( x, "x" );
        El::Print( y, "y" );
    }

    // Run the matrix-vector product
    if( grid.Rank() == 0 )
        El::Output("Starting Gemv (with Field=",El::TypeName<Field>(),")");
    El::Timer gemvElem;
    gemvElem.Start();
    // Form y := 3 A x + 4 y
    El::Gemv( orientation, Field(3), A, x, Field(4), y );
    gemvElem.Stop();
    if( grid.Rank() == 0 )
        El::Output("  Time: ",gemvElem.Total());

    if( print )
    {
        if( orientation == El::NORMAL )
            El::Print( y, "y := 3 A x + 4 y" );
        else
            El::Print( y, "y := 3 A^H x + 4 y" );
    }
}
コード例 #2
0
ファイル: Pseudoinverse.cpp プロジェクト: elemental/Elemental
int
main( int argc, char* argv[] )
{
    El::Environment env( argc, argv );
    El::mpi::Comm comm = El::mpi::COMM_WORLD;

    try
    {
        typedef double Real;
        typedef El::Complex<Real> Scalar;

        const El::Int m = El::Input("--height","height of matrix",100);
        const El::Int n = El::Input("--width","width of matrix",100);
        const bool print = El::Input("--print","print matrices?",false);
        El::ProcessInput();
        El::PrintInputReport();

        El::DistMatrix<Scalar> A;
        El::Uniform( A, m, n );

        El::Timer timer;
        // Compute the pseudoinverseof A (but do not overwrite A)
        El::DistMatrix<Scalar> pinvA( A );
        if( El::mpi::Rank(comm) == 0 )
            timer.Start();
        El::Pseudoinverse( pinvA );
        if( El::mpi::Rank(comm) == 0 )
            timer.Stop();
        if( print )
        {
            El::Print( A, "A" );
            El::Print( pinvA, "pinv(A)" );
        }

        const Real frobA = El::FrobeniusNorm( A );
        const Real frobPinvA = El::FrobeniusNorm( pinvA );

        if( El::mpi::Rank(comm) == 0 )
        {
            El::Output("PseudoInverse time: ",timer.Total()," secs");
            El::Output
            ("||   A     ||_F = ",frobA,"\n",
             "|| pinv(A) ||_F = ",frobPinvA,"\n");
        }
    }
    catch( std::exception& e ) { El::ReportException(e); }

    return 0;
}
コード例 #3
0
ファイル: ID.cpp プロジェクト: elemental/Elemental
int main( int argc, char* argv[] )
{
    El::Environment env( argc, argv );

    try
    {
        typedef double Real;
        typedef El::Complex<Real> Scalar;

        const El::Int m = El::Input("--height","height of matrix",20);
        const El::Int n = El::Input("--width","width of matrix",100);
        const El::Int r = El::Input("--rank","rank of matrix",5);
        const El::Int maxSteps =
          El::Input("--maxSteps","max # of steps of QR",10);
        const Real tol = El::Input("--tol","tolerance for ID",Real(-1));
        const bool print = El::Input("--print","print matrices?",false);
        const bool smallestFirst =
          El::Input("--smallestFirst","smallest norm first?",false);
        El::ProcessInput();
        El::PrintInputReport();

        El::mpi::Comm comm = El::mpi::COMM_WORLD;

        const El::Grid& grid = El::Grid::Default();
        El::DistMatrix<Scalar> U(grid), V(grid), A(grid);
        El::Uniform( U, m, r );
        El::Uniform( V, n, r );
        El::Gemm( El::NORMAL, El::ADJOINT, Scalar(1), U, V, A );
        const Real frobA = El::FrobeniusNorm( A );
        if( print )
            El::Print( A, "A" );

        El::DistPermutation Omega(grid);
        El::DistMatrix<Scalar,El::STAR,El::VR> Z(grid);
        El::QRCtrl<Real> ctrl;
        ctrl.boundRank = true;
        ctrl.maxRank = maxSteps;
        if( tol != -1. )
        {
            ctrl.adaptive = true;
            ctrl.tol = tol;
        }
        ctrl.smallestFirst = smallestFirst;
        El::Timer timer;
        if( El::mpi::Rank(comm) == 0 )
            timer.Start();
        El::ID( A, Omega, Z, ctrl );
        if( El::mpi::Rank(comm) == 0 )
            timer.Stop();
        const El::Int rank = Z.Height();
        if( print )
        {
            El::DistMatrix<El::Int> OmegaFull(grid);
            Omega.ExplicitMatrix( OmegaFull );
            El::Print( OmegaFull, "Omega" );
            El::Print( Z, "Z" );
        }

        // Pivot A and form the matrix of its (hopefully) dominant columns
        El::Timer permTimer("permTimer");
        permTimer.Start();
        Omega.PermuteCols( A );
        permTimer.Stop();

        auto hatA( A );
        hatA.Resize( m, rank );
        if( print )
        {
            El::Print( A, "A Omega^T" );
            El::Print( hatA, "\\hat{A}" );
        }

        // Check || A Omega^T - \hat{A} [I, Z] ||_F / || A ||_F
        El::DistMatrix<Scalar> AL(grid), AR(grid);
        El::PartitionRight( A, AL, AR, rank );
        El::Zero( AL );
        {
            El::DistMatrix<Scalar,El::MC,El::STAR> hatA_MC_STAR(grid);
            El::DistMatrix<Scalar,El::STAR,El::MR> Z_STAR_MR(grid);
            hatA_MC_STAR.AlignWith( AR );
            Z_STAR_MR.AlignWith( AR );
            hatA_MC_STAR = hatA;
            Z_STAR_MR = Z;
            El::LocalGemm
            ( El::NORMAL, El::NORMAL,
              Scalar(-1), hatA_MC_STAR, Z_STAR_MR, Scalar(1), AR );
        }
        const Real frobError = El::FrobeniusNorm( A );
        if( print )
            El::Print( A, "A Omega^T - \\hat{A} [I, Z]" );

        if( El::mpi::Rank(comm) == 0 )
        {
            El::Output("  ID time: ",timer.Total()," secs");
            El::Output
            ("|| A ||_F = ",frobA,"\n",
             "|| A Omega^T - \\hat{A} [I, Z] ||_F / || A ||_F = ",
             frobError/frobA);
        }
    }
    catch( std::exception& e ) { El::ReportException(e); }

    return 0;
}
コード例 #4
0
ファイル: QPBox.cpp プロジェクト: elemental/Elemental
int main( int argc, char* argv[] )
{
    El::Environment env( argc, argv );

    try
    {
        const El::Int n = El::Input("--n","problem size",200);
        const El::Int maxIter =
          El::Input("--maxIter","maximum # of iter's",500);
        const Real lb = El::Input("--lb","lower bound for x",0.5);
        const Real ub = El::Input("--ub","upper bound for x",1.0);
        const Real lbEig = El::Input("--lbEig","spectral lower bound",1.);
        const Real ubEig = El::Input("--ubEig","spectral upper bound",2.);
        const Real rho = El::Input("--rho","augmented Lagrangian param.",1.);
        const Real alpha = El::Input("--alpha","over-relaxation",1.2);
        const Real absTol = El::Input("--absTol","absolute tolerance",1e-6);
        const Real relTol = El::Input("--relTol","relative tolerance",1e-4);
        const bool inv = El::Input("--inv","form inv(LU) to avoid trsv?",true);
        const bool progress = El::Input("--progress","print progress?",true);
        const bool display = El::Input("--display","display matrices?",false);
        const bool print = El::Input("--print","print matrices",false);
        El::ProcessInput();
        El::PrintInputReport();

        El::ADMMCtrl<Real> ctrl;
        ctrl.rho = rho;
        ctrl.alpha = alpha;
        ctrl.maxIter = maxIter;
        ctrl.absTol = absTol;
        ctrl.relTol = relTol;
        ctrl.inv = inv;
        ctrl.print = progress;

        El::DistMatrix<Real> Q, c, xTrue;
        El::HermitianUniformSpectrum( Q, n, lbEig, ubEig );
        // Alternate the entries of xTrue between ub and lb
        El::Zeros( xTrue, n, 1 );
        if( xTrue.LocalWidth() == 1 )
            for( El::Int iLoc=0; iLoc<xTrue.LocalHeight(); ++iLoc )
                xTrue.SetLocal( iLoc, 0,
                    ( xTrue.GlobalRow(iLoc)%2==0 ? lb : ub ) );
        // Set c := - Q xTrue - du + dl
        El::Zeros( c, n, 1 );
        El::Hemv( El::LOWER, Real(-1), Q, xTrue, Real(0), c );
        if( c.LocalWidth() == 1 )
            for( El::Int iLoc=0; iLoc<c.LocalHeight(); ++iLoc )
                c.UpdateLocal( iLoc, 0,
                    ( c.GlobalRow(iLoc)%2==0 ? 0.5 : -0.5 ) );
        if( print )
        {
            El::Print( Q, "Q" );
            El::Print( c, "c" );
            El::Print( xTrue, "xTrue" );
        }
        if( display )
            El::Display( Q, "Q" );

        El::Timer timer;
        El::DistMatrix<Real> z;
        if( El::mpi::Rank() == 0 )
            timer.Start();
        El::qp::box::ADMM( Q, c, lb, ub, z, ctrl );
        if( El::mpi::Rank() == 0 )
            timer.Stop();

        if( print )
            El::Print( z, "z" );
        if( El::mpi::Rank() == 0 )
            El::Output("QPBox time: ",timer.Total(),"secs");
    }
    catch( std::exception& e ) { El::ReportException(e); }

    return 0;
}
コード例 #5
0
ファイル: DS.cpp プロジェクト: elemental/Elemental
int
main( int argc, char* argv[] )
{
    El::Environment env( argc, argv );

    try
    {
        const El::Int m = El::Input("--m","height of matrix",100);
        const El::Int n = El::Input("--n","width of matrix",200);
        // TODO(poulson): Add options for controlling IPM
        const El::Int maxIter =
          El::Input("--maxIter","maximum # of iter's",500);
        const Real lambda = El::Input("--lambda","DS parameter",0.5);
        const bool display = El::Input("--display","display matrices?",false);
        const bool print = El::Input("--print","print matrices",false);
        El::ProcessInput();
        El::PrintInputReport();

        El::SparseMatrix<Real> A;
        El::Matrix<Real> b, xTrue;
        El::Identity( A, m, n );
        El::Uniform( b, m, 1 );
        if( print )
        {
            El::Print( A, "A" );
            El::Print( b, "b" );
        }
        if( display )
            El::Display( A, "A" );

        El::lp::affine::Ctrl<Real> affineCtrl; 
        affineCtrl.mehrotraCtrl.print = true;

        El::Matrix<Real> x;
        El::Timer timer;
        if( El::mpi::Rank() == 0 )
            timer.Start();
        El::DS( A, b, lambda, x, affineCtrl );
        if( El::mpi::Rank() == 0 )
            timer.Stop();
        if( print )
            El::Print( x, "x" );
        const El::Int xZeroNorm = El::ZeroNorm( x );
        if( El::mpi::Rank() == 0 )
        {
            El::Output("Dantzig Selector time: ",timer.Total()," secs");
            El::Output("|| x ||_0 = ",xZeroNorm);
        }
        SoftThreshold( x, El::Sqrt(El::limits::Epsilon<Real>()) );
        if( print )
            El::Print( x, "xThresh" );
        const El::Int xZeroNormThresh = El::ZeroNorm( x );
        if( El::mpi::Rank() == 0 )
        {
            El::Output("|| xThresh ||_0 = ",xZeroNormThresh);
        }
    }
    catch( std::exception& e ) { El::ReportException(e); }

    return 0;
}
コード例 #6
0
ファイル: BusingerGolub.cpp プロジェクト: elemental/Elemental
int
main( int argc, char* argv[] )
{
    El::Environment env( argc, argv );

    try
    {
        const El::Int n = El::Input("--size","width of matrix",100);
        const bool display = El::Input("--display","display matrices?",false);
        const bool print = El::Input("--print","print matrices?",false);
        const bool smallestFirst =
          El::Input("--smallestFirst","smallest norm first?",false);
        El::ProcessInput();
        El::PrintInputReport();
        const El::Int m = n;

        El::QRCtrl<double> ctrl;
        ctrl.smallestFirst = smallestFirst;

        El::DistMatrix<El::Complex<double>> A;
        El::GKS( A, n );
        const double frobA = El::FrobeniusNorm( A );
        if( display )
            El::Display( A, "A" );
        if( print )
            El::Print( A, "A" );

        // Compute the pivoted QR decomposition of A, but do not overwrite A
        auto QRPiv( A );
        El::DistMatrix<El::Complex<double>> householderScalarsPiv;
        El::DistMatrix<double> signaturePiv;
        El::DistPermutation Omega;
        El::Timer PQRtimer;
        if( El::mpi::Rank() == 0 )
            PQRtimer.Start();
        El::QR( QRPiv, householderScalarsPiv, signaturePiv, Omega );
        if( El::mpi::Rank() == 0 )
            PQRtimer.Stop();
        if( display )
        {
            El::Display( QRPiv, "QRPiv" );
            El::Display( householderScalarsPiv, "householderScalarsPiv" );
            El::Display( signaturePiv, "signaturePiv" );

            El::DistMatrix<El::Int> OmegaFull;
            Omega.ExplicitMatrix( OmegaFull );
            El::Display( OmegaFull, "Omega" );
        }
        if( print )
        {
            El::Print( QRPiv, "QRPiv" );
            El::Print( householderScalarsPiv, "householderScalarsPiv" );
            El::Print( signaturePiv, "signaturePiv" );

            El::DistMatrix<El::Int> OmegaFull;
            Omega.ExplicitMatrix( OmegaFull );
            El::Print( OmegaFull, "Omega" );
        }

        // Compute the standard QR decomposition of A
        auto QRNoPiv( A );
        El::DistMatrix<El::Complex<double>> householderScalarsNoPiv;
        El::DistMatrix<double> signatureNoPiv;
        El::Timer QRtimer;
        if( El::mpi::Rank() == 0 )
            QRtimer.Start();
        El::QR( QRNoPiv, householderScalarsNoPiv, signatureNoPiv );
        if( El::mpi::Rank() == 0 )
            QRtimer.Start();
        if( display )
        {
            El::Display( QRNoPiv, "QRNoPiv" );
            El::Display( householderScalarsNoPiv, "householderScalarsNoPiv" );
            El::Display( signatureNoPiv, "signatureNoPiv" );
        }
        if( print )
        {
            El::Print( QRNoPiv, "QRNoPiv" );
            El::Print( householderScalarsNoPiv, "householderScalarsNoPiv" );
            El::Print( signatureNoPiv, "signatureNoPiv" );
        }

        // Check the error in the pivoted QR factorization,
        // || A Omega^T - Q R ||_F / || A ||_F
        auto E( QRPiv );
        El::MakeTrapezoidal( El::UPPER, E );
        El::qr::ApplyQ
        ( El::LEFT, El::NORMAL, QRPiv, householderScalarsPiv, signaturePiv, E );
        Omega.InversePermuteCols( E );
        E -= A;
        const double frobQRPiv = El::FrobeniusNorm( E );
        if( display )
            El::Display( E, "A P - Q R" );
        if( print )
            El::Print( E, "A P - Q R" );

        // Check the error in the standard QR factorization,
        // || A - Q R ||_F / || A ||_F
        E = QRNoPiv;
        El::MakeTrapezoidal( El::UPPER, E );
        El::qr::ApplyQ
        ( El::LEFT, El::NORMAL,
          QRNoPiv, householderScalarsNoPiv, signatureNoPiv, E );
        E -= A;
        const double frobQRNoPiv = El::FrobeniusNorm( E );
        if( display )
            El::Display( E, "A - Q R" );
        if( print )
            El::Print( E, "A - Q R" );

        // Check orthogonality of pivoted Q, || I - Q^H Q ||_F / || A ||_F
        El::Identity( E, m, n );
        El::qr::ApplyQ
        ( El::LEFT, El::NORMAL,
          QRPiv, householderScalarsPiv, signaturePiv, E );
        El::qr::ApplyQ
        ( El::LEFT, El::ADJOINT,
          QRPiv, householderScalarsPiv, signaturePiv, E );
        const El::Int k = El::Min(m,n);
        auto EUpper = E( El::IR(0,k), El::IR(0,k) );
        El::ShiftDiagonal( EUpper, El::Complex<double>(-1) );
        const double frobOrthogPiv = El::FrobeniusNorm( EUpper );
        if( display )
            El::Display( E, "pivoted I - Q^H Q" );
        if( print )
            El::Print( E, "pivoted I - Q^H Q" );

        // Check orthogonality of unpivoted Q, || I - Q^H Q ||_F / || A ||_F
        El::Identity( E, m, n );
        El::qr::ApplyQ
        ( El::LEFT, El::NORMAL,
          QRPiv, householderScalarsPiv, signaturePiv, E );
        El::qr::ApplyQ
        ( El::LEFT, El::ADJOINT,
          QRPiv, householderScalarsPiv, signaturePiv, E );
        EUpper = E( El::IR(0,k), El::IR(0,k) );
        El::ShiftDiagonal( EUpper, El::Complex<double>(-1) );
        const double frobOrthogNoPiv = El::FrobeniusNorm( EUpper );
        if( display )
            El::Display( E, "unpivoted I - Q^H Q" );
        if( print )
            El::Print( E, "unpivoted I - Q^H Q" );

        if( El::mpi::Rank() == 0 )
        {
            El::Output("Pivot QR time: ",PQRtimer.Total()," secs\n",
                       "      QR time: ",QRtimer.Total()," secs");
            El::Output
            ("|| A ||_F = ",frobA,"\n\n",
             "With pivoting: \n",
             "    || A P - Q R ||_F / || A ||_F = ",frobQRPiv/frobA,"\n",
             "    || I - Q^H Q ||_F / || A ||_F = ",frobOrthogPiv/frobA,"\n\n",
             "Without pivoting: \n",
             "    || A - Q R ||_F / || A ||_F = ",frobQRNoPiv/frobA,"\n",
             "    || I - Q^H Q ||_F / || A ||_F = ",frobOrthogNoPiv/frobA,"\n");
        }
    }
    catch( std::exception& e ) { El::ReportException(e); }

    return 0;
}
コード例 #7
0
ファイル: BasisPursuit.cpp プロジェクト: elemental/Elemental
int
main( int argc, char* argv[] )
{
    El::Environment env( argc, argv );

    try
    {
        const El::Int m = El::Input("--m","height of matrix",100);
        const El::Int n = El::Input("--n","width of matrix",200);
        const bool useIPM = El::Input("--useIPM","use Interior Point?",true);
        // TODO(poulson): Add options for controlling IPM
        const El::Int maxIter =
          El::Input("--maxIter","maximum # of iter's",500);
        const Real rho = El::Input("--rho","augmented Lagrangian param.",1.);
        const Real alpha = El::Input("--alpha","over-relaxation",1.2);
        const Real absTol = El::Input("--absTol","absolute tolerance",1e-6);
        const Real relTol = El::Input("--relTol","relative tolerance",1e-4);
        const Real pinvTol = El::Input("--pinvTol","pseudoinv tolerance",0.);
        const bool usePinv =
          El::Input("--usePinv","Directly compute pinv(A)",true);
        const bool progress = El::Input("--progress","print progress?",true);
        const bool display = El::Input("--display","display matrices?",false);
        const bool print = El::Input("--print","print matrices",false);
        El::ProcessInput();
        El::PrintInputReport();

        El::DistMatrix<Real> A, b, xTrue;
        El::Uniform( A, m, n );
        El::Uniform( b, m, 1 );
        if( print )
        {
            El::Print( A, "A" );
            El::Print( b, "b" );
        }
        if( display )
            El::Display( A, "A" );

        const bool sparse = false;
        El::BPCtrl<Real> ctrl(sparse);
        ctrl.useIPM = useIPM;
        ctrl.admmCtrl.rho = rho;
        ctrl.admmCtrl.alpha = alpha;
        ctrl.admmCtrl.maxIter = maxIter;
        ctrl.admmCtrl.absTol = absTol;
        ctrl.admmCtrl.relTol = relTol;
        ctrl.admmCtrl.usePinv = usePinv;
        ctrl.admmCtrl.pinvTol = pinvTol;
        ctrl.admmCtrl.progress = progress;
        ctrl.lpIPMCtrl.mehrotraCtrl.print = true;

        El::DistMatrix<Real> x;
        El::Timer timer;
        if( El::mpi::Rank() == 0 )
            timer.Start();
        El::BP( A, b, x, ctrl );
        if( El::mpi::Rank() == 0 )
            timer.Stop();
        if( print )
            El::Print( x, "x" );
        const El::Int xZeroNorm = El::ZeroNorm( x );
        if( El::mpi::Rank() == 0 )
        {
            El::Output("Basis Pursuit time: ",timer.Total()," secs");
            El::Output("|| x ||_0 = ",xZeroNorm);
        }
        SoftThreshold( x, El::Sqrt(El::limits::Epsilon<Real>()) );
        if( print )
            El::Print( x, "xThresh" );
        const El::Int xZeroNormThresh = El::ZeroNorm( x );
        if( El::mpi::Rank() == 0 )
        {
            El::Output("|| xThresh ||_0 = ",xZeroNormThresh);
        }
    }
    catch( std::exception& e ) { El::ReportException(e); }

    return 0;
}