MEX file (FORTRAN) calling a LAPACK routine crashed
Afficher commentaires plus anciens
I am now trying to learn some Fortran MEX coding using MATLAB 2010B on 64-bit Linux. I start with the simplest case, i.e. MATLAB calling a LAPACK subroutine, zlarfg. The code test.f90 is shown as follws. But I find it is very strange that the compilation command
mex -largeArrayDims -lifcore test.f90 -lmwlapack -lmwblas
results in a mex file which never works and makes Matlab GUI crash.
SUBROUTINE MEXFUNCTION( nlhs, plhs, nrhs, prhs )
IMPLICIT NONE
!
! .. Parameters ..
DOUBLE PRECISION ZERO, ONE
PARAMETER ( ZERO = 0.0D0, ONE = 1.0D0 )
COMPLEX*16 CZERO
PARAMETER ( CZERO = ( 0.0D0, 0.0D0 ) )
!
! .. Mex-file interface parameters ..
integer nlhs, nrhs
mwPointer plhs(*), prhs(*)
mwSignedIndex mxGetM, mxGetN, M, N
mwPointer mxCreateDoubleMatrix, mxGetPr, mxGetPi
integer mxIsNumeric, mxIsComplex
integer LDA, IP, istate
!
! .. Allocatable arrays ..
complex*16, Allocatable :: AZ(:,:), TAU(:)
COMPLEX*16 ALPHA, tau1
IP = 1
N = mxGetN( PRHS(IP) )
M = mxGetM( PRHS(IP) )
IF ( mxIsNumeric( PRHS(IP) ) == 0 ) CALL mexErrMsgTxt( 'A must be a numerical matrix' )
IF ( M /= N ) CALL mexErrMsgTxt( 'A must be a square matrix' )
IF ( mxIsComplex( PRHS(IP) ) == 0 ) CALL mexErrMsgTxt( 'A must be a complex matrix' )
LDA = N
ALLOCATE ( AZ(LDA,LDA), TAU(LDA), STAT = istate)
CALL mxCopyPtrToComplex16( mxGetPr(PRHS(1)), mxGetPi(PRHS(1)), AZ, N*N)
TAU = CZERO
IP = N
ALPHA = AZ( IP - 1, IP )
call zlarfg(IP-1, ALPHA, AZ(1:(IP-2), IP), 1, tau1)
TAU(IP-1) = tau1
open(unit=90, file='tmp1.txt')
write(90,*) 'AZ',AZ(1:IP, IP)
WRITE(90,*) 'TAU', TAU1
close(90)
PLHS(1) = mxCreateDoubleMatrix( N, N, 1 )
CALL mxCopyComplex16ToPtr(AZ, mxGetPr(PLHS(1)),mxGetPi(PLHS(1)), N*N)
PLHS(2) = mxCreateDoubleMatrix( N, 1, 1 )
CALL mxCopyComplex16ToPtr(TAU, mxGetPr(PLHS(2)),mxGetPi(PLHS(2)), LDA)
DEALLOCATE (AZ, TAU)
END
Réponse acceptée
Plus de réponses (1)
Tian
le 9 Mai 2014
0 votes
3 commentaires
James Tursa
le 9 Mai 2014
The simple advice is always look at the doc for the signature of the function you are calling, and use the exact variable types in the call. For Fortran in particular, the mwPointer type is an integer that is the same size as a pointer, and is used to hold a pointer value. So mwPointer will be a 32-bit integer in a 32-bit app and it will be a 64-bit integer in a 64-bit app. Thus it is convenient to use in the API to mimic a size_t in C for an integer in certain function calls (even though the name suggests pointer).
James Tursa
le 13 Mai 2014
You are still passing mwSize variables (I, IONE) instead of mwSignedIndex variables. Try fixing that first.
Catégories
En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!