analyze Subroutine

private subroutine analyze(me, m, n, irow, icol, istat)

Symbolic analysis of the sparsity pattern of A. This only has to be called once for a given sparsity pattern.

Forms the graph of , computes a reverse Cuthill-McKee ordering of the rows, and allocates the envelope storage for the Cholesky factor.

Note

Duplicate (irow,icol) entries are allowed (they are summed).

Type Bound

cholesky_minnorm_solver

Arguments

Type IntentOptional Attributes Name
class(cholesky_minnorm_solver), intent(inout) :: me
integer, intent(in) :: m

number of rows in A

integer, intent(in) :: n

number of columns in A

integer, intent(in), dimension(:) :: irow

row indices of the nonzero elements of A

integer, intent(in), dimension(:) :: icol

column indices of the nonzero elements of A (same size as irow)

integer, intent(out) :: istat

status code (CHOLESKY_SUCCESS if no errors)


Calls

proc~~analyze~~CallsGraph proc~analyze cholesky_minnorm_solver%analyze proc~destroy cholesky_minnorm_solver%destroy proc~analyze->proc~destroy proc~rcm rcm proc~analyze->proc~rcm

Called by

proc~~analyze~~CalledByGraph proc~analyze cholesky_minnorm_solver%analyze proc~nlesolver_solver nlesolver_type%nlesolver_solver proc~nlesolver_solver->proc~analyze

Source Code

    subroutine analyze(me, m, n, irow, icol, istat)

    class(cholesky_minnorm_solver),intent(inout) :: me
    integer,intent(in)               :: m      !! number of rows in `A`
    integer,intent(in)               :: n      !! number of columns in `A`
    integer,dimension(:),intent(in)  :: irow   !! row indices of the nonzero elements of `A`
    integer,dimension(:),intent(in)  :: icol   !! column indices of the nonzero elements of `A`
                                               !! (same size as `irow`)
    integer,intent(out)              :: istat  !! status code (`CHOLESKY_SUCCESS` if no errors)

    integer,dimension(:),allocatable :: colptr  !! `A` stored by columns: column pointers
    integer,dimension(:),allocatable :: rowidx  !! `A` stored by columns: row indices
    integer,dimension(:),allocatable :: mark    !! marker array
    integer,dimension(:),allocatable :: cnt     !! counter array
    integer :: i, j, k, kk, t, c, r, s, nadj, alloc_stat

    call me%destroy()
    istat = CHOLESKY_SUCCESS

    ! check inputs:
    if (m < 1 .or. n < 1 .or. size(irow) /= size(icol) .or. size(irow) < 1) then
        istat = CHOLESKY_ERR_INPUT; return
    end if
    if (any(irow < 1) .or. any(irow > m) .or. any(icol < 1) .or. any(icol > n)) then
        istat = CHOLESKY_ERR_INPUT; return
    end if

    me%m   = m
    me%n   = n
    me%nnz = size(irow)

    ! A by rows (CSR). the map array keeps track of the original element index:
    allocate(me%rowptr(m+1), me%colidx(me%nnz), me%map(me%nnz), me%val(me%nnz), cnt(max(m,n)))
    cnt(1:m) = 0
    do k = 1, me%nnz
        cnt(irow(k)) = cnt(irow(k)) + 1
    end do
    me%rowptr(1) = 1
    do i = 1, m
        me%rowptr(i+1) = me%rowptr(i) + cnt(i)
    end do
    cnt(1:m) = me%rowptr(1:m)  ! insertion points
    do k = 1, me%nnz
        i = irow(k)
        me%colidx(cnt(i)) = icol(k)
        me%map(cnt(i))    = k
        cnt(i) = cnt(i) + 1
    end do

    ! A by columns (only the row indices are needed):
    allocate(colptr(n+1), rowidx(me%nnz))
    cnt(1:n) = 0
    do k = 1, me%nnz
        cnt(icol(k)) = cnt(icol(k)) + 1
    end do
    colptr(1) = 1
    do j = 1, n
        colptr(j+1) = colptr(j) + cnt(j)
    end do
    cnt(1:n) = colptr(1:n)
    do k = 1, me%nnz
        j = icol(k)
        rowidx(cnt(j)) = irow(k)
        cnt(j) = cnt(j) + 1
    end do

    ! graph of A*A^T: rows i and j are adjacent if they share a column.
    ! two passes: count, then fill.
    allocate(mark(m), me%adjptr(m+1))
    mark = 0
    me%adjptr(1) = 1
    do i = 1, m
        mark(i) = i  ! exclude the diagonal
        nadj = 0
        do kk = me%rowptr(i), me%rowptr(i+1)-1
            c = me%colidx(kk)
            do t = colptr(c), colptr(c+1)-1
                j = rowidx(t)
                if (mark(j) /= i) then
                    mark(j) = i
                    nadj = nadj + 1
                end if
            end do
        end do
        me%adjptr(i+1) = me%adjptr(i) + nadj
    end do
    allocate(me%adj(me%adjptr(m+1)-1), stat=alloc_stat)
    if (alloc_stat /= 0) then
        call me%destroy(); istat = CHOLESKY_ERR_MEMORY; return
    end if
    mark = 0
    do i = 1, m
        mark(i) = i
        s = me%adjptr(i)
        do kk = me%rowptr(i), me%rowptr(i+1)-1
            c = me%colidx(kk)
            do t = colptr(c), colptr(c+1)-1
                j = rowidx(t)
                if (mark(j) /= i) then
                    mark(j) = i
                    me%adj(s) = j
                    s = s + 1
                end if
            end do
        end do
    end do
    deallocate(colptr, rowidx, mark, cnt)

    ! reverse Cuthill-McKee ordering:
    allocate(me%perm(m), me%iperm(m))
    call rcm(m, me%adjptr, me%adj, me%perm)
    do r = 1, m
        me%iperm(me%perm(r)) = r
    end do

    ! envelope of the reordered matrix:
    allocate(me%first(m), me%ptr(m+1))
    do r = 1, m
        me%first(r) = r
    end do
    do i = 1, m
        r = me%iperm(i)
        do t = me%adjptr(i), me%adjptr(i+1)-1
            s = me%iperm(me%adj(t))
            if (s < me%first(r)) me%first(r) = s
        end do
    end do
    me%ptr(1) = 1
    do r = 1, m
        me%ptr(r+1) = me%ptr(r) + int(r - me%first(r) + 1, ip)
    end do
    allocate(me%env(me%ptr(m+1)-1), stat=alloc_stat)
    if (alloc_stat /= 0) then
        call me%destroy(); istat = CHOLESKY_ERR_MEMORY; return
    end if

    me%analyzed = .true.

    end subroutine analyze