Skip to content

Instantly share code, notes, and snippets.

@stephanie-wang
Created January 12, 2024 22:55
Show Gist options
  • Save stephanie-wang/56b6bfa31f5c882160a2afc86f801096 to your computer and use it in GitHub Desktop.
Save stephanie-wang/56b6bfa31f5c882160a2afc86f801096 to your computer and use it in GitHub Desktop.
Custom resources workaround for nested tasks
import ray
import time
@ray.remote(num_cpus=0)
class Counter:
def __init__(self):
self.num_f = 0
self.num_g = 0
self.max_f, self.max_g = 0, 0
def inc_f(self, val):
self.num_f += val
if self.num_f > self.max_f:
self.max_f = self.num_f
def inc_g(self, val):
self.num_g += val
if self.num_g > self.max_g:
self.max_g = self.num_g
def get(self):
return (self.max_f, self.max_g)
@ray.remote(num_cpus=1)
class G:
def run(self, counter):
counter.inc_g.remote(1)
time.sleep(1)
ray.get(counter.inc_g.remote(-1))
@ray.remote
def f(counter):
counter.inc_f.remote(1)
time.sleep(1)
g = G.remote()
ray.get(g.run.remote(counter))
ray.get(counter.inc_f.remote(-1))
ray.init(num_cpus=2)
counter = Counter.remote()
ray.get(counter.get.remote())
ray.get([f.remote(counter) for _ in range(10)])
print(ray.get(counter.get.remote()))
import ray
import time
@ray.remote(num_cpus=0)
class Counter:
def __init__(self):
self.num_f = 0
self.num_g = 0
self.max_f, self.max_g = 0, 0
def inc_f(self, val):
self.num_f += val
if self.num_f > self.max_f:
self.max_f = self.num_f
def inc_g(self, val):
self.num_g += val
if self.num_g > self.max_g:
self.max_g = self.num_g
def get(self):
return (self.max_f, self.max_g)
@ray.remote(num_cpus=1)
class G:
def run(self, counter):
counter.inc_g.remote(1)
time.sleep(1)
ray.get(counter.inc_g.remote(-1))
@ray.remote(resources={"f": 1})
def f(counter):
counter.inc_f.remote(1)
time.sleep(1)
g = G.remote()
ray.get(g.run.remote(counter))
ray.get(counter.inc_f.remote(-1))
ray.init(num_cpus=2, resources={"f": 1})
counter = Counter.remote()
ray.get(counter.get.remote())
ray.get([f.remote(counter) for _ in range(10)])
print(ray.get(counter.get.remote()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment