Recursive @threadpool_limits.wrap() calls do not behave as expected when recursing

Chiusa
#293 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Una pull request collegata è già stata integrata.

  • #294 di @itamarst — integrata

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
performance

Direzione di ricerca

The failing case is the test test_recursive_wrap_releases_the_shared_pool_claim, which needs a non-OpenMP OpenBLAS build with at least two threads. Start by running it with recurse(1) and reading how threadpool_limits.wrap restores limits and releases the controlling thread on return. Done when the test passes with recurse(1) and the existing recursion behaviour still holds.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Returning from a wrapped function should return to the previous level of thread limits at the time they were called, even in the face of recursion. That's not what happens in practice, though.

The following test fails on both master and 3.7 (i.e. it's not due to the controlling thread changes), but passes if recurse(1) is switched to recurse(0).

from threading import Thread

from threadpoolctl import threadpool_limits, ThreadpoolController

import numpy
import pytest

def run_in_thread(f, *args, **kwargs):
    thread = Thread(target=lambda: f(*args, **kwargs))
    thread.start()
    thread.join()
    return thread


def _non_openmp_openblas():
    openblas = ThreadpoolController().select(internal_api="openblas")
    if not openblas:
        pytest.skip("requires OpenBLAS.")
    libs = []
    seen = set()
    for lib in openblas.lib_controllers:
        if lib.threading_layer == "openmp" or lib.filepath in seen:
            continue
        seen.add(lib.filepath)
        libs.append(lib)
    if not libs:
        pytest.skip("require non-OpenMP OpenBLAS.")
    return libs


def _require_resizable_non_openmp_openblas():
    libs = _non_openmp_openblas()
    lib = libs[0]
    orig = lib.get_num_threads()
    if orig is None or orig < 2:
        pytest.skip("Test requires a non-OpenMP OpenBLAS with at least 2 threads")
    return lib, orig


def test_recursive_wrap_releases_the_shared_pool_claim():
    # One wrapped function calling itself must keep the limit between the inner
    # return and the outer return, then restore the original count and drop the
    # controlling thread. Another thread can then set the count.
    lib, orig = _require_resizable_non_openmp_openblas()

    @threadpool_limits.wrap(limits=1, user_api="blas")
    def recurse(depth):
        assert lib.get_num_threads() == 1
        if depth:
            recurse(depth - 1)
            assert lib.get_num_threads() == 1

    recurse(1)
    assert lib.get_num_threads() == orig

    observed = {}

    def other_thread():
        lib.set_num_threads(1)
        observed["threads"] = lib.get_num_threads()

    worker = run_in_thread(other_thread)
    assert observed["threads"] == 1
Lingua principale
Python
Stelle
434
Fork
38
Merge medio
1g 4h
PR unite (30g)
18

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di joblib/threadpoolctl

Tutte le issue di joblib/threadpoolctl

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.