[Openmcl-devel] Accession already allocated arrays

Grégory Vanuxem g.vanuxem at gmail.com
Sun Feb 5 23:46:41 PST 2023


Hello,

Many, many thanks all!

I was really unclear, I must admit...
(I'm writing _draft_ code code for now, I was not inclined to speak about this)

Gary Byers, what you have tried to achieve is calling FriCAS and its
library from C or any other languages I guess.
SAGE does that, but I do not know actually how that has evolved.
Martin Rubey and Bill Page worked on that with WIlliam Stein and its
teams. From memory that was via shell redirections,  strings parsing
etc., things that I'm not a big fan of though I use that with VSCode
for interpreting/compiling files ('code' for the purely open source
version). Your approach with swig seems to me more effective.

What I'm working on is to add Julia [1] support to FriCAS, essentially
for heavy numerical computations. Axiom and forks are CAS that use a
strongly typed language for its "Algebra" library. This library mimics
Mathematical structures, for example it has Set, OrderedSet,
Commutative or not Rings, Fields etc., and descendants, Integer,
PrimeField, Uni or Multi Polynomial Rings over arbitrary rings... It's
principally Algebra oriented, it's a CAS. But it also supports the
usual Common Lisp data types as it is built on top of a Common Lisp
implementation (Clozure CL, SBCL, ECL etc.)

I do not need to interface different libraries since Julia has them
and the necessary stuff for my needs. So, for example, instead of
interfacing a BLAS/LAPACK library, I can have it easily at hand
without messing with  FFI  dialects and library interfaces.

So I'm using the Julia C interface [2] and have implemented support
for usual datas, but for arrays I'm stuck.
I tried the (ccl::%address-of ...) to obtain the address of array
_data_ , thanks Ron Garret, but I do not really understand what is
going on since I do not obtain the same address if I repeat the
operation, I'm not a Lisp specialist and I think I'm not doing what is
required:
The address to the array seems ok, but I want to give to Julia the
address to the first element of the array data, not what is
surrounding datas, I guess data type, dimensions etc.

? (setq a (make-array '(4 4) :element-type :double-float))
#2A((0 0 0 0) (0 0 0 0) (0 0 0 0) (0 0 0 0))
(ccl::%int-to-ptr (ccl::%address-of 'a))
#<A Foreign Pointer #x3020004DAD9E>
? (ccl::%int-to-ptr (ccl::%address-of 'a))
#<A Foreign Pointer #x3020004DAD9E>
? (ccl::%int-to-ptr (ccl::%address-of 'a))
#<A Foreign Pointer #x3020004DAD9E>

But:
? (ccl::%int-to-ptr (ccl::%address-of '(aref a (0 0))))
#<A Foreign Pointer #x3020004E7443>
? (ccl::%int-to-ptr (ccl::%address-of '(aref a (0 0))))
#<A Foreign Pointer #x3020004E5F93>
? (ccl::%int-to-ptr (ccl::%address-of '(aref a (0 0))))
#<A Foreign Pointer #x3020004E4AE3>

Furthermore, If I had the correct address of, here, double-float
datas, I'm asking myself how to circumvent the GC i.e. prevent array
data displacements etc. Something like, with SBCL, (with-pinned-object
...) [3].

In fact the more I'm thinking about that the more I think you're right
with (make-heap-ivector ...) and I will eventually use this data type
and create new types of Vectors and Matrices which take into account
datas, dimensions and all necessary surrounding informations. With an
ivector I can even choose the array ordering (row or column major). I
just need contiguous data not GC-ed during computations.

Let me say again, thanks a lot, that was instructive.

Regards,
__
Greg

[1]: https://julialang.org/
[2]: https://docs.julialang.org/en/v1/manual/embedding/
[3]: http://www.sbcl.org/manual/#index-alien_002dcallable_002dfunction

Le sam. 4 févr. 2023 à 06:52, R. Matthew Emerson <rme at acm.org> a écrit :
>
>
>
> > On Feb 3, 2023, at 3:56 AM, Grégory Vanuxem <g.vanuxem at gmail.com> wrote:
> >
> > Hello,
> >
> > My question is simple, how can I obtain a MACPTR to array data? For usual data type it's easy from the documentation but for arrays I do not know how.
>
> You might be able to use ccl:with-pointer-to-ivector.
>
> I wrote up a few notes on this some time back (12 years! where did the time go?) at https://trac.clozure.com/ccl/wiki/ForeignFunctionInterface#LispArraysandForeignArrays
>
>
>
>
>
> >
> > I’m adding code to FriCAS (0), a fork of Axiom (1), and the creation of arrays I’ll use is already implemented. I just need to have direct access to memory, so, in C, an address to the data stored in Clozure CL arrays.
> >
> > I insist, FriCAS arrays are already implemented, I do not want to use (make-heap-ivector …) for example.
> >
> > Regards,
> >
> > Greg
> >
> > (0): https://fricas.github.io/
> > (1): http ://axiom-developer.org/
> >
>


More information about the Openmcl-devel mailing list