MEX gfortran type mismatch error
4 views (last 30 days)
Show older comments
I have a fortran file which compiles cleanly on Windows with Intel, and compiles successfully but with a type mismatch error on Ubuntu with gfortran. It runs correctly on Windows. It crashes with a memory violation on Ubuntu. I have been using the same gateway code for a while, and it had been working for some time. Code is below. The warning I get when compiling is
>> mex FFLAGS="\$FFLAGS -ffree-line-length-none" LDFLAGS="\$LDFLAGS" construct_c_P_update.F90
Building with 'gfortran'.
/home/cfuser/ayurukog/CPv2/construct_c_P_update.F90:64:37:
call construct_c_P_update(%val(c_out), %val(P_update), %val(initial_P), %val(w), %val(gamma), %val(gamma_til), %val(gam_for_P), %val(z), %val(theta), %val(d), %val(use_tau), N, K)
1
Warning: Type mismatch in argument ‘c_out’ at (1); passed INTEGER(8) to REAL(8)
MEX completed successfully.
The error when crashing is
>> [c, P_update]=construct_c_P_update( P_update, w, gamma, gamma_til, gam_for_P, z, theta, d, 1+tau);
tol = 1.39999998
tol = 1.0000000000000000
tol = 1
tol = 6226.2719394719270
done tol = 6226.2719394719270
*** Error in `/home/.software/MATLAB-R2016a/bin/glnxa64/MATLAB': free(): invalid next size (normal): 0x00007f23b2a157d0 ***
... wall of text ...
I'm sure I am doing something wrong, but not sure what! Anyone know what is going wrong? Much appreciated!!
Also the version on Windows is R2018b with Intel Fortran, the version on Ubuntu is r2016b with gfortran.
Best,
Ali
#include <fintrf.h>
!
! Matlab call : [c, P_update]=construct_c_P_update( initial_P, w, gamma, gamma_til, gam_for_P, z, theta, d, tau)
!
! initial_P N x K
! w N x 1
! gamma N x (K+1) x K
! gamma_til N x K
! gam_for_P K x 1
! z N x K
! theta K x 1
! d N x N x K
! tau N x N x K
! Output
! c N x K
! P_update N x K
! Gateway function to import matlab arrays, define outputs, error checking, and call numerical subroutines
subroutine mexfunction(nlhs,plhs,nrhs,prhs)
implicit none
integer nlhs,nrhs, dims(3)
integer*4 complexflag, classid
mwpointer plhs(*),prhs(*)
mwpointer mxgetpr, mxcreatecellarray, mxcreatedoublematrix, mxcreatedoublescalar, mxCreateNumericArray, mxClassIDFromClassName, mxCreateNumericMatrix
mwpointer initial_P, w, gamma, gamma_til, gam_for_P, z, theta, d, use_tau
mwpointer c_out, P_update
mwsize mxgetm, mxgetn
mwsize N, K
! RHS parameters
initial_P = mxgetpr(prhs(1))
w = mxgetpr(prhs(2))
gamma = mxgetpr(prhs(3))
gamma_til = mxgetpr(prhs(4))
gam_for_P = mxgetpr(prhs(5))
z = mxgetpr(prhs(6))
theta = mxgetpr(prhs(7))
d = mxgetpr(prhs(8))
use_tau = mxgetpr(prhs(9))
N=mxgetm(prhs(1))
K=mxgetn(prhs(1))
!dims(1)=N
!dims(2)=N
!dims(3)=K
classid = mxClassIDFromClassName('double')
complexflag = 0
PLHS(1) = mxCreateNumericMatrix(N, K,classid,complexflag)
PLHS(2) = mxCreateNumericMatrix(N, K,classid,complexflag)
!PLHS(1) = MXCREATEDOUBLEMATRIX(N,K,complexflag)
!PLHS(2) = MXCREATEDOUBLEMATRIX(N,K,complexflag)
c_out = MXGETPR(PLHS(1))
P_update = MXGETPR(PLHS(2))
call construct_c_P_update(%val(c_out), %val(P_update), %val(initial_P), %val(w), %val(gamma), %val(gamma_til), %val(gam_for_P), %val(z), %val(theta), %val(d), %val(use_tau), N, K)
return
end
subroutine construct_c_P_update(c_out, P_update, initial_P, w, gamma, gamma_til, gam_for_P, z, theta, d, use_tau, N, K)
use omp_lib
implicit none
mwsize N, K
integer i, j, kk, mm, nn, matrix_row, matrix_column
double precision P_update(N,K), c_out(N,K)
double precision initial_P(N,K), w(N), gamma(N,K+1,K) , gamma_til(N,K), gam_for_P(K), z(N,K), theta(K), d(N,N,K), use_tau(N,N,K)
double precision phi(N,K), P_hold(N,K), pre_P_tol(N), P_tol
integer*4, external :: mexPrintf
character(len=80) :: line
write(line,*) "tol = ", 1.4
nn = mexPrintf(line//achar(10))
P_hold=initial_P
P_tol=1d0
mm=1
write(line,*) "tol = ", P_tol
nn = mexPrintf(line//achar(10))
write(line,*) "tol = ", mm
i = mexPrintf(line//achar(10))
do nn=1,N
do kk=1,K
!c(nn,kk)=gamma_til(nn,kk)*(w(nn)**gamma(nn,K+1,kk))*product(P_hold(nn,:)**gamma(nn,1:K,kk))
c_out(nn,kk)=1
end do
end do
do nn=1,N
do kk=1,K
!phi(nn,kk)= SUM( ((c(:,kk)/z(:,kk))*d(:,nn,kk)*use_tau(:,nn,kk))**(-theta(kk)) )
!P_update(nn,kk)=gam_for_P(kk)*(phi(nn,kk)**(-1/theta(kk)))
P_update(nn,kk)=3.1
end do
end do
pre_P_tol=MAXVAL(ABS(P_update-P_hold),1)
P_tol=MAXVAL(pre_P_tol)
P_hold=P_update
write(line,*) "tol = ", P_tol
j = mexPrintf(line//achar(10))
write(line,*) "done tol = ", P_tol
j = mexPrintf(line//achar(10))
return
end
0 Comments
Answers (3)
James Tursa
on 29 Oct 2018
The only thing funny I see in your code at first glance are these function return type declarations:
mwsize mxgetm, mxgetn
and
mwpointer ... mxClassIDFromClassName ...
The signature from the doc is this:
mwPointer mxGetM(pm)
mwPointer pm
and
integer*4 mxClassIDFromClassName(classname)
character*(*) classname
So you have a mismatch in the return type for these. As long as mwSize compiles the same as mwPointer you will be fine for the mxGetM and mxGetN. I am guessing this is true for your working code (both are same size integers). But if mwSize happens to compile as 32-bit and mwPointer compiles as 64-bit, then you will have a mismatch which could lead to downstream problems. Similar comments for mxClassIDFromClassName. It may not make a difference but to rule it out I would suggest you start by changing your return type declarations for these functions to match the doc exactly:
mwPointer mxgetm, mxgetn
integer*4 mxClassIDFromClassName
The warning for the argument type is perhaps explainable. You are passing an 8-byte integer by value to a routine that is expecting an address to a real(8) type (since it is an implicit interface), and the compiler isn't smart enough to realize that there is an address in that integer value. Have you used %VAL( ) before with this compiler? Have you gotten any such warning before?
0 Comments
See Also
Categories
Find more on Access Fortran Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!