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

Cython is broken in conda build #39250

Open
2 tasks done
TernaryM01 opened this issue Jan 3, 2025 · 9 comments
Open
2 tasks done

Cython is broken in conda build #39250

TernaryM01 opened this issue Jan 3, 2025 · 9 comments
Labels

Comments

@TernaryM01
Copy link

TernaryM01 commented Jan 3, 2025

Steps To Reproduce

  1. Install SageMath using conda/mamba: mamba create -n sagemath sage python=3.11.11
  2. Launch Jupyter Lab using either jupyter lab or sage -n jupyterlab (while sage conda env is active).
  3. Try to run any Cython code:
%%cython
from libc.math cimport cos, sin
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef intersection(...):
  ...

Expected Behavior

Cython code gets compiled.

Actual Behavior

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/miniconda3/envs/sagemath/lib/python3.11/site-packages/sage/misc/cachefunc.pyx:1010, in sage.misc.cachefunc.CachedFunction.__call__ (build/cythonized/sage/misc/cachefunc.c:8371)()
   1009 try:
-> 1010     return self.cache[k]
   1011 except TypeError:  # k is not hashable

KeyError: ((), ())

During handling of the above exception, another exception occurred:

PackageNotFoundError                      Traceback (most recent call last)

. . .

PackageNotFoundError: gdlib not found

Additional Information

This bug has been reported by someone else here.

Environment

  • OS: Ubuntu 22.04 (Windows WSL2)
  • Sage Versions tried: 10.4, 10.3, 10.2, 9.8 (but older installation of 9.8 used to work)
  • Python Versions tried: 3.12.8, 3.11.11, 3.10.16

Checklist

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
  • I have read the documentation and troubleshoot guide
@dimpase
Copy link
Member

dimpase commented Jan 3, 2025

Seems to be that the issue is PackageNotFoundError: gdlib not found, no?

That is, try mamba install gdlib before jupyter lab

@TernaryM01
Copy link
Author

I've dug deep into this issue, and the solution is not going to be that simple.
Running mamba install gdlib does nothing, because gdlib is already installed.

@dimpase
Copy link
Member

dimpase commented Jan 4, 2025

maybe @tobiasdiez knows what's going on here

@tobiasdiez
Copy link
Contributor

No real idea but there were problems with gdlib's pkconfig file (at least on windows).

Honestly, I think we should deprecate cython and fortan compilation. Sage is not really in the business to provide an IDE. Proper cython support (i.e here properly searching for gdlib) would require to call meson or a similar build system - numpy is indeed generating a meson file in the background and then call meson/ninja to build it for its fortan support. Except for very quick prototypes, just building your cython code with little meson file should give a better developer experience in my opinion.

@TernaryM01 TernaryM01 reopened this Jan 5, 2025
@TernaryM01
Copy link
Author

TernaryM01 commented Jan 5, 2025

(Forgive me for GitHub's terrible UI about closing this issue. I have no intention to close this issue until Cython works again.)

I disagree about deprecating Cython and Fortran compilation. Sagemath is dedicated for scientific computing, and speed matters. I ran across this issue (of Cython compilation) when I was trying to run a sample Jupyter notebook from SageManifolds about rendering the picture of a black hole. It's quite computation-heavy, and you can see at cell number 38 is where Cython gets invoked the first time, to speed up a crucial rendering function by x10000. This very cool Sagemath showcase wouldn't be possible without Cython.

@tobiasdiez
Copy link
Contributor

tobiasdiez commented Jan 5, 2025

This is natively supported by cython: https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#compiling-with-a-jupyter-notebook

The only advantage of Sage's %cython is that it tries to set the correct lib arguments etc for a couple of sage's dependencies. But as you noticed, this is only working half of the time.

@dimpase
Copy link
Member

dimpase commented Jan 5, 2025

numpy is indeed generating a meson file in the background and then call meson/ninja to build it for its fortan support.

perhaps Sage can borrow this setup from numpy?

@user202729
Copy link
Contributor

Maybe try %load_ext cython? This replaces the %%cython magic with the version provided by cython itself.

Just a band-aid at the moment, but better than not being able to use it at all.

@user202729
Copy link
Contributor

Potentially related? #39275

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants