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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
NotFound,
SearchContext,
find_via_ctk_root,
first_hit,
run_find_steps,
)
from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import ALL_AVAILABLE_LIBNAMES
Expand Down Expand Up @@ -62,16 +63,8 @@ def _load_driver_lib_no_cache(desc: LibDescriptor) -> LoadedDL:

def _try_ctk_root_canary(ctx: SearchContext) -> FindResult | NotFound:
"""Try CTK-root canary fallback for descriptor-configured libraries."""
missed = NotFound()
for canary_libname in ctx.desc.ctk_root_canary_anchor_libnames:
ctk_root = resolve_ctk_root_via_canary(canary_libname)
if ctk_root is None:
continue
result = find_via_ctk_root(ctx, ctk_root)
if result.found:
return result
missed += result
return missed
ctk_roots = (resolve_ctk_root_via_canary(libname) for libname in ctx.desc.ctk_root_canary_anchor_libnames)
return first_hit(find_via_ctk_root(ctx, ctk_root) for ctk_root in ctk_roots if ctk_root is not None)
Comment on lines 64 to +67

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability suggestion (non-blocking, no behavior change): same idea here, to avoid chaining two generators with a None filter on the second. A named generator makes "skip anchors that do not resolve" explicit, and it stays lazy, so the canary probe subprocess still runs one anchor at a time and stops at the first hit.

def _iter_canary_ctk_roots(ctx: SearchContext) -> Iterator[str]:
    """Yield the CTK root found by each canary anchor library, skipping anchors that do not resolve."""
    for libname in ctx.desc.ctk_root_canary_anchor_libnames:
        ctk_root = resolve_ctk_root_via_canary(libname)
        if ctk_root is not None:
            yield ctk_root


def _try_ctk_root_canary(ctx: SearchContext) -> FindResult | NotFound:
    """Try CTK-root canary fallback for descriptor-configured libraries."""
    return first_hit(find_via_ctk_root(ctx, ctk_root) for ctk_root in _iter_canary_ctk_roots(ctx))

Please keep the helper in this module: resolve_ctk_root_via_canary is looked up as a module attribute here, and tests may patch it at this location. This file would also need Iterator imported (from collections.abc).



def _load_lib_no_cache(libname: str) -> LoadedDL:
Expand Down
59 changes: 24 additions & 35 deletions cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import glob
import os
from collections.abc import Callable, Iterator
from collections.abc import Callable, Iterable, Iterator

from cuda.pathfinder._dynamic_libs.search_platform import (
FindResult,
Expand All @@ -49,6 +49,19 @@
FindStep = Callable[[SearchContext], FindResult | NotFound]


def first_hit(results: Iterable[FindResult | NotFound]) -> FindResult | NotFound:
"""Return the first hit in *results*, or else every miss combined in order.

*results* is read lazily: nothing after the first hit is evaluated.
"""
missed = NotFound()
for result in results:
if result.found:
return result
missed += result
return missed


def _iter_lib_dirs(root: str, rel_dirs: tuple[str, ...]) -> Iterator[str]:
"""Yield existing library directories under *root* in descriptor order."""
for rel_path in rel_dirs:
Expand All @@ -64,13 +77,7 @@ def _find_under_root(
found_via: str,
) -> FindResult | NotFound:
"""Resolve *rel_dirs* under *root*, then find the requested library."""
missed = NotFound()
for lib_dir in _iter_lib_dirs(root, rel_dirs):
result = find_lib_in_dir(ctx, lib_dir, found_via)
if result.found:
return result
missed += result
return missed
return first_hit(find_lib_in_dir(ctx, lib_dir, found_via) for lib_dir in _iter_lib_dirs(root, rel_dirs))


def _find_under_anchor_root(ctx: SearchContext, root: str, found_via: str) -> FindResult | NotFound:
Expand Down Expand Up @@ -156,16 +163,8 @@ def find_in_conda(ctx: SearchContext) -> FindResult | NotFound:
def find_in_install_root_env_vars(ctx: SearchContext) -> FindResult | NotFound:
"""Search installation roots named by descriptor-specific environment variables."""
rel_dirs = ctx.install_root_env_rel_dirs
missed = NotFound()
for env_var in ctx.install_root_env_vars:
root = os.environ.get(env_var)
if not root:
continue
result = _find_under_root(ctx, root, rel_dirs, env_var)
if result.found:
return result
missed += result
return missed
roots = ((env_var, os.environ.get(env_var)) for env_var in ctx.install_root_env_vars)
return first_hit(_find_under_root(ctx, root, rel_dirs, env_var) for env_var, root in roots if root)
Comment on lines 163 to +167

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability suggestion (non-blocking, no behavior change): the two-stage generator plus the trailing if root filter makes the reader assemble the "skip unset or empty env vars" rule from two expressions. A small named generator, in the style of _iter_lib_dirs, keeps the loop-and-if shape visible and leaves the caller as a one-line first_hit(...). It stays lazy, so os.environ.get is still read in the same order relative to the searches.

def _iter_install_root_env_vars(ctx: SearchContext) -> Iterator[tuple[str, str]]:
    """Yield (env_var, root) for each descriptor env var that is set and non-empty."""
    for env_var in ctx.install_root_env_vars:
        root = os.environ.get(env_var)
        if root:
            yield env_var, root


def find_in_install_root_env_vars(ctx: SearchContext) -> FindResult | NotFound:
    """Search installation roots named by descriptor-specific environment variables."""
    rel_dirs = ctx.install_root_env_rel_dirs
    return first_hit(
        _find_under_root(ctx, root, rel_dirs, env_var)
        for env_var, root in _iter_install_root_env_vars(ctx)
    )



def find_in_cuda_path(ctx: SearchContext) -> FindResult | NotFound:
Expand All @@ -187,16 +186,12 @@ def find_in_cuda_path(ctx: SearchContext) -> FindResult | NotFound:

def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound:
"""Search descriptor-configured installation roots under Program Files."""
missed = NotFound()
for root_glob in ctx.program_files_root_globs():
for root in sorted(glob.glob(root_glob), key=numeric_aware_path_sort_key, reverse=True):
if not os.path.isdir(root):
continue
result = _find_under_anchor_root(ctx, resolve_path(root), FOUND_VIA_PROGRAM_FILES)
if result.found:
return result
missed += result
return missed
return first_hit(
_find_under_anchor_root(ctx, resolve_path(root), FOUND_VIA_PROGRAM_FILES)
for root_glob in ctx.program_files_root_globs()
for root in sorted(glob.glob(root_glob), key=numeric_aware_path_sort_key, reverse=True)
if os.path.isdir(root)
)
Comment on lines 187 to +194

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability suggestion (non-blocking, no behavior change): this is the hardest of the new expressions to read. It has a nested for, a sorted(...) with a key and reverse, and a filter, all inside one call argument. Moving the candidate enumeration into a named generator documents the ordering rule ("newest version first") and leaves the caller trivial. Laziness is unchanged: glob.glob for the next root_glob still runs only after the previous roots missed.

def _iter_program_files_roots(ctx: SearchContext) -> Iterator[str]:
    """Yield existing Program Files install roots, newest version first."""
    for root_glob in ctx.program_files_root_globs():
        for root in sorted(glob.glob(root_glob), key=numeric_aware_path_sort_key, reverse=True):
            if os.path.isdir(root):
                yield root


def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound:
    """Search descriptor-configured installation roots under Program Files."""
    return first_hit(
        _find_under_anchor_root(ctx, resolve_path(root), FOUND_VIA_PROGRAM_FILES)
        for root in _iter_program_files_roots(ctx)
    )



# ---------------------------------------------------------------------------
Expand All @@ -221,10 +216,4 @@ def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound:

def run_find_steps(ctx: SearchContext, steps: tuple[FindStep, ...]) -> FindResult | NotFound:
"""Run find steps in order, returning the first hit or else every step's miss combined."""
missed = NotFound()
for step in steps:
result = step(ctx)
if result.found:
return result
missed += result
return missed
return first_hit(step(ctx) for step in steps)
Loading