forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cc
More file actions
59 lines (49 loc) · 1.64 KB
/
example.cc
File metadata and controls
59 lines (49 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// This is an example of Ray C++ application. Please visit
/// `https://docs.ray.io/en/master/ray-core/walkthrough.html#installation`
/// for more details.
/// including the `<ray/api.h>` header
#include <ray/api.h>
/// common function
int Plus(int x, int y) { return x + y; }
/// Declare remote function
RAY_REMOTE(Plus);
/// class
class Counter {
public:
int count;
Counter(int init) { count = init; }
/// static factory method
static Counter *FactoryCreate(int init) { return new Counter(init); }
/// non static function
int Add(int x) {
count += x;
return count;
}
};
/// 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));
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));
std::cout << "task_result = " << task_result << std::endl;
/// actor
ray::ActorHandle<Counter> actor = ray::Actor(Counter::FactoryCreate).Remote(0);
/// actor task
auto actor_object = actor.Task(&Counter::Add).Remote(3);
int actor_task_result = *(ray::Get(actor_object));
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));
std::cout << "actor_task_result2 = " << actor_task_result2 << std::endl;
/// shutdown
ray::Shutdown();
return 0;
}