目前共有4篇帖子。 字體大小:較小 - 100% (默認)▼  內容轉換:不轉換▼
 
點擊 回復
20 3
Linux系统运行Ray C++程序
一派掌門 二十級
1樓 發表于:2026-5-20 15:29
【test.cpp】
#include <ray/api.h>
#include <thread>

int MyFunction()
{
    return 1;
}
RAY_REMOTE(MyFunction);
int SlowFunction()
{
    std::this_thread::sleep_for(std::chrono::seconds(10));
    return 1;
}
RAY_REMOTE(SlowFunction);

int main()
{
    ray::Init();
    
    ray::ObjectRef res = ray::Task(MyFunction).Remote();
    std::shared_ptr<int> value = res.Get();
    std::cout << "value=" << *value << std::endl;
    
    for (int i = 0; i < 4; i++)
        ray::Task(SlowFunction).Remote();

    ray::Shutdown();
    return 0;
}

一派掌門 二十級
2樓 發表于:2026-5-20 15:29

【CMakeLists.txt】

cmake_minimum_required(VERSION 3.10)

add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)

project(test CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(ray::ray_api SHARED IMPORTED)
set(RAY_CPP_DIR "$ENV{HOME}/.local/lib/python3.10/site-packages/ray/cpp")
set_target_properties(ray::ray_api PROPERTIES
    IMPORTED_LOCATION "${RAY_CPP_DIR}/lib/libray_api.so"
    INTERFACE_INCLUDE_DIRECTORIES "${RAY_CPP_DIR}/include"
    INTERFACE_LINK_OPTIONS "-Wl,-rpath,./"
)

set(SOURCE test.cpp)

add_library(test_lib SHARED)
target_sources(test_lib PRIVATE ${SOURCE})
target_include_directories(test_lib PRIVATE ${RAY_CPP_DIR}/include)
target_link_libraries(test_lib PRIVATE ray::ray_api)

add_executable(test ${SOURCE})
target_include_directories(test PRIVATE ${RAY_CPP_DIR}/include)
target_link_libraries(test PRIVATE ray::ray_api)

 
一派掌門 二十級
3樓 發表于:2026-5-20 15:29

【程序运行结果】
oct1158@oct1158-ubuntu:~/Documents/Code/Python$ ls
CMakeLists.txt  ray_test.py  test.cpp
oct1158@oct1158-ubuntu:~/Documents/Code/Python$ cmake .
-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/oct1158/Documents/Code/Python
oct1158@oct1158-ubuntu:~/Documents/Code/Python$ make
[ 25%] Building CXX object CMakeFiles/test_lib.dir/test.cpp.o
[ 50%] Linking CXX shared library libtest_lib.so
[ 50%] Built target test_lib
[ 75%] Building CXX object CMakeFiles/test.dir/test.cpp.o
[100%] Linking CXX executable test
[100%] Built target test
oct1158@oct1158-ubuntu:~/Documents/Code/Python$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  libtest_lib.so  Makefile  ray_test.py  test  test.cpp
oct1158@oct1158-ubuntu:~/Documents/Code/Python$ ./test
[2026-05-20 15:28:32,594 I 12556 12556] config_internal.cc:218: No code search path found yet. The program location path "/home/oct1158/Documents/Code/Python" will be added for searching dynamic libraries by default. And you can add some search paths by '--ray_code_search_path'
[2026-05-20 15:28:32,595 I 12556 12556] process_helper.cc:51: ray start --head --port 6379 --redis-username default --redis-password 5241590000000000 --node-ip-address '192.168.71.129'
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.

Local node IP: 192.168.71.129

--------------------
Ray runtime started.
--------------------

Next steps
  To add another node to this Ray cluster, run
    ray start --address='192.168.71.129:6379'

  To connect to this Ray cluster:
    import ray
    ray.init(_node_ip_address='192.168.71.129')

  To submit a Ray job using the Ray Jobs CLI:
    RAY_API_SERVER_ADDRESS='http://127.0.0.1:8265' ray job submit --working-dir . -- python my_script.py

  See https://docs.ray.io/en/latest/cluster/running-applications/job-submission/index.html
  for more information on submitting Ray jobs to the Ray cluster.

  To terminate the Ray runtime, run
    ray stop

  To view the status of the cluster, use
    ray status

  To monitor and debug Ray, view the dashboard at
    127.0.0.1:8265

  If connection to the dashboard fails, check your firewall settings and network configuration.
[2026-05-20 15:28:56,815 I 12556 12556] gcs_client.cc:96: GcsClient has no Cluster ID set, and won't fetch from GCS.
[2026-05-20 15:28:56,826 I 12556 12556] gcs_client.cc:96: GcsClient has no Cluster ID set, and won't fetch from GCS.
value=1
Stopped only 6 out of 7 Ray processes within the grace period 16 seconds. Set `-v` to see more details. Remaining processes [psutil.Process(pid=12595, name='python3', status='terminated')] will be forcefully terminated.
You can also use `--force` to forcefully terminate processes or set higher `--grace-period` to wait longer time for proper termination.
oct1158@oct1158-ubuntu:~/Documents/Code/Python$

 
一派掌門 二十級
4樓 發表于:2026-5-20 15:33

【重要提示】

必须要把libtest_lib.so库编译出来,否则无法运行test程序,报ray::internal::RayFunctionNotFound: Executable function not found的错误。


oct1158@oct1158-ubuntu:~/Documents/Code/Python$ ./test
[2026-05-20 15:30:53,992 I 13205 13205] config_internal.cc:218: No code search path found yet. The program location path "/home/oct1158/Documents/Code/Python" will be added for searching dynamic libraries by default. And you can add some search paths by '--ray_code_search_path'
[2026-05-20 15:30:53,992 I 13205 13205] process_helper.cc:51: ray start --head --port 6379 --redis-username default --redis-password 5241590000000000 --node-ip-address '192.168.71.129'
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.

Local node IP: 192.168.71.129

--------------------
Ray runtime started.
--------------------

Next steps
  To add another node to this Ray cluster, run
    ray start --address='192.168.71.129:6379'

  To connect to this Ray cluster:
    import ray
    ray.init(_node_ip_address='192.168.71.129')

  To submit a Ray job using the Ray Jobs CLI:
    RAY_API_SERVER_ADDRESS='http://127.0.0.1:8265' ray job submit --working-dir . -- python my_script.py

  See https://docs.ray.io/en/latest/cluster/running-applications/job-submission/index.html
  for more information on submitting Ray jobs to the Ray cluster.

  To terminate the Ray runtime, run
    ray stop

  To view the status of the cluster, use
    ray status

  To monitor and debug Ray, view the dashboard at
    127.0.0.1:8265

  If connection to the dashboard fails, check your firewall settings and network configuration.
[2026-05-20 15:31:18,605 I 13205 13205] gcs_client.cc:96: GcsClient has no Cluster ID set, and won't fetch from GCS.
[2026-05-20 15:31:18,619 I 13205 13205] gcs_client.cc:96: GcsClient has no Cluster ID set, and won't fetch from GCS.
[2026-05-20 15:31:20,031 E 13205 13205] logging.cc:118: Unhandled exception: N3ray8internal16RayTaskExceptionE. what(): Invalid: An exception was thrown while executing function(MyFunction): ray::internal::RayFunctionNotFound: Executable function not found, the function name MyFunction /home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(+0x16db67a) [0x7dc17f6db67a] ray::operator<<()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(+0x16dc12c) [0x7dc17f6dc12c] ray::RayLog::operator<< <>()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(+0x9242e5) [0x7dc17e9242e5] ray::TerminateHandler()
/lib/x86_64-linux-gnu/libstdc++.so.6(+0xae20c) [0x7dc17dcae20c]
/lib/x86_64-linux-gnu/libstdc++.so.6(+0xae277) [0x7dc17dcae277]
/lib/x86_64-linux-gnu/libstdc++.so.6(+0xae4d8) [0x7dc17dcae4d8]
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(+0x8a2e06) [0x7dc17e8a2e06] ray::internal::NativeObjectStore::CheckException()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray8internal17NativeObjectStore6GetRawERKSt6vectorINS_8ObjectIDESaIS3_EEi+0x217) [0x7dc17e98dbf7] ray::internal::NativeObjectStore::GetRaw()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray8internal17NativeObjectStore6GetRawERKNS_8ObjectIDEi+0x9c) [0x7dc17e98d83c] ray::internal::NativeObjectStore::GetRaw()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray8internal11ObjectStore3GetERKNS_8ObjectIDEi+0x13) [0x7dc17e98ecb3] ray::internal::ObjectStore::Get()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray8internal18AbstractRayRuntime3GetERKSsRKi+0x4c) [0x7dc17e981afc] ray::internal::AbstractRayRuntime::Get()
./test(+0xdc9b) [0x5591eb015c9b] ray::GetFromRuntime<>()
./test(+0xb92b) [0x5591eb01392b] ray::ObjectRef<>::Get()
./test(+0x4e42) [0x5591eb00ce42] main
/lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7dc17d829d90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7dc17d829e40] __libc_start_main
./test(+0x4a65) [0x5591eb00ca65] _start

[2026-05-20 15:31:20,048 E 13205 13205] logging.cc:125: Stack trace:
 /home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(+0x16db67a) [0x7dc17f6db67a] ray::operator<<()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(+0x16dc12c) [0x7dc17f6dc12c] ray::RayLog::operator<< <>()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray16TerminateHandlerEv+0xa8) [0x7dc17f6de6e8] ray::TerminateHandler()
/lib/x86_64-linux-gnu/libstdc++.so.6(+0xae20c) [0x7dc17dcae20c]
/lib/x86_64-linux-gnu/libstdc++.so.6(+0xae277) [0x7dc17dcae277]
/lib/x86_64-linux-gnu/libstdc++.so.6(+0xae4d8) [0x7dc17dcae4d8]
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(+0x8a2e06) [0x7dc17e8a2e06] ray::internal::NativeObjectStore::CheckException()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray8internal17NativeObjectStore6GetRawERKSt6vectorINS_8ObjectIDESaIS3_EEi+0x217) [0x7dc17e98dbf7] ray::internal::NativeObjectStore::GetRaw()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray8internal17NativeObjectStore6GetRawERKNS_8ObjectIDEi+0x9c) [0x7dc17e98d83c] ray::internal::NativeObjectStore::GetRaw()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray8internal11ObjectStore3GetERKNS_8ObjectIDEi+0x13) [0x7dc17e98ecb3] ray::internal::ObjectStore::Get()
/home/oct1158/.local/lib/python3.10/site-packages/ray/cpp/lib/libray_api.so(_ZN3ray8internal18AbstractRayRuntime3GetERKSsRKi+0x4c) [0x7dc17e981afc] ray::internal::AbstractRayRuntime::Get()
./test(+0xdc9b) [0x5591eb015c9b] ray::GetFromRuntime<>()
./test(+0xb92b) [0x5591eb01392b] ray::ObjectRef<>::Get()
./test(+0x4e42) [0x5591eb00ce42] main
/lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7dc17d829d90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7dc17d829e40] __libc_start_main
./test(+0x4a65) [0x5591eb00ca65] _start

*** SIGABRT received at time=1779262280 on cpu 0 ***
PC: @     0x7dc17d8969fc  (unknown)  pthread_kill
    @     0x7dc17d842520  (unknown)  (unknown)
[2026-05-20 15:31:20,052 E 13205 13205] logging.cc:474: *** SIGABRT received at time=1779262280 on cpu 0 ***
[2026-05-20 15:31:20,052 E 13205 13205] logging.cc:474: PC: @     0x7dc17d8969fc  (unknown)  pthread_kill
[2026-05-20 15:31:20,053 E 13205 13205] logging.cc:474:     @     0x7dc17d842520  (unknown)  (unknown)
Aborted (core dumped)
oct1158@oct1158-ubuntu:~/Documents/Code/Python$

 

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:20 回複數:3
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2026-5-20 15:33
 
©2010-2026 Purasbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。