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

nogil multi-threading is slower than multi-threading with gil for CPU bound #118749

Open
Venkat2811 opened this issue May 8, 2024 · 7 comments
Open
Labels
topic-free-threading type-bug An unexpected behavior, bug, or error

Comments

@Venkat2811
Copy link

Venkat2811 commented May 8, 2024

Bug report

Bug description:

Hello Team,

Thanks for the great work so far and recent nogil efforts. I wanted to explore performance of asyncio with CPU-GPU bound multi-threading in nogil setup. So, started with simple benchmark as seen below:

import sys
from concurrent.futures import ThreadPoolExecutor
def fib(n):
    if n < 2: return 1
    return fib(n-1) + fib(n-2)

threads = 8
if len(sys.argv) > 1:
    threads = int(sys.argv[1])

with ThreadPoolExecutor(max_workers=threads) as executor:
    for _ in range(threads):
        executor.submit(lambda: print(fib(34)))

Original Source

Results:

  • python 3.13.a06 with gil:
    htop shows 2 running tasks, and only one core is utilized for 100%
$ time /usr/bin/python3.13 fib.py      
9227465
9227465
9227465
9227465
9227465
9227465
9227465
9227465
/usr/bin/python3.13 fib.py  6,46s user 0,04s system 100% cpu 6,447 total
  • libpython3.13-nogil amd64 3.13.0~a6-1+jammy2 source nogil:
    htop shows 9 running tasks, and 8 cores close to 100% utilization, but slower.
$ time /usr/bin/python3.13-nogil -X gil=0 fib.py
9227465
9227465
9227465
9227465
9227465
9227465
9227465
9227465
/usr/bin/python3.13-nogil -X gil=0 fib.py  168,81s user 0,06s system 788% cpu 21,410 total

My CPU: AMD Ryzen 7 5800X 8-Core Processor
OS: Ubuntu 22.04.4 LTS

So looks like there is overhead when using multiple cores. Is this expected with this version ? Are results similar with Intel & M1 CPUs as well ?

Results documented here in version 3.9.12 on Intel is better.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

@Venkat2811 Venkat2811 added the type-bug An unexpected behavior, bug, or error label May 8, 2024
@corona10
Copy link
Member

corona10 commented May 8, 2024

FYI, the performance issue is not a surprise because we turned off the specialization, and deferred reference counting is not yet implemented for free-threading version.

But worth to checkout if there is unexpected bottleneck here.

@colesbury
Copy link
Contributor

Please use 3.13 beta 1. There were a number of scaling bottlenecks fixed between 3.13 alpha 6 and beta 1 (#118527).

On my machine, I see a speedup in the free-threaded build vs. the default bulid (2.4s vs. 8.2s)

@corona10
Copy link
Member

corona10 commented May 8, 2024

out

With ed2b0fb
number of thread: os.cpu_count()
task: fib(5)

@Venkat2811
Copy link
Author

Thanks for confirming that things have been fixed in beta1 @colesbury

I'm waiting for beta1 build to be available on deadsnakes. I tried building from source, but unfortunately after trying several --with-ssl solutions, still getting not found errors.

@Venkat2811
Copy link
Author

Venkat2811 commented May 10, 2024

@colesbury I installed 3.13.0b1 via pyenv.

env PYTHON_CONFIGURE_OPTS='--disable-gil' pyenv install 3.13.0b1

Is this the way to create free-threaded build ?

On my M1 MBP:
--disable-gil vs default build: 15.5s vs 5.26s

On my AMD Ryzen 7 5800X 8-Core Processor, Ubuntu:
--disable-gil vs default build: 10.5s vs 6.4s

It has improved since 3.13.a06 for sure

@stonebig
Copy link

stonebig commented May 13, 2024

cpython-3.13.0b1 on Windows 11, for 20 threads, on a 4 cores/8 threads intel cpu: (faster is better)

  • free-threading = 12 s
  • standard = 19 s

@FeldrinH
Copy link

This is a slightly different benchmark, but there are some potentially useful data points about the speedup with free-threading here: winpython/winpython#1339.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-free-threading type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants