[fricas-devel] )fin, restart, (|spad|) with Clozure CL based FriCAS

2024-05-21 Thread Grégory Vanuxem
Hello,

I try to understand why a Clozure CL 'terminate' method is not called
from the interpreter. I have a CL class jlref, when a jlref object is
no longer referenced the CL garbage collector reclaims it. It is
possible in different CL implementations to add a 'hook' to do some
job at that time. Personally I then unreference the real Julia object
in such a way it can be GC reclaimed in Julia, jlref is just a class
that holds information about it.

For SBCL I have no problem, but 'ccl:terminate' which should be
automatically called is never called. But, and this is the main point,
following an advice in a Clozure mailing list, I tried to use/define
this method directly at execution time using ')fin', and surprisingly,
using ')fin' automatically triggers 'terminate' on every objects that
are/(were ?) reclaimed.

So my question, what does ')fin' do? And do you happen to know where I
need to look in the FriCAS source code to better understand its
effect?

Furthermore, in src/interp/int-top.boot, the documentation says that
to return to the interpreterafer after ')fin'  '(restart)' should be
used. In fact, before some change in the FriCAS source I think, so I
used (|spad|). Again following an advice. And, again, with Clozure CL,
contrary to SBCL, that leads to strange things after returning to the
interpreter and executing foreign code (principally bad addresses and
therefore segfault).

So maybe I first need to know a better CL command, to return to the
interpreter? But also, of course, to know why the ccl:terminate is not
called in the interpreter. R. Munyer in the CCL mailing list found a
way, see the attached transcript file, but he uses ')fin' and no
Clozure CL FFI. I tried to use different (eva-when) to define the
ccl:terminate method without success since the code is build/compiled
(src/lisp).

To resume, since I suspect that different thread issues may also
occur, I would like to better understand the side effects of ')fin'
and how to cleanly return to the interpreter after  'fin' session with
Clozure CL.

Regards,

- Greg

ccl:terminate mechanism:
https://ccl.clozure.com/manual/chapter13.12.html#f_terminate-when-unreachable

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fricas-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/fricas-devel/CAHnU2dbj90tgj6N3ijM7LyhC__-opXfBjX0PfBfYqJH85c-B0A%40mail.gmail.com.
$ fricas
Checking for foreign routines
FRICAS="/usr/local/lib/fricas/target/x86_64-linux-gnu"
spad-lib="/usr/local/lib/fricas/target/x86_64-linux-gnu/lib/libspad.so"
foreign routines found
openServer result 0
   FriCAS Computer Algebra System
  Version: FriCAS 1.3.10 built with openmcl 1.12.2
 Timestamp: Mon 20 May 2024 02:34:03 AM UTC
-
   Issue )copyright to view copyright notices.
   Issue )summary for a summary of useful system commands.
   Issue )quit to leave FriCAS and return to shell.
-

(1) -> )lisp *package*
Value = #
(1) -> )lisp (defclass garbage-counter () ((n :initarg :n)))
Value = #
(1) -> )fin
Clozure Common Lisp Version 1.12.2  LinuxX8664

For more information about CCL, please see http://ccl.clozure.com.

CCL is free software.  It is distributed under the terms of the Apache
Licence, Version 2.0.
? *package*
#
?
(defmethod initialize-instance :after
((x garbage-counter) &rest initargs)
  (declare (ignore initargs))
  (ccl:terminate-when-unreachable x))
#
?
(let ((s *terminal-io*))
  (defmethod ccl:terminate ((x garbage-counter))
(let ((n (1+ (slot-value x 'n
  (make-instance 'garbage-counter :n n)
  (format s "~&garbage-counter: ~s~%" n)
  (finish-output s
#
? (|spad|)
(1) -> )lisp (progn (make-instance 'garbage-counter :n 0) t)
Value = T
(1) -> )lisp (ccl:gc)
Value = NIL
(1) ->
garbage-counter: 1
)lisp (ccl:gc)
Value = NIL
(1) ->
garbage-counter: 2

Re: [fricas-devel] )fin, restart, (|spad|) with Clozure CL based FriCAS

2024-05-21 Thread Waldek Hebisch
On Tue, May 21, 2024 at 01:57:45PM +0200, Grégory Vanuxem wrote:
> Hello,
> 
> I try to understand why a Clozure CL 'terminate' method is not called
> from the interpreter. I have a CL class jlref, when a jlref object is
> no longer referenced the CL garbage collector reclaims it. It is
> possible in different CL implementations to add a 'hook' to do some
> job at that time. Personally I then unreference the real Julia object
> in such a way it can be GC reclaimed in Julia, jlref is just a class
> that holds information about it.
> 
> For SBCL I have no problem, but 'ccl:terminate' which should be
> automatically called is never called. But, and this is the main point,
> following an advice in a Clozure mailing list, I tried to use/define
> this method directly at execution time using ')fin', and surprisingly,
> using ')fin' automatically triggers 'terminate' on every objects that
> are/(were ?) reclaimed.
> 
> So my question, what does ')fin' do? And do you happen to know where I
> need to look in the FriCAS source code to better understand its
> effect?

AFAIK ')fin' is doing "nothing", is just returns back to whatever Lisp
was doing befor calling FriCAS.  To understand relation with Lisp,
look at 'src/lisp/fricas-lisp.lisp', in particular 'save-core-restart'.


> Furthermore, in src/interp/int-top.boot, the documentation says that
> to return to the interpreterafer after ')fin'  '(restart)' should be
> used. In fact, before some change in the FriCAS source I think, so I
> used (|spad|). Again following an advice. And, again, with Clozure CL,
> contrary to SBCL, that leads to strange things after returning to the
> interpreter and executing foreign code (principally bad addresses and
> therefore segfault).

The function to start FriCAS is 'fricas_restart'.  'fricas_restart'
is doing some initialization which may be unnecessary when executed
second time.  OTOH re-doing initalization should be harmless and
some probably is needed.

Note that ')fin' is really unsuppored by FriCAS, basically
it is "pray that Lisp will do what you want".  In particular IIRC
it simply quits FriCAS when using ECL and I think it never fully
worked with Clozure CL.

> So maybe I first need to know a better CL command, to return to the
> interpreter? But also, of course, to know why the ccl:terminate is not
> called in the interpreter. R. Munyer in the CCL mailing list found a
> way, see the attached transcript file, but he uses ')fin' and no
> Clozure CL FFI. I tried to use different (eva-when) to define the
> ccl:terminate method without success since the code is build/compiled
> (src/lisp).

I think that ')fin' is irrelevant, you should get the same effect
using ')lisp' or if that is inconvenient you can put code in the
file and use ')read'.  The main difference is that FriCAS needs
specific settings of several global variables and FriCAS catches
errors (there are several use of 'CATCH' and there is 'handler-bind').
This _should_ make no difference.  What _may_ matter is order of
initialization.  In particular FriCAS performs some initializations
before dumping image.  But several thing do not survive to
restarted image, so need to be done later, after start of new image.

> To resume, since I suspect that different thread issues may also
> occur, I would like to better understand the side effects of ')fin'
> and how to cleanly return to the interpreter after  'fin' session with
> Clozure CL.

If there are any side effects, they are purely due to Clozure CL.

-- 
  Waldek Hebisch

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fricas-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/fricas-devel/ZkzoyJ93yR2Jhd1j%40fricas.org.