Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,11 @@ python-flint.

As of python-flint 0.7.0, CPython 3.13 [PEP
703](https://peps.python.org/pep-0703/) free-threaded (no-GIL) builds of
python-flint are provided. In the the free-threaded build, mutating matrices or
polynomials from multiple threads can lead to memory corruption. There are some
other known issues with the free-threaded build so it should be considered
experimental.
python-flint are provided. In the the free-threaded build, `fmpz_mat` uses
per-object critical sections so that it can be mutated and read from multiple
threads, but mutating other matrix or polynomial types from multiple threads
can still lead to memory corruption. There are some other known issues with the
free-threaded build so it should be considered experimental.

CHANGELOG
=========
Expand All @@ -171,9 +172,14 @@ Contributors (0.10.0):
- Oscar Benjamin (OB)
- Ilia Gradina (IG)
- Uri K.H. (UKH)
- KainanP (KP)

Changes (0.10.0):

- [gh-456](https://github.com/flintlib/python-flint/pull/456),
Make `fmpz_mat` safe to mutate and read from multiple threads in the
free-threaded build by holding per-object critical sections in its methods
(see [gh-224](https://github.com/flintlib/python-flint/issues/224)). (KP)
- [gh-435](https://github.com/flintlib/python-flint/issues/435),
Fix a memory leak in `str()` (and so in `repr()`, which uses it unless
`flint.ctx.pretty` is `False`) of `fmpz_mpoly`, `fmpq_mpoly`,
Expand Down
3 changes: 2 additions & 1 deletion doc/source/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ As of python-flint 0.7.0, CPython ``3.13t`` free-threaded builds are tested in
CI but wheels are not provided on PyPI. There are no known issues related to
using python-flint in a `PEP 703 <https://peps.python.org/pep-0703/>`_
free-threaded build but it is likely that mutating objects shared by multiple
threads is not safe.
threads is not safe. The exception is ``fmpz_mat`` whose methods hold a
critical section on the matrices that they read or modify.

It is also possible to build and use python-flint for PyPy. Other Python
implementations may work but are not tested.
Expand Down
48 changes: 35 additions & 13 deletions src/flint/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -5359,27 +5359,24 @@ def test_python_threads():
#
# https://github.com/flintlib/python-flint/issues/224
#
# XXX: This test crashes under the free-threading mode because of memory
# corruption.
# Mutating an fmpz_mat that is shared between threads used to crash the
# free-threaded build because of memory corruption. The fmpz_mat methods
# now hold a critical section on the matrices that they read or write.
#
# It is not clear if this should be fixed or if mutating
# matrices/polynomials that are shared between multiple threads should just
# be disallowed.
# Other mutable types (polynomials, other matrix types) are not protected
# yet so mutating those from multiple threads is still unsafe.
#
# This thread is skipped on Emscripten/WASM builds as we can't start new
# threads in Pyodide.

# Skip the test on the free-threaded build and on WASM...
import sys
if (sys.version_info[:2] >= (3, 13) and not sys._is_gil_enabled()) or ( # type: ignore
sys.platform == "emscripten" or platform.machine() in ["wasm32", "wasm64"]):
if sys.platform == "emscripten" or platform.machine() in ["wasm32", "wasm64"]:
return

from threading import Thread

iterations = 10**5
nthreads = 3 + 1
size = 3
big = 10**128
M = flint.fmpz_mat([[0]*size for _ in range(size)])

def set_values():
Expand All @@ -5388,7 +5385,7 @@ def set_values():
j = random.randrange(M.ncols())
if random.uniform(0, 1) > 0.5:
# Bigger than 2**62:
M[i,j] = 10**128
M[i,j] = big
else:
# Smaller than 2**62:
M[i,j] = 0
Expand All @@ -5397,14 +5394,39 @@ def get_dets():
for _ in range(iterations):
M.det()

threads = [Thread(target=set_values) for _ in range(nthreads-1)]
threads.append(Thread(target=get_dets))
def other_reads():
for _ in range(iterations // 20):
assert M == M
M + M
M - M
M * M
M * 3
flint.fmpz_mat(M)
flint.fmpq_mat(M)
flint.nmod_mat(M, 7)
M.transpose()
M.rank()
assert M[1, 1] in (0, big)

errors = []

def run(func):
try:
func()
except BaseException as e:
errors.append(e)

funcs = [set_values] * 3 + [get_dets, other_reads]
threads = [Thread(target=run, args=(f,)) for f in funcs]

for t in threads:
t.start()
for t in threads:
t.join()

assert errors == []
assert all(x in (0, big) for x in M.entries())


def test_all_tests():
from flint.test.__main__ import collect_all_tests
Expand Down
6 changes: 4 additions & 2 deletions src/flint/types/acb_mat.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ cdef class acb_mat(flint_mat):
m = fmpz_mat_nrows((<fmpz_mat>val).val)
n = fmpz_mat_ncols((<fmpz_mat>val).val)
acb_mat_init(self.val, m, n)
acb_mat_set_fmpz_mat(self.val, (<fmpz_mat>val).val)
with cython.critical_section(val):
acb_mat_set_fmpz_mat(self.val, (<fmpz_mat>val).val)
elif typecheck(val, fmpq_mat):
m = fmpq_mat_nrows((<fmpq_mat>val).val)
n = fmpq_mat_ncols((<fmpq_mat>val).val)
Expand Down Expand Up @@ -568,7 +569,8 @@ cdef class acb_mat(flint_mat):
if isinstance(t, arb_mat):
return bool(acb_mat_contains(s.val, (<acb_mat>(acb_mat(t))).val))
if isinstance(t, fmpz_mat):
return bool(acb_mat_contains_fmpz_mat(s.val, (<fmpz_mat>t).val))
with cython.critical_section(t):
return bool(acb_mat_contains_fmpz_mat(s.val, (<fmpz_mat>t).val))
if isinstance(t, fmpq_mat):
return bool(acb_mat_contains_fmpq_mat(s.val, (<fmpq_mat>t).val))
raise TypeError("expected a matrix of compatible type")
Expand Down
6 changes: 4 additions & 2 deletions src/flint/types/arb_mat.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ cdef class arb_mat(flint_mat):
m = fmpz_mat_nrows((<fmpz_mat>val).val)
n = fmpz_mat_ncols((<fmpz_mat>val).val)
arb_mat_init(self.val, m, n)
arb_mat_set_fmpz_mat(self.val, (<fmpz_mat>val).val)
with cython.critical_section(val):
arb_mat_set_fmpz_mat(self.val, (<fmpz_mat>val).val)
elif typecheck(val, fmpq_mat):
m = fmpq_mat_nrows((<fmpq_mat>val).val)
n = fmpq_mat_ncols((<fmpq_mat>val).val)
Expand Down Expand Up @@ -649,7 +650,8 @@ cdef class arb_mat(flint_mat):
if isinstance(t, arb_mat):
return bool(arb_mat_contains(s.val, (<arb_mat>t).val))
if isinstance(t, fmpz_mat):
return bool(arb_mat_contains_fmpz_mat(s.val, (<fmpz_mat>t).val))
with cython.critical_section(t):
return bool(arb_mat_contains_fmpz_mat(s.val, (<fmpz_mat>t).val))
if isinstance(t, fmpq_mat):
return bool(arb_mat_contains_fmpq_mat(s.val, (<fmpq_mat>t).val))
raise TypeError("expected a matrix of compatible type")
Expand Down
3 changes: 2 additions & 1 deletion src/flint/types/fmpq_mat.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ cdef class fmpq_mat(flint_mat):
fmpq_mat_init(self.val,
fmpz_mat_nrows((<fmpz_mat>val).val),
fmpz_mat_ncols((<fmpz_mat>val).val))
fmpq_mat_set_fmpz_mat(self.val, (<fmpz_mat>val).val)
with cython.critical_section(val):
fmpq_mat_set_fmpz_mat(self.val, (<fmpz_mat>val).val)
elif isinstance(val, (list, tuple)):
m = len(val)
n = 0
Expand Down
Loading