Skip to content

Expose whether a library's limiting API is a shared thread pool #307

Closed

Description

@itamarst

In order for joblib to be able to set limits correctly, it wants to do something like this, except without hardcoding OpenBLAS-pthreads specifically since other libraries might be relevant in the future:

def _get_threadpoolctl_limits(tp_controller, limit, is_worker_thread):
    """Return limits for OpenMP/BLAS.

    The limits can then be passed to ``ThreadpoolController.limit()`` and
    related APIs.

    Allows setting different limits for worker threads and top-level managing
    thread.
    """
    limits = []

    for lib_controller in tp_controller.lib_controllers:
        if is_worker_thread and (
            isinstance(lib_controller, OpenBLASController)
            and lib_controller.threading_layer != "openmp"
        ):
            # Worker threads shouldn't try to set OpenBLAS-pthreads global
            # state in their initializer, because it might never get restored.
            continue

        # "filepath" is for threadpoolctl 3.8+, "prefix" for backwards compat
        # with older versions.
        limits.append(
            {
                "filepath": lib_controller.filepath,
                "prefix": lib_controller.prefix,
                "num_threads": limit,
            }
        )

    return limits

Options:

  1. Add API to LibController that return whether it's a shared process-wide pool (default result would probably be "unknown" rather than false). Then, document how to use it with code as above.
  2. Add the high-level logic to threadpoolctl itself, instead of having it in joblib. This simplifies the docs! There is the question of whether to do the per-LibController API or not. Design options:
    • Function similar to the above example.
    • Add is_worker_thread to limit() API variants. This makes backwards compatible usage annoying...
    • ThreadpoolController.limit_for_worker_thread(). Slightly nicer to add backwards compat support in joblib, and potentially there might be other custom policies other than worker threads.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions