2020-02-11 23:17:30 -08:00
Starting Ray
============
This page covers how to start Ray on your single machine or cluster of machines.
2020-09-21 11:27:50 -07:00
.. tip :: Be sure to have :ref: `installed Ray <installation>` before following the instructions on this page.
2020-02-11 23:17:30 -08:00
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
What is the Ray runtime?
------------------------
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
Ray programs are able to parallelize and distribute by leveraging an underlying *Ray runtime* .
The Ray runtime consists of multiple services/processes started in the background for communication, data transfer, scheduling, and more. The Ray runtime can be started on a laptop, a single server, or multiple servers.
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
There are three ways of starting the Ray runtime:
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
* Implicitly via `` ray.init() `` (:ref: `start-ray-init` )
* Explicitly via CLI (:ref: `start-ray-cli` )
* Explicitly via the cluster launcher (:ref: `start-ray-up` )
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
.. _start-ray-init:
2020-09-05 10:15:52 +08:00
2020-02-11 23:17:30 -08:00
Starting Ray on a single machine
--------------------------------
2020-09-21 11:27:50 -07:00
Calling `` ray.init() `` (without any `` address `` args) starts a Ray runtime on your laptop/machine. This laptop/machine becomes the "head node".
2021-08-11 13:13:22 -07:00
.. note ::
In recent versions of Ray (>=1.5), `` ray.init() `` will automatically be called on the first use of a Ray remote API.
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: Python
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
.. code-block :: python
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
import ray
# Other Ray APIs will not work until `ray.init()` is called.
ray.init()
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: Java
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
.. code-block :: java
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
import io.ray.api.Ray;
public class MyRayApp {
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
public static void main(String[] args) {
// Other Ray APIs will not work until `Ray.init()` is called.
Ray.init();
...
}
}
2021-08-12 22:40:09 +08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: C++
.. code-block :: c++
#include <ray/api.h>
// Other Ray APIs will not work until `ray::Init()` is called.
ray::Init()
2021-08-12 22:40:09 +08:00
2020-09-21 11:27:50 -07:00
When the process calling `` ray.init() `` terminates, the Ray runtime will also terminate. To explicitly stop or restart Ray, use the shutdown API.
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: Python
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
.. code-block :: python
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
import ray
ray.init()
... # ray program
ray.shutdown()
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: Java
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
.. code-block :: java
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
import io.ray.api.Ray;
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
public class MyRayApp {
2021-08-12 22:40:09 +08:00
2022-01-25 02:00:41 +01:00
public static void main(String[] args) {
Ray.init();
... // ray program
Ray.shutdown();
}
}
.. tabbed :: C++
.. code-block :: c++
2021-08-12 22:40:09 +08:00
2022-01-25 02:00:41 +01:00
#include <ray/api.h>
ray::Init()
... // ray program
ray::Shutdown()
2022-02-08 19:04:37 +08:00
To check if Ray is initialized, use the `` is_initialized `` API.
2020-02-11 23:17:30 -08:00
2022-02-08 19:04:37 +08:00
.. tabbed :: Python
2020-02-11 23:17:30 -08:00
2020-09-05 10:15:52 +08:00
.. code-block :: python
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
import ray
ray.init()
2022-05-02 00:01:10 -05:00
assert ray.is_initialized()
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
ray.shutdown()
2022-05-02 00:01:10 -05:00
assert not ray.is_initialized()
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: Java
2020-02-11 23:17:30 -08:00
2020-09-05 10:15:52 +08:00
.. code-block :: java
2022-01-25 02:00:41 +01:00
import io.ray.api.Ray;
2020-09-05 10:15:52 +08:00
2022-01-25 02:00:41 +01:00
public class MyRayApp {
2020-09-05 10:15:52 +08:00
public static void main(String[] args) {
2022-01-25 02:00:41 +01:00
Ray.init();
Assert.assertTrue(Ray.isInitialized());
Ray.shutdown();
Assert.assertFalse(Ray.isInitialized());
}
2020-09-05 10:15:52 +08:00
}
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: C++
2021-08-12 22:40:09 +08:00
.. code-block :: c++
2022-01-25 02:00:41 +01:00
#include <ray/api.h>
2021-08-12 22:40:09 +08:00
2022-01-25 02:00:41 +01:00
int main(int argc, char **argv) {
ray::Init();
assert(ray::IsInitialized());
2021-08-12 22:40:09 +08:00
2022-01-25 02:00:41 +01:00
ray::Shutdown();
assert(!ray::IsInitialized());
}
2021-08-12 22:40:09 +08:00
2020-02-11 23:17:30 -08:00
See the `Configuration <configure.html> `__ documentation for the various ways to configure Ray.
2020-09-21 11:27:50 -07:00
.. _start-ray-cli:
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
Starting Ray via the CLI (`` ray start `` )
----------------------------------------
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
Use `` ray start `` from the CLI to start a 1 node ray runtime on a machine. This machine becomes the "head node".
2020-09-09 00:46:32 +08:00
2020-09-21 11:27:50 -07:00
.. code-block :: bash
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
$ ray start --head --port=6379
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
Local node IP: 192.123.1.123
2020-09-20 10:38:54,193 INFO services.py:1166 -- View the Ray dashboard at http://localhost:8265
2020-09-05 10:15:52 +08:00
2020-09-21 11:27:50 -07:00
--------------------
Ray runtime started.
--------------------
2020-02-11 23:17:30 -08:00
2020-09-21 11:27:50 -07:00
...
2020-02-11 23:17:30 -08:00
2020-12-10 19:01:40 +08:00
You can connect to this Ray runtime by starting a driver process on the same node as where you ran `` ray start `` :
2022-01-25 02:00:41 +01:00
.. tabbed :: Python
2022-02-08 19:04:37 +08:00
2022-01-25 02:00:41 +01:00
.. code-block :: python
2020-02-11 23:17:30 -08:00
2020-12-10 19:01:40 +08:00
# This must
import ray
ray.init(address='auto')
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: java
2020-09-05 10:15:52 +08:00
2020-12-10 19:01:40 +08:00
.. code-block :: java
2020-09-05 10:15:52 +08:00
2020-12-10 19:01:40 +08:00
import io.ray.api.Ray;
2020-09-05 10:15:52 +08:00
2020-12-10 19:01:40 +08:00
public class MyRayApp {
2020-09-05 10:15:52 +08:00
2020-12-10 19:01:40 +08:00
public static void main(String[] args) {
Ray.init();
...
}
}
2020-02-11 23:17:30 -08:00
2020-09-21 11:27:50 -07:00
.. code-block :: bash
2020-02-11 23:17:30 -08:00
2020-12-10 19:01:40 +08:00
java -classpath <classpath> \
-Dray.address=<address> \
<classname> <args>
2020-02-11 23:17:30 -08:00
2022-01-25 02:00:41 +01:00
.. tabbed :: C++
2021-08-12 22:40:09 +08:00
.. code-block :: c++
#include <ray/api.h>
int main(int argc, char **argv) {
ray::Init();
...
}
.. code-block :: bash
RAY_ADDRESS=<address> ./<binary> <args>
2020-02-11 23:17:30 -08:00
2020-09-21 11:27:50 -07:00
You can connect other nodes to the head node, creating a Ray cluster by also calling `` ray start `` on those nodes. See :ref: `manual-cluster` for more details. Calling `` ray.init(address="auto") `` on any of the cluster machines will connect to the ray cluster.
2020-02-11 23:17:30 -08:00
2020-09-21 11:27:50 -07:00
.. _start-ray-up:
2020-02-11 23:17:30 -08:00
2020-09-21 11:27:50 -07:00
Launching a Ray cluster (`` ray up `` )
------------------------------------
2020-02-11 23:17:30 -08:00
2021-02-15 00:47:14 -08:00
Ray clusters can be launched with the :ref: `Cluster Launcher <cluster-cloud>` .
2020-09-21 11:27:50 -07:00
The `` ray up `` command uses the Ray cluster launcher to start a cluster on the cloud, creating a designated "head node" and worker nodes. Underneath the hood, it automatically calls `` ray start `` to create a Ray cluster.
2020-02-11 23:17:30 -08:00
2020-09-21 11:27:50 -07:00
Your code **only** needs to execute on one machine in the cluster (usually the head node). Read more about :ref: `running programs on a Ray cluster <using-ray-on-a-cluster>` .
2020-02-11 23:17:30 -08:00
2022-03-18 10:52:13 -07:00
To connect to the existing cluster, similar to the method outlined in :ref: `start-ray-cli` , you must call `` ray.init `` and specify the address of the Ray cluster when initializing Ray in your code. This allows your script to connect to the existing Ray runtime on the cluster.
2020-02-11 23:17:30 -08:00
2020-09-21 11:27:50 -07:00
.. code-block :: python
2020-09-09 00:46:32 +08:00
2020-09-21 11:27:50 -07:00
ray.init(address="auto")
2020-09-09 00:46:32 +08:00
2020-09-21 11:27:50 -07:00
Note that the machine calling `` ray up `` will not be considered as part of the Ray cluster, and therefore calling `` ray.init `` on that same machine will not attach to the cluster.
2020-02-11 23:17:30 -08:00
What's next?
------------
2020-11-23 21:52:36 +01:00
Check out our `Deployment section <cluster/index.html> `_ for more information on deploying Ray in different settings, including Kubernetes, YARN, and SLURM.