octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Mex Fortran Interface


From: Alexander Barth
Subject: Mex Fortran Interface
Date: Mon, 08 Jan 2007 21:41:31 -0500
User-agent: Thunderbird 1.5.0.7 (X11/20060913)

Hi all,

I'm planning to interface a Fortran program to octave and matlab using the MEX 
API. I tried to
compile the example programs yprimef.F from matlab in octave 2.9.9 with 
"mkoctfile --mex". I managed
to get a working mex file by writing some small wrapper functions around the 
MEX C interface
(mex_fortran.cc). The file fintrf.h just defines the macro MWPOINTER depending 
if you have a 32 or
64 bit system.

I think it would be nice to have those functions in octave.

Alex


-- 
_______________________________________________________________

  Alexander Barth

  Ocean Circulation Group
  University of South Florida
  College of Marine Science
  140 Seventh Avenue South
  St. Petersburg, Florida  33701
  USA

  Phone: +1-727-553-3508     FAX:   +1-727-553-1189

_______________________________________________________________
#include <bits/wordsize.h> /* __WORDSIZE */

/* MWPOINTER integer of the same size as a pointer */

#if __WORDSIZE == 64
#  define MWPOINTER integer*8
#else
#  define MWPOINTER integer*4
#endif
#include "mex.h"
// for intprt_t 
#include <stdint.h>
#include <octave/oct.h>
#include "f77-fcn.h"

extern "C" 
{

  int
  F77_FUNC (mxgetm, MXGETM) (intptr_t& ptr);
  
  int
  F77_FUNC (mxgetn, MXGETN) (intptr_t& ptr);

  intptr_t 
  F77_FUNC (mxcreatedoublematrix, MXCREATEDOUBLEMATRIX) (int& m, int& n, int& 
flag);

  intptr_t 
  F77_FUNC (mxgetpr,MXGETPR) (intptr_t& ptr);

  void 
  F77_FUNC (mxcopyptrtoreal8, MXCOPYPTRTOREAL8) (intptr_t& ptr,double* d,int& 
count);

  void
  F77_FUNC (mxcopyreal8toptr, MXCOPYREAL8TOPTR) (double* d,intptr_t& ptr,int& 
count);

  void 
  F77_FUNC (mexerrmsgtxt, MEXERRMSGTXT) (F77_CONST_CHAR_ARG_DECL 
F77_CHAR_ARG_LEN_DECL);

}

int 
F77_FUNC (mxgetm, MXGETM) (intptr_t& ptr)
{
  return mxGetM((mxArray*)ptr);
}

int 
F77_FUNC (mxgetn, MXGETN) (intptr_t& ptr)
{
  return mxGetN((mxArray*)ptr);
}

intptr_t 
F77_FUNC (mxcreatedoublematrix, MXCREATEDOUBLEMATRIX) (int& m, int& n, int& 
flag)
{
  return (intptr_t)mxCreateDoubleMatrix 
    (m,n,(mxComplexity)flag);
}

intptr_t 
F77_FUNC (mxgetpr,MXGETPR) (intptr_t& ptr)
{
  return (intptr_t)mxGetPr((mxArray*)ptr);
}

void 
F77_FUNC (mxcopyptrtoreal8, MXCOPYPTRTOREAL8) (intptr_t& ptr,double* d,int& 
count)
{
  double* s = (double*)ptr;

  for (int i=0; i<count; i++)
    d[i] = s[i];

}

void
F77_FUNC (mxcopyreal8toptr, MXCOPYREAL8TOPTR) (double* d,intptr_t& ptr,int& 
count)
{
  double* s = (double*)ptr;

  for (int i=0; i<count; i++)
    s[i] = d[i];

}

void 
F77_FUNC (mexerrmsgtxt, MEXERRMSGTXT) (F77_CONST_CHAR_ARG_DECL s 
F77_CHAR_ARG_LEN_DECL len) {

  // do we need to add \0 at the end of s?
  mexErrMsgTxt(s);
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]