octave-maintainers
[Top][All Lists]
Advanced

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

Re: some notes about mex support in Octave


From: John W. Eaton
Subject: Re: some notes about mex support in Octave
Date: Sat, 22 Jul 2006 04:31:21 -0400

Paul, thanks for your comments.  I've done some more work on the MEX
interface using a hybrid approach somewhat like I think you were
suggesting and I've just checked all my latest changes.  This is a
fairly complete reworking of your code, though some parts of the
original remain.

Most of the currently documented MEX interface is now implemented.  I
think the list of completely unimplemented functions is now just

  mxIsFromGlobalWS
  mexIsGlobal
  mexAtExit
  mexGet
  mexSet

The last two are for graphics properties that we don't have.  The
atexit function should not be too hard to write, but I didn't see it
as very necessary just now.  The check on global status will require
some work because we don't track that information with values, just
symbols in the symbol table.

The code for sparse matrices is also not quite finished.  I could use
some help writing the following functions:

  mex.cc:             mxArray_sparse::as_octave_value

  ov-bool-sparse.cc:  octave_sparse_bool_matrix::as_mxArray
  ov-cx-sparse.cc:    octave_sparse_complex_matrix::as_mxArray
  ov-re-sparse.cc:    octave_sparse_matrix::as_mxArray

Note that the arrays for row and column indices in the mxArray_sparse
class must be int but in Octave they are arrays of octave_idx_type
which can be wider than an int, so there could be trouble on 64-bit
systems.  I'm not sure what to do about this yet.  For now, just
making it work assuming that sizeof (int) == sizeof (octave_idx_type)
would be OK.

Once these four functions are written, I think the MEX interface for
sparse matrices will be complete.

The Fortran interface functions have been omitted for now.  I'd be
happy to add them back in if they are needed and/or someone would like
to help implement and test the missing functions.

The basic things I've tried all work, but I'm sure there are bugs.  I
plan to do more testing, but it seems best to get this code checked in
now and let other people look at it.

Here are some more notes about the implementation.  I'm reasonably
happy with it, but it is not set in stone.  Comments are welcome.

I decided to use a structure like this:

  class mxArray {
    // The main interface class.  The representation can be based on an
    // octave_value object or a separate object that tries to reproduce
    // the semantics of mxArray objects in Matlab more directly.

    mutable mxArray *rep;
    char *name;
    mutable bool persistent;
    // plus virtual functions that dispatch to the actual object through
    // the rep pointer
  };

  class mxArray_base : public mxArray {
    // A class to provide the default implemenation of some of the
    // virtual functions declared in the mxArray class
  };

  class mxArray_octave_value : public mxArray_base {
    // The object that handles values pass to MEX files from Octave.
    // Some methods in this class may set mutate_flag to TRUE to tell
    // the mxArray class to convert to the Matlab-style representation
    // and then invoke the method on that object instead (for example,
    // getting a pointer to real or imaginary data from a complex object
    // requires a mutation but getting a pointer to real data from a
    // real object does not).  Changing the representation causes a copy
    // so we try to avoid it unless it is really necessary.  Once the
    // conversion happens, we delete this representation, so the
    // conversion can only happen once per call to a MEX file.

    octave_value val;
    mutable bool mutate_flag;
    // Caching these does not cost much or lead to much duplicated
    // code.  For other things, we just request mutation to a
    // Matlab-style mxArray object.
    mutable mxClassID id;
    mutable char *class_name;
    mutable int ndims;
    mutable int *dims;
  };

  class mxArray_matlab : public mxArray_base
  {
    // The base clas for the Matlab-style representation.  All
    // Matlab-style objects have the following items in common.

    char *class_name;
    mxClassID id;
    int ndims;
    int *dims;
  };

  class mxArray_number : public mxArray_matlab
  {
    // Numeric, character, and logical data.

    void *pr;
    void *pi;
  };

  class mxArray_sparse : public mxArray_number
  {
    // Sparse arrays.

    int nzmax;
    int *ir;
    int *jc;
  };

  class mxArray_struct : public mxArray_matlab
  {
    // Struct arrays.

    int nfields;
    char **fields;
    mxArray **data;
  };

  class mxArray_cell : public mxArray_matlab
  {
    // Cell arrays

    mxArray **data;
  };



jwe



reply via email to

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