Calling Metis API using Fortran and C

放肆的年华 提交于 2019-12-25 04:39:07

问题


I wrote the following code for calling Metis using Fortran and C, but still getting segmentation fault. The target of the code is to do partition for the input file chanelElements.

module metis_vars
 use iso_c_binding

! Variables
integer              :: ia, ic
integer(kind=c_int)  :: ne, nn
integer(kind=c_int)  :: ncommon, objval
integer(kind=c_int)  :: nparts
integer(kind=c_int), allocatable, dimension(:) :: eptr, eind
integer(kind=c_int), allocatable, dimension(:) :: epart, npart
type(c_ptr)          :: vwgt, vsize, tpwgts
integer              :: opts(0:40)

interface

   subroutine METIS_PartMeshDual(ne,nn,eptr,eind,vwgt,vsize,ncommon,nparts,tpwgts,opts, & 
   objval,epart,npart)bind(C, name="METIS_PartMeshDual")

   !subroutine METIS_PartMeshDual(ne,nn,eptr,eind,vwgt,vsize,nparts,tpwgts,opts,objval,epart,npart) &
   !  bind(C, name="METIS_PartMeshDual")

        ! use binding module
        use iso_c_binding
        ! declare variables with C++ types semantics
        integer(kind=c_int)               :: ne, nn, ncommon, objval, nparts
        integer(kind=c_int), dimension(*) :: eptr, eind
        integer(kind=c_int), dimension(*) :: epart, npart
        type(c_ptr), value                :: vwgt, vsize, tpwgts
        integer(kind=c_int)               :: opts(0:40)
        !    printf('Hi');

    end subroutine METIS_PartMeshDual

end interface

end module

and the main program is

program METIS_PART_1

    use iso_c_binding
    use metis_vars

    implicit none

    open(unit=15, file="chanelData.dat")

    open(unit=1, file='channelElements.mesh')

    read(1,*), ne
    !ne = 2000
    nn = ne * 8


   allocate( eptr(ne+1), eind(8*ne) )
   allocate( epart(ne), npart(nn) )

    do ic=1,ne
        ia = (ic-1) * 8 + 1
        read(1,*), eind(ia:ia+7)
        write(15,*), eind(ia:ia+7)
  !       print*, eind(ia:ia+7)
        eptr(ic) = ia
  !      write(15,*),' ia:   ', ia
    enddo
   close(15)
   close(1)
    nparts = 4
    ncommon = 2


     vwgt   = c_null_ptr
     vsize  = c_null_ptr
     tpwgts = c_null_ptr
     !opts = c_null_ptr

   opts = 0
   opts(0) = 1
   opts(7) = 1
   ! opts(0)   = 1
   ! opts(7)   = 1
   print*, ' -----------------------'
    print*, ' ne:   ', ne
    print*, ' nn:   ', nn

    call METIS_PartMeshDual(ne,nn,eptr,eind,vwgt,vsize,ncommon,nparts,tpwgts, & 
     opts,objval,epart,npart)
  ! call METIS_PartMeshDual(ne,nn,eptr,eind,vwgt,vsize,nparts,tpwgts,opts,objval,& 
  ! epart,npart)

     print*, ' objval:   ', objval
  end program METIS_PART_1

I compile using and got error:

fobeidat@envenio05:~/Metis/metis-5.1.0/graphs$ gfortran test3.f90 libmetis.a 
fobeidat@envenio05:~/Metis/metis-5.1.0/graphs$ ./a.out
  -----------------------
  ne:           2000
  nn:          16000
Segmentation fault (core dumped)

来源:https://stackoverflow.com/questions/20099846/calling-metis-api-using-fortran-and-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!