0

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)
Feras
  • 1
  • 1
  • Are you compiling with `-g -fbacktrace` (gfortran) to see *where* the seg fault is occurring? Where is `opts` declared as an array? Actually, where are **any** of your variables declared? – Kyle Kanos Nov 20 '13 at 15:48
  • Hi Kyle,I just added the parameters declarations that was missed in the first post. – Feras Nov 20 '13 at 16:12
  • I used the following for compiling: fobeidat@ubuntu:~/MetisApplication/graphs$ gfortran -g -fbacktrace test3.f90 /opt/metis/lib/libmetis.so – Feras Nov 20 '13 at 16:16
  • fobeidat@ubuntu:~/MetisApplication/graphs$ ./a.out ----------------------- ne: 2000 nn: 16000 Program received signal 11 (SIGSEGV): Segmentation fault. Backtrace for this error: + /lib/libc.so.6(+0x33af0) [0x7f6ef19f8af0] + /opt/metis/lib/libmetis.so(libmetis__CreateGraphDual+0x3d) [0x7f6ef252b7dd] – Feras Nov 20 '13 at 16:17
  • And there you go: the seg-fault is in `libmetis/mesh.c`'s subroutine `CreateGraphDual` and not your Fortran code. – Kyle Kanos Nov 20 '13 at 16:38
  • Any idea, how to resolve it, Kyle? – Feras Nov 20 '13 at 17:45
  • Backtrace for this error: + /lib/x86_64-linux-gnu/libc.so.6(+0x364a0) [0x7fa0581be4a0] + function libmetis__CreateGraphDual (0x402DAF) + function METIS_MeshToDual (0x4032BE) + function METIS_PartMeshDual (0x40259B) + function metis_part_1 (0x401F92) at line 91 of file test3.f90 + /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7fa0581a976d] – Feras Nov 20 '13 at 17:48
  • I have no idea how to fix it, but perhaps reinstalling Metis would be in order? Please add these backtraces to the original question, it is very hard to read them as a comment. – Kyle Kanos Nov 20 '13 at 18:36
  • If the procedure is called with bad parameters, it can fault this way. Maybe the interface is wrong. – Vladimir F Героям слава Nov 21 '13 at 18:03

0 Answers0