Skip to content

Commit a69971e

Browse files
authored
Merge pull request #134 from pockerman/integrate_ray_for_distributed_computation
Update documentation
2 parents c460801 + c2f5111 commit a69971e

File tree

16 files changed

+472
-25
lines changed

16 files changed

+472
-25
lines changed

CMakeLists.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ IF(COMMAND cmake_policy)
2626
ENDIF(COMMAND cmake_policy)
2727

2828
OPTION(ENABLE_TESTS_FLAG OFF)
29-
OPTION(ENABLE_EXAMPLES_FLAG OFF)
29+
OPTION(ENABLE_EXAMPLES OFF)
3030
OPTION(ENABLE_DOC_FLAG OFF)
3131
OPTION(ENABLE_WEBOTS OFF)
32+
OPTION(ENABLE_RAY OFF)
3233

3334
SET(CMAKE_BUILD_TYPE "Debug")
3435

@@ -74,16 +75,26 @@ IF(ENABLE_WEBOTS)
7475
# we have webots include the directories
7576
SET(WEBOTS_LIB_DIRS ${PROJECT_SOURCE_DIR}/external/webots/lib/controller)
7677
SET(WEBOTS_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/external/webots/include/controller/cpp)
78+
SET(RLENVSCPP_WEBOTS ON)
7779

7880
INCLUDE_DIRECTORIES(${WEBOTS_INCLUDE_DIRS})
7981
LINK_DIRECTORIES(${WEBOTS_LIB_DIRS})
80-
#MESSAGE( STATUS "${WEBOTS_INCLUDE_DIRS}")
81-
82-
SET(RLENVSCPP_WEBOTS ON)
8382
ELSE()
8483
MESSAGE( WARNING "Building without Webots.")
8584
ENDIF()
8685

86+
IF(ENABLE_RAY)
87+
88+
SET(RLENVSCPP_RAY ON)
89+
SET(RAY_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/external/ray/thirdparty/include)
90+
SET(RAY_LIB_DIRS ${PROJECT_SOURCE_DIR}/external/ray/thirdparty/lib)
91+
92+
INCLUDE_DIRECTORIES(${RAY_INCLUDE_DIRS})
93+
LINK_DIRECTORIES(${RAY_LIB_DIRS})
94+
ELSE()
95+
MESSAGE( WARNING "Building without Ray.")
96+
ENDIF()
97+
8798
configure_file(config.h.in ${PROJECT_SOURCE_DIR}/src/rlenvs/rlenvscpp_config.h @ONLY)
8899
configure_file(version.h.in ${PROJECT_SOURCE_DIR}/src/rlenvs/rlenvscpp_version.h @ONLY)
89100

@@ -141,7 +152,7 @@ SET_TARGET_PROPERTIES(rlenvscpplib PROPERTIES LINKER_LANGUAGE CXX)
141152
INSTALL(TARGETS rlenvscpplib DESTINATION ${CMAKE_INSTALL_PREFIX})
142153
MESSAGE(STATUS "Installation destination at: ${CMAKE_INSTALL_PREFIX}")
143154

144-
IF(ENABLE_EXAMPLES_FLAG)
155+
IF(ENABLE_EXAMPLES)
145156
# Add the examples
146157
ADD_SUBDIRECTORY(examples)
147158
ELSE()

config.h.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
/*Use PyTorch */
88
#cmakedefine USE_PYTORCH
99

10+
/*Use Webots*/
1011
#cmakedefine RLENVSCPP_WEBOTS
1112

13+
/*Use Ray*/
14+
#cmakedefine RLENVSCPP_RAY
15+
1216
#endif
1317

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Examples
2+
============
3+
4+
5+
.. toctree::
6+
:maxdepth: 4
7+
8+
examples/ray/ray_example_1.rst
9+
10+
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
Using Ray core Tasks
2+
===========================
3+
4+
This example will show you how to use one of Ray's core abstractions.
5+
Namely how to create Ray `Tasks <a href="https://docs.ray.io/en/latest/ray-core/tasks.html">`_,
6+
7+
With Ray we can execute arbitrary functions on separate workers.
8+
These functions are called Ray tasks. The code snippet below shows
9+
you how to create a Ray Task:
10+
11+
.. code-block::
12+
13+
// we need to include the Ray API
14+
#include <ray/api.h>
15+
16+
// This is the C++ function we want to execute
17+
int do_sth() {
18+
return 1;
19+
}
20+
21+
// In order to be able to execute this function
22+
// we need to register it using `RAY_REMOTE`.
23+
RAY_REMOTE(do_sth);
24+
25+
int main(int argc, char **argv) {
26+
27+
// We need to initialize Ray before using it
28+
ray::Init();
29+
30+
// Invoke the above method as a Ray task.
31+
// This will immediately return an object ref (a future) and then create
32+
// a task that will be executed on a worker process.
33+
auto res = ray::Task(do_sth).Remote();
34+
35+
// The result can be retrieved with ``ray::ObjectRef::Get``.
36+
auto result = *res.Get();
37+
38+
// shutodown Ray
39+
ray::Shutdown();
40+
41+
std::cout<<"Result is: "<<result<<std::endl;
42+
std::cout<<"Running normal C++ stuff here..."<<std::endl;
43+
44+
result = do_sth();
45+
std::cout<<"Result is: "<<result<<std::endl;
46+
47+
48+
return 0;
49+
}
50+
51+
You can build the code using the ``cmake`` toolchain, but Ray favors `Bazel <https://bazel.build/>`_
52+
for building and running jobs. Let's see how we can use this to build the example.
53+
Copy the following files and directories from ``external/ray`` directory:
54+
55+
- ``run.sh``
56+
- ``BUILD.bazel``
57+
- ``WORKSPACE``
58+
- ``thirdparty``
59+
60+
into the directory where the ``ray_example_1`` is located.
61+
Edit the ``run.sh`` file and change the line where the ``bazel`` executable is called to this:
62+
63+
.. code-block::
64+
65+
bazel build //:ray_example_1
66+
67+
In addition you need to change where the Ray library is located.
68+
Edit the line where the ``LD_LIBRARY_PATH`` variable is declared. This should
69+
point to ``external/ray/thirdparty/lib`` path.
70+
71+
.. code-block::
72+
73+
LD_LIBRARY_PATH="thirdparty/lib" "${ROOT_DIR}"/bazel-bin/ray_example_1
74+
75+
Edit the ``BUILD.bazel`` file and change accordingly the variables:
76+
77+
.. code-block::
78+
79+
cc_binary(
80+
name = "ray_example_1",
81+
srcs = glob([
82+
"*.cpp",
83+
]),
84+
data = [
85+
"ray_example_1.so",
86+
],
87+
linkstatic = True,
88+
deps = [
89+
":ray_api",
90+
],
91+
)
92+
93+
cc_binary(
94+
name = "ray_example_1.so",
95+
srcs = glob([
96+
"*.cpp",
97+
]),
98+
linkopts = ["-shared"],
99+
linkstatic = True,
100+
deps = [
101+
":ray_api",
102+
],
103+
)
104+
105+
cc_library(
106+
name = "ray_api",
107+
srcs = [
108+
"thirdparty/lib/libray_api.so",
109+
],
110+
hdrs = glob([
111+
"thirdparty/include/**/*.h",
112+
"thirdparty/include/**/*.hpp",
113+
]),
114+
linkopts = ["-Wl,-rpath,./"],
115+
strip_include_prefix = "thirdparty/include",
116+
visibility = ["//visibility:public"],
117+
)
118+
119+
In order to build and run the example execute the ``run.sh`` script. This should
120+
produce the following output:
121+
122+
.. code-block::
123+
124+
INFO: Analyzed target //:ray_example_2 (1 packages loaded, 3440 targets configured).
125+
INFO: Found 1 target...
126+
Target //:ray_example_2 up-to-date:
127+
bazel-bin/ray_example_2
128+
INFO: Elapsed time: 2.509s, Critical Path: 2.15s
129+
INFO: 4 processes: 4 linux-sandbox.
130+
INFO: Build completed successfully, 6 total actions
131+
[2025-04-05 12:13:21,602 I 24601 24601] config_internal.cc:216: No code search path found yet. The program location path "/home/alex/.cache/bazel/_bazel_alex/25a018d10ef2129864cd574bc2dbc5b9/execroot/__main__/bazel-out/k8-fastbuild/bin" will be added for searching dynamic libraries by default. And you can add some search paths by '--ray_code_search_path'
132+
[2025-04-05 12:13:21,603 I 24601 24601] process_helper.cc:51: ray start --head --port 6379 --redis-username default --redis-password 5241590000000000 --node-ip-address '192.168.0.129'
133+
2025-04-05 12:13:22,376 - INFO - NumExpr defaulting to 8 threads.
134+
Usage stats collection is enabled. To disable this, add `--disable-usage-stats` to the command that starts the cluster, or run the following command: `ray disable-usage-stats` before starting the cluster. See https://docs.ray.io/en/master/cluster/usage-stats.html for more details.
135+
136+
Local node IP: 192.168.0.129
137+
138+
--------------------
139+
Ray runtime started.
140+
--------------------
141+
142+
Next steps
143+
To add another node to this Ray cluster, run
144+
ray start --address='192.168.0.129:6379'
145+
146+
To connect to this Ray cluster:
147+
import ray
148+
ray.init(_node_ip_address='192.168.0.129')
149+
150+
To submit a Ray job using the Ray Jobs CLI:
151+
RAY_ADDRESS='http://127.0.0.1:8265' ray job submit --working-dir . -- python my_script.py
152+
153+
See https://docs.ray.io/en/latest/cluster/running-applications/job-submission/index.html
154+
for more information on submitting Ray jobs to the Ray cluster.
155+
156+
To terminate the Ray runtime, run
157+
ray stop
158+
159+
To view the status of the cluster, use
160+
ray status
161+
162+
To monitor and debug Ray, view the dashboard at
163+
127.0.0.1:8265
164+
165+
If connection to the dashboard fails, check your firewall settings and network configuration.
166+
[2025-04-05 12:13:25,278 I 24601 24601] gcs_client.cc:98: GcsClient has no Cluster ID set, and won't fetch from GCS.
167+
[2025-04-05 12:13:25,292 I 24601 24601] gcs_client.cc:98: GcsClient has no Cluster ID set, and won't fetch from GCS.
168+
2025-04-05 12:13:26,854 - INFO - NumExpr defaulting to 8 threads.
169+
Stopped all 5 Ray processes.
170+
Result is: 1
171+
Running normal C++ stuff here...
172+
Result is: 1
173+
174+
175+

docs/images/ray_libs.png

152 KB
Loading

docs/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
overview
1414
how_to_use
15-
working_with_webots
1615
installation
16+
working_with_webots
17+
working_with_ray
18+
examples
1719
api/library_root
1820

1921

docs/installation.rst

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,27 @@ The library has the following general dependencies
1313
- `CMake >= 3.10 <https://cmake.org/>`_
1414
- `Gtest <https://github.com/google/googletest>`_ (if configured with tests)
1515
- `Eigen3 <https://eigen.tuxfamily.org/index.php?title=Main_Page>`_
16+
- `Ray Ray <https://docs.ray.io/en/master/index.html>`_ (if configured with Ray)
1617

1718

18-
``rlenvscpp`` also incorporates, see ``(src/extern)``, the following libraries
19+
``rlenvscpp`` also incorporates, see ``(src/extern)``, the following libraries:
1920

2021
- `HTTPRequest <https://github.com/elnormous/HTTPRequest>`_
2122
- `nlohmann/json <https://github.com/nlohmann/json>`_
2223

24+
.. note::
2325

24-
Using the Gymnasium environments requires `Gymnasium <https://github.com/Farama-Foundation/Gymnasium/tree/main>`_
25-
installed on your machine. In addition, you need to install
26+
Using the Gymnasium environments requires `Gymnasium <https://github.com/Farama-Foundation/Gymnasium/tree/main>`_
27+
installed on your machine. In addition, you need to install
2628

27-
- `FastAPI <https://fastapi.tiangolo.com/>`_
28-
- `Uvicorn <https://www.uvicorn.org/>`_
29-
- `Pydantic <a href="https://docs.pydantic.dev/latest/>`_
29+
- `FastAPI <https://fastapi.tiangolo.com/>`_
30+
- `Uvicorn <https://www.uvicorn.org/>`_
31+
- `Pydantic <a href="https://docs.pydantic.dev/latest/>`_
3032

31-
By installing the requirement under ``requirements.txt`` should set your Python environment up correctly.
33+
By installing the requirement under ``requirements.txt`` should set your Python environment up correctly.
3234

33-
There are extra dependencies if you want to generate the documentation. Namely,
3435

35-
- Doxygen
36-
- Sphinx
37-
- sphinx_rtd_theme
38-
- breathe
39-
- m2r2
40-
41-
Building the code
36+
Build the code
4237
-----------------
4338

4439
The usual CMake based installation process is used. Namely
@@ -55,24 +50,56 @@ You can toggle the following variables
5550
- ENABLE_TESTS_FLAG (default is OFF)
5651
- ENABLE_EXAMPLES_FLAG (default is OFF)
5752
- ENABLE_DOC_FLAG (default is OFF)
53+
- ENABLE_WEBOTS (default OFF)
54+
- ENABLE_RAY (default OFF)
5855

59-
For example enbling the examples
56+
For example enabling the examples:
6057

6158
.. code-block::
6259
6360
cmake -DENABLE_EXAMPLES_FLAG=ON ..
6461
make install
62+
6563
64+
.. note::
6665

67-
Run the tests
68-
-------------
66+
If you choose to enable Ray make sure you follow the instructions before building the library.
6967

70-
You can execute all the tests by running the helper script ``execute_tests.sh``.
68+
Run the unit tests
69+
-------------------
70+
71+
You can execute all the tests by running the helper script
72+
73+
.. code-block::
74+
75+
./execute_tests.sh
7176
7277
7378
Build the documentation
7479
-----------------------
7580

81+
Building the documentation is done via `Sphinx <https://www.sphinx-doc.org/en/master/>`_ and there are
82+
extra dependencies you need to install. Namely,
83+
84+
- `Doxygen <https://www.doxygen.nl/>`_
85+
- breathe
86+
- sphinx==5.0.2
87+
- breathe==4.35.0
88+
- exhale==0.3.7
89+
- sphinx_rtd_theme==3.0.2
90+
91+
In order to build the documentation locally execute:
92+
93+
.. code-block::
94+
95+
cd docs
96+
sphinx-build -M html . outputdir
97+
98+
99+
where ``outputdir`` indicates a path in your system where you want to install the documentation files.
100+
Check the official Sphinx `Getting started <https://www.sphinx-doc.org/en/master/usage/quickstart.html>`_ guide
101+
for more options.
102+
76103

77104
Issues
78105
-------

0 commit comments

Comments
 (0)