umfpack_binding.f90 5.27 KB
Newer Older
Rémy Mozul's avatar
Rémy Mozul committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

module LinearAlgebra

  use iso_c_binding

  implicit none 

  private

  interface
    function umfpack_symbolic(nrow,ncol,ap,ai,ax,symbolic,control,info) bind(c, name="c_symbolic")
      import c_int, c_double, c_ptr

      integer(c_int), intent(in), value :: nrow
      integer(c_int), intent(in), value :: ncol

      type(c_ptr), value :: ap, ai, ax
      type(c_ptr) :: symbolic, control

      integer(c_int) :: umfpack_symbolic

      real(c_double), dimension(90) :: info

    end function
  end interface

  interface
    function umfpack_numeric(ap,ai,ax,symbolic,numeric,control,info) bind(c, name="c_numeric")
      import c_int, c_double, c_ptr

      type(c_ptr), value :: ap, ai, ax
      type(c_ptr) :: symbolic, numeric, control

      integer(c_int) :: umfpack_numeric

      real(c_double), dimension(90) :: info

    end function
  end interface

  interface
    function umfpack_solve(ap,ai,ax,x,b,numeric,control,info) bind(c, name="c_solve")
      import c_int, c_double, c_ptr

      type(c_ptr), value :: ap, ai, ax
      type(c_ptr) :: x, b, numeric, control

      integer(c_int) :: umfpack_solve

      real(c_double), dimension(90) :: info

    end function
  end interface

  interface
    subroutine umfpack_free_symbolic(symbolic) bind(c, name="c_free_symbolic")
      import c_ptr
      type(c_ptr) :: symbolic
    end subroutine
  end interface

  interface
    subroutine umfpack_free_numeric(numeric) bind(c, name="c_free_numeric")
      import c_ptr
      type(c_ptr) :: numeric
    end subroutine
  end interface


  type, public :: sparse_mat
    private

    integer(c_int) :: n

    type(c_ptr) :: symbolic = c_null_ptr
    type(c_ptr) :: numeric  = c_null_ptr

    type(c_ptr) :: Ap = c_null_ptr
    type(c_ptr) :: Ai = c_null_ptr
    type(c_ptr) :: Ax = c_null_ptr

    type(c_ptr) :: control = c_null_ptr
    real(kind=c_double), dimension(90) :: info

    real(kind=c_double), dimension(:), pointer :: sol => null()

  end type sparse_mat


  public declare, build, &
         solve  , erase, &
         init   , finalize


  contains


  subroutine init()
    implicit none

    print *, 'empty initialization'

  end subroutine

  subroutine declare(matrix, nb_dofs, nb_non_zero, i_indices, j_indices, is_sym, info)
    implicit none

    type(sparse_mat) :: matrix

    integer(kind=4)                        :: nb_dofs, nb_non_zero, info
    integer(kind=4), dimension(:), pointer :: i_indices
    integer(kind=4), dimension(:), pointer :: j_indices

    logical                                :: is_sym

    ! Ap ~ system%cc_adj_dof
    ! Ai ~ system%adj_dof

    integer(kind=c_int), dimension(:), pointer :: Ap, Ai
    real(kind=c_double), dimension(:), pointer :: control

    integer(kind=4) :: idx, i

    if( is_sym ) then
      print *, 'ERROR : umfpack available only for non-symmetrical systems'
    end if

    allocate( Ap(nb_dofs+1  ) )
    allocate( Ai(nb_non_zero) )

    Ap(:) = 0
    Ai(:) = j_indices(:)-1

    ! assume i_indices is ordered in an increasing order

    ! counting non zero on a line
    idx = 0

    do i = 2, nb_non_zero
      if( idx /= i_indices(i) ) idx = i_indices(i)
      Ap(idx+1) = Ap(idx+1) + 1
    end do

    ! cumulating count
    Ap(1) = 1
    do i = 1, nb_dofs
      Ap(i+1) = Ap(i) + Ap(i+1)
    end do
    Ap(1) = 0

    matrix%Ap = c_loc( Ap(1) )
    matrix%Ai = c_loc( Ai(1) )

    matrix%n  = nb_dofs

    allocate( control(20) )
    control(:) = 0.d0
    matrix%control  = c_loc( control(1) )


    info = umfpack_symbolic(nb_dofs,nb_dofs,matrix%Ap,matrix%Ai,matrix%Ax,matrix%symbolic,matrix%control,matrix%info)

    allocate( matrix%sol(nb_dofs) )
    matrix%sol = 0.d0

  end subroutine


  subroutine build(matrix, val, info)
    implicit none
    type(sparse_mat) :: matrix
    real(kind=8)   , dimension(:), pointer :: val
    integer(kind=4) :: info

    matrix%Ax = c_loc( val(1) )

    ! Factorize
    info = umfpack_numeric(matrix%Ap,matrix%Ai,matrix%Ax,matrix%symbolic,matrix%numeric,matrix%control,matrix%info)

  end subroutine

  subroutine solve(matrix, rhs, info)
    implicit none

    type(sparse_mat) :: matrix

    real(kind=8)   , dimension(:), pointer :: rhs
    integer(kind=4) :: info

    ! Solve
    info = umfpack_solve( matrix%Ap, matrix%Ai, matrix%Ax, c_loc(matrix%sol(1)), c_loc(rhs(1)), &
                          matrix%numeric, matrix%control, matrix%info )

    rhs = matrix%sol

  end subroutine

  subroutine erase(matrix)
    implicit none
    type(sparse_mat) :: matrix

    integer(kind=c_int), dimension(:), pointer :: Ap, Ai
    real(kind=c_double), dimension(:), pointer :: Ax, sol, control

    call umfpack_free_symbolic( matrix%symbolic )
    call umfpack_free_numeric(  matrix%numeric  )

    matrix%symbolic = c_null_ptr
    matrix%numeric  = c_null_ptr

    call c_f_pointer(matrix%Ap, ap, (/matrix%n/))

    call c_f_pointer(matrix%Ai, ai, (/ap(matrix%n+1)/))

    if(associated(ap)) deallocate(ap)
    if(associated(ai)) deallocate(ai)

    matrix%Ap = c_null_ptr
    matrix%Ai = c_null_ptr
    matrix%Ax = c_null_ptr

    call c_f_pointer(matrix%control, control, (/20/))
    deallocate(control)
    matrix%control = c_null_ptr

    if( associated(matrix%sol) ) deallocate(matrix%sol)
    matrix%sol => null()

  end subroutine

  subroutine finalize()
    implicit none

    print *, 'empty finalization'

  end subroutine

end module