I am trying to call the LAPACK routine dgtsv in a Fortran mex function in MATLAB2012b on 64-bit Linux. Calling the function always returns a segmentation fault.
I have reduced it to a minimal test case, shown below, by adding a call to dgtsv inside the mex file timestwo.F (an example mex file that comes with MATLAB). I know the problem is not with the mex gateway function or interface with MATLAB, since the example mex file timestwo.F runs fine when I comment out the call to dgtsv.
Further, the subroutine timestwo runs just fine when I call it from a Fortran program outside MATLAB.
Compilation:
mex -g -largeArrayDims timestwo.F -lmwlapack -lmwblas -o timestwo.mexa64
mex-file (timestwo.F):
#include "fintrf.h"C======================================================================#if 0CC timestwo.FC .F file needs to be preprocessed to generate .for equivalentC#endifCC timestwo.fCC Computational function that takes a scalar and doubles it.C This is a MEX-file for MATLAB.C Copyright 1984-2011 The MathWorks, Inc.C $Revision: 1.12.2.9 $C======================================================================C Gateway routine subroutine mexFunction(nlhs, plhs, nrhs, prhs)C Declarations implicit noneC mexFunction arguments: mwPointer plhs(*), prhs(*) integer nlhs, nrhsC Function declarations: mwPointer mxGetPr mwPointer mxCreateDoubleMatrix integer mxIsNumeric mwPointer mxGetM, mxGetNC Pointers to input/output mxArrays: mwPointer x_ptr, y_ptrC Array information: mwPointer mrows, ncols mwSize sizeC Arguments for computational routine: real*8 x_input, y_outputC-----------------------------------------------------------------------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.') endifC Validate inputsC Check that the input is a number. if(mxIsNumeric(prhs(1)) .eq. 0) then call mexErrMsgIdAndTxt ('MATLAB:timestwo:NonNumeric', + 'Input must be a number.') endifC Get the size of the input array. mrows = mxGetM(prhs(1)) ncols = mxGetN(prhs(1)) size = mrows*ncolsC Create Fortran array from the input argument. x_ptr = mxGetPr(prhs(1)) call mxCopyPtrToReal8(x_ptr,x_input,size)C Create matrix for the return argument. plhs(1) = mxCreateDoubleMatrix(mrows,ncols,0) y_ptr = mxGetPr(plhs(1))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) return endC Computational routine subroutine timestwo(y_output, x_input) real*8 x_input, y_output integer*4 :: n, info, nrhs, ldb double precision, dimension(10) :: d, b double precision, dimension(9) :: dl, du n = 10 nrhs = 1 ldb = 10 d = 1. dl = 0. du = 0. b = 5. call dgtsv (n, nrhs, dl, d, du, b, ldb, info) y_output = 2.0 * x_input return end
Best Answer