Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
MEX FORTRAN programming

Subject: MEX FORTRAN programming

From: Weijie

Date: 24 Oct, 2007 16:48:44

Message: 1 of 6

This is probably a least discussed/documented topic, but it
could be very useful as we have so many (old) Fortran code
in scientific computation and we have so few Fortran
programmers nowadays. I'm posting here on what I plan to do
and your experience/hints may save my time.

In Matlab, I would like to call
M>[r1,r2...]=myfnc(v1,str1,str2,par1,par2,mpar1,mpar2,mpar3...)
where myfnc is a compiled MEX function that use mexFunction
and old Fortran code to do the computation task; v1 is a
vector parameter, str1 and str2 are two strings representing
the names of two Matlab functions (.m files). par1 and par2
are other parameters to myfnc; mpar1,mpar2...are input
arguments to str1.m and str2.m (assume they have exactly the
same arguments). In the mex function, I'll call a Fortran
function (previously developed and tested):

call oldfnc(n, d, x, objf, objg, iv, liv, lv, v, uip,urp,ufp)

where objf and objg are two Fortran functions and uip, urp,
and ufp are user-defined parameters input to objf and objg.

Now, in the Fortran functions objf and objg that I define, I
would like to use mexCallMATLAB(...) to call str1.m and
str2.m respectively.

It's easy to see that once I done all these, the users can
define their own str1.m and str2.m and call myfnc to do
thier job using the old Fortran package.

The key difficulty is that: how to transfer mpar1,
mpar2...to uip, urp, and ufp in mes function AND then, in
objf and objg, how to transfer uip, urp, and ufp to input
arguments of mexCallMATLAB function?

Thanks a million! I'd share my experience once I complete
this project.

Subject: MEX FORTRAN programming

From: dpb

Date: 24 Oct, 2007 19:23:32

Message: 2 of 6

Weijie wrote:
...
> In Matlab, I would like to call
> M>[r1,r2...]=myfnc(v1,str1,str2,par1,par2,mpar1,mpar2,mpar3...)
> where myfnc is a compiled MEX function that use mexFunction
> and old Fortran code to do the computation task; v1 is a
> vector parameter, str1 and str2 are two strings representing
> the names of two Matlab functions (.m files). par1 and par2
> are other parameters to myfnc; mpar1,mpar2...are input
> arguments to str1.m and str2.m (assume they have exactly the
> same arguments). In the mex function, I'll call a Fortran
> function (previously developed and tested):
>
> call oldfnc(n, d, x, objf, objg, iv, liv, lv, v, uip,urp,ufp)
>
> where objf and objg are two Fortran functions and uip, urp,
> and ufp are user-defined parameters input to objf and objg.
>
> Now, in the Fortran functions objf and objg that I define, I
> would like to use mexCallMATLAB(...) to call str1.m and
> str2.m respectively.
>
> It's easy to see that once I done all these, the users can
> define their own str1.m and str2.m and call myfnc to do
> thier job using the old Fortran package.
>
> The key difficulty is that: how to transfer mpar1,
> mpar2...to uip, urp, and ufp in mes function AND then, in
> objf and objg, how to transfer uip, urp, and ufp to input
> arguments of mexCallMATLAB function?
>
> Thanks a million! I'd share my experience once I complete
> this project.

The parameters to the called function would have to be wrapped in
mxArray per the calling protocol as expected by mexCallMATLAB. The user
parameters and functions have to be aware of the expectations on both
the caller and the callee side. Other than that, I don't see the problem?

--

Subject: MEX FORTRAN programming

From: Weijie

Date: 24 Oct, 2007 20:51:02

Message: 3 of 6

dpb <none@non.net> wrote in message <ffo69s$2pf$1@aioe.org>...
> Weijie wrote:
> ...
> > In Matlab, I would like to call
> >
M>[r1,r2...]=myfnc(v1,str1,str2,par1,par2,mpar1,mpar2,mpar3...)
> > where myfnc is a compiled MEX function that use mexFunction
> > and old Fortran code to do the computation task; v1 is a
> > vector parameter, str1 and str2 are two strings representing
> > the names of two Matlab functions (.m files). par1 and par2
> > are other parameters to myfnc; mpar1,mpar2...are input
> > arguments to str1.m and str2.m (assume they have exactly the
> > same arguments). In the mex function, I'll call a Fortran
> > function (previously developed and tested):
> >
> > call oldfnc(n, d, x, objf, objg, iv, liv, lv, v,
uip,urp,ufp)
> >
> > where objf and objg are two Fortran functions and uip, urp,
> > and ufp are user-defined parameters input to objf and objg.
> >
> > Now, in the Fortran functions objf and objg that I define, I
> > would like to use mexCallMATLAB(...) to call str1.m and
> > str2.m respectively.
> >
> > It's easy to see that once I done all these, the users can
> > define their own str1.m and str2.m and call myfnc to do
> > thier job using the old Fortran package.
> >
> > The key difficulty is that: how to transfer mpar1,
> > mpar2...to uip, urp, and ufp in mes function AND then, in
> > objf and objg, how to transfer uip, urp, and ufp to input
> > arguments of mexCallMATLAB function?
> >
> > Thanks a million! I'd share my experience once I complete
> > this project.
>
> The parameters to the called function would have to be
wrapped in
> mxArray per the calling protocol as expected by
mexCallMATLAB. The user
> parameters and functions have to be aware of the
expectations on both
> the caller and the callee side. Other than that, I don't
see the problem?
>

The problem is just how to wrap-up the parameters. I forgot
to note that uip must be an integer vector, and urp must be
a float vector. To avoid the pack-unpack, I tried to use the
static (global) variables like the following:

      MODULE global
        character*10 :: MATLAB_OBJ_NAME,MATLAB_GRAD_NAME
        mwPointer :: mprhs, mplhs
      END MODULE global

The idea is that, then in the mexFunction, I got the
contents of MATLAB_OBJ_NAME from str1, and other input
arguments from prhs (e.g., mprhs=prhs(3)). Then in the objf,
I set the nlhs and nrhs and together with the global
variables mprhs, mplhs, and MATLAB_OBJ_NAME to call
mexCallMATLAB function. But it's not working so far....am I
in the right direction?

Subject: MEX FORTRAN programming

From: Weijie

Date: 24 Oct, 2007 20:52:05

Message: 4 of 6

dpb <none@non.net> wrote in message <ffo69s$2pf$1@aioe.org>...
> Weijie wrote:
> ...
> > In Matlab, I would like to call
> >
M>[r1,r2...]=myfnc(v1,str1,str2,par1,par2,mpar1,mpar2,mpar3...)
> > where myfnc is a compiled MEX function that use mexFunction
> > and old Fortran code to do the computation task; v1 is a
> > vector parameter, str1 and str2 are two strings representing
> > the names of two Matlab functions (.m files). par1 and par2
> > are other parameters to myfnc; mpar1,mpar2...are input
> > arguments to str1.m and str2.m (assume they have exactly the
> > same arguments). In the mex function, I'll call a Fortran
> > function (previously developed and tested):
> >
> > call oldfnc(n, d, x, objf, objg, iv, liv, lv, v,
uip,urp,ufp)
> >
> > where objf and objg are two Fortran functions and uip, urp,
> > and ufp are user-defined parameters input to objf and objg.
> >
> > Now, in the Fortran functions objf and objg that I define, I
> > would like to use mexCallMATLAB(...) to call str1.m and
> > str2.m respectively.
> >
> > It's easy to see that once I done all these, the users can
> > define their own str1.m and str2.m and call myfnc to do
> > thier job using the old Fortran package.
> >
> > The key difficulty is that: how to transfer mpar1,
> > mpar2...to uip, urp, and ufp in mes function AND then, in
> > objf and objg, how to transfer uip, urp, and ufp to input
> > arguments of mexCallMATLAB function?
> >
> > Thanks a million! I'd share my experience once I complete
> > this project.
>
> The parameters to the called function would have to be
wrapped in
> mxArray per the calling protocol as expected by
mexCallMATLAB. The user
> parameters and functions have to be aware of the
expectations on both
> the caller and the callee side. Other than that, I don't
see the problem?
>

The problem is just how to wrap-up the parameters. I forgot
to note that uip must be an integer vector, and urp must be
a float vector. To avoid the pack-unpack, I tried to use the
static (global) variables like the following:

      MODULE global
        character*10 :: MATLAB_OBJ_NAME,MATLAB_GRAD_NAME
        mwPointer :: mprhs, mplhs
      END MODULE global

The idea is that, then in the mexFunction, I got the
contents of MATLAB_OBJ_NAME from str1, and other input
arguments from prhs (e.g., mprhs=prhs(3)). Then in the objf,
I set the nlhs and nrhs and together with the global
variables mprhs, mplhs, and MATLAB_OBJ_NAME to call
mexCallMATLAB function. But it's not working so far....am I
in the right direction?

Subject: MEX FORTRAN programming

From: James Tursa

Date: 25 Oct, 2007 04:51:05

Message: 5 of 6

On Wed, 24 Oct 2007 20:52:05 +0000 (UTC), "Weijie "
<mricad@yahoo.no000spppam.com> wrote:

>dpb <none@non.net> wrote in message <ffo69s$2pf$1@aioe.org>...
>> Weijie wrote:
>> ...
>> > In Matlab, I would like to call
>> >
>M>[r1,r2...]=myfnc(v1,str1,str2,par1,par2,mpar1,mpar2,mpar3...)
>> > where myfnc is a compiled MEX function that use mexFunction
>> > and old Fortran code to do the computation task; v1 is a
>> > vector parameter, str1 and str2 are two strings representing
>> > the names of two Matlab functions (.m files). par1 and par2
>> > are other parameters to myfnc; mpar1,mpar2...are input
>> > arguments to str1.m and str2.m (assume they have exactly the
>> > same arguments). In the mex function, I'll call a Fortran
>> > function (previously developed and tested):
>> >
>> > call oldfnc(n, d, x, objf, objg, iv, liv, lv, v,
>uip,urp,ufp)
>> >
>> > where objf and objg are two Fortran functions and uip, urp,
>> > and ufp are user-defined parameters input to objf and objg.
>> >
>> > Now, in the Fortran functions objf and objg that I define, I
>> > would like to use mexCallMATLAB(...) to call str1.m and
>> > str2.m respectively.
>> >
>> > It's easy to see that once I done all these, the users can
>> > define their own str1.m and str2.m and call myfnc to do
>> > thier job using the old Fortran package.
>> >
>> > The key difficulty is that: how to transfer mpar1,
>> > mpar2...to uip, urp, and ufp in mes function AND then, in
>> > objf and objg, how to transfer uip, urp, and ufp to input
>> > arguments of mexCallMATLAB function?
>> >
>> > Thanks a million! I'd share my experience once I complete
>> > this project.
>>
>> The parameters to the called function would have to be
>wrapped in
>> mxArray per the calling protocol as expected by
>mexCallMATLAB. The user
>> parameters and functions have to be aware of the
>expectations on both
>> the caller and the callee side. Other than that, I don't
>see the problem?
>>
>
>The problem is just how to wrap-up the parameters. I forgot
>to note that uip must be an integer vector, and urp must be
>a float vector. To avoid the pack-unpack, I tried to use the
>static (global) variables like the following:
>
> MODULE global
> character*10 :: MATLAB_OBJ_NAME,MATLAB_GRAD_NAME
> mwPointer :: mprhs, mplhs
> END MODULE global
>
>The idea is that, then in the mexFunction, I got the
>contents of MATLAB_OBJ_NAME from str1, and other input
>arguments from prhs (e.g., mprhs=prhs(3)). Then in the objf,
>I set the nlhs and nrhs and together with the global
>variables mprhs, mplhs, and MATLAB_OBJ_NAME to call
>mexCallMATLAB function. But it's not working so far....am I
>in the right direction?

I am still not quite sure what the basic problem is. Are you simply
trying to convert mexFunction input prhs() mxArray pointers into
Fortran variables? And then at some point later on convert Fortran
variables into mxArray pointers for use with the mexCallMATLAB
function? Are you doing this only for integer scalar, integer rank 1,
rank 2, ...? Float scalar, rank 1, 2, ...? Just real or possibly
complex? Do you need your code to check to see if integer compiles as
integer*4 or integer*8? Do you need to handle dynamically sized
variables (i.e., allocatable) or will they always be the same fixed
size? Etc etc. And what do you mean by "not working?" Are you getting
the MATLAB version of a stack dump? Is it not compiling? I can help
with all of this if you can be more specific.

James Tursa

Subject: MEX FORTRAN programming

From: dpb

Date: 25 Oct, 2007 14:30:48

Message: 6 of 6

Weijie wrote:
...
> The problem is just how to wrap-up the parameters. ...
...
> ...But it's not working so far....am I
> in the right direction?

Can't tell -- as James says, it's not clear what "not working" means nor
where your problems really are from your description.

What would be simplest to try to follow/debug would be a _minimal_
outline of the code to read rather than the verbal description. Include
actual error messages, not translations of them.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us