ISO_C_BINDING和OpenMP的问题

时间:2013-11-08 21:32:38

标签: c fortran openmp fortran-iso-c-binding

我有一个使用OpenMP并行化的fortran程序。 main函数调用Fortran函数,该函数包含具有常微分方程的系统的定义。当分割不同的线程时,这运行正常。我还想要选择在一个C文件中链接ODE的类似定义,我可以在不使用OpenMP时运行它。使用OpenMP时,ODE计算错误。

!************* Main Program OpenMP part **************
!SETUPBV contains a function that contains the ODE
!definition

!$OMP PARALLEL DEFAULT(SHARED) PRIVATE(I,IT,NT)

      IT = 0
!$    IT = OMP_GET_THREAD_NUM()
      NT = 1
!$    NT = OMP_GET_NUM_THREADS()
      IF(NLLV>=0.OR.IFST==1) &
        CALL SETUBV(NDIM,NA,NCOL,NINT,NFPR,NRC,NROW,NCLM,                &
         FUNI,ICNI,AP,PAR,NPAR,ICP,A,B,C,DD(1,1,BASE+1),DUPS,            &
         FCFC(1,BASE+1),UPS,UOLDPS,RLOLD,UDOTPS,UPOLDP,DTM,THU,          &
         IFST,IAM,IT,NT,IRF,ICF,IID,NLLV)

      I = (IT*NA+NT-1)/NT+1
      CALL BRBD(A(1,1,I),B(1,1,I),C(1,1,I),D,DD,DUPS(1,(I-1)*NCOL),FAA,FC,&
        FCFC,P0,P1,IFST,IID,NLLV,DET,NDIM,NTST,NA,NBC,NROW,NCLM,         &
        NFPR,NFC,A1,A2,BB,CC,C2,CDBC,                                    &
        SOL,S1,S2,IPR,IPC,IRF(1,I),ICF(1,I),IAM,KWT,IT,NT)

!$OMP END PARALLEL

!************* Extract from Module containing INTERFACE definition **************

SUBROUTINE FUNCINTFC(NDIM,U,ICP,PAR,IJAC,F,DFDU,DFDP) BIND(C,NAME='func')
   USE, INTRINSIC :: ISO_C_BINDING
   INTEGER(C_INT), INTENT(IN) :: NDIM, IJAC, ICP(*)
   REAL(C_DOUBLE), INTENT(IN), VALUE :: U(NDIM), PAR(*)
   REAL(C_DOUBLE), INTENT(OUT), VALUE :: F(NDIM)
   REAL(C_DOUBLE), INTENT(INOUT) :: DFDU(NDIM,NDIM),DFDP(NDIM,*)
END SUBROUTINE FUNCINTFC

!************* Function calling c-function with ODE **************

SUBROUTINE FUNC(NDIM,U,ICP,PAR,IJAC,F,DFDU,DFDP) 
!--------- ---- 

  USE INTERFACES, ONLY: FUNCINTFC

  IMPLICIT NONE
  INTEGER, INTENT(IN) :: NDIM, IJAC, ICP(*)
  DOUBLE PRECISION, INTENT(IN) :: U(NDIM), PAR(*)
  DOUBLE PRECISION, INTENT(OUT) :: F(NDIM)
  DOUBLE PRECISION, INTENT(INOUT) :: DFDU(NDIM,*), DFDP(NDIM,*)

  CALL FUNCINTFC(NDIM,U,ICP,PAR,IJAC,F,DFDU,DFDP) 

END SUBROUTINE FUNC


!************* C-function containing ODE definition **************

int func(integer *ndim, const doublereal u, const integer *icp,
               const doublereal par, const integer *ijac,
               doublereal f, doublereal *dfdu, doublereal *dfdp) 

{
    /* Builtin functions */
    double exp(doublereal);

    /* Local variables */
    static doublereal e, u1, u2;

/*     ---------- ---- */

/* Evaluates the algebraic equations or ODE right hand side */

/* Input arguments : */
/*      NDIM   :   Dimension of the ODE system */
/*      U      :   State variables */
/*      ICP    :   Array indicating the free parameter(s) */
/*      PAR    :   Equation parameters */

/* Values to be returned : */
/*      F      :   ODE right hand side values */

/* Normally unused Jacobian arguments : IJAC, DFDU, DFDP (see manual) */

    /* Function Body */
    u1 = u[0];
    u2 = u[1];

    e = exp(u2);

    f[0] = -u1 + par[0] * (1 - u1) * e;
    f[1] = -u2 + par[0] * par[1] * (1 - u1) * e - par[2] * u2;

    return 0;
} /* func_ */

1 个答案:

答案 0 :(得分:0)

在C中,static局部变量不是线程安全的。从位置变量的声明中删除static。您不需要跨调用持久化值。如果你这样做,你必须非常小心线程安全。