Skip to content

Mark all extensions as free-threading safe #28819

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

Merged
merged 2 commits into from
Oct 18, 2024

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Sep 14, 2024

PR summary

This PR includes all the current and future pybind11 PRs, so it might be rebased or otherwise modified without notice. So don't bother reviewing any of the changes, unless you want to note them in the original PRs.

The only important thing here is the resulting wheels, which should be useful for testing.

@greglucas

PR checklist

@QuLogic QuLogic added the CI: Run cibuildwheel Run wheel building tests on a PR label Sep 14, 2024
@QuLogic QuLogic added the CI: Run cibuildwheel Run wheel building tests on a PR label Sep 21, 2024
@github-actions github-actions bot removed the CI: Run cibuildwheel Run wheel building tests on a PR label Sep 21, 2024
@QuLogic QuLogic added the CI: Run cibuildwheel Run wheel building tests on a PR label Sep 21, 2024
@github-actions github-actions bot removed the CI: Run cibuildwheel Run wheel building tests on a PR label Sep 21, 2024
@QuLogic QuLogic added the CI: Run cibuildwheel Run wheel building tests on a PR label Sep 24, 2024
@github-actions github-actions bot removed topic: text/mathtext CI: Run cibuildwheel Run wheel building tests on a PR Documentation: examples files in galleries/examples status: needs rebase labels Oct 10, 2024
@tacaswell
Copy link
Member

I put together this script to test our thread safety:

import threading
from pathlib import Path
import time

import numpy as np
import matplotlib.figure as mfigure

target = Path("/tmp/p_test")
target.mkdir(parents=True, exist_ok=True)

ω = 2
N = 1024 * 10

N_threads = 70


def gen_frames(steps):
    fig = mfigure.Figure(layout="constrained")
    ax = fig.subplots()
    ax.set_xlim(0, 2 * np.pi)
    ax.set_ylim(-1.1, 1.1)
    # ax.axis("off")

    x = np.linspace(0, 2 * np.pi, 1024 * 10)
    (ln,) = ax.plot(x, np.ones_like(x))

    fr_ann = ax.annotate(
        "step:        ",
        (1, 1),
        xytext=(-1, -5),
        xycoords="axes fraction",
        textcoords="offset points",
        ha="right",
        va="top",
        font="hack",
        visible=True,
    )
    φ_ann = ax.annotate(
        "φ:        ",
        (1, 0),
        xytext=(0, -5),
        xycoords=fr_ann,
        textcoords="offset points",
        ha="right",
        va="top",
        font="hack",
        visible=True,
    )

    for step in steps:
        φ = 2 * np.pi * step / 1024
        y = np.sin(x * ω + φ)
        ln.set_data(x, y)
        fr_ann.set_text(f"n: {step: 5d}")
        φ_ann.set_text(f"φ: {φ:.4f}")
        fig.savefig(target / f"sin_{step:05d}.png")


threads = []
for j in range(N_threads):
    threads.append(threading.Thread(target=gen_frames, args=(range(j, N, N_threads),)))

start = time.monotonic()
[t.start() for t in threads]
[t.join() for t in threads]
stop = time.monotonic()

print(f"Took {stop - start:.2f} for {N} frames with {N_threads} threads")

which can be stitched with ffmpeg -f image2 -framerate 60 -i sin_%05d.png a.mp4. The idea is to generate a movie which by eye we should be able to notice if anything went wrong.

It seems to work! We currently have a lock on Figure.draw so we only ever draw one figure at a time. I did a bit of experimentation with removing that and it still seems to work (we now do fonts pre-thread and the mathtext parser has its own lock).

@QuLogic
Copy link
Member Author

QuLogic commented Oct 16, 2024

Next release of NumPy should have Windows free-threading wheels as well.

@ianthomas23
Copy link
Member

FYI there are now ContourPy Windows free-threaded wheels available at https://anaconda.org/scientific-python-nightly-wheels/contourpy/files

This is not actually guaranteed to be true at the moment, but may be
useful for testing.
NumPy should have those on the nightly channel, and they should be
available in the next release.
@QuLogic QuLogic added the CI: Run cibuildwheel Run wheel building tests on a PR label Oct 17, 2024
@QuLogic
Copy link
Member Author

QuLogic commented Oct 17, 2024

I enabled free-threading wheels on Windows now that there are wheels in the nightly repo, which is enough for cibuildwheel's limited testing.

@QuLogic QuLogic marked this pull request as ready for review October 17, 2024 21:20
@QuLogic QuLogic changed the title WIP: Mark all extensions as free-threading safe Mark all extensions as free-threading safe Oct 17, 2024
@ianthomas23 ianthomas23 merged commit 9489b93 into matplotlib:main Oct 18, 2024
51 of 53 checks passed
@QuLogic QuLogic deleted the freethread branch October 18, 2024 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants