I am using Matlab R2013a 64bit. I am writing a mex function in fortran. I am using the Intel Visual Fortran 14.0 compiler. I am calling a BLAS function. I have the following problem: when I call the BLAS function from the main body of the mex function, it works; but when I call it from a different routine, it crashes Matlab. The following is a minimal code showing this problem, calling the BLAS function dnrm2.
#include "fintrf.h"
!============================================================================
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
implicit none
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
mwPointer mxCreateDoubleScalar, mxGetPr, mxGetM
integer n
mwPointer x
double precision norm1x, norm2x
double precision dnrm2, compute_norm
integer complexFlag
complexFlag = 0
n = int4(mxGetM(prhs(1)))
x = mxGetPr(prhs(1))
norm1x = dnrm2(n, %val(x), 1) ! WORKS
plhs(1) = mxCreateDoubleScalar(norm1x)
norm2x = compute_norm(n, %val(x)) ! DOES NOT WORK
plhs(2) = mxCreateDoubleScalar(norm2x)
end subroutine mexFunction
!============================================================================
function compute_norm(n, x)
implicit none
integer n
double precision x(n)
double precision compute_norm, dnrm2
compute_norm = dnrm2(n, x, 1)
end function compute_norm
To build, I use
mex -v -largeArrayDims mynorm.F90 'C:\Program Files\MATLAB\R2013a\extern\lib\win64\microsoft\libmwblas.lib'
I am wondering what might be the reason. Thank you.
Best Answer