Ticket #8159 (closed enhancement: fixed)

Opened 7 months ago

Last modified 6 months ago

Updated Cython backend for mpmath

Reported by: fredrik.johansson Owned by: tbd
Priority: major Milestone: sage-4.3.4
Component: performance Keywords: mpmath
Cc: burcin Author(s): Fredrik Johansson
Report Upstream: N/A Reviewer(s): Harald Schilly
Merged in: sage-4.3.4.alpha0 Work issues:

Description

This update of sage.libs.mpmath, along with recent changes to mpmath itself and the patch in #6199, results in a 3x overall speedup of mpmath (as measured by mpmath.runtests() performance). Elementary functions, hypergeometric series, and other "low-level" transcendental functions are not affected much, but functions that do a lot of arithmetic with mpf/mpc instances (examples: lambertw, polylog, bernpoly and many others; numerical summation, numerical integration, etc) can be 3x-10x faster. A similar speedup should be attainable in the future for the "low-level" functions by implementing those in Cython as well.

This extension works if site-packages/mpmath is replaced with an svn trunk checkout. There are essentially no tests in the Cython modules themselves; testing can be done with

import mpmath; mpmath.runtests(); mpmath.doctests()

(There is a very small number of doctests that fail due to numerical noise; this is nothing to worry about.)

This is not the final version of the code to be committed (it will synchronized with the next release of mpmath), but I'm uploading it to have a safe copy and for potential early review. I have not tested this on a 32-bit system. There could be some subtle overflow or memory leak issues that aren't caught by the tests.

It's not thread-safe due to the use of global state (which is used mostly because I was lazy, but it possibly also helps performance). I don't consider this a serious bug since vanilla-Python mpmath isn't fully thread-safe either. But it should be fixed some time in the future.

I think a number of optimizations are possible, including optimizing MPF_normalize and caching MPF and mpf/mpc instances. I have also not updated the mpmath <-> Sage conversion code, which could be improved not to go via tuple values.

Sorry that it's not in the form of a patch (my current hg copy of Sage being dirty).

Attachments

mpmath_cython.tar.gz Download (23.8 KB) - added by fredrik.johansson 7 months ago.
contains edited sage.libs.mpmath (update: old files removed)
mpmath_cython.patch Download (81.7 KB) - added by fredrik.johansson 7 months ago.
importfix.patch Download (0.6 KB) - added by fredrik.johansson 7 months ago.

Change History

Changed 7 months ago by fredrik.johansson

contains edited sage.libs.mpmath (update: old files removed)

Changed 7 months ago by burcin

  • cc burcin added

Changed 7 months ago by schilly

Here is an  0.14 spkg

Changed 7 months ago by fredrik.johansson

Changed 7 months ago by fredrik.johansson

Patch uploaded; let's see if it works.

Slightly updated spkg (still might make some minor changes before making it 0.14 final): http://boxen.math.washington.edu/home/fredrik/spkg/mpmath-0.14.spkg

Changed 7 months ago by schilly

  • status changed from new to needs_work

Hi, I applied the patch and installed your spkg. works + your tests pass.

I also tried the patch+spkg on 4.3.2.rc0 ... and it worked! all tests+your doctests pass here too! I also doctested the entire sage library, but there were some complaints.

Therefore positive review for the spkg from me, but others should test it on other platforms, too. Negative for the implications on the sage library because doctests fail on 4.3.1 and 4.3.2.rc0 /w mpmath 0.14 and above patch in sage/libs/mpmath/utils.pyx and /sage/functions/transcendental.py all say:

ImportError: No module named mptypes

This exception pops up about 20 times ...

Changed 7 months ago by fredrik.johansson

Changed 7 months ago by fredrik.johansson

Attached a fix for the ImportError.

Changed 7 months ago by schilly

  • status changed from needs_work to needs_review

That was easy, all tests pass ... green light from me!

I'm setting this to needs_review, and start with a positiv review from me.

Changed 7 months ago by schilly

  • status changed from needs_review to positive_review

Changed 7 months ago by schilly

  • reviewer set to schilly

Changed 7 months ago by schilly

  • milestone set to sage-4.3.3

Changed 7 months ago by fredrik.johansson

Just a reminder: the spkg should be updated to the actual 0.14 release version.

Changed 7 months ago by mpatel

Where is the new spkg?  It does not seem to be  here.

Changed 6 months ago by schilly

I don't know, there seems there was a misunderstanding. I've build a new 0.14 spkg with the actual 0.14 release.

Changed 6 months ago by schilly

I've tested this once again with 4.3.3 and it's still working and my positive review is still valid.

Changed 6 months ago by schilly

dear release manager. to get this done, merge the mpmath_cython.patch and the importfix.patch patch. after that get the updated spkg from  here and that's it ;)

Changed 6 months ago by mvngu

Harald's mpmath spkg has some changes that are not yet checked in:

[mvngu@sage mpmath-0.14]$ hg diff
diff -r fa9536e74343 SPKG.txt
--- a/SPKG.txt
+++ b/SPKG.txt
@@ -21,6 +21,9 @@
 
 == Changelog ==
 
+=== mpmath-0.14 (Harald Schilly, Feb 20th, 2010) ===
+ * updated to mpmath-0.14.
+
 === mpmath-0.13 (Fredrik Johansson, August 14th, 2009) ===
  * Updated to mpmath-0.13.

I have committed these changes in his name and uploaded the resulting spkg to

 http://sage.math.washington.edu/home/mvngu/spkg/standard/mpmath/mpmath-0.14.spkg

Changed 6 months ago by mvngu

  • status changed from positive_review to closed
  • reviewer changed from schilly to Harald Schilly
  • resolution set to fixed
  • merged set to sage-4.3.4.alpha0
  • author changed from fredrik.johansson to Fredrik Johansson

Merged in this order:

  1.  mpmath_cython.patch
  2.  importfix.patch
  3. Merged  mpmath-0.14.spkg in the standard spkg repository.

Changed 6 months ago by fredrik.johansson

Harald, Minh, thanks a lot!

Note: See TracTickets for help on using tickets.