You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 30, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 30, 2026. It is now read-only.
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:
importcupynumericascpnfromlegate.coreimportTaskTarget, VariantCode, broadcast, get_machinefromlegate.core.taskimporttask, InputArray@task(variants=(VariantCode.GPU,))definner_task(x : InputArray):
passdefouter_fn():
# lots of initialization here, want to run this on the CPU...y=cpn.zeros(10)
# ... eventually call this GPU taskinner_task()
if__name__=="__main__":
withget_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:
importcupynumericascpnfromlegate.coreimportTaskTarget, VariantCode, broadcast, get_machinefromlegate.core.taskimporttask, InputArray@task(variants=(VariantCode.GPU,))definner_task(x : InputArray):
passdefouter_fn():
# lots of initialization here, want to run this on the CPU...y=cpn.zeros(10)
# ... eventually call this GPU taskwithget_machine().only(TaskTarget.GPU):
inner_task()
if__name__=="__main__":
withget_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.
@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
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.
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.
Activity
ipdemes commentedon Mar 26, 2025
@elliottslaughter ,
I think you would have to either put upper part of the
outer_fninto some function and then wrap it withwith get_machine().only(TaskTarget.OMP):or just wrap this part withwithlike belowWhen you do:
then some_func can only be run on OMP. Legate "slice"
machine's processors and set onlyOMPprocessors visible withinsome_funcelliottslaughter commentedon Mar 26, 2025
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.
manopapad commentedon Apr 3, 2025
I'll keep the issue open to track this (otherwise legitimate) request, even if it's not critical for the moment.
MPI Wrappers 2 (nv-legate#977)
manopapad commentedon May 22, 2025
@elliottslaughter would this API help to work around your underlying problem (needing to move memory out of the GPU to make space)?
elliottslaughter commentedon Jun 3, 2025
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.