import os
|
|
import ray
|
|
|
|
'''
|
|
The following switch allows the program to run locally or on the Agit distributed cluster without modifications.
|
|
'''
|
|
if 'CLOUD_PROVIDER' in os.environ and os.environ['CLOUD_PROVIDER'] == 'Agit':
|
|
from agit import ray_init
|
|
ray_init()
|
|
else:
|
|
ray.init()
|
|
|
|
|
|
@ray.remote
|
|
class Counter(object):
|
|
def __init__(self):
|
|
self.value = 0
|
|
|
|
def increment(self):
|
|
self.value += 1
|
|
return self.value
|
|
|
|
|
|
# Create an actor
|
|
c = Counter.remote()
|
|
|
|
object_id = c.increment.remote()
|
|
assert ray.get(object_id) == 1
|
|
|
|
# Methods called on different actors can execute in parallel, and methods
|
|
# called on the same actor are executed serially in the order that they
|
|
# are called. Methods on the same actor will share state with one another,
|
|
# as shown below.
|
|
|
|
# Create ten Counter actors.
|
|
counters = [Counter.remote() for _ in range(10)]
|
|
|
|
# Increment each Counter once and get the results. These tasks all happen in
|
|
# parallel.
|
|
results = ray.get([c.increment.remote() for c in counters])
|
|
print(results) # prints [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
|
|
|
|
# Increment the first Counter five times. These tasks are executed serially
|
|
# and share state.
|
|
results = ray.get([counters[0].increment.remote() for _ in range(5)])
|
|
print(results) # prints [2, 3, 4, 5, 6]
|