File: compiled_code.rst

package info (click to toggle)
scipy 1.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 232,636 kB
  • sloc: cpp: 497,140; python: 327,782; ansic: 190,592; javascript: 89,553; fortran: 59,012; cs: 3,081; f90: 1,150; sh: 839; makefile: 780; pascal: 277; csh: 135; lisp: 134; xml: 56; perl: 51
file content (59 lines) | stat: -rw-r--r-- 2,238 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
.. _other-languages:

=============
Beyond Python
=============

This is a small collection of thoughts related to the inclusion of code written
in languages other than Python. Currently, the only option for languages other
than Python that we have extra documentation for is :ref:`Cython<adding-cython>`.

*Can I use a programming language other than Python to speed up my code?*

Yes. The languages used in SciPy are Python, Cython, Pythran, C, C++, and
Fortran. All of these have their pros and cons. If Python really doesn't offer
enough performance, one of those languages can be used. Important concerns when
using compiled languages are maintainability and portability. For
maintainability, Pythran and Cython are preferred over C/C++/Fortran. Cython, C
and C++ are more portable than Fortran. A lot of the existing Fortran
code in SciPy is older, battle-tested code that was only wrapped in (but not
specifically written for) Python/SciPy.

Our basic advice is: use Pythran or Cython for accelerating smaller pieces of
code. In cases where Pythran or Cython are no longer enough, prefer C or C++.
If there are specific reasons why Fortran is preferred, please discuss those
reasons first.

*Can I use Numba?*

Not yet, but we're considering it for the future. It is possible to write code
that takes user-defined functions which are generated by Numba, see
:ref:`ndimage-ccallbacks`.

*How do I debug code written in C/C++/Fortran inside SciPy?*

The easiest way to do this is to first write a Python script that
invokes the C code whose execution you want to debug. For instance
``mytest.py``::

    from scipy.special import hyp2f1
    print(hyp2f1(5.0, 1.0, -1.8, 0.95))

Build SciPy in debug mode::

    python dev.py build -d

Now, you can run::

    gdb --args python dev.py python mytest.py

If you didn't compile with debug symbols enabled before, remove the
``build`` directory first. While in the debugger::

    (gdb) break cephes_hyp2f1
    (gdb) run

The execution will now stop at the corresponding C function and you
can step through it as usual. Instead of plain ``gdb`` you can, of
course, use your favorite alternative debugger; run it on the
``python`` binary with arguments ``python dev.py python mytest.py``.