2017-03-17 4 views
2

Matlab에 Fortran 루틴을 추가하는 중입니다 (Mex 함수를 통해). 저는 Matlab, Mex, Fortran 사이의 링크가 어떻게 만들어 졌는지 이해할 수있는 간단한 프로그램을 작성하려고합니다.Windows에서 GFortran을 사용하여 Matlab에서 Mex 파일 만들기

나는 xy의 값을 취하는 간단한 프로그램을 작성하여 함께 더하고 z을 출력합니다. 그러나 컴파일 후 Matlab에서 실행하려고하면 Matlab은 설명없이 간단히 충돌합니다. 내가 여기서 뭘 잘못했는지 알기나 해?

! MEX FILE EXAMPLE 
! 


module yprime_mod ! test module for gnumex and g95 
    use mexinterface 
contains 
    subroutine yprime(x, y, z) ! subroutine yprime(z, t, y, error, x) 
    implicit none 
    double precision :: x, y, z 
    intent(in) :: x, y 
    intent(out) :: z 
    ! 
    z=x+y; 
    end subroutine yprime 
end module yprime_mod 

subroutine mexfunction(nlhs, plhs, nrhs, prhs) 
    use yprime_mod 
    implicit none 
    integer :: nlhs, nrhs, plhs(nlhs), prhs(nrhs) 
    double precision, pointer :: xp, yp, z 
    ! 
    if (nrhs /= 2) call mexerrmsgtxt('yprime requires two input arguments') 
    if (nlhs > 1) call mexerrmsgtxt('yprime requires one output argument') 
    call c_f_pointer(mxgetpr(prhs(1)), xp) ! assign pointers to parameters 
    call c_f_pointer(mxgetpr(prhs(2)), yp) 
    call c_f_pointer(mxgetpr(plhs(1)), z) 
    call yprime(xp, yp, z) 
end subroutine mexfunction 
+0

p * hs 인수에 기본 정수 유형이 충분히 큽니까? 'mwPointer's로 선언 해보십시오. 또한, MEX Fortran API를 사용하여 직접 C 컴파일을하지 않고 컴파일한다면,'c_f_pointer'가 필요 없다고 생각합니다. – TroyHaskin

+0

[Fortran 소스 파일의 기본 예제] (https://in.mathworks.com/help/matlab/matlab_external/create-fortran-source-mex-file.html)가 작동하는지 확인할 수 있습니까? –

답변

1

포트란을 모른 채, 나는 기본적인 예를 따라 갔다.

c_f_pointer (충돌과 관련이있을 수 있음)을 사용하는 것에 대해서는 알지 못합니다.
반환 인수에 대한 행렬을 만들 때 plhs(1) = mxCreateDoubleMatrix(mrows,ncols,0)을 호출하는 것을 잊어 버렸기 때문에 코드가 가장 많이 충돌하는 것으로 생각됩니다.

! MEX FILE EXAMPLE 
! 

#include "fintrf.h" 

module yprime_mod ! test module for gnumex and g95 
    !use mexinterface 
contains 
    subroutine yprime(x, y, z) ! subroutine yprime(z, t, y, error, x) 
    implicit none 
    double precision :: x, y, z 
    intent(in) :: x, y 
    intent(out) :: z 
    ! 
    z=x+y; 
    end subroutine yprime 
end module yprime_mod 
! 
!  
subroutine mexfunction(nlhs, plhs, nrhs, prhs) 
    use yprime_mod 
    implicit none 
    !integer :: nlhs, nrhs, plhs(nlhs), prhs(nrhs) 
    !double precision, pointer :: xp, yp, zp 
    !double precision x, y, z 

    !mexFunction arguments: 
    mwPointer plhs(*), prhs(*) 
    integer nlhs, nrhs 

    !Function declarations: 
    mwPointer mxGetPr 
    mwPointer mxCreateDoubleMatrix 
    mwPointer mxGetM, mxGetN 

    !Pointers to input/output mxArrays: 
    mwPointer xp, yp, zp 

    !Arguments for computational routine: 
    real*8 x, y, z 

    if (nrhs /= 2) call mexerrmsgtxt('yprime requires two input arguments') 
    if (nlhs > 1) call mexerrmsgtxt('yprime requires one output argument') 

    !To points to the input matrices data, use the mxGetPr function. 
    xp = mxGetPr(prhs(1)) 
    yp = mxGetPr(prhs(2)) 

    !Create Fortran arrays from the input arguments. 
    call mxCopyPtrToReal8(xp,x,1) 
    call mxCopyPtrToReal8(yp,y,1) 

    !Create scalar for the return argument. 
    plhs(1) = mxCreateDoubleMatrix(1,1,0) 

    !Use the mxGetPr function to assign the y_ptr argument to plhs(1). 
    zp = mxGetPr(plhs(1)) 

    !call c_f_pointer(mxgetpr(prhs(1)), xp) ! assign pointers to parameters 
    !call c_f_pointer(mxgetpr(prhs(2)), yp) 
    !call c_f_pointer(mxgetpr(plhs(1)), z) 

    !call yprime(xp, yp, zp) 

    !Perform Calculation 
    call yprime(x, y, z) 

    !Copy Results to Output Argument 
    call mxCopyReal8ToPtr(z,zp,1) 

end subroutine mexfunction 

내가 컴파일 인텔 컴파일러를 사용하고, Visual Studio에서 코드 (단계적으로) 디버깅 : 여기

수정 된 코드입니다.
mex 파일이 올바르게 작동 중입니다 ...

+0

안녕하세요, Adam, 제 답변에 대한 의견을 보내 주시겠습니까? – Rotem

관련 문제