Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maxima (sbcl) arithmetic is very slow #34849

Closed
tornaria opened this issue Jan 4, 2022 · 3 comments · Fixed by #34887
Closed

maxima (sbcl) arithmetic is very slow #34849

tornaria opened this issue Jan 4, 2022 · 3 comments · Fixed by #34887

Comments

@tornaria
Copy link
Contributor

tornaria commented Jan 4, 2022

Maybe coming from slow arithmetic in sbcl:

$ rmaxima
Maxima 5.45.1 https://maxima.sourceforge.io
using Lisp SBCL 2.1.10
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) showtime : true $
Evaluation took 0.0001 seconds (0.0000 elapsed) using 0 bytes.
(%i2) a : 10^(10^5) $
Evaluation took 0.0192 seconds (0.0200 elapsed) using 96.047 KB.
(%i3) b : a^10 $
Evaluation took 1.1198 seconds (1.1194 elapsed) using 1023.875 KB.
(%i4) b : a^20 $
Evaluation took 4.5570 seconds (4.5607 elapsed) using 2.147 MB.
(%i5) 

Contrast with maxima-ecl:

$ rmaxima -l ecl
;;; Loading #P"/usr/lib64/ecl-21.2.1/sb-bsd-sockets.fas"
;;; Loading #P"/usr/lib64/ecl-21.2.1/sockets.fas"
Maxima 5.45.1 https://maxima.sourceforge.io
using Lisp ECL 21.2.1
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) showtime : true $
Evaluation took 0.0000 seconds (0.0000 elapsed)
(%i2) a : 10^(10^5) $
Evaluation took 0.0030 seconds (0.0020 elapsed)
(%i3) b : a^10 $
Evaluation took 0.0420 seconds (0.0440 elapsed)
(%i4) b : a^20 $
Evaluation took 0.0680 seconds (0.0450 elapsed)
(%i5) b : a^40 $
Evaluation took 0.0750 seconds (0.0780 elapsed)
(%i6) b : a^80 $
Evaluation took 0.2420 seconds (0.2000 elapsed)
(%i7) b : a^160 $
Evaluation took 0.4430 seconds (0.3890 elapsed)
(%i8) b : a^320 $
Evaluation took 0.8530 seconds (0.7970 elapsed)
(%i9) b : a^640 $
Evaluation took 1.7850 seconds (1.7040 elapsed)
(%i10) ) b : a^1280 $
Evaluation took 3.7550 seconds (3.6670 elapsed)
(%i11) 

Note: ecl compiles with gmp so that's probably where the long arithmetic comes from.

@tornaria
Copy link
Contributor Author

tornaria commented Jan 4, 2022

This seems to be due to slow default arithmetic in sbcl:

$ sbcl 
This is SBCL 2.2.0, an implementation of ANSI Common Lisp.
[...]
* (* 0 (expt (expt 10 (expt 10 5)) 30))
[takes ~ 10s]
0

Now

$ sbcl 
This is SBCL 2.2.0, an implementation of ANSI Common Lisp.
[...]
* (require :sb-gmp)
("SB-GMP")
* (sb-gmp:install-gmp-funs)
* (* 0 (expt (expt 10 (expt 10 5)) 30))
[takes < 0.5 s]]
0

The solution might be to somehow link maxima to sb-gmp and run (sb-gmp:install-gmp-funs) at startup.

@tornaria
Copy link
Contributor Author

tornaria commented Jan 4, 2022

Indeed:

$ rmaxima 
Maxima 5.45.1 https://maxima.sourceforge.io
using Lisp SBCL 2.2.0
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) :lisp (require :sb-gmp "/usr/lib/sbcl/contrib/sb-gmp.fasl")

(SB-GMP)
(%i1) :lisp (sb-gmp:install-gmp-funs)

(%i1) showtime : true $
Evaluation took 0.0001 seconds (0.0000 elapsed) using 0 bytes.
(%i2) a : 10^(10^5) $
Evaluation took 0.0015 seconds (0.0010 elapsed) using 3.578 KB.
(%i3) b : a^1280 $
Evaluation took 1.5059 seconds (1.5080 elapsed) using 50.689 MB.
(%i4) 

Caveats:

  • this only works if sbcl is installed (for the file "/usr/lib/sbcl/contrib/sb-gmp.fasl")
  • moreover the version of sbcl used to compile maxima has to match the version of sbcl used to compile sb-gmp.fasl (rn maxima was compiled with 2.1.11 but sbcl is at 2.2.0)
  • the "require" statement needs the full path to sb-gmp.fasl (not a real problem)

@tornaria
Copy link
Contributor Author

tornaria commented Jan 4, 2022

I had success with the following patch

--- a/src/init-cl.lisp	2021-05-15 20:20:53.000000000 -0300
+++ b/src/init-cl.lisp	2022-01-04 17:45:03.838273626 -0300
@@ -574,8 +574,13 @@
 			 (delete-file file)))))
 	   *temp-files-list*))
 
+#+sbcl
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (require 'sb-gmp))
+
 (defun cl-user::run ()
   "Run Maxima in its own package."
+  #+sbcl (sb-gmp:install-gmp-funs)
   (in-package :maxima)
   (initialize-runtime-globals)
   (let ((input-stream *standard-input*)

Now maxima is automatically initialized using sb-gmp and arithmetic is fast:

$ rmaxima 
Maxima 5.45.1 https://maxima.sourceforge.io
using Lisp SBCL 2.2.0
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) showtime : true $
Evaluation took 0.0001 seconds (0.0000 elapsed) using 0 bytes.
(%i2) a : 10^(10^5) $
Evaluation took 0.0017 seconds (0.0020 elapsed) using 13.516 KB.
(%i3) b : a^1280 $
Evaluation took 1.5245 seconds (1.5270 elapsed) using 50.689 MB.
(%i4) 

I want to try to fix an (unrelated) bug with the heap on maxima-ecl (#34030 (comment)); I'll make a single PR with both.

tornaria added a commit to tornaria/void-packages that referenced this issue Jan 6, 2022
 - patch maxima-sbcl so it uses gmp for arithmetic (closes void-linux#34849)
   also add libgmp to shlib_requires since it won't be detected
 - remove `nopie=yes` to fix void-linux#34861, replace by `nopie_files`
 - remove `nostrip=yes`, replace by `nostrip_files`
 - add checks for void-linux#34849 and void-linux#34861
 - run testsuite only for full check (not in CI)
 - some html and info files are shipped with source: do not rebuild
tornaria added a commit to tornaria/void-packages that referenced this issue Jan 8, 2022
 - patch maxima-sbcl so it uses gmp for arithmetic (closes void-linux#34849)
   also add libgmp to shlib_requires since it won't be detected
 - remove `nopie=yes` to fix void-linux#34861, replace by `nopie_files`
 - remove `nostrip=yes`, replace by `nostrip_files`
 - add checks for void-linux#34849 and void-linux#34861
 - run testsuite only for full check (not in CI)
 - some html and info files are shipped with source: do not rebuild
tornaria added a commit to tornaria/void-packages that referenced this issue Jan 10, 2022
 - patch maxima-sbcl so it uses gmp for arithmetic (closes void-linux#34849)
   also add libgmp to shlib_requires since it won't be detected
 - remove `nopie=yes` to fix void-linux#34861, replace by `nopie_files`
 - remove `nostrip=yes`, replace by `nostrip_files`
 - add checks for void-linux#34849 and void-linux#34861
 - run testsuite only for full check (not in CI)
 - some html and info files are shipped with source: do not rebuild
tornaria added a commit to tornaria/void-packages that referenced this issue Jan 10, 2022
 - patch maxima-sbcl so it uses gmp for arithmetic (closes void-linux#34849)
   also add libgmp to shlib_requires since it won't be detected
 - remove `nopie=yes` to fix void-linux#34861, replace by `nopie_files`
 - remove `nostrip=yes`, replace by `nostrip_files`
 - add checks for void-linux#34849 and void-linux#34861
 - run testsuite only for full check (not in CI)
 - some html and info files are shipped with source: do not rebuild
leahneukirchen pushed a commit that referenced this issue Jan 11, 2022
 - patch maxima-sbcl so it uses gmp for arithmetic (closes #34849)
   also add libgmp to shlib_requires since it won't be detected
 - remove `nopie=yes` to fix #34861, replace by `nopie_files`
 - remove `nostrip=yes`, replace by `nostrip_files`
 - add checks for #34849 and #34861
 - run testsuite only for full check (not in CI)
 - some html and info files are shipped with source: do not rebuild
Sqvid pushed a commit to Sqvid/void-packages that referenced this issue Jan 28, 2022
 - patch maxima-sbcl so it uses gmp for arithmetic (closes void-linux#34849)
   also add libgmp to shlib_requires since it won't be detected
 - remove `nopie=yes` to fix void-linux#34861, replace by `nopie_files`
 - remove `nostrip=yes`, replace by `nostrip_files`
 - add checks for void-linux#34849 and void-linux#34861
 - run testsuite only for full check (not in CI)
 - some html and info files are shipped with source: do not rebuild
ElDifinitivo pushed a commit to ElDifinitivo/void-packages that referenced this issue Feb 13, 2022
 - patch maxima-sbcl so it uses gmp for arithmetic (closes void-linux#34849)
   also add libgmp to shlib_requires since it won't be detected
 - remove `nopie=yes` to fix void-linux#34861, replace by `nopie_files`
 - remove `nostrip=yes`, replace by `nostrip_files`
 - add checks for void-linux#34849 and void-linux#34861
 - run testsuite only for full check (not in CI)
 - some html and info files are shipped with source: do not rebuild
ElDifinitivo pushed a commit to ElDifinitivo/void-packages that referenced this issue Feb 14, 2022
 - patch maxima-sbcl so it uses gmp for arithmetic (closes void-linux#34849)
   also add libgmp to shlib_requires since it won't be detected
 - remove `nopie=yes` to fix void-linux#34861, replace by `nopie_files`
 - remove `nostrip=yes`, replace by `nostrip_files`
 - add checks for void-linux#34849 and void-linux#34861
 - run testsuite only for full check (not in CI)
 - some html and info files are shipped with source: do not rebuild
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant