void Apply ( const DistMultiVec<Real>& x, const DistMultiVec<Real>& y, DistMultiVec<Real>& z, const DistMultiVec<Int>& orders, const DistMultiVec<Int>& firstInds, Int cutoff ) { DEBUG_ONLY(CSE cse("soc::Apply")) soc::Dots( x, y, z, orders, firstInds ); auto xRoots = x; auto yRoots = y; cone::Broadcast( xRoots, orders, firstInds ); cone::Broadcast( yRoots, orders, firstInds ); const Int firstLocalRow = x.FirstLocalRow(); const Int localHeight = x.LocalHeight(); const Real* xBuf = x.LockedMatrix().LockedBuffer(); const Real* xRootBuf = xRoots.LockedMatrix().LockedBuffer(); const Real* yBuf = y.LockedMatrix().LockedBuffer(); const Real* yRootBuf = yRoots.LockedMatrix().LockedBuffer(); Real* zBuf = z.Matrix().Buffer(); const Int* firstIndBuf = firstInds.LockedMatrix().LockedBuffer(); for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = iLoc + firstLocalRow; const Int firstInd = firstIndBuf[iLoc]; if( i != firstInd ) zBuf[iLoc] += xRootBuf[iLoc]*yBuf[iLoc] + yRootBuf[iLoc]*xBuf[iLoc]; } }
Int NumOutside( const DistMultiVec<Real>& A ) { EL_DEBUG_CSE const Int localHeight = A.LocalHeight(); const Int width = A.Width(); const Real* ABuf = A.LockedMatrix().LockedBuffer(); const Int ALDim = A.LockedMatrix().LDim(); Int numLocalNonPos = 0; for( Int iLoc=0; iLoc<localHeight; ++iLoc ) for( Int j=0; j<width; ++j ) if( ABuf[iLoc+j*ALDim] <= Real(0) ) ++numLocalNonPos; return mpi::AllReduce( numLocalNonPos, A.Comm() ); }
void RowMaxNorms( const DistMultiVec<F>& A, DistMultiVec<Base<F>>& norms ) { DEBUG_CSE norms.SetComm( A.Comm() ); norms.Resize( A.Height(), 1 ); RowMaxNorms( A.LockedMatrix(), norms.Matrix() ); }
void NesterovTodd ( const DistMultiVec<Real>& s, const DistMultiVec<Real>& z, DistMultiVec<Real>& w ) { DEBUG_CSE w.SetComm( s.Comm() ); w.Resize( s.Height(), 1 ); const Real* sBuf = s.LockedMatrix().LockedBuffer(); const Real* zBuf = z.LockedMatrix().LockedBuffer(); Real* wBuf = w.Matrix().Buffer(); const Int localHeight = w.LocalHeight(); for( Int iLoc=0; iLoc<localHeight; ++iLoc ) wBuf[iLoc] = Sqrt(sBuf[iLoc]/zBuf[iLoc]); }
void EntrywiseMap ( const DistMultiVec<S>& A, DistMultiVec<T>& B, function<T(S)> func ) { DEBUG_CSE B.SetComm( A.Comm() ); B.Resize( A.Height(), A.Width() ); EntrywiseMap( A.LockedMatrix(), B.Matrix(), func ); }
Real MaxStep ( const DistMultiVec<Real>& s, const DistMultiVec<Real>& ds, Real upperBound ) { EL_DEBUG_CSE const Int kLocal = s.LocalHeight(); const Real* sBuf = s.LockedMatrix().LockedBuffer(); const Real* dsBuf = ds.LockedMatrix().LockedBuffer(); Real alpha = upperBound; for( Int iLoc=0; iLoc<kLocal; ++iLoc ) { const Real si = sBuf[iLoc]; const Real dsi = dsBuf[iLoc]; if( dsi < Real(0) ) alpha = Min(alpha,-si/dsi); } return mpi::AllReduce( alpha, mpi::MIN, s.Comm() ); }
Real ComplementRatio ( const DistMultiVec<Real>& s, const DistMultiVec<Real>& z ) { DEBUG_CSE const Int localHeight = s.LocalHeight(); const Real* sBuf = s.LockedMatrix().LockedBuffer(); const Real* zBuf = z.LockedMatrix().LockedBuffer(); Real maxLocProd = 0; for( Int iLoc=0; iLoc<localHeight; ++iLoc ) maxLocProd = Max( sBuf[iLoc]*zBuf[iLoc], maxLocProd ); const Real maxProd = mpi::AllReduce( maxLocProd, mpi::MAX, s.Comm() ); Real minLocProd = maxProd; for( Int iLoc=0; iLoc<localHeight; ++iLoc ) minLocProd = Min( sBuf[iLoc]*zBuf[iLoc], minLocProd ); const Real minProd = mpi::AllReduce( minLocProd, mpi::MIN, s.Comm() ); return maxProd/minProd; }
Int Degree( const DistMultiVec<Int>& firstInds ) { DEBUG_CSE Int localDegree = 0; const Int localHeight = firstInds.LocalHeight(); auto& firstIndsLoc = firstInds.LockedMatrix(); for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = firstInds.GlobalRow(iLoc); if( i == firstIndsLoc(iLoc) ) ++localDegree; } return mpi::AllReduce( localDegree, firstInds.Comm() ); }
void QP ( const DistSparseMatrix<Real>& A, const DistMultiVec<Real>& B, DistMultiVec<Real>& X, const qp::direct::Ctrl<Real>& ctrl ) { DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); const Int k = B.Width(); mpi::Comm comm = A.Comm(); DistSparseMatrix<Real> Q(comm), AHat(comm); DistMultiVec<Real> bHat(comm), c(comm); Herk( LOWER, ADJOINT, Real(1), A, Q ); MakeHermitian( LOWER, Q ); Zeros( AHat, 0, n ); Zeros( bHat, 0, 1 ); Zeros( X, n, k ); DistMultiVec<Real> q(comm), y(comm), z(comm); auto& qLoc = q.Matrix(); auto& XLoc = X.Matrix(); auto& BLoc = B.LockedMatrix(); for( Int j=0; j<k; ++j ) { auto xLoc = XLoc( ALL, IR(j) ); auto bLoc = BLoc( ALL, IR(j) ); Zeros( c, n, 1 ); Zeros( q, m, 1 ); qLoc = bLoc; Multiply( ADJOINT, Real(-1), A, q, Real(0), c ); Zeros( q, n, 1 ); qLoc = xLoc; El::QP( Q, AHat, bHat, c, q, y, z, ctrl ); xLoc = qLoc; } }
void IPM ( const DistSparseMatrix<Real>& A, const DistMultiVec<Real>& d, Real lambda, DistMultiVec<Real>& x, const qp::affine::Ctrl<Real>& ctrl ) { EL_DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); mpi::Comm comm = A.Comm(); DistSparseMatrix<Real> Q(comm), AHat(comm), G(comm); DistMultiVec<Real> c(comm), b(comm), h(comm); auto& dLoc = d.LockedMatrix(); auto& cLoc = c.Matrix(); auto& hLoc = h.Matrix(); // Q := | I 0 0 | // | 0 0 0 | // | 0 0 0 | // ============== Zeros( Q, n+m+1, n+m+1 ); { // Count the number of local entries in the top-left I // --------------------------------------------------- Int numLocalUpdates = 0; for( Int iLoc=0; iLoc<Q.LocalHeight(); ++iLoc ) if( Q.GlobalRow(iLoc) < n ) ++numLocalUpdates; else break; Q.Reserve( numLocalUpdates ); for( Int iLoc=0; iLoc<Q.LocalHeight(); ++iLoc ) if( Q.GlobalRow(iLoc) < n ) Q.QueueLocalUpdate( iLoc, Q.GlobalRow(iLoc), Real(1) ); Q.ProcessLocalQueues(); } // c := [0;0;lambda] // ================= Zeros( c, n+m+1, 1 ); for( Int iLoc=0; iLoc<c.LocalHeight(); ++iLoc ) if( c.GlobalRow(iLoc) > n ) cLoc(iLoc) = lambda; // AHat = [] // ========= Zeros( AHat, 0, n+m+1 ); // b = [] // ====== Zeros( b, 0, 1 ); // G := |-diag(d) A, -d, -I| // | 0, 0, -I| // ========================= Zeros( G, 2*m, n+m+1 ); G.Reserve ( A.NumLocalEntries()+d.LocalHeight()+G.LocalHeight(), A.NumLocalEntries()+d.LocalHeight() ); for( Int e=0; e<A.NumLocalEntries(); ++e ) { const Int i = A.Row(e); const Int j = A.Col(e); const Int iLoc = A.LocalRow(i); const Real value = -dLoc(iLoc)*A.Value(e); G.QueueUpdate( i, j, value ); } for( Int iLoc=0; iLoc<d.LocalHeight(); ++iLoc ) { const Int i = d.GlobalRow(iLoc); G.QueueUpdate( i, n, -dLoc(iLoc) ); } for( Int iLoc=0; iLoc<G.LocalHeight(); ++iLoc ) { const Int i = G.GlobalRow(iLoc); if( i < m ) G.QueueLocalUpdate( iLoc, i+n+1, Real(-1) ); else G.QueueLocalUpdate( iLoc, (i-m)+n+1, Real(-1) ); } G.ProcessQueues(); // h := [-ones(m,1); zeros(m,1)] // ============================= Zeros( h, 2*m, 1 ); for( Int iLoc=0; iLoc<h.LocalHeight(); ++iLoc ) if( h.GlobalRow(iLoc) < m ) hLoc(iLoc) = Real(-1); else break; // Solve the affine QP // =================== DistMultiVec<Real> y(comm), z(comm), s(comm); QP( Q, AHat, G, b, c, h, x, y, z, s, ctrl ); }
void RLS ( const DistSparseMatrix<Real>& A, const DistMultiVec<Real>& b, Real rho, DistMultiVec<Real>& x, const socp::affine::Ctrl<Real>& ctrl ) { DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); mpi::Comm comm = A.Comm(); DistMultiVec<Int> orders(comm), firstInds(comm); Zeros( orders, m+n+3, 1 ); Zeros( firstInds, m+n+3, 1 ); { const Int localHeight = orders.LocalHeight(); for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = orders.GlobalRow(iLoc); if( i < m+1 ) { orders.SetLocal( iLoc, 0, m+1 ); firstInds.SetLocal( iLoc, 0, 0 ); } else { orders.SetLocal( iLoc, 0, n+2 ); firstInds.SetLocal( iLoc, 0, m+1 ); } } } // G := | -1 0 0 | // | 0 0 A | // | 0 -1 0 | // | 0 0 -I | // | 0 0 0 | DistSparseMatrix<Real> G(comm); { Zeros( G, m+n+3, n+2 ); // Count the number of entries of G to reserve // ------------------------------------------- Int numLocalUpdates = 0; const Int localHeight = G.LocalHeight(); for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = G.GlobalRow(iLoc); if( i == 0 || i == m+1 || (i>m+1 && i<m+n+2) ) ++numLocalUpdates; } const Int numEntriesA = A.NumLocalEntries(); G.Reserve( numLocalUpdates+numEntriesA, numEntriesA ); // Queue the local updates // ----------------------- for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = G.GlobalRow(iLoc); if( i == 0 ) G.QueueLocalUpdate( iLoc, 0, -1 ); else if( i == m+1 ) G.QueueLocalUpdate( iLoc, 1, -1 ); else if( i > m+1 && i < m+n+2 ) G.QueueLocalUpdate( iLoc, i-m, -1 ); } // Queue the remote updates // ------------------------ for( Int e=0; e<numEntriesA; ++e ) G.QueueUpdate( A.Row(e)+1, A.Col(e)+2, A.Value(e) ); G.ProcessQueues(); } // h := | 0 | // | b | // | 0 | // | 0 | // | 1 | DistMultiVec<Real> h(comm); Zeros( h, m+n+3, 1 ); auto& bLoc = b.LockedMatrix(); { const Int bLocalHeight = b.LocalHeight(); h.Reserve( bLocalHeight ); for( Int iLoc=0; iLoc<bLocalHeight; ++iLoc ) h.QueueUpdate( b.GlobalRow(iLoc)+1, 0, bLoc(iLoc) ); h.ProcessQueues(); } h.Set( END, 0, 1 ); // c := [1; rho; 0] DistMultiVec<Real> c(comm); Zeros( c, n+2, 1 ); c.Set( 0, 0, 1 ); c.Set( 1, 0, rho ); DistSparseMatrix<Real> AHat(comm); Zeros( AHat, 0, n+2 ); DistMultiVec<Real> bHat(comm); Zeros( bHat, 0, 1 ); DistMultiVec<Real> xHat(comm), y(comm), z(comm), s(comm); SOCP( AHat, G, bHat, c, h, orders, firstInds, xHat, y, z, s, ctrl ); x = xHat( IR(2,END), ALL ); }