Beispiel #1
0
def SolveWeighted(A, B, D, lambd):
    AScale = El.DistSparseMatrix()
    El.Copy(A, AScale)
    El.Scale(lambd, AScale)
    AEmb = El.HCat(AScale, B)
    if display:
        El.Display(AEmb, "[lambda*A, B]")
    if output:
        El.Print(AEmb, "[lambda*A, B]")

    ctrl.alpha = baseAlpha
    if worldRank == 0:
        print "lambda=", lambd, ": ctrl.alpha=", ctrl.alpha
    XEmb = El.LeastSquares(AEmb, D, ctrl)

    X = XEmb[0:n0 * n1, 0:numRHS]
    Y = XEmb[n0 * n1:n0 * n1 + numColsB, 0:numRHS]
    El.Scale(lambd, X)

    YNorm = El.FrobeniusNorm(Y)
    if worldRank == 0:
        print "lambda=", lambd, ": || Y ||_F =", YNorm

    El.Copy(D, E)
    El.Multiply(El.NORMAL, -1., A, X, 1., E)
    El.Multiply(El.NORMAL, -1., B, Y, 1., E)
    residNorm = El.FrobeniusNorm(E)
    if worldRank == 0:
        print "lambda=", lambd, ": || D - A X - B Y ||_F / || D ||_F =", residNorm / DNorm
Beispiel #2
0
def ConcatFD2D(N0,N1):
  A = El.DistSparseMatrix(El.zTag)
  height = N0*N1
  width = 2*N0*N1
  A.Resize(height,width)
  localHeight = A.LocalHeight()
  A.Reserve(11*localHeight)
  for sLoc in xrange(localHeight):
    s = A.GlobalRow(sLoc)
    x0 = s % N0
    x1 = s / N0
    sRel = s + N0*N1

    A.QueueUpdate( s, s,    El.ComplexDouble(1,1) )
    A.QueueUpdate( s, sRel, El.ComplexDouble(20,2) )
    if x0 > 0:
      A.QueueUpdate( s, s-1,    El.ComplexDouble(-1,3) )
      A.QueueUpdate( s, sRel-1, El.ComplexDouble(-17,4) )
    if x0+1 < N0:
      A.QueueUpdate( s, s+1,    El.ComplexDouble(2,5) )
      A.QueueUpdate( s, sRel+1, El.ComplexDouble(-20,6) )
    if x1 > 0:
      A.QueueUpdate( s, s-N0,    El.ComplexDouble(-30,7) )
      A.QueueUpdate( s, sRel-N0, El.ComplexDouble(-3,8) )
    if x1+1 < N1:
      A.QueueUpdate( s, s+N0,    El.ComplexDouble(4,9) )
      A.QueueUpdate( s, sRel+N0, El.ComplexDouble(3,10) )

  A.ProcessLocalQueues()
  return A
Beispiel #3
0
def ConcatFD2D(N0, N1):
    A = El.DistSparseMatrix()
    height = N0 * N1
    width = 2 * N0 * N1
    A.Resize(height, width)
    localHeight = A.LocalHeight()
    A.Reserve(7 * localHeight)
    for sLoc in xrange(localHeight):
        s = A.GlobalRow(sLoc)
        x0 = s % N0
        x1 = s / N0

        # The finite-difference stencil
        A.QueueLocalUpdate(sLoc, s, 15)
        if x0 > 0:
            A.QueueLocalUpdate(sLoc, s - 1, -1)
        if x0 + 1 < N0:
            A.QueueLocalUpdate(sLoc, s + 1, 2)
        if x1 > 0:
            A.QueueLocalUpdate(sLoc, s - N0, -3)
        if x1 + 1 < N1:
            A.QueueLocalUpdate(sLoc, s + N0, 4)

        # The identity
        sRel = s + N0 * N1
        A.QueueLocalUpdate(sLoc, sRel, 1)

        # The dense last column
        A.QueueLocalUpdate(sLoc, width - 1, -10 / height)

    A.ProcessQueues()
    return A
Beispiel #4
0
def FD2D(N0, N1):
    A = El.DistSparseMatrix()
    height = N0 * N1
    width = N0 * N1
    A.Resize(height, width)
    localHeight = A.LocalHeight()
    A.Reserve(6 * localHeight)
    for sLoc in xrange(localHeight):
        s = A.GlobalRow(sLoc)
        x0 = s % N0
        x1 = s / N0
        A.QueueLocalUpdate(sLoc, s, 11)
        if x0 > 0:
            A.QueueLocalUpdate(sLoc, s - 1, -1)
        if x0 + 1 < N0:
            A.QueueLocalUpdate(sLoc, s + 1, 2)
        if x1 > 0:
            A.QueueLocalUpdate(sLoc, s - N0, -3)
        if x1 + 1 < N1:
            A.QueueLocalUpdate(sLoc, s + N0, 4)

        # The dense last column
        A.QueueLocalUpdate(sLoc, width - 1, -10 / height)

    A.ProcessQueues()
    return A
Beispiel #5
0
def ConcatFD2D(N0, N1):
    A = El.DistSparseMatrix()
    height = N0 * N1
    width = 2 * N0 * N1
    A.Resize(height, width)
    localHeight = A.LocalHeight()
    A.Reserve(11 * localHeight)
    for sLoc in xrange(localHeight):
        s = A.GlobalRow(sLoc)
        x0 = s % N0
        x1 = s / N0
        sRel = s + N0 * N1

        A.QueueUpdate(s, s, 11)
        A.QueueUpdate(s, sRel, -20)
        if x0 > 0:
            A.QueueUpdate(s, s - 1, -1)
            A.QueueUpdate(s, sRel - 1, -17)
        if x0 + 1 < N0:
            A.QueueUpdate(s, s + 1, 2)
            A.QueueUpdate(s, sRel + 1, -20)
        if x1 > 0:
            A.QueueUpdate(s, s - N0, -30)
            A.QueueUpdate(s, sRel - N0, -3)
        if x1 + 1 < N1:
            A.QueueUpdate(s, s + N0, 4)
            A.QueueUpdate(s, sRel + N0, 3)

        # The dense last column
        #A.QueueUpdate( s, width-1, -10/height );

    A.ProcessLocalQueues()
    return A
Beispiel #6
0
def Semidefinite(height):
  Q = El.DistSparseMatrix()
  Q.Resize(height,height)
  localHeight = Q.LocalHeight()
  Q.Reserve(localHeight)
  for sLoc in xrange(localHeight):
    s = Q.GlobalRow(sLoc)
    Q.QueueLocalUpdate( sLoc, s, 1 );

  Q.MakeConsistent()
  return Q
Beispiel #7
0
def Deriv(height):
    A = El.DistSparseMatrix()
    A.Resize(height - 1, height)
    localHeight = A.LocalHeight()
    A.Reserve(2 * localHeight)
    for iLoc in xrange(localHeight):
        i = A.GlobalRow(iLoc)
        A.QueueLocalUpdate(iLoc, i, 1.)
        A.QueueLocalUpdate(iLoc, i + 1, -1.)

    A.ProcessQueues()
    return A
Beispiel #8
0
def Constraints(numRows, N0, N1):
    B = El.DistSparseMatrix()
    El.Zeros(B, numRows, N0 * N1)
    localHeight = B.LocalHeight()
    B.Reserve(localHeight * N0 * N1)
    for sLoc in xrange(localHeight):
        s = B.GlobalRow(sLoc)
        for j in xrange(N0 * N1):
            B.QueueLocalUpdate(sLoc, j, random.uniform(0, 1))

    B.MakeConsistent()
    return B
Beispiel #9
0
def Constraints(numCols, N0, N1):
    B = El.DistSparseMatrix()
    El.Zeros(B, N0 * N1, numCols)
    localHeight = B.LocalHeight()
    B.Reserve(localHeight * numCols)
    for sLoc in xrange(localHeight):
        s = B.GlobalRow(sLoc)
        for j in xrange(numCols):
            B.QueueLocalUpdate(sLoc, j, random.uniform(0, 1))

    B.ProcessQueues()
    return B
Beispiel #10
0
def Rectang(height,width):
  A = El.DistSparseMatrix()
  A.Resize(height,width)
  localHeight = A.LocalHeight()
  A.Reserve(5*localHeight)
  for sLoc in xrange(localHeight):
    s = A.GlobalRow(sLoc)
    A.QueueLocalUpdate( sLoc, s%width, 11 )
    A.QueueLocalUpdate( sLoc, (s-1)%width, -1 )
    A.QueueLocalUpdate( sLoc, (s+1)%width,  2 )
    A.QueueLocalUpdate( sLoc, (s-height)%width, -3 )
    A.QueueLocalUpdate( sLoc, (s+height)%width,  4 )
    # The dense last column
    #A.QueueLocalUpdate( sLoc, width-1, -5/height );

  A.ProcessQueues()
  return A
Beispiel #11
0
def CreateFactor(height,width):
  F = El.DistSparseMatrix()
  El.Zeros( F, height, width )
  localHeight = F.LocalHeight()
  F.Reserve(localHeight*width)
  for iLoc in xrange(localHeight):
    i = F.GlobalRow(iLoc)
    for j in xrange(width):
      F.QueueLocalUpdate( iLoc, j, math.log(i+j+1.) )

  F.ProcessQueues()
  # NOTE: Without this rescaling, the problem is much harder, as the variables
  #       s becomes quite large 
  #       (|| F^T x ||_2 <= u, u^2 <= t, would imply u and t are large).
  FFrob = El.FrobeniusNorm( F )
  El.Scale( 1./FFrob, F )
  return F
Beispiel #12
0
def Laplacian(xSize, ySize):
    A = El.DistSparseMatrix(El.dTag)
    A.Resize(xSize * ySize, xSize * ySize)
    localHeight = A.LocalHeight()
    A.Reserve(5 * localHeight)
    hxInvSq = (1. * (xSize + 1))**2
    hyInvSq = (1. * (ySize + 1))**2
    for sLoc in xrange(localHeight):
        s = A.GlobalRow(sLoc)
        x = s % xSize
        y = s / xSize
        A.QueueLocalUpdate(sLoc, s, 2 * (hxInvSq + hyInvSq))
        if x != 0: A.QueueLocalUpdate(sLoc, s - 1, -hxInvSq)
        if x != xSize - 1: A.QueueLocalUpdate(sLoc, s + 1, -hxInvSq)
        if y != 0: A.QueueLocalUpdate(sLoc, s - xSize, -hyInvSq)
        if y != ySize - 1: A.QueueLocalUpdate(sLoc, s + xSize, -hyInvSq)

    A.ProcessQueues()
    return A
Beispiel #13
0
    def distr_mat(local_mat):
        """Converts the given matrix to a distributed sparse matrix.

        Parameters
        ----------
        local_mat : NumPy 2D array.

        Returns
        -------
        Elemental distributed sparse matrix.
        """
        import El
        local_mat = local_mat.tocoo()
        mat = El.DistSparseMatrix()
        mat.Resize(*local_mat.shape)
        mat.Reserve(len(local_mat.data))
        for val, i, j in zip(local_mat.data, local_mat.row.astype(int),
                             local_mat.col.astype(int)):
            mat.QueueUpdate(i, j, val)
        mat.ProcessQueues()
        return mat
Beispiel #14
0
def StackedFD2D(N0, N1):
    A = El.DistSparseMatrix()
    height = 2 * N0 * N1
    width = N0 * N1
    A.Resize(height, width)
    localHeight = A.LocalHeight()
    A.Reserve(6 * localHeight)
    for sLoc in xrange(localHeight):
        s = A.GlobalRow(sLoc)
        if s < N0 * N1:
            x0 = s % N0
            x1 = s / N0
            A.QueueLocalUpdate(sLoc, s, 11)
            if x0 > 0:
                A.QueueLocalUpdate(sLoc, s - 1, -10)
            if x0 + 1 < N0:
                A.QueueLocalUpdate(sLoc, s + 1, 20)
            if x1 > 0:
                A.QueueLocalUpdate(sLoc, s - N0, -30)
            if x1 + 1 < N1:
                A.QueueLocalUpdate(sLoc, s + N0, 40)
        else:
            sRel = s - N0 * N1
            x0 = sRel % N0
            x1 = sRel / N0
            A.QueueLocalUpdate(sLoc, sRel, -20)
            if x0 > 0:
                A.QueueLocalUpdate(sLoc, sRel - 1, -1)
            if x0 + 1 < N0:
                A.QueueLocalUpdate(sLoc, sRel + 1, -2)
            if x1 > 0:
                A.QueueLocalUpdate(sLoc, sRel - N0, -3)
            if x1 + 1 < N1:
                A.QueueLocalUpdate(sLoc, sRel + N0, 3)

        # The dense last column
        A.QueueLocalUpdate(sLoc, width - 1, -10 / height)

    A.MakeConsistent()
    return A
Beispiel #15
0
def StackedFD2D(N0, N1):
    A = El.DistSparseMatrix()
    height = 2 * N0 * N1
    width = N0 * N1
    A.Resize(height, width)
    localHeight = A.LocalHeight()
    A.Reserve(6 * localHeight)
    for sLoc in xrange(localHeight):
        s = A.GlobalRow(sLoc)
        if s < N0 * N1:
            x0 = s % N0
            x1 = s / N0
            A.QueueUpdate(s, s, 11, passive=True)
            if x0 > 0:
                A.QueueUpdate(s, s - 1, -10, passive=True)
            if x0 + 1 < N0:
                A.QueueUpdate(s, s + 1, 20, passive=True)
            if x1 > 0:
                A.QueueUpdate(s, s - N0, -30, passive=True)
            if x1 + 1 < N1:
                A.QueueUpdate(s, s + N0, 40, passive=True)
        else:
            sRel = s - N0 * N1
            x0 = sRel % N0
            x1 = sRel / N0
            A.QueueUpdate(s, sRel, -20, passive=True)
            if x0 > 0:
                A.QueueUpdate(s, sRel - 1, -1, passive=True)
            if x0 + 1 < N0:
                A.QueueUpdate(s, sRel + 1, -2, passive=True)
            if x1 > 0:
                A.QueueUpdate(s, sRel - N0, -3, passive=True)
            if x1 + 1 < N1:
                A.QueueUpdate(s, sRel + N0, 3, passive=True)

        # The dense last column
        A.QueueUpdate(s, width - 1, -10 / height, passive=True)

    A.ProcessQueues()
    return A
Beispiel #16
0
def StackedFD2D(N0,N1):
  A = El.DistSparseMatrix()
  height = 2*N0*N1
  width = N0*N1
  A.Resize(height,width)
  localHeight = A.LocalHeight()
  A.Reserve(6*localHeight)
  for sLoc in xrange(localHeight):
    s = A.GlobalRow(sLoc)
    if s < N0*N1:
      x0 = s % N0
      x1 = s / N0
      A.QueueLocalUpdate( sLoc, s, 11.1 )
      if x0 > 0:
        A.QueueLocalUpdate( sLoc, s-1, -1.2 )
      if x0+1 < N0:
        A.QueueLocalUpdate( sLoc, s+1, 2.3 )
      if x1 > 0:
        A.QueueLocalUpdate( sLoc, s-N0, -3.4 )
      if x1+1 < N1:
        A.QueueLocalUpdate( sLoc, s+N0, 4.5 )
    else:
      sRel = s-N0*N1
      x0 = sRel % N0
      x1 = sRel / N0
      A.QueueLocalUpdate( sLoc, sRel, -20.1 )
      if x0 > 0:
        A.QueueLocalUpdate( sLoc, sRel-1, -1.7 )
      if x0+1 < N0:
        A.QueueLocalUpdate( sLoc, sRel+1, -2.2 )
      if x1 > 0:
        A.QueueLocalUpdate( sLoc, sRel-N0, -3.3 )
      if x1+1 < N1:
        A.QueueLocalUpdate( sLoc, sRel+N0, 3.4 )

    # The dense last column
    A.QueueLocalUpdate( sLoc, width-1, -10/height );

  A.ProcessQueues()
  return A
Beispiel #17
0
def SolveWeighted(A,B,C,D,lambd):
  BScale = El.DistSparseMatrix()
  El.Copy( B, BScale )
  El.Scale( lambd, BScale )

  DScale = El.DistMultiVec()
  El.Copy( D, DScale ) 
  El.Scale( lambd, DScale )

  AEmb = El.VCat(A,BScale)
  CEmb = El.VCat(C,DScale)
  if output:
    El.Print( AEmb, "AEmb" )

  ctrl.alpha = baseAlpha
  if worldRank == 0:
    print('lambda={}, ctrl.alpha={}'.format(lambd,ctrl.alpha))
  X=El.LeastSquares(AEmb,CEmb,ctrl)

  El.Copy( C, E )
  El.Multiply( El.NORMAL, -1., A, X, 1., E )
  residNorm = El.FrobeniusNorm( E )
  if display:
    El.Display( E, "C - A X" )
  if output:
    El.Print( E, "C - A X" )
  if worldRank == 0:
    print('lambda={}: || C - A X ||_F / || C ||_F = {}'.format(lambd, \
      residNorm/CNorm))

  El.Copy( D, E )
  El.Multiply( El.NORMAL, -1., B, X, 1., E )
  equalNorm = El.FrobeniusNorm( E )
  if display:
    El.Display( E, "D - B X" )
  if output:
    El.Print( E, "D - B X" )
  if worldRank == 0:
    print('lambda={}: || D - B X ||_F / || D ||_F = {}'.format(lambd, \
      equalNorm/DNorm))
Beispiel #18
0
def Rectang(height, width):
    A = El.DistSparseMatrix()
    A.Resize(height, width)
    localHeight = A.LocalHeight()
    A.Reserve(5 * localHeight)
    for sLoc in xrange(localHeight):
        s = A.GlobalRow(sLoc)
        if s < width:
            A.QueueLocalUpdate(sLoc, s, 11)
        if s >= 1 and s - 1 < width:
            A.QueueLocalUpdate(sLoc, s - 1, -1)
        if s + 1 < width:
            A.QueueLocalUpdate(sLoc, s + 1, 2)
        if s >= height and s - height < width:
            A.QueueLocalUpdate(sLoc, s - height, -3)
        if s + height < width:
            A.QueueLocalUpdate(sLoc, s + height, 4)
        # The dense last column
        A.QueueLocalUpdate(sLoc, width - 1, -5 / height)

    A.MakeConsistent()
    return A
Beispiel #19
0
def Rectang(height, width):
    A = El.DistSparseMatrix()
    A.Resize(height, width)
    localHeight = A.LocalHeight()
    A.Reserve(5 * localHeight)
    for sLoc in xrange(localHeight):
        s = A.GlobalRow(sLoc)
        if s < width:
            A.QueueUpdate(s, s, 11, passive=True)
        if s >= 1 and s - 1 < width:
            A.QueueUpdate(s, s - 1, -1, passive=True)
        if s + 1 < width:
            A.QueueUpdate(s, s + 1, 2, passive=True)
        if s >= height and s - height < width:
            A.QueueUpdate(s, s - height, -3, passive=True)
        if s + height < width:
            A.QueueUpdate(s, s + height, 4, passive=True)
        # The dense last column
        A.QueueUpdate(s, width - 1, -5 / height, passive=True)

    A.ProcessQueues()
    return A
Beispiel #20
0
def RemoteStackedFD2D(N0, N1):
    A = El.DistSparseMatrix()
    height = 2 * N0 * N1
    width = N0 * N1
    A.Resize(height, width)
    customLocalHeight = (height / worldSize) + 1
    A.Reserve(6 * customLocalHeight, 6 * customLocalHeight)
    for s in xrange(worldRank, height, worldSize):
        if s < N0 * N1:
            x0 = s % N0
            x1 = s / N0
            A.QueueUpdate(s, s, 1)
            if x0 > 0:
                A.QueueUpdate(s, s - 1, -1)
            if x0 + 1 < N0:
                A.QueueUpdate(s, s + 1, 2)
            if x1 > 0:
                A.QueueUpdate(s, s - N0, -3)
            if x1 + 1 < N1:
                A.QueueUpdate(s, s + N0, 4)
        else:
            sRel = s - N0 * N1
            x0 = sRel % N0
            x1 = sRel / N0
            A.QueueUpdate(s, sRel, -2)
            if x0 > 0:
                A.QueueUpdate(s, sRel - 1, -1)
            if x0 + 1 < N0:
                A.QueueUpdate(s, sRel + 1, -2)
            if x1 > 0:
                A.QueueUpdate(s, sRel - N0, -3)
            if x1 + 1 < N1:
                A.QueueUpdate(s, sRel + N0, 3)

    A.ProcessQueues()
    return A
Beispiel #21
0
#
#  Copyright (c) 2009-2015, Jack Poulson
#  All rights reserved.
#
#  This file is part of Elemental and is under the BSD 2-Clause License,
#  which can be found in the LICENSE file in the root directory, or at
#  http://opensource.org/licenses/BSD-2-Clause
#
import El
import time

n = 30
A = El.DistSparseMatrix()
El.DynamicRegCounter(A, n)
El.Display(A, "A")

# Require the user to press a button before the figures are closed
worldSize = El.mpi.WorldSize()
El.Finalize()
if worldSize == 1:
    raw_input('Press Enter to exit')
Beispiel #22
0
h = El.DistMultiVec(El.dTag)
El.Zeros(h, 21, 1)
h.Set(3, 0, 1)
h.Set(4, 0, 1)
h.Set(6, 0, 1)
h.Set(7, 0, -1)
h.Set(11, 0, 2)
h.Set(12, 0, 1)
h.Set(13, 0, -1)

c = El.DistMultiVec(El.dTag)
El.Zeros(c, 8, 1)
c.Set(1, 0, 1)

G = El.DistSparseMatrix(El.dTag)
El.Zeros(G, 21, 8)
G.Reserve(28)
G.QueueUpdate(0, 3, -1, passive=True)
G.QueueUpdate(0, 4, -1, passive=True)
G.QueueUpdate(1, 3, -1, passive=True)
G.QueueUpdate(1, 4, 1, passive=True)
G.QueueUpdate(2, 2, -2, passive=True)
G.QueueUpdate(3, 5, -1, passive=True)
G.QueueUpdate(4, 5, 1, passive=True)
G.QueueUpdate(5, 4, -2, passive=True)
G.QueueUpdate(6, 1, -1, passive=True)
G.QueueUpdate(7, 1, -1, passive=True)
G.QueueUpdate(8, 6, -2, passive=True)
G.QueueUpdate(9, 6, -1, passive=True)
G.QueueUpdate(9, 7, -1, passive=True)