How best to interface with a simple C function in Fricas (SBCL)

23 views
Skip to first unread message

Neven Sajko

unread,
Dec 13, 2021, 11:48:43 AM12/13/21
to fricas-devel
Hello again,

I have an algorithm implemented in both Fricas (Spad) and in C++. The
C++ code turns out to be 13 to 14 times faster than Spad, and a single
call can last arbitrarily many minutes, depending on the parameters.
Thus I would prefer to use the C++ implementation in the rest of my
Fricas Spad code.

It's actually the case that in Spad I'm using bigints (type Integer),
while the C++ code just uses machine arithmetic, which is one reason
for the speed difference. I guess the Fricas code would be greatly
sped up if I converted it to use SingleInteger, but nonetheless I
think my time would be better spent making an interface to the C++
code. At least I know I have SIngleIntegers as an option if this
fails.

I'd prefer the interfacing to be fast, so I don't like the idea of
converting my integers to ASCII text and back again. Thus I'd like to
interface my C/C++ code with Fricas by building the C++ code as a
shared library and interfacing between Fricas and the library with
SBCL (I hope that makes sense ;)

I searched the mailing lists a bit, so I'm sure that what I want to do
is possible, but there seem to be multiple possible approaches, none
too documented AFAIK.

For example, I know that Fricas has a Lisp function called
fricas-foreign-call, and it seems like maybe it would be useful to me,
but I'm not sure how to use it to achieve what I want. Also, there are
multiple options for switching to the Lisp shell from Fricas, and
there's also the option (if I'm correct) of running Fricas from the
Lisp shell. So I feel kind of lost, I can't tell what direction should
I go.

Could someone give a suggestion as to what could be the best approach
here? I'd prefer writing as little Lisp code as possible (is it
perhaps possible to do everything from Fricas/Spad?).

This is the type signature of my C++ function:
extern "C" long f(int, long, long)

This is the type signature of my Spad function:
f: (Integer, Integer, Integer) -> Integer

My system configuration is:

Hardware: 64-bit Intel
OS: Linux, glibc
Lisp: SBCL 2.1.11


Thanks,
Neven

Ralf Hemmecke

unread,
Dec 13, 2021, 12:00:33 PM12/13/21
to fricas...@googlegroups.com
> I have an algorithm implemented in both Fricas (Spad) and in C++. The
> C++ code turns out to be 13 to 14 times faster than Spad, and a single
> call can last arbitrarily many minutes, depending on the parameters.
> Thus I would prefer to use the C++ implementation in the rest of my
> Fricas Spad code.

I would very much love to see a FriCAS--C/C++ interface, so I don't want
to reduce you motivation to produce one in any way. But sorry, I cannot
help you.

However, although it is, of course, possible that FriCAS performs slower
than C++ code, but since you haven't made it public only you can judge
whether your SPAD-code is optimally coded.

Ralf

Neven Sajko

unread,
Dec 17, 2021, 4:48:16 AM12/17/21
to fricas-devel
I succeeded at calling the C++ function from Fricas, using Waldek's
example from an earlier thread in the mailing list.

It was however necessary to (trivially) extend some Lisp code that
comes with Fricas, the pull request is on Github.
Reply all
Reply to author
Forward
0 new messages