Hello, all.  I'm trying to bind some BLAS/LAPACK primitives into
Ikarus to support some machine learning algorithms, and I've got a
question about best practices.

It's pretty easy to use the FFI to call LAPACK functions.  What I'm
having trouble with is how to do this the "right" way.

Ultimately, I'd like to be able to write code like the following:

(define a (make-blas-vector 10))  ; create a length 10 vector
(define b (make-blas-vector 10))
; ...populate with data...
(define c (* (+ a b) 5))   ; <-- this is the important part!

What I do NOT want to have to worry about is the temporary vector
which is created as a result of calling (+ a b).

That is, under the hood, make-blas-vector is going to malloc() some
memory, or possibly allocate something via an appropriate FFI
function.  It's easy to overload the + operator to call a BLAS vector
adder, and to create a new BLAS vector to store the result of the
addition.

But I don't want to have to manually free that temporary vector.  When
it's no longer used / goes out of scope, I want it to be garbage-
collected.

The problem is that I need to "know" when it's being garbage
collected, because I need to trigger a low-level free() operation.

So I think that what I want is to create a new native datatype, and
register appropriate constructors and destructors along with it.

What's the best way to accomplish this (or is my thinking off)?  I've
poked around, and have not (unsurprisingly) found an easy way to do it
within user-land ikarus.  My guess is that I'll have to hack something
more low-level, but I'd like to avoid that if at all possible.

Thanks!

                            -- David

Reply via email to