2021-08-09 19:39:16 +08:00
|
|
|
/// This is an example of Ray C++ application. Please visit
|
2022-02-08 19:04:37 +08:00
|
|
|
/// `https://docs.ray.io/en/master/ray-core/walkthrough.html#installation`
|
|
|
|
/// for more details.
|
2021-01-02 04:18:41 +02:00
|
|
|
|
2021-08-09 19:39:16 +08:00
|
|
|
/// including the `<ray/api.h>` header
|
2021-01-02 04:18:41 +02:00
|
|
|
#include <ray/api.h>
|
|
|
|
|
2021-08-09 19:39:16 +08:00
|
|
|
/// common function
|
2021-01-02 04:18:41 +02:00
|
|
|
int Plus(int x, int y) { return x + y; }
|
2021-08-09 19:39:16 +08:00
|
|
|
/// Declare remote function
|
|
|
|
RAY_REMOTE(Plus);
|
2021-01-02 04:18:41 +02:00
|
|
|
|
2021-08-09 19:39:16 +08:00
|
|
|
/// class
|
2021-01-02 04:18:41 +02:00
|
|
|
class Counter {
|
|
|
|
public:
|
|
|
|
int count;
|
|
|
|
|
|
|
|
Counter(int init) { count = init; }
|
2021-08-09 19:39:16 +08:00
|
|
|
/// static factory method
|
2021-01-02 04:18:41 +02:00
|
|
|
static Counter *FactoryCreate(int init) { return new Counter(init); }
|
2021-08-09 19:39:16 +08:00
|
|
|
|
2021-01-02 04:18:41 +02: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);
|
2021-05-19 18:03:39 +08:00
|
|
|
|
2021-01-02 04:18:41 +02:00
|
|
|
int main(int argc, char **argv) {
|
2021-08-12 10:59:21 +08:00
|
|
|
/// initialization
|
|
|
|
ray::Init();
|
2021-01-02 04:18:41 +02:00
|
|
|
|
|
|
|
/// put and get object
|
2021-08-10 11:17:59 +08:00
|
|
|
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
|
2021-08-10 11:17:59 +08:00
|
|
|
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
|
2021-08-10 11:17:59 +08:00
|
|
|
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);
|
2021-08-10 11:17:59 +08:00
|
|
|
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);
|
2021-08-10 11:17:59 +08:00
|
|
|
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;
|
2021-02-01 19:24:33 +08:00
|
|
|
|
|
|
|
/// shutdown
|
2021-08-10 11:17:59 +08:00
|
|
|
ray::Shutdown();
|
2021-01-02 04:18:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|