ray/cpp/example/example.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.6 KiB
C++
Raw Permalink Normal View History

2021-08-09 19:39:16 +08:00
/// This is an example of Ray C++ application. Please visit
/// `https://docs.ray.io/en/master/ray-core/walkthrough.html#installation`
/// for more details.
2021-08-09 19:39:16 +08:00
/// including the `<ray/api.h>` header
#include <ray/api.h>
2021-08-09 19:39:16 +08:00
/// common function
int Plus(int x, int y) { return x + y; }
2021-08-09 19:39:16 +08:00
/// Declare remote function
RAY_REMOTE(Plus);
2021-08-09 19:39:16 +08:00
/// class
class Counter {
public:
int count;
Counter(int init) { count = init; }
2021-08-09 19:39:16 +08:00
/// static factory method
static Counter *FactoryCreate(int init) { return new Counter(init); }
2021-08-09 19:39:16 +08:00
/// non static function
int Add(int x) {
count += x;
return count;
}
};
2021-08-09 19:39:16 +08:00
/// Declare remote function
RAY_REMOTE(Counter::FactoryCreate, &Counter::Add);
int main(int argc, char **argv) {
/// initialization
ray::Init();
/// put and get object
auto object = ray::Put(100);
auto put_get_result = *(ray::Get(object));
2021-08-09 19:39:16 +08:00
std::cout << "put_get_result = " << put_get_result << std::endl;
/// common task
auto task_object = ray::Task(Plus).Remote(1, 2);
int task_result = *(ray::Get(task_object));
2021-08-09 19:39:16 +08:00
std::cout << "task_result = " << task_result << std::endl;
/// actor
ray::ActorHandle<Counter> actor = ray::Actor(Counter::FactoryCreate).Remote(0);
2021-08-09 19:39:16 +08:00
/// actor task
auto actor_object = actor.Task(&Counter::Add).Remote(3);
int actor_task_result = *(ray::Get(actor_object));
2021-08-09 19:39:16 +08:00
std::cout << "actor_task_result = " << actor_task_result << std::endl;
/// actor task with reference argument
auto actor_object2 = actor.Task(&Counter::Add).Remote(task_object);
int actor_task_result2 = *(ray::Get(actor_object2));
2021-08-09 19:39:16 +08:00
std::cout << "actor_task_result2 = " << actor_task_result2 << std::endl;
/// shutdown
ray::Shutdown();
return 0;
}