calling mex function multiple times

1 次查看(过去 30 天)
Sivaram
Sivaram 2014-6-18
Hi i have a standalone fortran code and i have written a mex interface function to access it from MATLAB. I have wrapped this mex function around the matlab optimizer. The optimizer calls this mex function multiple times but i have noticed something abnormal happening after 20-30 call's. The mex function returns me wrong values. When i stop the simulation there and just run the mex function with previous input value it gives me the right answer. i assume there is some error accummulation of sort. I guess the mex function is also not cleared after i exit the mex file. I am furnishing below my mex interface code, can some one help me with this.
#define __LP64__
#include "fintrf.h"
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
IMPLICIT NONE
MWPOINTER :: PLHS(*),PRHS(*)
INTEGER*4 :: NLHS,NRHS ! REMAINS THE SAME FOR 64BIT system
MWSIZE,PARAMETER::iwp=SELECTED_REAL_KIND(15)
MWSIZE :: m, n, ndim, err
MWSIZE :: dims(3)
MWSIZE, EXTERNAL :: MXGETM, MXGETN
MWPOINTER :: A_pr,B_pr
MWPOINTER, EXTERNAL :: MXGETPR,MXCREATENUMERICARRAY
REAL(iwp), ALLOCATABLE :: A(:,:),B(:,:,:)
INTEGER*4 :: CLASSID, COMPLEXFLAG, MXCLASSIDFROMCLASSNAME
#if defined MSWIND
INTEGER(2) CONTROL
CALL GETCONTROLFPQQ(CONTROL)
CONTROL = CONTROL .OR. FPCW$ZERODIVIDE
CONTROL = CONTROL .OR. FPCW$INVALID
CONTROL = CONTROL .OR. FPCW$OVERFLOW
CALL SETCONTROLFPQQ(CONTROL)
#endif
IF (NRHS .NE. 1) THEN
CALL MEXERRMSGTXT('MultMexError: 1 INPUT ARGUMENT IS REQUIRED')
ENDIF
IF (NLHS .NE. 1) THEN
CALL MEXERRMSGTXT('MultMexError: 1 OUTPUT ARGUMENT IS REQUIRED')
ENDIF
A_pr = MXGETPR(PRHS(1))
m = MXGETM(PRHS(1))
n = MXGETN(PRHS(1))
ALLOCATE( A(m,n), STAT = err )
IF (err .NE. 0) THEN
CALL MEXERRMSGTXT('MultMexError: Out of memory A')
ENDIF
ALLOCATE( B(m,n,m), STAT = err)
IF (ERR .NE. 0) THEN
CALL MEXERRMSGTXT('MultMexError: Out of memory B')
END IF
CALL MXCOPYPTRTOREAL8(A_pr, A, m*n)
CLASSID = MXCLASSIDFROMCLASSNAME('double')
COMPLEXFLAG = 0
ndim = 3
dims(1) = m
dims(2) = n
dims(3) = m
PLHS(1) = MXCREATENUMERICARRAY(ndim,dims,CLASSID,COMPLEXFLAG)
B_pr = MXGETPR(PLHS(1))
CALL timesmultmex(A,B,m,n)
CALL MXCOPYREAL8TOPTR(B,B_pr,m*n*m)
DEALLOCATE(A)
RETURN
END SUBROUTINE MEXFUNCTION

回答(1 个)

James Tursa
James Tursa 2014-6-18
Try changing this line:
MWSIZE, EXTERNAL :: MXGETM, MXGETN
to this:
MWPOINTER, EXTERNAL :: MXGETM, MXGETN
This will match the signature in the doc exactly.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by