Segmentation fault when calling LAPACK routine in Fortran mex file
2 views (last 30 days)
Show older comments
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 0
C
C timestwo.F
C .F file needs to be preprocessed to generate .for equivalent
C
#endif
C
C timestwo.f
C
C 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 none
C mexFunction arguments:
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
C Function declarations:
mwPointer mxGetPr
mwPointer mxCreateDoubleMatrix
integer mxIsNumeric
mwPointer mxGetM, mxGetN
C Pointers to input/output mxArrays:
mwPointer x_ptr, y_ptr
C Array information:
mwPointer mrows, ncols
mwSize size
C Arguments for computational routine:
real*8 x_input, y_output
C-----------------------------------------------------------------------
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
C Validate inputs
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
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.
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
end
C 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
0 Comments
Accepted Answer
James Tursa
on 4 Mar 2013
Edited: James Tursa
on 4 Mar 2013
Since you are using -largeArrayDims and on a 64-bit system, you may need to make sure the integers you are psssing to dgtsv are 8-bytes each. Try this instead:
integer*8 :: n, info, nrhs, ldb
Or use the type mwSignedIndex instead of integer*8, which should work regardless of machine or installation.
Side Note: The timestwo.F routine itself only works for scalar inputs, even though the code looks like it should work for arrays, because x_input and y_output are only scalars.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!