2022-03-10 20:39:44 +01:00
.. include :: /_includes/core/announcement.rst
2020-10-16 10:10:56 -07:00
.. _core-walkthrough:
2020-02-11 23:17:30 -08:00
Ray Core Walkthrough
====================
2019-08-05 23:33:14 -07:00
This walkthrough will overview the core concepts of Ray:
2020-02-11 23:17:30 -08:00
1. Starting Ray
2020-07-30 11:13:38 +08:00
2. Using remote functions (tasks)
2022-03-17 11:26:17 -07:00
3. Using remote classes (actors)
4. Working with Ray Objects
2019-08-05 23:33:14 -07:00
2020-02-11 23:17:30 -08:00
With Ray, your code will work on a single machine and can be easily scaled to large cluster.
2020-07-31 15:35:43 +08:00
Java demo code in this documentation can be found `here <https://github.com/ray-project/ray/blob/master/java/test/src/main/java/io/ray/docdemo/WalkthroughDemo.java> `__ .
2020-07-30 11:13:38 +08:00
2020-02-11 23:17:30 -08:00
Installation
------------
2022-01-25 02:00:41 +01:00
.. tabbed :: Python
2020-09-05 10:15:52 +08:00
To run this walkthrough, install Ray with `` pip install -U ray `` . For the latest wheels (for a snapshot of `` master `` ), you can use these instructions at :ref: `install-nightlies` .
2022-01-25 02:00:41 +01:00
.. tabbed :: Java
2020-09-05 10:15:52 +08:00
2020-09-11 06:40:16 +08:00
To run this walkthrough, add `Ray API <https://mvnrepository.com/artifact/io.ray/ray-api> `_ and `Ray Runtime <https://mvnrepository.com/artifact/io.ray/ray-runtime> `_ as dependencies. Snapshot versions can be found in `sonatype repository <https://oss.sonatype.org/#nexus-search;quick~io.ray> `_ .
2020-02-11 23:17:30 -08:00
2021-03-09 18:03:13 +08:00
Note: To run your Ray Java application, you need to install Ray Python with `pip install -U ray` first. (For Ray Java snapshot versions, install nightly Ray Python wheels.) The versions of Ray Java and Ray Python must match.
2022-01-25 02:00:41 +01:00
.. tabbed :: C++
2021-08-12 22:40:09 +08:00
2022-03-18 10:52:57 +08:00
The C++ Ray API is currently experimental with limited support and it's not supported on Windows. You can track its development `here <https://github.com/ray-project/ray/milestone/17> `__ and report issues on GitHub.
2022-02-08 19:04:37 +08:00
Run the following commands to get started:
Install ray with C++ API support and generate a bazel project with the ray command.
.. code-block :: shell
pip install "ray[cpp]"
mkdir ray-template && ray cpp --generate-bazel-project-template-to ray-template
The project template comes with a simple example application. You can try this example out in 2 ways:
1. Run the example application directly, which will start a Ray cluster locally.
.. code-block :: shell
cd ray-template && bash run.sh
2. Connect the example application to an existing Ray cluster by specifying the RAY_ADDRESS env var.
.. code-block :: shell
ray start --head
RAY_ADDRESS=127.0.0.1:6379 bash run.sh
Now you can build your own Ray C++ application based on this project template.
2021-08-12 22:40:09 +08:00
2022-06-24 02:14:50 +08:00
.. note ::
If you build Ray from source, please remove the build option `` build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" `` from the file `` cpp/example/.bazelrc `` before you run the example application. The related issue is `here <https://github.com/ray-project/ray/issues/26031> `_ .
2020-02-11 23:17:30 -08:00
Starting Ray
------------
2020-07-30 11:13:38 +08:00
You can start Ray on a single machine by adding this to your code.
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.
2022-01-25 02:00:41 +01:00
.. tabbed :: Python
2020-07-30 11:13:38 +08:00
2022-01-25 02:00:41 +01:00
.. code-block :: python
2020-07-30 11:13:38 +08:00
2022-01-25 02:00:41 +01:00
import ray
2019-08-05 23:33:14 -07:00
2022-01-25 02:00:41 +01:00
# Start Ray. If you're connecting to an existing cluster, you would use
# ray.init(address=<cluster-address>) instead.
ray.init()
2019-08-05 23:33:14 -07:00
2022-01-25 02:00:41 +01:00
...
2019-08-05 23:33:14 -07:00
2022-01-25 02:00:41 +01:00
.. tabbed :: Java
2019-08-05 23:33:14 -07:00
2022-01-25 02:00:41 +01:00
.. code-block :: java
2020-07-30 11:13:38 +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) {
// Start Ray runtime. If you're connecting to an existing cluster, you can set
// the `-Dray.address=<cluster-address>` java system property.
Ray.init();
...
}
}
.. tabbed :: C++
2021-08-12 22:40:09 +08:00
2022-01-25 02:00:41 +01:00
.. code-block :: c++
// Run `ray cpp --show-library-path` to find headers and libraries.
#include <ray/api.h>
int main(int argc, char **argv) {
// Start Ray runtime. If you're connecting to an existing cluster, you can set
// the `RAY_ADDRESS` env var.
ray::Init();
...
}
2021-08-12 22:40:09 +08:00
2020-02-11 23:17:30 -08:00
Ray will then be able to utilize all cores of your machine. Find out how to configure the number of cores Ray will use at :ref: `configuring-ray` .
2020-06-26 09:29:22 -07:00
To start a multi-node Ray cluster, see the :ref: `cluster setup page <cluster-index>` .
2019-08-05 23:33:14 -07:00
2022-03-17 11:26:17 -07:00
Using Tasks, Actors, and Objects
--------------------------------
2020-05-29 09:55:47 -07:00
2022-03-17 11:26:17 -07:00
Click through below to walk through using Ray's key concepts: Tasks, Actors, and Objects.
2021-08-12 22:40:09 +08:00
2021-10-29 10:02:39 -07:00
.. tip ::
2022-03-17 11:26:17 -07:00
We suggest reading through the walkthrough for each section prior to browsing more deeply into the materials.
2021-08-05 08:48:34 -07:00
2022-03-17 11:26:17 -07:00
.. panels ::
:container: container pb-4
:column: col-md-4 px-2 py-2
:img-top-cls: pt-5 w-50 d-block mx-auto
2019-08-05 23:33:14 -07:00
2022-03-17 11:26:17 -07:00
---
:img-top: /images/tasks.png
2019-08-05 23:33:14 -07:00
2022-03-17 11:26:17 -07:00
.. link-button :: ray-remote-functions
:type: ref
:text: Using remote functions (Tasks)
:classes: btn-link btn-block stretched-link
2019-08-05 23:33:14 -07:00
2022-03-17 11:26:17 -07:00
---
:img-top: /images/actors.png
2019-08-05 23:33:14 -07:00
2022-03-17 11:26:17 -07:00
.. link-button :: ray-remote-classes
:type: ref
:text: Using remote classes (Actors)
:classes: btn-link btn-block stretched-link
2019-08-05 23:33:14 -07:00
2022-03-17 11:26:17 -07:00
---
:img-top: /images/objects.png
2021-08-12 22:40:09 +08:00
2022-03-17 11:26:17 -07:00
.. link-button :: objects-in-ray
:type: ref
:text: Working with Ray Objects
:classes: btn-link btn-block stretched-link
2021-08-12 22:40:09 +08:00
2022-03-10 20:39:44 +01:00
.. include :: /_includes/core/announcement_bottom.rst