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()
|
|
|
|
|
|
# A regular Python function.
|
|
def regular_function():
|
|
return 1
|
|
|
|
|
|
# A Ray remote function.
|
|
@ray.remote
|
|
def remote_function():
|
|
return 1
|
|
|
|
|
|
# A Ray remote function that returns three values.
|
|
@ray.remote(num_return_vals=3)
|
|
def return_multiple():
|
|
return 1, 2, 3
|
|
|
|
|
|
@ray.remote
|
|
def add_three_values(a, b, c):
|
|
return a + b + c
|
|
|
|
|
|
assert regular_function() == 1
|
|
|
|
# Ray function (task) returns an object ID (a future)
|
|
object_id = remote_function.remote()
|
|
assert ray.get(object_id) == 1
|
|
|
|
# remote functions can return multiple object IDs.
|
|
a_id, b_id, c_id = return_multiple.remote()
|
|
assert ray.get(a_id) == 1 and ray.get(b_id) == 2 and ray.get(c_id) == 3
|
|
|
|
# Object IDs can also be passed into remote functions. When the function
|
|
# actually gets executed, the argument will be retrieved as a regular Python object.
|
|
# Note that the second task will not be executed until the first task has
|
|
# finished executing because the second task depends on the output of the
|
|
# first task.
|
|
chained_id = add_three_values.remote(a_id, b_id, c_id)
|
|
assert ray.get(chained_id) == 6
|