Skip to content
This repository was archived by the owner on Oct 30, 2026. It is now read-only.
This repository was archived by the owner on Oct 30, 2026. It is now read-only.

Non-strict nested machine scoping #977

Open

Description

@elliottslaughter

I have a very long function for which I only want to run a small part at the end on the GPU. At the moment, I can't figure out how to do this with Legate without wrapping nearly the entire body of the function in a with scope to set the machine, which is undesirable because it creates a very large diff on my program and causes a lot of code to get needlessly indented.

The basic pattern looks like this:

import cupynumeric as cpn
from legate.core import TaskTarget, VariantCode, broadcast, get_machine
from legate.core.task import task, InputArray

@task(variants=(VariantCode.GPU,))
def inner_task(x : InputArray):
    pass

def outer_fn():
    # lots of initialization here, want to run this on the CPU...
    y = cpn.zeros(10)

    # ... eventually call this GPU task
    inner_task()

if __name__ == "__main__":
    with get_machine().only(TaskTarget.OMP):
        outer_fn()

I only want to switch to GPU execute when we hit the call to inner_task. If we execute the rest of outer_fn on the GPU, we will run out of memory because the temporaries used in this function are very large.

Doing this in a naive way with Legate leads to:

LEGATE ERROR: #0 std::invalid_argument: Task inner_task {GPU_VARIANT:[1516abf8b5e0, ()],} does not have any valid variant for the current machine configuration Machine(preferred_target: OMP, OMP: Proc([0,4], 4 per node))

My initial thought was to introduce a nested scope just for inner_task. However this also fails:

import cupynumeric as cpn
from legate.core import TaskTarget, VariantCode, broadcast, get_machine
from legate.core.task import task, InputArray

@task(variants=(VariantCode.GPU,))
def inner_task(x : InputArray):
    pass

def outer_fn():
    # lots of initialization here, want to run this on the CPU...
    y = cpn.zeros(10)

    # ... eventually call this GPU task
    with get_machine().only(TaskTarget.GPU):
        inner_task()

if __name__ == "__main__":
    with get_machine().only(TaskTarget.OMP):
        outer_fn()
LEGATE ERROR: #0 std::runtime_error: Empty machines cannot be used for resource scoping

At this point I think I'm stuck. There is a LOT of code at the top of outer_fn that I would rather not wrap with a with ... block, but it appears that Legate is too restrictive to let me say "actually, run this on the GPU again" inside a nested CPU/OMP scope.

Activity

  1. ipdemes commented on Mar 26, 2025

    @ipdemes
    Contributor

    @elliottslaughter ,
    I think you would have to either put upper part of the outer_fn into some function and then wrap it with with get_machine().only(TaskTarget.OMP): or just wrap this part with with like below

    import cupynumeric as cpn
    from legate.core import TaskTarget, VariantCode, broadcast, get_machine
    from legate.core.task import task, InputArray
    
    @task(variants=(VariantCode.GPU,))
    def inner_task(x : InputArray):
        pass
    
    def outer_fn():
        with get_machine().only(TaskTarget.OMP):
            # lots of initialization here, want to run this on the CPU...
            y = cpn.zeros(10)
    
        # ... eventually call this GPU task
        inner_task()
    
    if __name__ == "__main__":
            outer_fn()
    
    

    When you do:

     with get_machine().only(TaskTarget.OMP):
         some_func()
    

    then some_func can only be run on OMP. Legate "slice" machine's processors and set only OMP processors visible within some_func

  2. elliottslaughter commented on Mar 26, 2025

    @elliottslaughter
    Author

    I found a way to make this use case work for me, by reducing memory usage enough that I can fit everything onto GPUs. For now, this addresses my use case.

  3. manopapad commented on Apr 3, 2025

    @manopapad
    Contributor

    I'll keep the issue open to track this (otherwise legitimate) request, even if it's not critical for the moment.

  4. added a commit that references this issue on Apr 11, 2025
  5. manopapad commented on May 22, 2025

    @manopapad
    Contributor

    @elliottslaughter would this API help to work around your underlying problem (needing to move memory out of the GPU to make space)?

  6. elliottslaughter commented on Jun 3, 2025

    @elliottslaughter
    Author

    Maybe, but figuring out what arrays are taking up space (and not immediately required) is still tricky and not something I'd want to inflict on our end users in general.

    Current evidence seems to indicate that we do get a benefit from putting initialization on the GPU, so I'm honestly not sure what the best answer is. Putting it entirely on the CPU (until I get to the really performance critical part) may work at some level but appears to be sacrificing performance.

    In the meantime, like I said before, I just fiddled with it to make it all fit on GPU.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions