Fortran 소스 MEX 파일 만들기
이 예제에서는 MATLAB® 행렬을 사용하여 MATLAB에서 Fortran 서브루틴 timestwo를 호출하는 MEX 파일을 작성하는 방법을 보여줍니다. 여기에서 전체 소스 파일을 볼 수 있습니다. 이 예제에서는 MEX 함수를 만들기 위해 MATLAB 편집기를 사용하여 소스 코드와 MATLAB mex 명령을 작성합니다.
Fortran 서브루틴 timestwo
다음 코드는 n차원 배열 x_input에 2를 곱한 다음 결과값을 배열 y_output으로 반환하는 timestwo 서브루틴을 정의합니다.
subroutine timestwo(y_output, x_input)
real*8 x_input, y_output
y_output = 2.0 * x_input
return
end소스 파일 만들기
MATLAB 편집기를 열고 파일을 만든 후 다음 정보를 MEX 파일에 기록합니다.
C====================================================================== C timestwo.f C Computational function that takes a scalar and doubles it. C This is a MEX file for MATLAB. C======================================================================
MATLAB API 함수 선언이 포함된 Fortran 헤더 파일 fintrf.h를 추가합니다.
#include "fintrf.h"
파일을 MATLAB 경로(예: c:\work 내)에 저장하고 이름을 timestwo.F로 지정합니다. 작성한 MEX 파일의 이름은 timestwo입니다.
게이트웨이 루틴 만들기
MATLAB은 게이트웨이 루틴 mexfunction을 Fortran 서브루틴에 대한 진입점으로 사용합니다. 다음 mexFunction 코드를 추가하십시오.
C Gateway routine
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C Declarations
C Statements
return
end
모든 변수를 강제로 선언하도록 다음 명령문을 mexfunction 서브루틴에 추가합니다.
implicit none
64비트 배열에는 명시적 유형의 선언이 필요합니다.
mexfunction 인수 선언하기
mxArray 변수를 선언하려면 MATLAB 유형 mwPointer를 사용하십시오. Declarations 명령문 다음에 아래 코드를 추가합니다.
C mexFunction arguments:
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
함수와 지역 변수 선언하기
이 MEX 파일에 사용된 MATLAB API 함수의 유형과 기호 이름을 선언합니다.
C Function declarations: mwPointer mxGetDoubles mwPointer mxCreateDoubleMatrix integer mxIsNumeric mwPointer mxGetM, mxGetN함수 유형을 확인하려면 MATLAB API Function Reference 문서를 참조하십시오. 예를 들어
mxGetDoubles에 대한 문서를 참조하십시오.mexfunction인수에 대한 지역 변수를 선언합니다.C Pointers to input/output mxArrays: mwPointer x_ptr, y_ptr행렬 변수를 선언합니다.
C Array information: mwPointer mrows, ncols mwSize size
MEX 파일의 입력 인수와 출력 인수 확인하기
nrhs 인수와 nlhs 인수를 사용하여 MEX 파일의 입력 인수와 출력 인수 개수를 확인합니다. 아래 명령문을 mexfunction 코드 블록에 추가합니다.
C Check for proper number of arguments.
if(nrhs .ne. 1) then
call mexErrMsgIdAndTxt ('MATLAB:timestwo:nInput',
+ 'One input required.')
elseif(nlhs .gt. 1) then
call mexErrMsgIdAndTxt ('MATLAB:timestwo:nOutput',
+ 'Too many output arguments.')
endif
prhs 인수를 사용하여 입력 인수 유형을 확인합니다.
C Check that the input is a number.
if(mxIsNumeric(prhs(1)) .eq. 0) then
call mexErrMsgIdAndTxt ('MATLAB:timestwo:NonNumeric',
+ 'Input must be a number.')
endif
계산 루틴 만들기
timestwo 코드를 추가합니다. 이 서브루틴은 계산 루틴, 즉 MATLAB에서 사용하고자 하는 기능을 수행하는 소스 코드입니다.
C Computational routine
subroutine timestwo(y_output, x_input)
real*8 x_input, y_output
y_output = 2.0 * x_input
return
end
계산 루틴은 선택 사항입니다. 또는, 이 코드를 mexfunction 함수 블록 내에 넣을 수 있습니다.
계산 루틴의 변수 선언하기
mexFunction에 다음 변수 선언을 넣으십시오.
C Arguments for computational routine:
real*8 x_input, y_output입력 배열 읽어오기
입력 행렬 데이터를 가리키려면 mxGetDoubles 함수를 사용하십시오.
x_ptr = mxGetDoubles(prhs(1))
Fortran 배열 x_input을 만들려면 mxCopyPtrToReal8 함수를 사용하십시오.
C Get the size of the input array.
mrows = mxGetM(prhs(1))
ncols = mxGetN(prhs(1))
size = mrows*ncols
C Create Fortran array from the input argument.
call mxCopyPtrToReal8(x_ptr,x_input,size)
출력 데이터 준비하기
출력 인수 plhs(1)을 만들려면 mxCreateDoubleMatrix 함수를 사용하십시오.
C Create matrix for the return argument.
plhs(1) = mxCreateDoubleMatrix(mrows,ncols,0)
mxGetDoubles 함수를 사용하여 y_ptr 인수를 plhs(1)에 할당합니다.
y_ptr = mxGetDoubles(plhs(1))
계산 수행하기
인수를 timestwo에 전달합니다.
C Call the computational subroutine.
call timestwo(y_output, x_input)
출력 인수에 결과 복사하기
C Load the data into y_ptr, which is the output to MATLAB.
call mxCopyReal8ToPtr(y_output,y_ptr,size)
완성된 소스 파일 보기
작성한 소스 파일을 폴더에 있는 matlabroot/extern/examples/refbooktimestwo.F와 비교해 보십시오. 편집기에서 파일을 엽니다.
이진 MEX 파일 빌드하기
MATLAB 명령 프롬프트에서 이진 MEX 파일을 빌드합니다.
mex -R2018a timestwo.F
MEX 함수 테스트하기
x = 99; y = timestwo(x)
y = 198
참고 항목
mexfunction | mwPointer | mwSize | mxIsNumeric | mxGetM | mxGetN | mxCreateDoubleMatrix | mxGetDoubles