Re: speeding up wrapper for "new" datatype



On 12 abr, 11:44, Tamas K Papp <tkp...@xxxxxxxxx> wrote:
Hi,

I was cleaning up code that interfaces to BLAS/LAPACK/UMFPack
libraries, and it occured to me that I could unify the interface for
various special matrices using CLOS.  The idea is that I create a
matrix class, and specialize that as needed, including dense matrices
which are really just wrappers around 2d arrays.

If possible, I would like dense-matrix element access to be (nearly)
as fast as aref, but I don't know how.  I thought of compiler macros,
but I can't think of an advantageous transformation.  As a general
issue, I am wondering if it is possible to make "new" datatypes as
fast as "native" ones.

I don't know if this is a great idea and to what extent this would
work, but you can do something more or less the way CFFI does it:

(define-compiler-macro mref (&whole whole matrix i j)
(or (mref-expand (get-declared-type-of-or-give-up matrix) matrix i
j)
whole))

(defgeneric mref-expand (matrix-type matrix i j)
(:method ((matrix-type t) matrix i j)
nil))

(defmethod mref-expand ((matrix-type (eql 'dense-matrix)) matrix i j)
`(aref (elements ,matrix) ,i ,j))

The problem would be to get the declared type of some element. There
is http://common-lisp.net/project/parse-declarations/ , but some outer
macro will need to get these declarations and pass them along.

An alternative is to use some implementation-dependant feature (SBCL's
deftransform seems to fit very well for this purpose, just look at a
couple of them from SBCL's source code and you would see how they
work).

I include some code snippets and benchmarks below, help is
appreciated, I am using SBCL but I am also interested in general
techniques - when I package this up as a library (eventually), I would
prefer if it was general to all implementations.

Thanks,

Tamas

.