【发布时间】:2019-07-28 20:05:34
【问题描述】:
我正在为一段 Fortran 代码编写一个 mex 网关。
在 Fortran 代码中,为了可移植性,浮点变量声明为
REAL(kind(0.0D0)) :: x, y, etc
(顺便说一句,我知道有更好的方法可以做到这一点,如在 Fortran: integer*4 vs integer(4) vs integer(kind=4), What does "real*8" mean?,和 https://software.intel.com/en-us/blogs/2017/03/27/doctor-fortran-in-it-takes-all-kinds)
但是,在我看来,mex 只支持 REAL*8 和 REAL*4,前者是 Double,后者是 Single。我从以下函数/子例程中得到了这个印象:
mxIsDouble、mxIsSingle、mxCopyPtrToReal8、mxCopyReal8ToPtr、mxCopyPtrToReal4、mxCopyReal4ToPtr
我的问题如下。
mex 是否只支持 REAL*8 和 REAL*4?
-
如果我将双精度浮点变量声明为,是否会提高 mex 网关的可移植性
REAL(kind(0.0D0)) :: x, y 等
甚至
integer, parameter :: dp = selected_real_kind(15, 307)
real(kind=dp) :: x, y, etc
或者我应该简单地声明
REAL*8 :: x, y, etc
是否所有平台都支持 REAL*8 和/或 REAL*4?如果不是,这是否意味着 MATLAB mex 本质上是不可移植的?
在 Fortran 代码的 mex 网关中指定浮点变量类型的最佳方法是什么?
以下代码是一个示例。请参阅 x、y 和 xs 的声明。
#include "fintrf.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C y = square (x)
C x: a floating point scalar
C y: x^2
implicit none
C mexFunction arguments
integer, intent(in) :: nlhs, nrhs
mwPointer, intent(in) :: prhs(nrhs)
mwPointer, intent(inout) :: plhs(nlhs)
C function declarations:
mwPointer, external :: mxCreateDoubleScalar, mxGetPr
mwSize, external :: mxGetM, mxGetN
integer*4, external :: mxIsDouble, mxIsSingle
C variables
mwSize, parameter :: mwOne = 1
integer, parameter :: dKind = kind(0.0D0)
integer, parameter :: sKind = kind(0.0)
real(kind=dKind) :: x, y ! Does this improve the portablity?
real(kind=sKind) :: xs ! Does this improve the portablity?
C validate number of arguments
if (nrhs .ne. 1) then
call mexErrMsgIdAndTxt ('mex:nInput', '1 input required.')
endif
if (nlhs .gt. 1) then
call mexErrMsgIdAndTxt ('mex:nOutput', 'At most 1 output.')
endif
C validate input
if (mxIsDouble(prhs(1)) .ne. 1 .and. mxIsSingle(prhs(1)) .ne. 1)
! What if the input is a floating point number but neither Double nor Single?
+ then
call mexErrMsgIdAndTxt ('mex:Input', 'Input a real number.')
endif
if (mxGetM(prhs(1)) .ne. 1 .or. mxGetN(prhs(1)) .ne. 1) then
call mexErrMsgIdAndTxt ('mex:Input', 'Input a scalar.')
endif
C read input
if (mxIsDouble(prhs(1)) .eq. 1) then
call mxCopyPtrToReal8(mxGetPr(prhs(1)), x, mwOne)
else
call mxCopyPtrToReal4(mxGetPr(prhs(1)), xs, mwOne)
x = real(xs, dKind)
! What if the input is a floating point number but neither REAL*8 nor REAL*4
endif
C do the calculation
y = x**2
C write output
plhs(1) = mxCreateDoubleScalar(y)
return
end subroutine mexFunction
代码运行正确。但我不确定它是否便携。
【问题讨论】:
-
也许还有
real*4 single; real(kind(single)) x。同样不便携,但限制了需要担心的部件数量。 -
“这是否意味着 MATLAB mex 本质上是不可移植的?”。 MATLAB 仅适用于 3 个操作系统和the documentation lists exactly which version of which compilers you can use to make MEX-files。所以是的,MEX 本质上是不可移植的,因为您无法在其他平台上或使用其他编译器编译 MEX 文件。
-
感谢@francescalus 和 Cris Luengo 提供的信息丰富的 cmets!
标签: matlab fortran mex matlab-deployment