2

两个多星期以来,我一直在努力从我的 fortran 代码中调用一个用 C 编写的METIS库。而且,不幸的是,没有你的帮助,这似乎不是一个快乐的结局。我发现了一些关于直接调用使用接口的帖子。我更喜欢后者,因为我可以监视变量以进行调试。我附上了三个代码。

1.我想使用的c函数
2.fortran接口模块
3.fortran程序

(1)c函数

int METIS_PartMeshNodal(idx_t *ne, idx_t *nn, idx_t *eptr, idx_t *eind, 
      idx_t *vwgt, idx_t *vsize, idx_t *nparts, real_t *tpwgts, 
      idx_t *options, idx_t *objval, idx_t *epart, idx_t *npart)

我删除了 c 函数体。没有必要了解我的问题

这里,idx_t是整数,real_t是单精度或双精度。从neoptions是输入,最后三个参数是输出。并且vwgtvsizetpwgtsoptions可以接收 null 作为默认设置的输入 我编写了使用这样的 c 函数的接口模块

(2) Fortran接口模块

固定的!

  1. 在use 常量下插入use iso_c_bind
  2. nenn和其他变量使用integer(c_int)而不是integer 。
  3. 删除未使用的模块常量

.

module Calling_METIS

  !use constants,  only : p2 !this is for double precision
  use iso_c_bind            !inserted later

  implicit none

  !integer                                    :: ne, nn              !modified
  integer(c_int)                              :: ne, nn 
  !integer,  dimension(:), allocatable        :: eptr, eind          !modified
  integer(c_int),  dimension(:), allocatable  :: eptr, eind
  !integer,  dimension(:), allocatable        :: vwgt, vsize         !modified
  type(c_ptr)                                 :: vwgt, vsize         
  !integer                                    :: nparts              !modified
  integer(c_int)                              :: nparts
  !real(p2), dimension(:), allocatable        :: tpwgts              !modified 
  type(c_ptr)                                 :: tpwgts      
  !integer,  dimension(0:39)                  :: opts                !modified
  integer(c_int),  dimension(0:39)            :: opts        
  !integer                                    :: objval              !modified
  integer(c_int)                              :: objval
  !integer,  dimension(:), allocatable        :: epart, npart        !modified 
  integer(c_int),  dimension(:), allocatable  :: epart, npart 

  interface
    subroutine METIS_PartMeshNodal( ne, nn, eptr, eind, vwgt, vsize, nparts, tpwgt, &
                                    opts, objval, epart, npart) bind(c)
      use intrinsic        :: iso_c_binding
      !use constants,  only  : p2

      implicit none

      integer (c_int),                  intent(in)  :: ne, nn
      integer (c_int), dimension(*),    intent(in)  :: eptr, eind
      !integer (c_int), dimension(*),    intent(in) :: vwgt, vsize  !modified
      type(c_ptr),                          value   :: vwgt, vsize   
      integer (c_int),                  intent(in)  :: nparts
      !real(c_double),  dimension(*),    intent(in) :: tpwgt        !modified
      type(c_ptr),                          value   :: tpwgt
      integer (c_int), dimension(0:39), intent(in)  :: opts
      integer (c_int),                  intent(out) :: objval
      integer (c_int), dimension(*),    intent(out) :: epart
      integer (c_int), dimension(*),    intent(out) :: npart

    end subroutine METIS_PartMeshNodal  
  end interface
end module

这是我调用该函数的程序代码

(3) Fortran程序

固定的!

  1. npart的分配大小是固定的。不是ne而是nn
  2. 添加 opts(7)=1 以获得 epart、npart 的 Fortran 样式数组(直到现在才生效)

.

program METIS_call_test

 !some 'use' statments
 use Calling_METIS
 use iso_c_binging         !added

 implicit none

 ! Local variable
 integer         :: iC
 character(80)   :: grid_file !grid_file

 grid_file = 'test.grid'

 ! (1) Read grid files
 call read_grid(grid_file)

 ! (2) Construction Input Data for calling METIS Function
 ! # of cells, vertices
 ne = ncells
 nn = nvtxs

 ! eptr, eind allocation 
 allocate(eptr(0:ne), eind(0:3*ntria + 4*nquad - 1))

 ! eptr and eind building
 eptr(0) = 0
 do iC=1, ncells
   eptr(iC) = eptr(iC-1) + cell(iC)%nvtxs
   eind(eptr(iC-1):eptr(iC)-1) = cell(iC)%vtx
 end do

 ! epart, npart building
 !allocate(epart(ne), npart(ne))
 allocate(epart(ne), npart(nn))   ! modified

 ! # of partition setting
 nparts = 2
 vwgt   = c_null_ptr    !added
 vsize  = c_null_ptr    !added
 tpwgt  = c_null_ptr    !added     

 ! (3) Call METIS_PartMeshNodal
 call METIS_SetDefaultOptions(opts)

 opts(7) = 1                      !Added. For fortran style output array epart, npart. 

 call METIS_PartMeshNodal(ne, nn, eptr, eind, vwgt, vsize, nparts, tpwgt, &
                           opts, objval, epart, npart)
 !call METIS_PartMeshNodal(ne, nn, eptr, eind, null(), null(), nparts, null(), &
 !                         opts, objval, epart, npart)         !wrong...

end program

但问题是我收到如下错误消息,尽管我为 tpwgt 设置了 null。

输入错误:约束 0 的 tpwgts 的 0.000000 总和不正确。

这条消息在下面的代码中处理。

for (i=0; i<ctrl->ncon; i++) {
    sum = rsum(ctrl->nparts, ctrl->tpwgts+i, ctrl->ncon);
    if (sum < 0.99 || sum > 1.01) {
      IFSET(dbglvl, METIS_DBG_INFO, 
          printf("Input Error: Incorrect sum of %"PRREAL" for 
                  tpwgts for constraint %"PRIDX".\n", sum, i));
      return 0;
    }
  }

无论如何,为了看看如果我为 tpwgts 放置一个数组而不是 null 会得到什么,tpwgts(:) = 1.0/nparts,这使得 tpwgts 的总和等于 1.0。但我得到了与1.75相同的消息。

这些是我的问题
1. 我是否使用 null() 正确传递参数?
2. 我必须将所有参数的指针传递给 c 函数吗?那怎么办?
3. 将整数放入 opts(0:39) 是否足够使用?例如,在没有“接口模块”的帖子中,使用了像 options(3)=1 这样的简单代码。但在 c 代码中,options 有 16 个命名变量,如 options[METIS_OPTION_NUMBERING]、options[METIS_OPTION_UFACTOR]。我认为设置选项是必要的,但我不知道。4. 有没有 METIS fortran 的例子?

任何形式的提示/建议都会对我有很大帮助。谢谢你。

结论

我遇到的问题是c 函数无法从 fortran 代码中识别空指针。

接口模块中有一些变量的错误声明(请参阅“已修复”和评论)

看起来代码工作正常。但是fortran 样式输出的option(7) = 1不起作用,现在我正在研究它。

4

2 回答 2

3
  1. 不,您不能通过null(),这是一个 Fortran 指针常量。您必须C_NULL_PTR从模块传递,ISO_C_BINDING并且接口必须反映这一点。虚拟参数必须是type(c_ptr),很可能带有VALUE属性。由于相同的内部表示,它实际上可能有效,但我不会指望它。

  2. 不,如果你传递一些普通的变量,你可以直接通过引用传递它。就像通常在 Fortran 中一样。如果接口是BIND(C),编译器知道它必须发送一个指针。

    有一个更新 Fortran 2008 的新 TS,您可以在其中将可互操作过程中的虚拟参数定义为OPTIONAL. 然后你可以通过省略它们来传递空指针。Gfortran 应该已经支持这一点。

注意:在这里我可以看到你的函数有一个非常不同的 C 签名,你确定你的函数没问题吗?http://charm.cs.uiuc.edu/doxygen/charm/meshpart_8c.shtml

于 2013-02-06T14:26:42.360 回答
0

我认为您opts(7)不起作用,因为您还需要 METIS 功能的接口METIS_SetDefaultOptions。根据来自http://glaros.dtc.umn.edu/gkhome/node/877的回答,我创建了一个metisInterface.F90带有我需要的接口的包装模块 ( ):

module metisInterface
! module to allows us to call METIS C functions from the main Fortran code

   use,intrinsic :: ISO_C_BINDING

   integer :: ia,ic
   integer(C_INT) :: metis_ne,metis_nn
   integer(C_INT) :: ncommon,objval
   integer(C_INT) :: nparts
   integer(C_INT),allocatable,dimension(:) :: eptr,eind,perm,iperm
   integer(C_INT),allocatable,dimension(:) :: epart,npart
   type(C_PTR) :: vwgt,vsize,twgts,tpwgts
   integer(C_INT) :: opts(0:40)


   interface
      integer(C_INT) function METIS_SetDefaultOptions(opts) bind(C,name="METIS_SetDefaultOptions")
         use,intrinsic :: ISO_C_BINDING
         implicit none
         integer(C_INT) :: opts(0:40)
      end function METIS_SetDefaultOptions
   end interface 

   interface
      integer(C_INT) function METIS_PartMeshDual(ne,nn,eptr,eind,vwgt,vsize,ncommon,nparts,tpwgts, &
                              opts,objval,epart,npart) bind(C,name="METIS_PartMeshDual")
         use,intrinsic :: ISO_C_BINDING
         implicit none
         integer(C_INT):: ne, nn
         integer(C_INT):: ncommon, objval
         integer(C_INT):: nparts
         integer(C_INT),dimension(*) :: eptr, eind
         integer(C_INT),dimension(*) :: epart, npart
         type(C_PTR),value :: vwgt, vsize, tpwgts
         integer(C_INT) :: opts(0:40)
      end function METIS_PartMeshDual
   end interface    

end module metisInterface

然后,在主程序中(或者你调用 METIS 函数的任何地方),你需要拥有(为了完整起见,我还添加了对 的调用METIS_PartMeshDual):

use metisInterface

integer :: metis_call_status
.
.
.
metis_call_status = METIS_SetDefaultOptions(opts)

! METIS_OPTION_NUMBERING for Fortran
opts(17) = 1

metis_call_status = METIS_PartMeshDual(metis_ne,metis_nn,eptr,eind, &
                    vwgt,vsize,ncommon,nparts,tpwgts,opts,objval,epart,npart)

请注意,epart并且npart将根据需要使用 Fortran 编号(从 1 开始)。但是,处理器也将从 1 开始编号。例如,如果您在 4 个处理器中运行,则根处理器为 1,您可能有epart(n)=4,但您不会有任何epart(n)=0.

最后,metis.c还需要一个文件,其中包含一行:

#include "metis.h"

编译说明

  1. metis.c用 C 编译器编译
  2. 使用与已编译metisInterface.F90C 对象链接的 Fortran 编译器进行编译
  3. 使用链接到的 Fortran 编译器编译主程序metisInterface.o
于 2018-07-26T04:25:10.840 回答