Learning Ray(1)

Well, I decided to spend some time learning Ray. Why do I do this? Mainly two reasons:

  • I would like to get some understanding regarding how distributed computing framework works
  • I would like to learn to read source code of an open-source project (I know it is kinda embarrassing that I haven’t studied any open source project after being a software engineer for 6 years)

Hello World

The ray website has the first tiny example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import ray

ray.init()

# Define the square task.
@ray.remote
def square(x):
return x * x

# Launch four parallel square tasks.
futures = [square.remote(i) for i in range(4)]

# Retrieve results.
print(ray.get(futures))

Running this code snippet will give the following result

1
[0, 1, 4, 9]

I will ignore the ray.init() for now as there are likely many details there. The interesting thing here is the @ray.remote decorator, it turns the square function into a remote function. Later when we call square.remote(i), it gives us a future, which can be resolved via ray.get().

Check the source code of ray.remote:

1
2
3
4
return ray.remote_function.RemoteFunction(
function_or_class, num_cpus, num_gpus, memory,
object_store_memory, resources, num_return_vals, max_calls,
max_retries)